ASSEMBLY LANGUAGE. For this program you will call a procedure that finds the min
ID: 3581492 • Letter: A
Question
ASSEMBLY LANGUAGE.
For this program you will call a procedure that finds the minimum and maximum elements of an array, and the last positions in the array where the minimum and maximum element occurs.
The output of the program is:
The position in array of last occurrence of minimum element is: 4
The minimum element in the array is: 5
The position in array of last occurrence of maximum element is: 6
The maximum element in the array is: 90
Use an array named Array to contain the elements (as DWORDS):
5, 46, 90, 50, 5, 6, 90, 8, 20
All variables in main are DWORDS.
The name of the procedure is MinandMax, and its equivalent call in C++ would be:
MinandMax(int &max, int &maxposn, int &min, int &maxposn,
int& Array, int Arrsze);
All arguments must be passed on the stack. You must use the C++ calling conventions for ordering of arguments on the stack. You must use the conventions for caller and callee saved registers on slide 26 of SS7 assuming that all registers EAX, EBX, ECX, EDX, EDI, ESI may be used for some purpose in main other than support for calling MinandMax.
Explanation / Answer
Answer:
.zero 1
;gets the Array size
.LC0:
.string " ENTER THE SIZE OF THE ARRAY…:"
; entering the elements
.LC1:
.string " ENTER THE ELEMENTS OF THE ARRAY…: "
;getting the largest element
.LC2:
.string " THE LARGEST ELEMENT IS…: %d"
;position of the largest element
.LC3:
.string " AND ITS POSSITION IS…: %d"
.LC4:
;getting the largest element
.string " THE SMALLEST ELEMENT IS…: %d"
.LC5:
;position of the SMALLEST element
.string " AND ITS POSSITION IS…: %d"
;main program
main:
;pushing the value
push rbp
;moves rsp to rbp
mov rbp, rsp
;decrements the value
sub rsp, 144
;sets the offset to LC0
mov esi, OFFSET FLAT:.LC0
;moves the edi offset
mov edi, OFFSET FLAT:std::cout
;performs the c++ operation
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
;lea to rax
lea rax, [rbp-132]
;moves the values
mov rsi, rax
;setting the offset value
mov edi, OFFSET FLAT:std::cin
call std::basic_istream<char, std::char_traits<char> >::operator>>(int&)
;moves the offset ot LC1
mov esi, OFFSET FLAT:.LC1
;outputs the value
mov edi, OFFSET FLAT:std::cout
;performs the c++ operation
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
;performs the word pointer operation
mov DWORD PTR [rbp-20], 1
.L3:
;moves the word
mov eax, DWORD PTR [rbp-132]
;compares the word
cmp DWORD PTR [rbp-20], eax
jg .L2
;lea for rax
lea rax, [rbp-128]
;moves the word
mov edx, DWORD PTR [rbp-20]
;moves the value
movsx rdx, edx
;includes the sals instruction for 2
sal rdx, 2
;adds the value of rax
add rax, rdx
;moves the rax value
mov rsi, rax
;determines the offset
mov edi, OFFSET FLAT:std::cin
;includes the c++ cin get value
call std::basic_istream<char, std::char_traits<char> >::operator>>(int&)
;adds the word pointer
add DWORD PTR [rbp-20], 1
;jumps to the third loop
jmp .L3
.L2:
;moves the word ptr
mov eax, DWORD PTR [rbp-124]
;moves the eax value
mov DWORD PTR [rbp-4], eax
;moves the word pointer
mov DWORD PTR [rbp-12], 1
;moves the word pointer
mov DWORD PTR [rbp-20], 1
.L6:
;moves the word pointer
mov eax, DWORD PTR [rbp-132]
;compares the word pointer
cmp DWORD PTR [rbp-20], eax
;jumps to the loop 4
jg .L4
;moves the word pointer
mov eax, DWORD PTR [rbp-20]
cdqe
;moves the word pointer value
mov eax, DWORD PTR [rbp-128+rax*4]
;compares the value
cmp eax, DWORD PTR [rbp-4]
;jle to the loop 5
jle .L5
;moves the value of the pointer
mov eax, DWORD PTR [rbp-20]
cdqe
;moves the value of the pointer
mov eax, DWORD PTR [rbp-128+rax*4]
;moves the value of the pointer
mov DWORD PTR [rbp-4], eax
;moves the word pointer to 20
mov eax, DWORD PTR [rbp-20]
;moves the word pointer to eax
mov DWORD PTR [rbp-12], eax
.L5:
;adds the word pointer value
add DWORD PTR [rbp-20], 1
;jumps to the loop 6
jmp .L6
.L4:
;moves the word pointer value
mov eax, DWORD PTR [rbp-124]
;moves the word pointer value
mov DWORD PTR [rbp-8], eax
;moves the word pointer value
mov DWORD PTR [rbp-16], 1
;moves the word pointer value
mov DWORD PTR [rbp-20], 1
.L9:
;moves the word pointer value
mov eax, DWORD PTR [rbp-132]
;compares the word pointer value
cmp DWORD PTR [rbp-20], eax
jg .L7
;moves the word pointer value
mov eax, DWORD PTR [rbp-20]
cdqe
;moves the word pointer value
mov eax, DWORD PTR [rbp-128+rax*4]
;compares the word pointer value
cmp eax, DWORD PTR [rbp-8]
jge .L8
;moves the word pointer value
mov eax, DWORD PTR [rbp-20]
cdqe
;moves the word pointer value
mov eax, DWORD PTR [rbp-128+rax*4]
;moves the word pointer value
mov DWORD PTR [rbp-8], eax
;moves the word pointer value
mov eax, DWORD PTR [rbp-20]
;moves the word pointer value
mov DWORD PTR [rbp-16], eax
.L8:
;adds the value of the pointer
add DWORD PTR [rbp-20], 1
jmp .L9
.L7:
;checks the offset to the loop 3
mov esi, OFFSET FLAT:.LC2
;checks the offset flat values
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
;moves rax to rdx value
mov rdx, rax
;moves the word pointer
mov eax, DWORD PTR [rbp-4]
;moves the value of the eax to the esi values
mov esi, eax
;moves the values of the rdx to the rdi values
mov rdi, rdx
;includes the c++ operations
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
;includes the flat LC3 loop
mov esi, OFFSET FLAT:.LC3
;moves the offset flat values
;moves the offset flat value
mov edi, OFFSET FLAT:std::cout
;includes the c++ operations
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
;moves the values of the rdx to the rax values
mov rdx, rax
;moves the word pointer
mov eax, DWORD PTR [rbp-12]
;moves the value of the eax to the esi values
mov esi, eax
;moves the values of the rdx to the rdi values
mov rdi, rdx
;includes the c++ operations
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
;includes the flat LC4 loop
mov esi, OFFSET FLAT:.LC4
;includes the c++ operations
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
;moves the values of the rdx to the rax values
mov rdx, rax
;moves the word pointer
mov eax, DWORD PTR [rbp-8]
;moves the value of the eax to the esi values
mov esi, eax
;moves the values of the rdx to the rdi values
mov rdi, rdx
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov esi, OFFSET FLAT:.LC3
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
;moves the values of the rdx to the rax values
mov rdx, rax
;moves the word pointer
mov eax, DWORD PTR [rbp-16]
mov esi, eax
;moves the values of the rdx to the rdi values
mov rdi, rdx
;includes the c++ operations
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
;moves the value of eax to 0
mov eax, 0
leave
ret
__static_initialization_and_destruction_0(int, int):
;pushing the rbp
push rbp
;moving rsp to rbp
mov rbp, rsp
;subtracts the 16
sub rsp, 16
;moves the word pointer to edi
mov DWORD PTR [rbp-4], edi
;moves the word pointer to esi
mov DWORD PTR [rbp-8], esi
;compares the word pointer
cmp DWORD PTR [rbp-4], 1
;jump not equal to loop 13
jne .L13
;compares the word using pointer
cmp DWORD PTR [rbp-8], 65535
jne .L13
;moves the offset value
mov edi, OFFSET FLAT:std::__ioinit
call std::ios_base::Init::Init()
;moves the offset value
mov edx, OFFSET FLAT:__dso_handle
;moves the offset value
mov esi, OFFSET FLAT:std::__ioinit
;moves the offset value
mov edi, OFFSET FLAT:std::ios_base::Init::~Init()
call __cxa_atexit
.L13:
nop
leave
ret
push rbp
mov rbp, rsp
mov esi, 65535
mov edi, 1
call __static_initialization_and_destruction_0(int, int)
pop rbp
ret