Advertisement
emanuvalza99

Untitled

Nov 19th, 2020 (edited)
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .data
  2. A: .word 24 6 20 10 7 21 4 9
  3. N: .word 8
  4.  
  5. .text
  6. .globl main
  7.  
  8. main:
  9. la $a0, A # $a0 = A
  10. la $t0, N
  11. lw $a1, 0($t0) # $a1 = n
  12. # Alloco lo stack
  13. addi $sp, $sp, -4
  14. # Salvo $ra sullo stack
  15. sw $ra, 0($sp)
  16. jal find_max # find_Max(A,n)
  17. # Fine procedura
  18. #Dealloco lo stack e carico $ra
  19. lw $ra, 0($sp)
  20. addi $sp, $sp, 4
  21. # Print MAX
  22. add $a0, $zero, $v0
  23. li $v0, 1
  24. syscall
  25. jr $ra # return
  26.  
  27.  
  28.  
  29. find_max:
  30. bne $a1, 1, else # if n != 1 goto else
  31. lw $v0, 0($a0) # $v0 = A[0]
  32. jr $ra
  33. else:
  34. addi $a1, $a1, -1 # $a1 = n-1
  35. # Alloco lo stack per salvare i registri $a e $ra
  36. addi $sp, $sp, -12
  37. sw $ra, 8($sp)
  38. sw $a0, 4($sp)
  39. sw $a1, 0($sp)
  40. jal find_max # find_max(A,n-1)
  41. # Carico i registri e dealloco lo Stack tenendo $ra per la chiamata max
  42. lw $a1, 0($sp) # $a1 conterrร  l'indice dell'elemento i-esimo da confrontare con maxSoFar
  43. lw $a0, 4($sp) # $a0 = A
  44. addi $sp, $sp, 8
  45. # Mi preparo alla chiamata max
  46. add $t0, $zero, $a0 # $t0 = A
  47. add $a0, $zero, $v0 # $a0 = maxSoFar
  48. sll $t1, $a1, 2 # carico in $t1 lo spiazzamento per accedere all' elem successivo dell'array
  49. add $t0, $t0, $t1 # $t0 = &A[i]
  50. lw $a1, 0($t0) # $a1 = A[i]
  51. jal max
  52. # Dealloco lo stack e carico $ra
  53. lw $ra, 0($sp)
  54. addi $sp, $sp, 4
  55. jr $ra #return
  56.  
  57.  
  58. # Procedura foglia
  59. max:
  60. slt $t0, $a0, $a1
  61. beq $t0 ,$zero, old_max # if (maxSoFar >= A[i])
  62. add $v0, $zero, $a1 # $v0 = newMax
  63. jr $ra
  64.  
  65. old_max:
  66. add $v0, $zero, $a0 # $v0 = oldMax
  67. jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement