Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the min-max code from the lab as a starting point, write and implement a M

ID: 3779180 • Letter: U

Question

Using the min-max code from the lab as a starting point, write and implement a MIPS program which performs a sort of an array, with the following considerations:

The array is to be sorted by insertion sort, not selection sort. For details of the algorithm, see http://en.wikipedia.org/wiki/Insertion_sort;

The array is to be stored in doublewords, not words;

The array is to be sorted by absolute value (e.g. 1, -2, 3, -4, 5, -6...) but the values of the array elements themselves should not be changed; and

Use at least one subroutine which makes use of a stack frame, including frame offsets for argument or return value access.

Bonus: provide a visual interface showing the progress of the search at each step.

UPDATE: doubleword and absolute value features are now considered bonuses rather than key requirements. You must still perform an insertion sort and you must still use a stack frame for your subroutine.

Explanation / Answer

Answer:

MIPS Assembly Language Code :

.zero 1
.LC0:
.string "Enter number of elements in an array "
.LC1:
.string "Enter element "
.LC2:
.string ":"
.LC3:
.string " Maximum element is :"
.LC4:
.string " Minimum element is :"
main:
push rbp
mov rbp, rsp
push rbx
sub rsp, 440
mov esi, OFFSET FLAT:.LC0
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*)
lea rax, [rbp-436]
mov rsi, rax
mov edi, OFFSET FLAT:std::cin
call std::basic_istream<char, std::char_traits<char> >::operator>>(int&)
mov DWORD PTR [rbp-20], 0
.L3:
mov eax, DWORD PTR [rbp-436]
cmp DWORD PTR [rbp-20], eax
jge .L2
mov eax, DWORD PTR [rbp-20]
lea ebx, [rax+1]
mov esi, OFFSET FLAT:.LC1
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*)
mov esi, ebx
mov rdi, rax
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov esi, OFFSET FLAT:.LC2
mov rdi, rax
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 rax, [rbp-432]
mov edx, DWORD PTR [rbp-20]
movsx rdx, edx
sal rdx, 2
add rax, rdx
mov rsi, rax
mov edi, OFFSET FLAT:std::cin
call std::basic_istream<char, std::char_traits<char> >::operator>>(int&)
add DWORD PTR [rbp-20], 1
jmp .L3
.L2:
mov eax, DWORD PTR [rbp-432]
mov DWORD PTR [rbp-24], eax
mov eax, DWORD PTR [rbp-432]
mov DWORD PTR [rbp-28], eax
mov DWORD PTR [rbp-20], 1
.L7:
mov eax, DWORD PTR [rbp-436]
cmp DWORD PTR [rbp-20], eax
jge .L4
mov eax, DWORD PTR [rbp-20]
cdqe
mov eax, DWORD PTR [rbp-432+rax*4]
cmp eax, DWORD PTR [rbp-24]
jge .L5
mov eax, DWORD PTR [rbp-20]
cdqe
mov eax, DWORD PTR [rbp-432+rax*4]
mov DWORD PTR [rbp-24], eax
.L5:
mov eax, DWORD PTR [rbp-20]
cdqe
mov eax, DWORD PTR [rbp-432+rax*4]
cmp eax, DWORD PTR [rbp-28]
jle .L6
mov eax, DWORD PTR [rbp-20]
cdqe
mov eax, DWORD PTR [rbp-432+rax*4]
mov DWORD PTR [rbp-28], eax
.L6:
add DWORD PTR [rbp-20], 1
jmp .L7
.L4:
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*)
mov rdx, rax
mov eax, DWORD PTR [rbp-28]
mov esi, eax
mov rdi, rdx
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov esi, OFFSET FLAT:.LC4
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*)
mov rdx, rax
mov eax, DWORD PTR [rbp-24]
mov esi, eax
mov rdi, rdx
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov eax, 0
add rsp, 440
pop rbx
pop rbp
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 .L11
cmp DWORD PTR [rbp-8], 65535
jne .L11
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
.L11:
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