Arrays in Assembler The purpose of this assignment is to explore methods to repr
ID: 3598913 • Letter: A
Question
Arrays in Assembler
The purpose of this assignment is to explore methods to represent, access, and process arrays in Pentium Assembler. Briefly, you are going to define an array, initialize it with random values, and determine the sum, average, minimum, and maximum of the values in the array. For this assignment, you should use a new program skeleton called utilities.asm which is available on the software course web page.
Declare a symbol called N with a value of 10 to indicate the table size; declare a table called T of size N where each entry in T is 32 bits.
In addition to dump, utilities.asm also contains a function called rand8 which returns a random number in eax in the range [0..255]. Write a function called init that uses rand8 to initialize T to random values. Do not pass any arguments to init; init can refer to N and T directly. This function should not permanently modify any registers.*
Write a function that calculates the sum of the contents of T. Pass the address of T to the function in eax; pass N in ebx. Return the sum in eax. Do not modify any other registers.*
Write a function that calculates the minimum and maximum of the contents of T. Pass (the address of) T and (the value of) N by pushing them on the stack. Return the min in eax and the max in ebx. Do not modify any other registers.*
Extra credit. Write a function that calculates the average (using integer arithmetic) of the contents of T. Pass arguments by passing the address of a parameter block in eax. The parameter block should contain the address of T, the value of N, followed by space for the average. Do not modify any registers.*
Call dump (one last time) with the values of sum in eax, min in ebx, max in ecx, and avg in edx. (An easy way to do this is to define sum, etc. as variables and save the values as they are calculated.)
Demonstrate that your program works for 10 values. Make sure that your program can be easily changed to accommodate other values (such as 100000) for N.
Email a copy of your .asm file(s), as well as a file containing the output messages from your program(s).
Hints:
The following are instructions that are useful for this assignment: add, call, cmp, dec, div, inc, jxx, and mov. One useful addressing mode for accessing the contents of tables/arrays is the following:
*This means that you are free to use registers but you must push and later pop them in the function that modifies them.
The purpose of this assignment is to explore methods to represent, access, and process arrays in Pentium Assembler. Briefly, you are going to define an array, initialize it with random values, and determine the sum, average, minimum, and maximum of the values in the array. For this assignment, you should use a new program skeleton called utilities.asm which is available on the software course web page.
Declare a symbol called N with a value of 10 to indicate the table size; declare a table called T of size N where each entry in T is 32 bits.
In addition to dump, utilities.asm also contains a function called rand8 which returns a random number in eax in the range [0..255]. Write a function called init that uses rand8 to initialize T to random values. Do not pass any arguments to init; init can refer to N and T directly. This function should not permanently modify any registers.*
Write a function that calculates the sum of the contents of T. Pass the address of T to the function in eax; pass N in ebx. Return the sum in eax. Do not modify any other registers.*
Write a function that calculates the minimum and maximum of the contents of T. Pass (the address of) T and (the value of) N by pushing them on the stack. Return the min in eax and the max in ebx. Do not modify any other registers.*
Extra credit. Write a function that calculates the average (using integer arithmetic) of the contents of T. Pass arguments by passing the address of a parameter block in eax. The parameter block should contain the address of T, the value of N, followed by space for the average. Do not modify any registers.*
Call dump (one last time) with the values of sum in eax, min in ebx, max in ecx, and avg in edx. (An easy way to do this is to define sum, etc. as variables and save the values as they are calculated.)
Demonstrate that your program works for 10 values. Make sure that your program can be easily changed to accommodate other values (such as 100000) for N.
Email a copy of your .asm file(s), as well as a file containing the output messages from your program(s).
Hints:
The following are instructions that are useful for this assignment: add, call, cmp, dec, div, inc, jxx, and mov. One useful addressing mode for accessing the contents of tables/arrays is the following:
.data T dword 100 dup(?) .code mov eax, 4 ;to refer to second entry mov ebx, [T+eax] ;move the second entry of T into ebx mov ebx, T[eax] ;same as above or mov eax, offset T ;get addr of array add eax, 4 ;to refer to second entry mov ebx, [eax] ;move the second entry of T into ebx
*This means that you are free to use registers but you must push and later pop them in the function that modifies them.
Explanation / Answer
test::arrtot(int*, int):
push rbp
mov rbp, rsp
sub rsp, 48
mov QWORD PTR [rbp-24], rdi
mov QWORD PTR [rbp-32], rsi
mov DWORD PTR [rbp-36], edx
mov DWORD PTR [rbp-8], 0
mov DWORD PTR [rbp-4], 0
.L3:
mov eax, DWORD PTR [rbp-4]
cmp eax, DWORD PTR [rbp-36]
jge .L2
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-32]
add rax, rdx
mov eax, DWORD PTR [rax]
add DWORD PTR [rbp-8], eax
add DWORD PTR [rbp-4], 1
jmp .L3
.L2:
mov eax, DWORD PTR [rbp-8]
mov esi, eax
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov esi, OFFSET FLAT:std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
mov rdi, rax
call std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))
nop
leave
ret
test::maxmin(int*, int):
push rbp
mov rbp, rsp
sub rsp, 48
mov QWORD PTR [rbp-24], rdi
mov QWORD PTR [rbp-32], rsi
mov DWORD PTR [rbp-36], edx
mov DWORD PTR [rbp-4], 0
mov rax, QWORD PTR [rbp-32]
mov eax, DWORD PTR [rax]
mov DWORD PTR [rbp-8], eax
mov DWORD PTR [rbp-12], 0
.L8:
mov eax, DWORD PTR [rbp-12]
cmp eax, DWORD PTR [rbp-36]
jge .L5
mov eax, DWORD PTR [rbp-12]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-32]
add rax, rdx
mov eax, DWORD PTR [rax]
cmp eax, DWORD PTR [rbp-4]
jle .L6
mov eax, DWORD PTR [rbp-12]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-32]
add rax, rdx
mov eax, DWORD PTR [rax]
mov DWORD PTR [rbp-4], eax
jmp .L7
.L6:
mov eax, DWORD PTR [rbp-12]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-32]
add rax, rdx
mov eax, DWORD PTR [rax]
mov DWORD PTR [rbp-8], eax
.L7:
add DWORD PTR [rbp-12], 1
jmp .L8
.L5:
mov eax, DWORD PTR [rbp-4]
mov esi, eax
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov esi, OFFSET FLAT:std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
mov rdi, rax
call std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))
mov eax, DWORD PTR [rbp-8]
mov esi, eax
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov esi, OFFSET FLAT:std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
mov rdi, rax
call std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))
nop
leave
ret
main:
push rbp
mov rbp, rsp
sub rsp, 64
mov edi, 0
call time
mov edi, eax
call srand
mov DWORD PTR [rbp-4], 0
.L11:
mov eax, DWORD PTR [rbp-4]
lea edx, [rax+1]
mov DWORD PTR [rbp-4], edx
cmp eax, 9
setle al
test al, al
je .L10
call rand
mov ecx, eax
mov edx, 1374389535
mov eax, ecx
imul edx
sar edx, 5
mov eax, ecx
sar eax, 31
sub edx, eax
mov eax, edx
imul eax, eax, 100
sub ecx, eax
mov eax, ecx
add eax, 1
mov DWORD PTR [rbp-8], eax
mov eax, DWORD PTR [rbp-4]
cdqe
mov edx, DWORD PTR [rbp-8]
mov DWORD PTR [rbp-48+rax*4], edx
jmp .L11
.L10:
lea rcx, [rbp-48]
lea rax, [rbp-49]
mov edx, 10
mov rsi, rcx
mov rdi, rax
call test::arrtot(int*, int)
lea rcx, [rbp-48]
lea rax, [rbp-49]
mov edx, 10
mov rsi, rcx
mov rdi, rax
call test::maxmin(int*, int)
mov eax, 0
leave
ret
__static_initialization_and_destruction_0(int, int):
push rbp
mov rbp, rsp
sub rsp, 16
mov DWORD PTR [rbp-4], edi
mov DWORD PTR [rbp-8], esi
cmp DWORD PTR [rbp-4], 1
jne .L15
cmp DWORD PTR [rbp-8], 65535
jne .L15
mov edi, OFFSET FLAT:std::__ioinit
call std::ios_base::Init::Init()
mov edx, OFFSET FLAT:__dso_handle
mov esi, OFFSET FLAT:std::__ioinit
mov edi, OFFSET FLAT:std::ios_base::Init::~Init()
call __cxa_atexit
.L15:
nop
leave
ret
_GLOBAL__sub_I_main:
push rbp
mov rbp, rsp
mov esi, 65535
mov edi, 1
call __static_initialization_and_destruction_0(int, int)
pop rbp
ret
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.