I am converting an MASM procedure to MIPS.
Here's the MASM:
; --------------------------------------------------------
; Find basic list stats - minimum, median, and maximum.
; Arguments passed:
; list, addr (4)
; length, value (6)
; minimum, addr (8)
; maximum, addr (10)
; median, addr (12)
stats1 proc near
pop bp
mov bp, sp
push ax
push bx
push cx
push dx
; -----
; Find max.
mov bx, word ptr 4[bp]
mov ax, word ptr [bx]
push bx
mov bx, word ptr 10[bp]
mov word ptr [bx], ax
; -----
; Find min.
mov ax, word ptr 6[bp]
dec ax
add ax, ax
pop bx
add bx, ax
mov ax, word ptr [bx]
mov bx, word ptr 8[bp]
mov word ptr [si], ax
; -----
; Find median.
mov bx, word ptr 4[bp]
mov cx, word ptr 6[bp]
mov si, cx
mov ax, si
and ax, 1
cmp ax, 0
je even_lp
dec si
mov ax, word ptr [bx][si]
jmp odd_lp
; -----
; Length is even.
even_lp:
mov ax, word ptr [bx][si]
sub si, 2
add ax, word ptr [bx][si]
mov dx, 0
div wtwo
; -----
; Length is odd.
odd_lp:
mov bx, word ptr 12[bp]
mov word ptr [bx], ax
pop dx
pop cx
pop bx
pop ax
pop bp
ret 10
stats1 endp
Now here's my conversion to MIPS:
# -----
# Arguments:
# $a0 - starting address of the list
# $a1 - list length
# $a2 - addr of minimum
# $a3 - addr of median
# ($fp) - addr of maximum
# 4($fp) - addr of sum
# 8($fp) - addr of average
.globl stats
.ent stats
stats:
subu $sp, $sp, 12
sw $s0, 0($sp)
sw $fp, 4($sp)
sw $ra, 8($sp)
addu $fp, $sp, 12
# -----
# Find min.
lw $t0, ($a0)
sw $t0, ($a2) #minimum, first element of array
# -----
#Find max.
subu $t0, $a1, 1
mul $t0, $t0, 4
addu $t0, $t0, $a0
lw $t1, ($t0)
sw $t1, ($fp) #maximum, last element of array
# -----
# Find median.
lw $t0, ($a0)
move $t1, $a1
andi $t2, $t1, 1
beq $t2, $zero, even_lp
subu $t1, $a1, 1
mul $t1, $t1, 4
addu $t1, $t1, $a0
lw $t2, ($t1)
b odd_lp
even_lp:
mul $t3, $t1, 4
addu $t3, $t3, $a0
lw $t4, ($t3)
subu $t1, $t1, 1
mul $t5, $t1, 4
addu $t5, $t5, $a0
lw $t6, ($t5)
addu $t6, $t6, $t4
div $t6, $t6, 2
odd_lp:
sw $t2, ($a2) #median
sw $ra, 8($sp)
sw $fp, 4($fp)
sw $s0, 0($sp)
addu $sp, $sp, 12
jr $ra
.end stats
Now I don't know why the console displays arithmetic overflow.
What am I doing wrong?