Advertisement
Guest User

MIPS assembly - function calls example

a guest
Dec 4th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .text:00400660      # =============== S U B R O U T I N E =======================================
  2. .text:00400660
  3. .text:00400660
  4. .text:00400660                     .globl c_function1
  5. .text:00400660     c_function1:                             # CODE XREF: main+28p
  6. .text:00400660
  7. .text:00400660     old_fp_offset   = -8
  8. .text:00400660     old_ra_offset   = -4
  9. .text:00400660
  10. .text:00400660 000                 addiu   $sp, -0x18       # Allocate 0x18 bytes on the stack
  11. .text:00400664 018                 sw      $ra, 0x18+old_ra_offset($sp)  # push the value from $ra (return address) to the stack
  12. .text:00400668 018                 sw      $fp, 0x18+old_fp_offset($sp)  # push the value of $fp (frame pointer) to the stack
  13. .text:0040066C 018                 move    $fp, $sp         # set $fp = $sp
  14. .text:00400670 018                 lui     $v0, 0x40        # Load 0x40 as the MSB in $v0
  15. .text:00400674 018                 addiu   $a0, $v0, (aInsideC_functi - 0x400000)  # Load the offset to the "Inside c_function1" string into $a0
  16. .text:00400678 018                 lui     $v0, 0x40        # Load 0x40 as the MSB in $v0
  17. .text:0040067C 018                 addiu   $t9, $v0, (_puts - 0x400000)  # Load the offset to puts in $t9
  18. .text:00400680 018                 jalr    $t9 ; _puts      # call puts($a0)
  19. .text:00400684 018                 nop
  20. .text:00400688 018                 li      $v0, 5           # $v0 = 5 (to be returned)
  21. .text:0040068C 018                 move    $sp, $fp         # Restore $sp (stack pointer) from $fp (frame pointer)
  22. .text:00400690 018                 lw      $ra, 0x18+old_ra_offset($sp)  # pop the return address ($ra)
  23. .text:00400694 018                 lw      $fp, 0x18+old_fp_offset($sp)  # pop the old frame pointer ($fp)
  24. .text:00400698 018                 addiu   $sp, 0x18        # Move the stack pointer back (free memory)
  25. .text:0040069C 000                 jr      $ra              # ret (return from function call)
  26. .text:004006A0 000                 nop
  27. .text:004006A0      # End of function c_function1
  28. .text:004006A0
  29. .text:004006A4
  30. .text:004006A4      # =============== S U B R O U T I N E =======================================
  31. .text:004006A4
  32. .text:004006A4
  33. .text:004006A4                     .globl c_function2
  34. .text:004006A4     c_function2:                             # CODE XREF: main+3Cp
  35. .text:004006A4
  36. .text:004006A4     old_fp_offset   = -8
  37. .text:004006A4     old_ra_offset   = -4
  38. .text:004006A4     arg_0_offset    =  0
  39. .text:004006A4
  40. .text:004006A4 000                 addiu   $sp, -0x18       # Allocate 0x18 bytes on the stack
  41. .text:004006A8 018                 sw      $ra, 0x18+old_ra_offset($sp)  # Push the value of the return address on the stack ($ra)
  42. .text:004006AC 018                 sw      $fp, 0x18+old_fp_offset($sp)  # Push the value of the frame pointer on the stack ($fp)
  43. .text:004006B0 018                 move    $fp, $sp         # Initialize the new frame pointer ($fp = $sp)
  44. .text:004006B4 018                 sw      $a0, 0x18+arg_0_offset($fp)  # Store the 1st argument ($a0) to the stack (so that it doesn't get lost)
  45. .text:004006B8 018                 lui     $v0, 0x40        # Load Upper Immediate
  46. .text:004006BC 018                 addiu   $a0, $v0, (aInsideC_func_0 - 0x400000)  # Prepare $a0 with the address of the "Inside c_function2" string
  47. .text:004006C0 018                 lw      $a1, 0x18+arg_0_offset($fp)  # Load arg0 from the stack as $a1 for the next function call
  48. .text:004006C4 018                 lui     $v0, 0x40        # Load Upper Immediate
  49. .text:004006C8 018                 addiu   $t9, $v0, (_printf - 0x400000)  # Prepare the address of printf
  50. .text:004006CC 018                 jalr    $t9 ; _printf    # call printf($a0, $a1)
  51. .text:004006D0 018                 nop
  52. .text:004006D4 018                 move    $sp, $fp         # $sp = $fp
  53. .text:004006D8 018                 lw      $ra, 0x18+old_ra_offset($sp)  # pop return address from the stack ($ra)
  54. .text:004006DC 018                 lw      $fp, 0x18+old_fp_offset($sp)  # pop old frame pointer from the stack ($fp)
  55. .text:004006E0 018                 addiu   $sp, 0x18        # Free 0x18 bytes of stack
  56. .text:004006E4 000                 jr      $ra              # ret - return from procedure call
  57. .text:004006E8 000                 nop
  58. .text:004006E8      # End of function c_function2
  59. .text:004006E8
  60. .text:004006EC
  61. .text:004006EC      # =============== S U B R O U T I N E =======================================
  62. .text:004006EC
  63. .text:004006EC
  64. .text:004006EC                     .globl c_function3
  65. .text:004006EC     c_function3:                             # CODE XREF: main+4Cp
  66. .text:004006EC
  67. .text:004006EC     old_fp_offset   = -8
  68. .text:004006EC     old_ra_offset   = -4
  69. .text:004006EC     arg_0_offset    =  0
  70. .text:004006EC     arg_1_offset    =  4
  71. .text:004006EC
  72. .text:004006EC 000                 addiu   $sp, -0x18       # Allocate 0x18 bytes on the stack
  73. .text:004006F0 018                 sw      $ra, 0x18+old_ra_offset($sp)  # Push the value of the return address on the stack ($ra)
  74. .text:004006F4 018                 sw      $fp, 0x18+old_fp_offset($sp)  # Push the value of the frame pointer on the stack ($fp)
  75. .text:004006F8 018                 move    $fp, $sp         #  Initialize the new frame pointer ($fp = $sp)
  76. .text:004006FC 018                 sw      $a0, 0x18+arg_0_offset($fp)  # Store the 1st argument ($a0) to the stack (so that it doesn't get lost)
  77. .text:00400700 018                 sw      $a1, 0x18+arg_1_offset($fp)  # Store the 2nd argument ($a1) to the stack (so that it doesn't get lost)
  78. .text:00400704 018                 lui     $v0, 0x40        # Load Upper Immediate
  79. .text:00400708 018                 addiu   $a0, $v0, (aInsideC_func_1 - 0x400000)  # Prepare $a0 with the address of "Inside c_function3"
  80. .text:0040070C 018                 lw      $a1, 0x18+arg_0_offset($fp)  # Save $a1 = arg_0
  81. .text:00400710 018                 lw      $a2, 0x18+arg_1_offset($fp)  # Save $a2 = arg_1
  82. .text:00400714 018                 lui     $v0, 0x40        # Load Upper Immediate
  83. .text:00400718 018                 addiu   $t9, $v0, (_printf - 0x400000)  # Get the address of printf
  84. .text:0040071C 018                 jalr    $t9 ; _printf    # Call printf($a0, $a1, $a2)
  85. .text:00400720 018                 nop
  86. .text:00400724 018                 lw      $v1, 0x18+arg_0_offset($fp)  # $v1 = arg_0 (from stack)
  87. .text:00400728 018                 lw      $v0, 0x18+arg_1_offset($fp)  # $v0 = arg_1
  88. .text:0040072C 018                 addu    $v0, $v1, $v0    # $v0 = $v1 + $v0
  89. .text:00400730 018                 move    $sp, $fp         # $sp = $fp
  90. .text:00400734 018                 lw      $ra, 0x18+old_ra_offset($sp)  # pop $ra
  91. .text:00400738 018                 lw      $fp, 0x18+old_fp_offset($sp)  # pop $fp
  92. .text:0040073C 018                 addiu   $sp, 0x18        # free 0x18 bytes of stack
  93. .text:00400740 000                 jr      $ra              # ret - return from procedure call
  94. .text:00400744 000                 nop
  95. .text:00400744      # End of function c_function3
  96. .text:00400744
  97. .text:00400748
  98. .text:00400748      # =============== S U B R O U T I N E =======================================
  99. .text:00400748
  100. .text:00400748
  101. .text:00400748                     .globl c_function4
  102. .text:00400748     c_function4:                             # CODE XREF: main+70p
  103. .text:00400748
  104. .text:00400748     arg_3_converted_offset= -0x10
  105. .text:00400748     old_fp_offset   = -8
  106. .text:00400748     old_ra_offset   = -4
  107. .text:00400748     arg_0_offset    =  0
  108. .text:00400748     arg_1_offset    =  4
  109. .text:00400748     arg_2_offset    =  8
  110. .text:00400748     arg_3_offset    =  0xC
  111. .text:00400748
  112. .text:00400748 000                 addiu   $sp, -0x20       # Allocate 0x20 bytes on the stack
  113. .text:0040074C 020                 sw      $ra, 0x20+old_ra_offset($sp)  # Push the value of the return address on the stack ($ra)
  114. .text:00400750 020                 sw      $fp, 0x20+old_fp_offset($sp)  # Push the value of the frame pointer on the stack ($fp)
  115. .text:00400754 020                 move    $fp, $sp         # Initialize the new frame pointer ($fp = $sp)
  116. .text:00400758 020                 sw      $a0, 0x20+arg_0_offset($fp)  # Store the 1st argument ($a0) to the stack (so that it doesn't get lost)
  117. .text:0040075C 020                 sw      $a1, 0x20+arg_1_offset($fp)  # Store the 2nd argument ($a1) to the stack (so that it doesn't get lost)
  118. .text:00400760 020                 move    $v0, $a2         # $v0 = $a2
  119. .text:00400764 020                 sw      $a3, 0x20+arg_3_offset($fp)  # Store the 4th argument ($a3) to the stack (so that it doesn't get lost)
  120. .text:00400768 020                 sb      $v0, 0x20+arg_2_offset($fp)  # Store the 3rd argument ($a3 -> $v0) into the stack as a byte
  121. .text:0040076C 020                 lb      $v1, 0x20+arg_2_offset($fp)  # Load $arg_3 into $v1
  122. .text:00400770 020                 lwc1    $f0, 0x20+arg_3_offset($fp)  # Load arg_3 as a floating point into $f0
  123. .text:00400774 020                 cvt.d.s $f0, $f0         # Floating-point Convert to Double Floating-Point Format
  124. .text:00400778 020                 sdc1    $f0, 0x20+arg_3_converted_offset($sp)  # Store Double FPU
  125. .text:0040077C 020                 lui     $v0, 0x40        # Load Upper Immediate
  126. .text:00400780 020                 addiu   $a0, $v0, (aInsideC_func_2 - 0x400000)  # Put the address of "Inside c_function4" in $a0
  127. .text:00400784 020                 lw      $a1, 0x20+arg_0_offset($fp)  # $a1 = arg_0
  128. .text:00400788 020                 lw      $a2, 0x20+arg_1_offset($fp)  # $a2 = $arg_1
  129. .text:0040078C 020                 move    $a3, $v1         # $a3 = $v1
  130. .text:00400790 020                 lui     $v0, 0x40        # Load Upper Immediate
  131. .text:00400794 020                 addiu   $t9, $v0, (_printf - 0x400000)  # Load $t9 with the address of printf
  132. .text:00400798 020                 jalr    $t9 ; _printf    # call printf($a0, $a1, $a2, $a3 ...)
  133. .text:0040079C 020                 nop
  134. .text:004007A0 020                 lw      $v1, 0x20+arg_0_offset($fp)  # $v1 = arg_0
  135. .text:004007A4 020                 lw      $v0, 0x20+arg_1_offset($fp)  # $v0 = $arg_1
  136. .text:004007A8 020                 addu    $v0, $v1, $v0    # $v0 = $v1 + $v0
  137. .text:004007AC 020                 move    $sp, $fp         # $sp = $fp
  138. .text:004007B0 020                 lw      $ra, 0x20+old_ra_offset($sp)  # pop $ra
  139. .text:004007B4 020                 lw      $fp, 0x20+old_fp_offset($sp)  # pop $fp
  140. .text:004007B8 020                 addiu   $sp, 0x20        # free 0x20 bytes from the stack
  141. .text:004007BC 000                 jr      $ra              # ret - return from procedure call
  142. .text:004007C0 000                 nop
  143. .text:004007C0      # End of function c_function4
  144. .text:004007C0
  145. .text:004007C4
  146. .text:004007C4      # =============== S U B R O U T I N E =======================================
  147. .text:004007C4
  148. .text:004007C4
  149. .text:004007C4                     .globl main
  150. .text:004007C4     main:                                    # DATA XREF: _ftext+Co
  151. .text:004007C4
  152. .text:004007C4     result_offset   = -0x10
  153. .text:004007C4     old_fp_offset   = -8
  154. .text:004007C4     old_ra_offset   = -4
  155. .text:004007C4
  156. .text:004007C4 000                 addiu   $sp, -0x20       # Allocate 0x20 bytes on the stack
  157. .text:004007C8 020                 sw      $ra, 0x20+old_ra_offset($sp)  # push the value of the return address ($ra) on the stack
  158. .text:004007CC 020                 sw      $fp, 0x20+old_fp_offset($sp)  # push the old frame pointer ($fp) to the stack
  159. .text:004007D0 020                 move    $fp, $sp         # Allocate the new $fp
  160. .text:004007D4 020                 lui     $v0, 0x40        # Load Upper Immediate
  161. .text:004007D8 020                 addiu   $a0, $v0, (aMain___ - 0x400000)  # prepare parameter 0 ("main...") in $a0
  162. .text:004007DC 020                 lui     $v0, 0x40        # Load Upper Immediate
  163. .text:004007E0 020                 addiu   $t9, $v0, (_puts - 0x400000)  # Put the address of puts in a temporary register $t9
  164. .text:004007E4 020                 jalr    $t9 ; _puts      # call puts($a0)
  165. .text:004007E8 020                 nop
  166. .text:004007EC 020                 jal     c_function1      # call c_function1()
  167. .text:004007F0 020                 nop
  168. .text:004007F4 020                 li      $v0, 0x58        # $v0 = 0x58 (int result=88);
  169. .text:004007F8 020                 sw      $v0, 0x20+result_offset($fp)  # push $v0 to the stack (relative to $fp) for storage
  170. .text:004007FC 020                 li      $a0, 8           # $a0 = 8
  171. .text:00400800 020                 jal     c_function2      # call c_function2($a0)
  172. .text:00400804 020                 nop
  173. .text:00400808 020                 lw      $a0, 0x20+result_offset($fp)  # load result back from stack into $a0 as a parameter of next function call
  174. .text:0040080C 020                 li      $a1, 0xC         # load second parameter $a1 = 12
  175. .text:00400810 020                 jal     c_function3      # call c_function3($a0, $a1)
  176. .text:00400814 020                 nop
  177. .text:00400818 020                 sw      $v0, 0x20+result_offset($fp)  # save the function result ($v0) back on the stack
  178. .text:0040081C 020                 lui     $v0, 0x40        # Load Upper Immediate
  179. .text:00400820 020                 lwc1    $f0, float_314159  # Load 3.14159 as a float into the FPU
  180. .text:00400824 020                 lw      $a0, 0x20+result_offset($fp)  # Load result from the stack into $a0 as a parameter for the next function
  181. .text:00400828 020                 li      $a1, 0x26        # Load 38 (0x26) as the second parameter for the function call
  182. .text:0040082C 020                 li      $a2, 0x4D        # Load 0x4D (the ASCII code for the character M) as the 3rd parameter
  183. .text:00400830 020                 mfc1    $a3, $f0         # Load the floating point number from $f0 to $a3 as the 4th argument
  184. .text:00400834 020                 jal     c_function4      # call (c_function4($a0, $a1, $a2, $a3))
  185. .text:00400838 020                 nop
  186. .text:0040083C 020                 sw      $v0, 0x20+result_offset($fp)  # Save the function result back on the stack as result
  187. .text:00400840 020                 lui     $v0, 0x40        # Load Upper Immediate
  188. .text:00400844 020                 addiu   $a0, $v0, (aFinalResultD - 0x400000)  # Prepare the "Final result:" string
  189. .text:00400848 020                 lw      $a1, 0x20+result_offset($fp)  # Copy the actual result as the second parameter
  190. .text:0040084C 020                 lui     $v0, 0x40        # Load Upper Immediate
  191. .text:00400850 020                 addiu   $t9, $v0, (_printf - 0x400000)  # Put printf's address in $t9
  192. .text:00400854 020                 jalr    $t9 ; _printf    # Call printf($a0, $a1)
  193. .text:00400858 020                 nop
  194. .text:0040085C 020                 move    $v0, $zero       # Ignore printf's return value and write zeros over it
  195. .text:00400860 020                 move    $sp, $fp         # $sp = $fp
  196. .text:00400864 020                 lw      $ra, 0x20+old_ra_offset($sp)  # Restore (pop) return address $ra
  197. .text:00400868 020                 lw      $fp, 0x20+old_fp_offset($sp)  # Restore (pop) old frame pointer
  198. .text:0040086C 020                 addiu   $sp, 0x20        # Free 0x20 bytes of stack memory
  199. .text:00400870 000                 jr      $ra              # ret (return from procedure call)
  200. .text:00400874 000                 nop
  201. .text:00400874      # End of function main
  202. .text:00400874
  203. .text:00400874      # ---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement