Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Stack. ARMSim memory space: 0x11400 + Unused 0x09400-0x11400 Stack 0x01400-0x09400 Heap 0x?????-0x01400 Data 0x01000-0x????? Text 0x00000-0x01000.

Similar presentations


Presentation on theme: "The Stack. ARMSim memory space: 0x11400 + Unused 0x09400-0x11400 Stack 0x01400-0x09400 Heap 0x?????-0x01400 Data 0x01000-0x????? Text 0x00000-0x01000."— Presentation transcript:

1 The Stack

2 ARMSim memory space: 0x11400 + Unused 0x09400-0x11400 Stack 0x01400-0x09400 Heap 0x?????-0x01400 Data 0x01000-0x????? Text 0x00000-0x01000 Reserved

3 Stack possibilites How to use the space in stack? Grow up a)Keep track of top item in stack (Full Ascending) b)Keep track of first empty slot (Empty Ascending)

4 Stack possibilites How to use the space in stack? Grow down c)Keep track of top item in stack (Full Descending) d)Keep track of first empty slot (Empty Descending)

5 Stack possibilites ARM Convention : Full Descending R13 Holds "top" of stack

6 Push Recipe Subtract 4 from sp … 81818181 0x113F4 81818181 0x113F8 81818181 0x113FC 81818181 0x11400 ????????? sp0x11400

7 Push Recipe Subtract 4 from sp … 81818181 0x113F4 81818181 0x113F8 81818181 0x113FC 81818181 0x11400 ????????? sp0x113FC

8 Push Recipe Subtract 4 from sp STR data … 81818181 0x113F4 81818181 0x113F8 81818181 0x113FC AA 0x11400 ????????? sp0x113FC

9 Push Recipe Subtract 4 from sp … 81818181 0x113F4 81818181 0x113F8 81818181 0x113FC AA 0x11400 ????????? sp0x113F8

10 Push Recipe Subtract 4 from sp STR data … 81818181 0x113F4 81818181 0x113F8 BB 0x113FC AA 0x11400 ????????? sp0x113F8

11 Pop Recipe LDR data … 81818181 0x113F4 81818181 0x113F8 BB 0x113FC AA 0x11400 ????????? sp0x113F8

12 Pop Recipe LDR data Add 4 to sp … 81818181 0x113F4 81818181 0x113F8 BB 0x113FC AA 0x11400 ????????? sp0x113FC

13 Pop Recipe LDR data … 81818181 0x113F4 81818181 0x113F8 BB 0x113FC AA 0x11400 ????????? sp0x113FC

14 Pop Recipe LDR data Add 4 to sp … 81818181 0x113F4 81818181 0x113F8 BB 0x113FC AA 0x11400 ????????? sp0x11400

15 Push / Pop Always tear down stack in reverse order – Push A – Push B – Pop B – Pop A B Stack:AAA Time

16 Machine Push/Pop Common need : machine instructions Store Multiple to Full Descending use sp and update it (!) store r1 and r2

17 Machine Push/Pop Common need : machine instructions Load Multiple from Full Descending use sp and update it (!) load r2 then r1 (reverse order)

18 Pseudo Instructions Easier pseudinstructions: PUSH {register list} POP {register list}

19 Function Calls

20 Assembly Function Assembly Functions are branches

21 Assembly Function Assembly Functions are branches – Need to know where to return to when done!

22 Assembly Function BL label Branch and Link – Branch (change PC) – But store Store next instruction address in R14 (lr = link register)

23 Assembly Function BL label Branch and Link – Branch (change PC) – But store Store next instruction address in R14 (lr = link register)

24 Assembly Function Return – Copy lr back into pc

25 Assembly Function Return – Copy lr back into pc

26 UDiv Unsigned division function – Parameters in r0, r1 – Results in r0, r1

27 UDiv Calling from main – Parameters in r0, r1 – Results in r0, r1

28 Using Functions GetTime program – Get system time – Do lots of divisions to get Hour, Min, Sec

29 Calling Conventions

30 Calling Convetions Calling Convention – Agreement for how subroutines work How we pass info What registers we can use … – Differs between platforms, compilers, etc…

31 ARM Conventions ARM Register Conventions Register RangeUse r0-r3 -Passing parameters & results -Temporary values r4-r9 -Storing information r10-r15 Special purpose

32 ARM Conventions ARM Register Conventions Register RangeUseCallerFunction 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 informationNo worries… If you want to use these you MUST store/restore data that was there!!! r10-r15 Special purpose Only use for intended purpose

33 ABS Absolute Value – Does what it wants with r0-r3 – Makes use of r4 and r5 – must save

34 ABS Main that calls abs – must store any r0-r3 used across function call – r4+ are safe

35 Smarter ABS If function ONLY uses r0-r3 to do work, nothing to save:

36 Smarter ABS If main only uses r0-r3 between function calls, nothing to save

37 Nested Calls

38 Real functions often call other functions:

39 Nested Calls Multiple "return addresses" needed – Proc_A needs address D – Proc_B needs address B

40 Nested Calls Multiple "return addresses" needed – Proc_A needs address D – Proc_B needs address B Store return addresses on stack

41 Calling Convention Update If function call other functions: – Save/restore your return address (lr) to stack with registers r4+

42 Calling Convention Update If function does not call others: – Don't worry… just save r4+ if needed

43 Process Review Subroutine Call (done by the caller): Subroutine Call – Push any of r0-r3 that are in use – Put argument values into r0-r3 – Call the subroutine using bl

44 Process Review Subroutine Prolog (done to start subroutine): Subroutine Prolog – If we want to use r4+ push them – If we will call other functions, push lr also – Get parameters from r0-r3

45 Process Review Subroutine Body – Use r0-r3 and any from r4+ you saved

46 Process Review Subroutine Epilog (done at end of sub): Subroutine Epilog – Put returned values in r0-r3 – Pop from the stack anything you saved (r4+, lr) – Return by moving lr to pc

47 Process Review Regaining Control (done by the caller): Regaining Control – Get results from r0-r3 – Pop any old r0-r3 values you stored before call


Download ppt "The Stack. ARMSim memory space: 0x11400 + Unused 0x09400-0x11400 Stack 0x01400-0x09400 Heap 0x?????-0x01400 Data 0x01000-0x????? Text 0x00000-0x01000."

Similar presentations


Ads by Google