Advertisement
Jelster

Assembly assignment

Oct 10th, 2017
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. #This program is meant to ask a base (then print it to check
  2. #if it's interpreted correctly) and an exponent (then print that
  3. #too), then do the calculation and print out the result. I don't know
  4. #why it doesn't work, so please help me. This is what
  5. #the code would look like in Java: http://i.imgur.com/njBXecr.png
  6.  
  7. .text
  8. string: .asciz "Jelster\nAssignment 2: power\n"
  9. enterbase: .asciz "Enter your non-negative base: "
  10. enterexp: .asciz "Enter your non-negative exponent: "
  11. base: .asciz "%ld"
  12. exp: .asciz "%ld"
  13. result: .asciz "Result is: %ld"
  14.  
  15. .global main #main routine
  16.  
  17. main:
  18. pushq %rbp #initialize base pointer
  19. movq %rsp, %rbp
  20. subq $16, %rsp
  21. call writestring #goes to the start of writestring
  22. call getbase #goes to the start of getbase
  23. movq %rax, -8(%rbp)
  24. call getexp #goes to the start of getexp
  25. movq %rax, -16(%rbp)
  26. movq -8(%rbp), %rdi
  27. movq -16(%rbp), %rsi
  28. call pow #goes to start of pow
  29.  
  30. movq %rax, %rsi #load first argument of printf
  31. movq $result, %rdi
  32. movq $0, %rax #no vector arguments
  33. call printf #prints result
  34.  
  35. movq %rbp, %rsp #sets base pointer to its original position
  36. pop %rbp
  37.  
  38. call end #goes to start of end
  39.  
  40. writestring:
  41. movq $string, %rdi #writes string to register
  42. movq $0, %rax #no vector arguments
  43. call printf #prints string
  44. ret
  45.  
  46. getbase:
  47. pushq %rbp #initialize base pointer
  48. movq %rsp, %rbp
  49.  
  50. movq $enterbase, %rdi#writes enterbase to register
  51. movq $0, %rax #no vector arguments
  52. call printf #prints enterbase
  53.  
  54. subq $8, %rsp #reserve stack space for base variable
  55. leaq -8(%rbp), %rsi #load address of stack var in rsi
  56. movq $base, %rdi #load first argument of scanf
  57. movq $0, %rax #no vector registers for scanf
  58. call scanf
  59.  
  60. movq $base, %rdi #load first argument of printf
  61. movq -8(%rbp), %rsi #load second argument of printf: number
  62. #in address -8 to rsi
  63. movq $0, %rax #no vector arguments
  64. call printf #prints base
  65.  
  66. movq %rbp, %rsp #sets base pointer to its original position
  67. pop %rbp
  68. ret
  69.  
  70. getexp:
  71. pushq %rbp #initialize base pointer
  72. movq %rsp, %rbp
  73.  
  74. movq $enterexp, %rdi #writes enterexp to register
  75. movq $0, %rax #no vector arguments
  76. call printf #prints enterexp
  77.  
  78. subq $8, %rsp #reserve stack space for exp variable
  79. leaq -8(%rbp), %rsi #load address of stack var in rsi
  80. movq $exp, %rdi #load first argument of scanf
  81. movq $0, %rax #no vector registers for scanf
  82. call scanf
  83.  
  84. movq $exp, %rdi #load first argument of printf
  85. movq -8(%rbp), %rsi #load second argument of printf: number
  86. #in address -16 to rsi
  87. movq $0, %rax #no vector arguments
  88. call printf #prints base
  89.  
  90. movq %rsi, %rax
  91.  
  92. movq %rbp, %rsp #sets base pointer to its original position
  93. pop %rbp
  94. ret
  95.  
  96. pow:
  97. pushq %rbp #initialize base pointer
  98. movq %rsp, %rbp
  99.  
  100. movq $1, %rax #sets rax to 1 (this is int res)
  101. movq $0, %rcx #sets rcx to 0 (this is int i)
  102.  
  103. jmp loop
  104.  
  105. loop:
  106. cmpq %rsi, %rcx #if rcx (i) => rsi (exp) jump to endloop
  107. jge endloop
  108.  
  109. imulq %rdi #multiplies rax (res) with rdi (base)
  110. incq %rcx #increments rcx by 1
  111.  
  112. jmp loop #repeat the loop
  113.  
  114. endloop:
  115. movq %rbp, %rsp #sets base pointer to its original position
  116. pop %rbp
  117. ret
  118.  
  119. end:
  120. movq $0, %rdi #program ran succesfully
  121. call exit #exits program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement