diff options
| author | Carlos Maiolino <[email protected]> | 2025-07-10 22:20:01 +0200 |
|---|---|---|
| committer | Carlos Maiolino <[email protected]> | 2025-07-10 22:20:01 +0200 |
| commit | 20834dcc57537cd95260a4a22f5d91a027adfd35 (patch) | |
| tree | 60f21143382380c3cd54110b4134f6fa98000a9b /x86_64/exponent.s | |
| parent | 8c6fc0c15415b32080a848bbde640e104098cf13 (diff) | |
Add x86_64 asm
Signed-off-by: Carlos Maiolino <[email protected]>
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 |
