Advertisement
pushrbx

MIPS ASM Exercise

Apr 17th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. Question: What is the output of the following assembly code?
  2. Original: http://i1183.photobucket.com/albums/x479/Szenya/532355_521882794525379_1012823075_n_zpse30e6bec.jpg
  3.  
  4. Parsed by me (scarysandwich):
  5.  
  6. OutChar:
  7. lui $v0, 10; /// $v0 = 10 << 16
  8. ori $v0, $v0, 10; /// $v0 | 10 pad it with zeros
  9. syscall
  10. jump to address $ra // jump to return address
  11.  
  12. main:
  13. $s0 = $zero + 1 aka s0 = 1
  14. $s0 << 6 // shift left by 6 so $s0 * (2*6) = 12
  15. $a0 = $s0 + 1 // 13
  16. jump to OutChar // OutChar(13)
  17. $s0 += $s0 // 24
  18. $s1 = $zero + 1 // s1 = 1
  19. $s1 << 4 //shift left aka $s0 * (2*4) // s1 = 8
  20. $a0 = $s0 - $s1 // 16
  21. jump OutChar // OutChar(16)
  22. $s0 = 19
  23. $s1 = 3
  24. $s0 * $s1
  25. $a0 = 57
  26. $a0 << 1 // $a0 * 2
  27. jump OutChar // OutChar(114)
  28. $s2 = $zero + $a0 // 16
  29. $s2 -= $s0 // -8
  30. $s2 += 10 // 18
  31. $a0 = $zero + $s2
  32. jump OutChar // OutChar(18)
  33. $s2 = $s2 + $s1 // 26
  34. $sp = -4 // set stackpointer
  35. memstore $s2 in ($sp + 0) // store $s2 at -4 memory location
  36. $a0 = $s2
  37. jump OutChar // OutChar(26)
  38. $s0 = 1
  39. $s0 << 5 // $s0 = 10
  40. $a0 = $s0
  41. jump OutChar // OutChar(10)
  42. $a0 = $s2 - 6
  43. jump OutChar // OutChar(26)
  44. $s0 = 37
  45. $s0 * $s1
  46. $a0 = 111
  47. jump Outchar // OutChar(111) o
  48. jump OutChar // OutChar(111) o
  49. $a0 = mem[$sp + 0] // load from address -4 (pop stackpointer)
  50. $sp += 4 // 0
  51. jump OutChar // OutChar(26)
  52. $a0 = 111
  53. $a0 += 4
  54. jump OutChar // OutChar(115) s
  55. $a0 = $s0 - 4
  56. jump OutChar // OutChar(33)
  57.  
  58. // note: $a0-$a3 -> function argument registers
  59.  
  60. // C++
  61.  
  62. int OutChar(int v)
  63. {
  64. // call system to write to screen
  65. }
  66.  
  67. int main()
  68. {
  69. int s0 = 12;
  70. OutChar(s0 + 1);
  71. // continue it from here..
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement