Commit 7b01dae4 authored by Aaron Erhardt's avatar Aaron Erhardt
Browse files

Add subroutines section


Signed-off-by: default avatarAaron Erhardt <aaron.erhardt@t-online.de>
parent 7821555f
Pipeline #488 passed with stage
in 1 minute and 40 seconds
......@@ -90,4 +90,70 @@ pop {r0-r5} ; pop r0-r5 (in the correct order)
push {lr, r3, r10-12} ; push lr, r3 and r10-r12
pop {lr, r3, r10-12} ; pop lr, r3 and r10-r12
```
\ No newline at end of file
```
# Subroutines
```asm
main
bl label ; run subroutine
label
; push all registers the subroutine changes except returned registers
push {lr, ...}
; ... ; other code
pop {lr, ...} ; pop the registers again
bx lr ; jump back
```
## Pass parameters
### Register
```asm
main
ldr r0, =1 ; initialize parameters
ldr r1, =2
bl add_nums ; run subroutine
; r2 can now be used as returned value
add_nums
push {lr}
add r2, r0, r1 ; add r0 and r1 and return value at r2
pop {lr}
bx lr
```
### Stack
```asm
main
ldr r7, =1 ; initialize parameters
ldr r8, =2
; prepare parameters and return values
push {r7} ; parameter 1
push {r8} ; parameter 2
push {r9} ; return value 1
bl add_nums ; run subroutine
pop {r9} ; get return value from the stack
pop {r8} ; free stack space again
pop {r7} ; free stack space again
add_nums
push {lr, r0-r2} ; increments stack pointer by 4 * (amount of registers)!
; load parameters into registers
ldr r0, [sp, #4*6] ; 4*4 + 4*2 -> r7 (was pushed first, highest address)
ldr r1, [sp, #4*5] ; 4*4 + 4*1 -> r8
add r2, r0, r1 ; perform actual calculation
; store return values on reserved the stack space
str r2, [sp, #4*4] ; 4*4 + 4*0 -> r9
pop {lr, r0-r2}
bx lr
```
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment