SHOW:
|
|
- or go back to the newest paste.
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, -12 |
13 | + | addi $sp, $sp, -4 |
14 | - | # Salvo i registri sullo stack |
14 | + | # Salvo $ra sullo stack |
15 | - | sw $ra, 8($sp) |
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 | - | lw $ra, 8($sp) |
21 | + | |
22 | - | addi $sp, $sp, 12 |
22 | + | |
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 | - | bne $a1, 1, else # is n != 1 goto else |
32 | + | |
33 | else: | |
34 | addi $a1, $a1, -1 # $a1 = n-1 | |
35 | # Alloco lo stack per salvare i registri $a e $ra | |
36 | - | addi $a1, $a1, -1 # n = n-1 |
36 | + | |
37 | - | sll $t0, $a1, 2 # $t0 = (n-1)*4 |
37 | + | |
38 | - | add $t0, $a0, $t0 # $t0 = &A[n-1] |
38 | + | |
39 | - | lw $a0, 0($t0) # $a0 = A[n-1] |
39 | + | |
40 | - | # Alloco lo stack per salvare i registri $a e $ra |
40 | + | |
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 | - | jal max # max(A[n-1], find_max(A,n-1)) |
45 | + | # Mi preparo alla chiamata max |
46 | add $t0, $zero, $a0 # $t0 = A | |
47 | - | lw $ra, 8($sp) |
47 | + | add $a0, $zero, $v0 # $a0 = maxSoFar |
48 | - | addi $sp, $sp, 12 |
48 | + | sll $t1, $a1, 2 # carico in $t1 lo spiazzamento per accedere all' elem successivo dell'array |
49 | - | jr $ra # return max |
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 $a0, 16($sp) # $a0 = A |
53 | + | lw $ra, 0($sp) |
54 | - | # Salvo sullo stack i registri $a e $ra |
54 | + | addi $sp, $sp, 4 |
55 | - | addi $sp, $sp, -12 |
55 | + | jr $ra #return |
56 | - | sw $ra, 8($sp) |
56 | + | |
57 | ||
58 | # Procedura foglia | |
59 | max: | |
60 | - | # Dealloco lo stack |
60 | + | slt $t0, $a0, $a1 |
61 | - | lw $ra, 8($sp) |
61 | + | beq $t0 ,$zero, old_max # if (maxSoFar >= A[i]) |
62 | - | addi $sp, $sp, 12 |
62 | + | add $v0, $zero, $a1 # $v0 = newMax |
63 | - | lw $a0, 4($sp) # Ricavo A[n-1] per il confronto e lo carico in $a0 |
63 | + | |
64 | - | slt $t0, $v0, $a0 |
64 | + | |
65 | - | beq $t0, $zero, max_soFar # $if A[i] > A[i+1] goto max_soFar |
65 | + | old_max: |
66 | - | add $v0, $zero, $a0 # max = new_Max |
66 | + | add $v0, $zero, $a0 # $v0 = oldMax |
67 | jr $ra |