mirror of
https://github.com/janishutz/eth-summaries.git
synced 2026-01-13 21:08:28 +00:00
[SPCA] Improve Stack section
This commit is contained in:
12
semester3/spca/code-examples/01_asm/06_factorial.s
Normal file
12
semester3/spca/code-examples/01_asm/06_factorial.s
Normal file
@@ -0,0 +1,12 @@
|
||||
factorial:
|
||||
pushq %rbx # Preserve frame pointer
|
||||
movl %edi, %ebx
|
||||
movl $1, %eax
|
||||
cmpl $1, %edi
|
||||
jle .QUIT # Base case reached: quit
|
||||
leal -1(%rdi), %edi # Prepare args for next function call
|
||||
call factorial
|
||||
imull %ebx, %eax # Use result of function call
|
||||
.QUIT:
|
||||
popq %rbx # Restore frame pointer
|
||||
ret
|
||||
15
semester3/spca/code-examples/01_asm/07_swap_and_sum.s
Normal file
15
semester3/spca/code-examples/01_asm/07_swap_and_sum.s
Normal file
@@ -0,0 +1,15 @@
|
||||
swap_and_sum:
|
||||
movq %rbx, -16(%rsp) # Save %rbx
|
||||
movslq %esi,%rbx # Save i (and extend)
|
||||
movq %r12, -8(%rsp) # Save %r12
|
||||
movq %rdi, %r12 # Save a
|
||||
leaq (%rdi,%rbx,8), %rdi # & a[i] -> %rdi (arg 1)
|
||||
subq $16, %rsp # Allocate stack frame
|
||||
leaq 8(%rdi), %rsi # & a[i+1] -> %rsi (arg 2)
|
||||
call swap
|
||||
movq (%r12,%rbx,8), %rax # a[i]
|
||||
addq %rax, sum(%rip) # sum += a[i]
|
||||
movq (%rsp), %rbx # Restore %rbx
|
||||
movq 8(%rsp), %r12 # Restore %r12
|
||||
addq $16, %rsp # Deallocate stack frame
|
||||
ret
|
||||
Reference in New Issue
Block a user