Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #This program is meant to ask a base (then print it to check
- #if it's interpreted correctly) and an exponent (then print that
- #too), then do the calculation and print out the result. I don't know
- #why it doesn't work, so please help me. This is what
- #the code would look like in Java: http://i.imgur.com/njBXecr.png
- .text
- string: .asciz "Jelster\nAssignment 2: power\n"
- enterbase: .asciz "Enter your non-negative base: "
- enterexp: .asciz "Enter your non-negative exponent: "
- base: .asciz "%ld"
- exp: .asciz "%ld"
- result: .asciz "Result is: %ld"
- .global main #main routine
- main:
- pushq %rbp #initialize base pointer
- movq %rsp, %rbp
- subq $16, %rsp
- call writestring #goes to the start of writestring
- call getbase #goes to the start of getbase
- movq %rax, -8(%rbp)
- call getexp #goes to the start of getexp
- movq %rax, -16(%rbp)
- movq -8(%rbp), %rdi
- movq -16(%rbp), %rsi
- call pow #goes to start of pow
- movq %rax, %rsi #load first argument of printf
- movq $result, %rdi
- movq $0, %rax #no vector arguments
- call printf #prints result
- movq %rbp, %rsp #sets base pointer to its original position
- pop %rbp
- call end #goes to start of end
- writestring:
- movq $string, %rdi #writes string to register
- movq $0, %rax #no vector arguments
- call printf #prints string
- ret
- getbase:
- pushq %rbp #initialize base pointer
- movq %rsp, %rbp
- movq $enterbase, %rdi#writes enterbase to register
- movq $0, %rax #no vector arguments
- call printf #prints enterbase
- subq $8, %rsp #reserve stack space for base variable
- leaq -8(%rbp), %rsi #load address of stack var in rsi
- movq $base, %rdi #load first argument of scanf
- movq $0, %rax #no vector registers for scanf
- call scanf
- movq $base, %rdi #load first argument of printf
- movq -8(%rbp), %rsi #load second argument of printf: number
- #in address -8 to rsi
- movq $0, %rax #no vector arguments
- call printf #prints base
- movq %rbp, %rsp #sets base pointer to its original position
- pop %rbp
- ret
- getexp:
- pushq %rbp #initialize base pointer
- movq %rsp, %rbp
- movq $enterexp, %rdi #writes enterexp to register
- movq $0, %rax #no vector arguments
- call printf #prints enterexp
- subq $8, %rsp #reserve stack space for exp variable
- leaq -8(%rbp), %rsi #load address of stack var in rsi
- movq $exp, %rdi #load first argument of scanf
- movq $0, %rax #no vector registers for scanf
- call scanf
- movq $exp, %rdi #load first argument of printf
- movq -8(%rbp), %rsi #load second argument of printf: number
- #in address -16 to rsi
- movq $0, %rax #no vector arguments
- call printf #prints base
- movq %rsi, %rax
- movq %rbp, %rsp #sets base pointer to its original position
- pop %rbp
- ret
- pow:
- pushq %rbp #initialize base pointer
- movq %rsp, %rbp
- movq $1, %rax #sets rax to 1 (this is int res)
- movq $0, %rcx #sets rcx to 0 (this is int i)
- jmp loop
- loop:
- cmpq %rsi, %rcx #if rcx (i) => rsi (exp) jump to endloop
- jge endloop
- imulq %rdi #multiplies rax (res) with rdi (base)
- incq %rcx #increments rcx by 1
- jmp loop #repeat the loop
- endloop:
- movq %rbp, %rsp #sets base pointer to its original position
- pop %rbp
- ret
- end:
- movq $0, %rdi #program ran succesfully
- call exit #exits program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement