diff options
Diffstat (limited to 'x86_64/exponent.s')
| -rw-r--r-- | x86_64/exponent.s | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/x86_64/exponent.s b/x86_64/exponent.s new file mode 100644 index 0000000..e469d26 --- /dev/null +++ b/x86_64/exponent.s @@ -0,0 +1,37 @@ +# Compute a^b, where %rbx=a and %rcx=b +# +# Result is added to the accumulator (%rax) + + +.globl _start + +.section .text + +_start: + movq $2, %rbx # Base + movq $5, %rcx # Exponent + + movq $1, %rax # Initialize accumulator, take advantage of the fact + # that anything to the power of 0 is 1. + + # We just need to run this once, as decq will set ZF if %rcx reaches + # zero, and the jmp instruction does nothing with the ZF + cmpq $0, %rcx + je complete + +mainloop: + + # Multiply %rbx by %rax, and stores the result in %rax + # Hence %rax is the accumulator register + mulq %rbx + + # loopq instruction is very nice, in a single instruction it does: + # - decrement %rcx - The counter register - + # - compare %rcx to zero + # - Jump to the location specified if after decrementing %rcx is not 0 + loopq mainloop + +complete: + movq %rax, %rdi + movq $60, %rax + syscall |
