Develop MIPS assembly code for a merge sort algorithm given above. < Requirement
ID: 3630088 • Letter: D
Question
Develop MIPS assembly code for a merge sort algorithm given above.
< Requirements >
1. Array should be used for initial data set (word size).
2. Print the final result using the syscall on the console window.
3. A sorting result should be correct.
4. Your code must be relevant with the original source code. Try to translate the given C code to assembly code. Different algorithm is not allowed.
5. Show your register mapping plan using a table. (e.g. low -> $s0)
6. Comment for your code and explain how to implement the algorithm.
< Initial Data Set >
8, 14, 5, 1, 20, 7, 2, 30, 3, 9, 21, 6, 29, 11, 17, 4, 24, 28
- End of Question -
cf.
I have to run this assembly code using SPIM simulator.
The simulator should show me the correct result, otherwise I'll not be able to get any grade point.
Please implement the complete MIPS code to run.
If your solution doesn't work, I cannot give you any Karma Point.
I hope there is somebody who can help me.
Thank you so much. :)
Comment added:
I could give you 8 hours for this one with 350 karma points
Explanation / Answer
main:
mov edi, sorted
mov esi, array
mov ecx, 10
rep movsd
push 10
push 0
push sorted
call mergesort
add esp, 12
push sorted
push 10
call print
add esp, 8
ret
; 20 max
; 16 med
; 12 min
; 8 array
merge:
push ebp
mov ebp, esp
push eax
push ecx
push edx
push edi
push esi
;mov edi, temp
;mov eax, 0
;mov ecx, 10
;rep stosw
mov ecx, [ebp+20]
sub ecx, [ebp+12]
shl DWORD[ebp+12], 2
shl DWORD[ebp+16], 2
shl DWORD[ebp+20], 2
mov edx, temp ; dest
mov edi, [ebp+8] ; middle source
add edi, [ebp+16] ;
; messing with the stack. vamos somar aos 3 idxs o addr base
mov esi, [ebp+8]
add [ebp+12], esi
add [ebp+16], esi
add [ebp+20], esi
mov esi, [ebp+12]
.next:
; source != med
cmp esi, [ebp+16]
jnz .second
; middle != max
cmp edi, [ebp+20]
jnz .first
; sao os dois iguais. fim
jmp .end
; quatro condicoes manhosas... medo.
; vao ser usadas totil de labels
.first:
cmp esi, [ebp+16]
jnz .second
;vamos meter o edi no edx.
mov eax, [edi]
mov [edx], eax
add edx, 4
add edi, 4
jmp .next
.second:
cmp edi, [ebp+20]
jnz .third
;vamos meter o esi no edx
mov eax, [esi]
mov [edx], eax
add edx, 4
add esi, 4
jmp .next
.third:
mov eax, [esi]
cmp eax, [edi]
jnl .forth
; meter o esi no edx
mov [edx], eax
add edx, 4
add esi, 4
jmp .next
.forth:
mov eax, [edi]
mov [edx], eax
add edi, 4
add edx, 4
jmp .next
.end:
mov esi, temp
mov edi, [ebp+12]
rep movsd
pop esi
pop edi
pop edx
pop ecx
pop eax
mov esp, ebp
pop ebp
ret
mergesort:
push ebp
mov ebp, esp
push eax
mov eax, DWORD[ebp+16] ; max
sub eax, [ebp+12] ; min
cmp eax, 2
JL .end
push edx
push ebx
xor edx, edx
mov ebx, 2
div ebx
pop ebx
pop edx
add eax, [ebp+12] ; med
; siga recursivar.
push eax ; max
push DWORD[ebp+12] ; min
push DWORD[ebp+8] ; array
call mergesort
add esp, 12
push DWORD[ebp+16] ; max
push eax ; min
push DWORD[ebp+8] ; array
call mergesort
add esp, 12
push DWORD[ebp+16] ; max
push eax ; med
push DWORD[ebp+12] ; min
push DWORD[ebp+8] ; array
call merge
add esp, 16
.end:
pop eax
mov esp, ebp
pop ebp
ret
print:
push ebp ; Prologue
mov ebp, esp
push ecx
push edx
mov edx, [ebp+12]
mov ecx, [ebp+8]
.args:
push ecx
push edx
push DWORD[edx]
push format
call printf
add esp, 8
pop edx
pop ecx
add edx, 4
LOOP .args
push endl
call printf
add esp, 4
pop edx ;
pop ecx ;
mov esp, ebp ;
pop ebp ; Epilogue
ret ;
format:
db ' %2d', 0
endl:
db 10, 0
array:
dd 10, 9, 8, 4, 5, 6, 7, 3, 2, 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.