From d98f46ce647846b0aa30b2e16a30fd4e152a1bf5 Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Thu, 10 Jul 2025 22:55:07 +0200 Subject: Add new code Signed-off-by: Carlos Maiolino --- PGU/OLD/chapter4/libpower.s | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 PGU/OLD/chapter4/libpower.s (limited to 'PGU/OLD/chapter4/libpower.s') diff --git a/PGU/OLD/chapter4/libpower.s b/PGU/OLD/chapter4/libpower.s new file mode 100644 index 0000000..5172cda --- /dev/null +++ b/PGU/OLD/chapter4/libpower.s @@ -0,0 +1,48 @@ +# Simple example of functions in ASM, following System V ABI +# and C calling convention + +# Everything is stored in registers, so, we have nothing in data section + +#################### +# # +# Function power() # +# # +#################### + +# Remember the return address (where the program should keep executing after +# function return), is also pushed into the stack by the 'call' command. + +# Remember, in x86_64 architecture, the Stack grows 'downwards', so, every time +# we want to look back in the stack, we need to add to the current stack +# pointer, other than subtract. + +# Ex: +# 56 <- stack 'bottom' +# 48 pushq +# 40 pushq +# 32 <- %rsp (top of the stack) + +.type power, @function +power: + pushq %rbp + movq %rsp, %rbp + movq 16(%rbp), %rcx # Holds the Base number. 16 because: + # Current %rsp (pushq %rbp) plus the + # implicit return address pushed by + # the call command + + movq 24(%rbp), %rbx # Holds the power (pushed first before + # function call + movq %rcx, %rax + +loop: + cmpq $1, %rbx + je return + imul %rcx, %rax + decq %rbx + jmp loop + +return: + movq %rbp, %rsp + popq %rbp + ret -- cgit v1.2.3