Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .data
- A: .word 24 6 20 10 7 21 4 9
- N: .word 8
- .text
- .globl main
- main:
- la $a0, A # $a0 = A
- la $t0, N
- lw $a1, 0($t0) # $a1 = n
- # Alloco lo stack
- addi $sp, $sp, -4
- # Salvo $ra sullo stack
- sw $ra, 0($sp)
- jal find_max # find_Max(A,n)
- # Fine procedura
- #Dealloco lo stack e carico $ra
- lw $ra, 0($sp)
- addi $sp, $sp, 4
- # Print MAX
- add $a0, $zero, $v0
- li $v0, 1
- syscall
- jr $ra # return
- find_max:
- bne $a1, 1, else # if n != 1 goto else
- lw $v0, 0($a0) # $v0 = A[0]
- jr $ra
- else:
- addi $a1, $a1, -1 # $a1 = n-1
- # Alloco lo stack per salvare i registri $a e $ra
- addi $sp, $sp, -12
- sw $ra, 8($sp)
- sw $a0, 4($sp)
- sw $a1, 0($sp)
- jal find_max # find_max(A,n-1)
- # Carico i registri e dealloco lo Stack tenendo $ra per la chiamata max
- lw $a1, 0($sp) # $a1 conterrà l'indice dell'elemento i-esimo da confrontare con maxSoFar
- lw $a0, 4($sp) # $a0 = A
- addi $sp, $sp, 8
- # Mi preparo alla chiamata max
- add $t0, $zero, $a0 # $t0 = A
- add $a0, $zero, $v0 # $a0 = maxSoFar
- sll $t1, $a1, 2 # carico in $t1 lo spiazzamento per accedere all' elem successivo dell'array
- add $t0, $t0, $t1 # $t0 = &A[i]
- lw $a1, 0($t0) # $a1 = A[i]
- jal max
- # Dealloco lo stack e carico $ra
- lw $ra, 0($sp)
- addi $sp, $sp, 4
- jr $ra #return
- # Procedura foglia
- max:
- slt $t0, $a0, $a1
- beq $t0 ,$zero, old_max # if (maxSoFar >= A[i])
- add $v0, $zero, $a1 # $v0 = newMax
- jr $ra
- old_max:
- add $v0, $zero, $a0 # $v0 = oldMax
- jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement