Silver_Smoulder

[ASM] [Almost Functional] Functions Calling Functions

Nov 20th, 2019
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. .text
  2. .globl main
  3.  
  4. main:
  5. li $t0, 0 #initialize t0
  6. li $t1, 0 #initialize t1
  7.  
  8. jal input #call input
  9.  
  10. li $v0, 10 #exit
  11. syscall
  12.  
  13. input:
  14. sub $sp, $sp, 4
  15. sw $ra, 0($sp) #store the value in $ra on the stack position 4
  16.  
  17. li $v0, 4 #prompt stuff
  18. la $a0, prompt
  19. syscall
  20.  
  21. li $v0, 5 #get number
  22. syscall
  23.  
  24. move $a0, $v0 #store number
  25. #we can keep number in a0
  26.  
  27. jal check
  28.  
  29. check:
  30. sub $sp, $sp, 4
  31. sw $ra, 0($sp) #store the value in $ra on the stack position 8
  32.  
  33. #bltz $a0, negative
  34. #bgtz $a0, positive
  35. #beqz $a0, zero
  36.  
  37. jal addnum
  38. jal output
  39.  
  40. addnum:
  41. sub $sp, $sp, 4
  42. sw $ra, 0($sp) #store the value in $ra on the stack position 12
  43.  
  44. bltz $a0, negative
  45. bgtz $a0, positive
  46. beqz $a0, zero
  47.  
  48. negative:
  49. add $t0, $t0, $a0
  50.  
  51. lw $ra, 0($sp)
  52. addu $sp, $sp, 8 #go back 8 spaces, to input
  53.  
  54. jr $ra
  55.  
  56. positive:
  57. add $t1, $t1, $a0
  58.  
  59. lw $ra, 0($sp)
  60. addu $sp, $sp, 8 #go back 8 spaces, to input
  61.  
  62. jr $ra
  63.  
  64. zero:
  65. lw $ra, 0($sp)
  66. addu $sp, $sp, 8 #go back 4 spaces, to check
  67.  
  68. jr $ra
  69.  
  70. output:
  71. li $v0, 4
  72. la $a0, posi_sum
  73. syscall
  74.  
  75. move $a0, $t1
  76. li $v0, 1 #output positive sums
  77.  
  78. li $v0, 4
  79. la $a0, nega_sum
  80. syscall
  81.  
  82. move $a0, $t0
  83. li $v0, 1 #output negative sum
  84.  
  85. lw $ra, 0($sp)
  86. addu $sp, $sp, 12 #go back to main
  87.  
  88. jr $ra
  89. .data
  90.  
  91. prompt:
  92. .asciiz "\nInput your number: "
  93.  
  94. posi_sum:
  95. .asciiz "\nYour positive sum is: "
  96.  
  97. nega_sum:
  98. .asciiz "\nYour negative sum is: "
Advertisement
Add Comment
Please, Sign In to add comment