the stack
play

The Stack and Subroutines The Stack p. 1/9 The Subroutine Allow - PowerPoint PPT Presentation

Systems Architecture The Stack and Subroutines The Stack p. 1/9 The Subroutine Allow re-use of code Write (and debug) code once, use it many times A subroutine is called Subroutine will return on completion Defer


  1. Systems Architecture The Stack and Subroutines The Stack – p. 1/9

  2. The Subroutine Allow re-use of code • Write (and debug) code once, use it many times • A subroutine is called • Subroutine will return on completion • Defer writing actual code until later • Helps with readability and maintenance • We can nest subroutines: • subroutine calls other subroutines May be a procedure: does not return a value • May be a function: does return a value • The Stack – p. 2/9

  3. Operation of The Subroutine Branch to Subroutine (Branch and Link) • BL � cc � label Link Register : LR (R 14 ) • Holds address of instruction after the BL instruction Return from Subroutine • MOV � cc � PC, LR The Stack – p. 3/9

  4. Operation of The Subroutine Branch to Subroutine (Branch and Link) • � cc � : LR ← PC + 4 BL � cc � label � cc � : PC ← PC + IR(offset) Link Register : LR (R 14 ) • Holds address of instruction after the BL instruction Return from Subroutine • � cc � : PC ← LR MOV � cc � PC, LR The Stack – p. 3/9

  5. Example Subroutines ; Read line from keyboard into string at R10, uses R0 and R13 GetStr MOV R13, LR ; Save Return Address BLAL GetChar ; Read keyboard into R0 ; Is it � return � key ? CMP R0, #13 BEQ GetStr1 ; Yes ⇒ Exit Subroutine STRB R0, [R10], #1 ; No ⇒ Save char in string BAL GetStr ; Get next character GetStr1 EOR R0, R0, R0 ; Clear R0 STRB R0, [R10] ; Add terminating zero byte MOV PC, R13 ; Return from GetStr ; Read char from keyboard into R0 GetChar SWI &4 ; Reach char into R0 MOV PC, LR ; Return from GetChar The Stack – p. 4/9

  6. The Stack Stack used to ‘remember’ values • Provides temporary (local) storage • Allows for subroutines (functions) • R 13 points to current Top Of Stack (TOS) • Also known as the Stack Pointer ( SP ) Stack is a LIFO (last-in-first-out) device • Most recent value push ed on is first pop ped off Stack grows ‘downwards’ in memory Each mode has it’s own stack pointer: • R13_fiq , R13_svc , R13_abt , R13_undef , . . . R13 or SP refer to current mode The Stack – p. 5/9

  7. Operation of The Stack 1 Stack Empty ← SP The Stack – p. 6/9

  8. Operation of The Stack 1 Stack Empty 2 Push A A ← SP The Stack – p. 6/9

  9. Operation of The Stack 1 Stack Empty 2 Push A 3 Push B A B ← SP The Stack – p. 6/9

  10. Operation of The Stack 1 Stack Empty 2 Push A 3 Push B 4 Push C A B C ← SP The Stack – p. 6/9

  11. Operation of The Stack 1 Stack Empty 2 Push A 3 Push B 4 Push C A 5 Push D B C D ← SP The Stack – p. 6/9

  12. Operation of The Stack 1 Stack Empty 2 Push A 3 Push B 4 Push C A 5 Push D B 6 Pop C ← SP The Stack – p. 6/9

  13. Operation of The Stack 1 Stack Empty 2 Push A 3 Push B 4 Push C A 5 Push D B 6 Pop C 7 Push E E ← SP The Stack – p. 6/9

  14. Operation of The Stack 1 Stack Empty 2 Push A 3 Push B 4 Push C A 5 Push D B 6 Pop C 7 Push E ← SP 8 Pop The Stack – p. 6/9

  15. Operation of The Stack 1 Stack Empty 2 Push A 3 Push B 4 A Push C 5 B Push D 6 ← SP Pop 7 Push E 8 Pop 9 Pop The Stack – p. 6/9

  16. Operation of The Stack 1 Stack Empty 2 Push A 3 Push B 4 A Push C 5 ← SP Push D 6 Pop 7 Push E 8 Pop 9 Pop 10 Pop The Stack – p. 6/9

  17. Operation of The Stack 1 Stack Empty 2 Push A 3 Push B 4 ← SP Push C 5 Push D 6 Pop 7 Push E 8 Pop 9 Pop 10 Pop 11 Pop The Stack – p. 6/9

  18. Operation of The Stack 1 Stack Empty 2 Push A 3 Push B 4 ← SP Push C 5 Push D 6 Pop 7 Push E 8 Pop 9 Pop 10 Pop 11 Pop Stack Empty The Stack – p. 6/9

  19. Stack as Temporary Storage Push : Save register on the stack • STR � cc � R s , [SP], #-4 Pop : Recover register from stack • LDR � cc � R d , [SP, #4]! The Stack – p. 7/9

  20. Stack as Temporary Storage Push : Save register on the stack • � cc � : MBR STR � cc � R s , [SP], #-4 ← R s � cc � : MAR ← SP � cc � : SP ← SP - 4 � cc � : M(MAR) ← MBR Pop : Recover register from stack • LDR � cc � R d , [SP, #4]! The Stack – p. 7/9

  21. Stack as Temporary Storage Push : Save register on the stack • � cc � : MBR STR � cc � R s , [SP], #-4 ← R s � cc � : MAR ← SP � cc � : SP ← SP - 4 � cc � : M(MAR) ← MBR Pop : Recover register from stack • � cc � : SP LDR � cc � R d , [SP, #4]! ← SP + 4 � cc � : MAR ← SP � cc � : MBR ← M(MAR) � cc � : R d ← MBR The Stack – p. 7/9

  22. Push/Pop a Set of Register Push : Save a set of registers on the stack • STM � cc �� mode � SP!, { � Register List � } Pop : Recover the set of registers • LDM � cc �� mode � SP!, { � Register List � } � mode � can be one of: • IB: Increment Before IA: Increment After DB: Decrement Before DA: Decrement After � Register List � • A list of the registers to load/store E.g., R0-R7, R10 The Stack – p. 8/9

  23. Nested Subroutines Use Stack to store Return Address (Link Register) • Save all register used in the subroutine • just in case the caller is using them Must pop off all values pushed onto stack ! • Pass parameters ( arguments ) into a subroutine • Three types of variable passing ⇒ by value / reference / name Three methods of passing variables ⇒ In registers / on Stack / in parameter block Return a value from the subroutine • The Stack – p. 9/9

Recommend


More recommend