From 20834dcc57537cd95260a4a22f5d91a027adfd35 Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Thu, 10 Jul 2025 22:20:01 +0200 Subject: Add x86_64 asm Signed-off-by: Carlos Maiolino --- x86_64/exponent.s | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 x86_64/exponent.s (limited to 'x86_64/exponent.s') 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 -- cgit v1.2.3