Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .data
- # Problem: The sum of an array's elements
- .align 2 # will make the following data to be aligned on 4 bytes (2^2)
- numere: .word 1,2,3,4,5,6,7,8,9,10 # a word is written on 4 bytes
- # starting address is the same as the label (numere)
- outs: .asciiz "The sum is: " # the string to be printed
- quot: .asciiz "Quotient: "
- remn: .asciiz "Remainder: "
- .text
- xor $t0, $t0, $t0 # $t0 = 0 -> the sum
- li $t1, 9 # $t1 = 9 -> the offset numere+2^offset
- loop:
- li $t5, 4 # the skip value
- la $t4, numere # $t2 = numere
- mul $t3, $t5, $t1 # $t3 = $t5 * $t1 -> index = offset * 4
- add $t5, $t4, $t3 # $t5 = $t4 + $t3 -> address = numere + index
- lw $t2, ($t5) # $t2 = MEM(numere[offset])
- # if we didn't put parantheses, the assembler would have thought that we want to load the ADDRESS value (0xWhatever)
- add $t0, $t0, $t2 # $t0 = $t0 + $t2 -> sum = sum + numere[offset]
- sub $t1, $t1, 1 # $t1 = $t1 - 1 -> offset = offset - 1
- bgez $t1, loop # if offset >= 0 then loop
- puts outs # print the string
- puti $t0 # print the sum
- putc '\n' # print two new lines
- putc '\n'
- # alternatively, we could have used the following code to print the sum
- li $v0, 4 # $v0 = 4 -> the print string syscall
- la $a0, outs # $a0 = outs -> the string to be printed
- syscall # print the string
- li $v0, 1 # $v0 = 1 -> the print integer syscall
- move $a0, $t0 # $a0 = $t0 -> the integer to be printed
- syscall # print the integer
- li $t1, 10
- div $t0, $t1 # Lo = $t0 / $t1, Hi = $t0 % $t1
- mflo $t2 # $t2 = Lo -> $t2 = $t0 / $t1
- mfhi $t3 # $t3 = Hi -> $t3 = $t0 % $t1
- putc '\n' # print a new line
- puts quot # print the string
- puti $t2 # print the integer $t2
- putc '\n' # print a new line
- puts remn # print the string
- puti $t3 # print the integer $t3
- li $v0, 10 # $v0 = 10 -> the exit syscall
- syscall # exit
Advertisement
Add Comment
Please, Sign In to add comment