Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #############################################
- # Exercici 1) #
- #############################################
- # int A[4][4] = { -1, 0, 0, 0, #
- # -2, 3, 0, 0, #
- # 4, -5, -6, 0, #
- # 7, -8, 9, 0 }; #
- # #
- # int sum; #
- # #
- # main() { #
- # int i, j; #
- # sum = 0; #
- # for (i=0; i<4; i++) #
- # for (j=0; j<=i; j++) #
- # { #
- # if (A[i][j] < 0) #
- # A[i][j] = -A[i][j] #
- # sum = sum + A[i][j]; #
- # } #
- # } #
- #############################################
- .data
- A: .word -1, 0, 0, 0
- .word -2, 3, 0, 0
- .word 4, -5, -6, 0
- .word 7, -8, 9, 0
- sum: .word 0
- .text
- .globl main
- main:
- li $t7, 4
- move $t0, $zero # $t0 = i
- whilei: move $t1, $zero # $t1 = j
- la $t2, A
- sll $t3, $t0, 4
- add $t2, $t2, $t3 # &A[i][0]
- whilej: lw $t3, 0($t2) # $t3 = A[i][j]
- bge $t3, $zero, endif
- sub $t3, $zero, $t3
- sw $t3, 0($t2)
- endif: la $t4, sum # $t4 = &sum
- lw $t5, 0($t4) # $t5 = sum
- add $t5, $t5, $t3 # sum += A[i][j]
- sw $t5, 0($t4)
- addiu $t1, $t1, 1 # ++j
- addiu $t2, $t2, 4 # &A[i][j] (offset = 4 = 1 * int size)
- ble $t1, $t0, whilej
- addiu $t0, $t0, 1 # ++i
- blt $t0, $t7, whilei
- jr $ra
- #########################################################################
- # Exercici 2) #
- #########################################################################
- # int ackermann(int m, int n) #
- # { #
- # int resultat = 0; #
- # #
- # if (m == 0) { #
- # resultat = n + 1; #
- # } else if (n == 0) { #
- # resultat = ackerman(m - 1, 1); #
- # } else { #
- # resultat = ackermann(m - 1, ackermann(m, n - 1)); #
- # } #
- # return resultat; #
- # } #
- #########################################################################
- .data
- res:.word 1, 2, 3, 4
- .word 2, 3, 4, 5
- .word 3, 5, 7, 9
- .word 5, 13, 29, 61
- .text
- .globl main
- main:
- addiu $sp, $sp, -4
- sw $ra, 0($sp)
- la $t0, res
- lw $a0, 16($t0) # 2 valors random de la matriu per a passar com a paràmetre
- lw $a1, 60($t0)
- jal ackermann
- move $t0, $v0
- lw $ra, 0($sp)
- addiu $sp, $sp, 4
- jr $ra
- ackermann:
- addiu $sp, $sp, -12 # Guardem registres segurs
- sw $s0, 0($sp)
- sw $s1, 4($sp)
- sw $ra, 8($sp)
- move $s0, $a0
- move $s1, $a1
- bne $s0, $zero, elif # Entrem al if si $s0 == $zero
- addiu $v0, $s1, 1 # resultat = n + 1
- b endif
- elif:
- bne $s1, $zero, else # Entrem al elif si $s1 == $zero
- addiu $a0, $s0, -1 # Paràmetre 0 = m - 1
- li $a1, 1 # Paràmetre 1 = 1
- jal ackermann
- b endif
- else:
- move $a0, $s0 # Paràmetre 0 = m
- addiu $a1, $s1, -1 # Paràmetre 1 = n - 1
- jal ackermann
- addiu $a0, $s0, -1 # Paràmetre 0 = m - 1
- move $a1, $v0 # Paràmetre 1 = $v0 = ackerman(m, n - 1)
- jal ackermann
- endif:
- lw $s0, 0($sp)
- lw $s1, 4($sp)
- lw $ra, 8($sp)
- addiu $sp, $sp, 12 # Recuperem registres segurs
- jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement