The Stack
The Stack ARMSim memory space: 0x11400 + Unused 0x09400-0x11400 Stack Heap 0x?????-0x01400 Data 0x01000-0x????? Text 0x00000-0x01000 Reserved
Stack possibilites How to use the space in stack? Grow up Keep track of top item in stack (Full Ascending) Keep track of first empty slot (Empty Ascending)
Stack possibilites How to use the space in stack? Grow down Keep track of top item in stack (Full Descending) Keep track of first empty slot (Empty Descending)
Stack possibilites ARM Convention : Full Descending R13 Holds "top" of stack
Push Recipe Subtract 4 from sp … 81818181 0x113F4 0x113F8 0x113FC ????????? sp 0x11400
Push Recipe Subtract 4 from sp … 81818181 0x113F4 0x113F8 0x113FC ????????? sp 0x113FC
Push Recipe Subtract 4 from sp STR data … 81818181 0x113F4 0x113F8 0x113FC AA 0x11400 ????????? sp 0x113FC
Push Recipe Subtract 4 from sp … 81818181 0x113F4 0x113F8 0x113FC AA ????????? sp 0x113F8
Push Recipe Subtract 4 from sp STR data … 81818181 0x113F4 0x113F8 BB 0x113FC AA 0x11400 ????????? sp 0x113F8
Pop Recipe LDR data … 81818181 0x113F4 0x113F8 BB 0x113FC AA 0x11400 ????????? sp 0x113F8
Pop Recipe LDR data Add 4 to sp … 81818181 0x113F4 0x113F8 BB 0x113FC ????????? sp 0x113FC
Pop Recipe LDR data … 81818181 0x113F4 0x113F8 BB 0x113FC AA 0x11400 ????????? sp 0x113FC
Pop Recipe LDR data Add 4 to sp … 81818181 0x113F4 0x113F8 BB 0x113FC ????????? sp 0x11400
Push / Pop Always tear down stack in reverse order Push A Push B Pop B Time
Machine Push/Pop Common need : machine instructions Store Multiple to Full Descending use sp and update it (!) store r1 and r2
Machine Push/Pop Common need : machine instructions Load Multiple from Full Descending use sp and update it (!) load r2 then r1 (reverse order)
Pseudo Instructions Easier pseudinstructions: PUSH {register list} POP {register list}
Function Calls
Assembly Function Assembly Functions are branches
Assembly Function Assembly Functions are branches Need to know where to return to when done!
Assembly Function BL label Branch and Link Branch (change PC) But store Store next instruction address in R14 (lr = link register)
Assembly Function BL label Branch and Link Branch (change PC) But store Store next instruction address in R14 (lr = link register)
Assembly Function Return Copy lr back into pc
Assembly Function Return Copy lr back into pc
UDiv Unsigned division function Parameters in r0, r1 Results in r0, r1
UDiv Calling from main Parameters in r0, r1 Results in r0, r1
Using Functions GetTime program Get system time Do lots of divisions to get Hour, Min, Sec
Calling Conventions
Calling Convetions Calling Convention Agreement for how subroutines work How we pass info What registers we can use … Differs between platforms, compilers, etc…
ARM Conventions ARM Register Conventions r0-r3 r4-r9 r10-r15 Register Range Use r0-r3 -Passing parameters & results -Temporary values r4-r9 -Storing information r10-r15 Special purpose
ARM Conventions ARM Register Conventions r0-r3 r4-r9 r10-r15 Register Range Use Caller Function r0-r3 -Passing parameters & results -Temporary values These may get destroyed. Store/restore values if you care about them!!! Change these all you want… r4-r9 -Storing information No worries… If you want to use these you MUST store/restore data that was there!!! r10-r15 Special purpose Only use for intended purpose
ABS Absolute Value Does what it wants with r0-r3 Makes use of r4 and r5 – must save
ABS Main that calls abs must store any r0-r3 used across function call r4+ are safe
Smarter ABS If function ONLY uses r0-r3 to do work, nothing to save:
Smarter ABS If main only uses r0-r3 between function calls, nothing to save
Nested Calls
Nested Calls Real functions often call other functions:
Nested Calls Multiple "return addresses" needed Proc_A needs address D Proc_B needs address B
Nested Calls Multiple "return addresses" needed Proc_A needs address D Proc_B needs address B Store return addresses on stack
Calling Convention Update If function call other functions: Save/restore your return address (lr) to stack with registers r4+
Process Review Subroutine Call (done by the caller): Push any of r0-r3 that are in use Put argument values into r0-r3 Call the subroutine using bl
Process Review Subroutine Prolog (done to start subroutine): If we want to use r4+ push them If we will call other functions, push lr also Get parameters from r0-r3
Process Review Subroutine Body Use r0-r3 and any from r4+ you saved
Process Review Subroutine Epilog (done at end of sub): Put returned values in r0-r3 Pop from the stack anything you saved (r4+, lr) Return by moving lr to pc
Process Review Regaining Control (done by the caller): Get results from r0-r3 Pop any old r0-r3 values you stored before call