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, -12
- # Salvo i registri sullo stack
- sw $ra, 8($sp)
- sw $a0, 4($sp)
- sw $a1, 0($sp)
- jal find_max # find_Max(A,n)
- # Fine procedura
- #Dealloco lo stack e carico $ra
- lw $ra, 8($sp)
- addi $sp, $sp, 12
- # Print MAX
- add $a0, $zero, $v0
- li $v0, 1
- syscall
- jr $ra # return
- find_max:
- bne $a1, 1, else # is n != 1 goto else
- lw $v0, 0($a0) # $v0 = A[0]
- jr $ra
- else:
- addi $a1, $a1, -1 # n = n-1
- sll $t0, $a1, 2 # $t0 = (n-1)*4
- add $t0, $a0, $t0 # $t0 = &A[n-1]
- lw $a0, 0($t0) # $a0 = A[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 max # max(A[n-1], find_max(A,n-1))
- # Dealloco lo stack e carico $ra
- lw $ra, 8($sp)
- addi $sp, $sp, 12
- jr $ra # return max
- max:
- lw $a0, 16($sp) # $a0 = A
- # Salvo sullo stack 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)
- # Dealloco lo stack
- lw $ra, 8($sp)
- addi $sp, $sp, 12
- lw $a0, 4($sp) # Ricavo A[n-1] per il confronto e lo carico in $a0
- slt $t0, $v0, $a0
- beq $t0, $zero, max_soFar # $if A[i] > A[i+1] goto max_soFar
- add $v0, $zero, $a0 # max = new_Max
- jr $ra
- max_soFar:
- add $v0, $zero, $v0 # max = old_Max
- jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement