LordMirai

lab 1 ScM

Mar 2nd, 2023 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. .data
  2.  
  3. # Problem: The sum of an array's elements
  4. .align 2 # will make the following data to be aligned on 4 bytes (2^2)
  5. numere: .word 1,2,3,4,5,6,7,8,9,10 # a word is written on 4 bytes
  6. # starting address is the same as the label (numere)
  7.  
  8. outs: .asciiz "The sum is: " # the string to be printed
  9.  
  10. quot: .asciiz "Quotient: "
  11. remn: .asciiz "Remainder: "
  12.  
  13. .text
  14.  
  15. xor $t0, $t0, $t0 # $t0 = 0 -> the sum
  16. li $t1, 9 # $t1 = 9 -> the offset numere+2^offset
  17.  
  18. loop:
  19. li $t5, 4 # the skip value
  20. la $t4, numere # $t2 = numere
  21.  
  22. mul $t3, $t5, $t1 # $t3 = $t5 * $t1 -> index = offset * 4
  23. add $t5, $t4, $t3 # $t5 = $t4 + $t3 -> address = numere + index
  24.  
  25. lw $t2, ($t5) # $t2 = MEM(numere[offset])
  26. # if we didn't put parantheses, the assembler would have thought that we want to load the ADDRESS value (0xWhatever)
  27.  
  28. add $t0, $t0, $t2 # $t0 = $t0 + $t2 -> sum = sum + numere[offset]
  29.  
  30. sub $t1, $t1, 1 # $t1 = $t1 - 1 -> offset = offset - 1
  31. bgez $t1, loop # if offset >= 0 then loop
  32.  
  33. puts outs # print the string
  34. puti $t0 # print the sum
  35.  
  36. putc '\n' # print two new lines
  37. putc '\n'
  38.  
  39.  
  40. # alternatively, we could have used the following code to print the sum
  41. li $v0, 4 # $v0 = 4 -> the print string syscall
  42. la $a0, outs # $a0 = outs -> the string to be printed
  43. syscall # print the string
  44.  
  45. li $v0, 1 # $v0 = 1 -> the print integer syscall
  46. move $a0, $t0 # $a0 = $t0 -> the integer to be printed
  47. syscall # print the integer
  48.  
  49. li $t1, 10
  50. div $t0, $t1 # Lo = $t0 / $t1, Hi = $t0 % $t1
  51. mflo $t2 # $t2 = Lo -> $t2 = $t0 / $t1
  52. mfhi $t3 # $t3 = Hi -> $t3 = $t0 % $t1
  53.  
  54. putc '\n' # print a new line
  55. puts quot # print the string
  56. puti $t2 # print the integer $t2
  57.  
  58. putc '\n' # print a new line
  59. puts remn # print the string
  60. puti $t3 # print the integer $t3
  61.  
  62. li $v0, 10 # $v0 = 10 -> the exit syscall
  63. syscall # exit
  64.  
Advertisement
Add Comment
Please, Sign In to add comment