summaryrefslogtreecommitdiff
path: root/PGU/OLD/chapter4/libpower.s
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2025-07-10 22:55:07 +0200
committerCarlos Maiolino <[email protected]>2025-07-10 22:56:55 +0200
commitd98f46ce647846b0aa30b2e16a30fd4e152a1bf5 (patch)
tree267474fcc77cf20b428f6f4c7f768ca09f4cfe0e /PGU/OLD/chapter4/libpower.s
parent869e68986aa8f69af6e7842260a68d1e5c6f796f (diff)
Add new code
Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'PGU/OLD/chapter4/libpower.s')
-rw-r--r--PGU/OLD/chapter4/libpower.s48
1 files changed, 48 insertions, 0 deletions
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