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

the stack
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Systems Architecture

The Stack

and Subroutines

The Stack – p. 1/9

slide-2
SLIDE 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

slide-3
SLIDE 3

Operation of The Subroutine

  • Branch to Subroutine (Branch and Link)

BLcc label

  • Link Register: LR (R14)

Holds address of instruction after the BL instruction

  • Return from Subroutine

MOVcc PC, LR

The Stack – p. 3/9

slide-4
SLIDE 4

Operation of The Subroutine

  • Branch to Subroutine (Branch and Link)

BLcc label

cc: LR ← PC + 4 cc: PC ← PC + IR(offset)

  • Link Register: LR (R14)

Holds address of instruction after the BL instruction

  • Return from Subroutine

MOVcc PC, LR

cc: PC ← LR

The Stack – p. 3/9

slide-5
SLIDE 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 CMP R0, #13 ; Is it return key ? 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

slide-6
SLIDE 6

The Stack

  • Stack used to ‘remember’ values
  • Provides temporary (local) storage
  • Allows for subroutines (functions)
  • R13 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 pushed on is first popped 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

slide-7
SLIDE 7

Operation of The Stack

1 Stack Empty

← SP

The Stack – p. 6/9

slide-8
SLIDE 8

Operation of The Stack

1 Stack Empty 2 Push A A

← SP

The Stack – p. 6/9

slide-9
SLIDE 9

Operation of The Stack

1 Stack Empty 2 Push A 3 Push B A B

← SP

The Stack – p. 6/9

slide-10
SLIDE 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

slide-11
SLIDE 11

Operation of The Stack

1 Stack Empty 2 Push A 3 Push B 4 Push C 5 Push D A B C D

← SP

The Stack – p. 6/9

slide-12
SLIDE 12

Operation of The Stack

1 Stack Empty 2 Push A 3 Push B 4 Push C 5 Push D 6 Pop A B C

← SP

The Stack – p. 6/9

slide-13
SLIDE 13

Operation of The Stack

1 Stack Empty 2 Push A 3 Push B 4 Push C 5 Push D 6 Pop 7 Push E A B C E

← SP

The Stack – p. 6/9

slide-14
SLIDE 14

Operation of The Stack

1 Stack Empty 2 Push A 3 Push B 4 Push C 5 Push D 6 Pop 7 Push E 8 Pop A B C

← SP

The Stack – p. 6/9

slide-15
SLIDE 15

Operation of The Stack

1 Stack Empty 2 Push A 3 Push B 4 Push C 5 Push D 6 Pop 7 Push E 8 Pop 9 Pop A B

← SP

The Stack – p. 6/9

slide-16
SLIDE 16

Operation of The Stack

1 Stack Empty 2 Push A 3 Push B 4 Push C 5 Push D 6 Pop 7 Push E 8 Pop 9 Pop 10 Pop A

← SP

The Stack – p. 6/9

slide-17
SLIDE 17

Operation of The Stack

1 Stack Empty 2 Push A 3 Push B 4 Push C 5 Push D 6 Pop 7 Push E 8 Pop 9 Pop 10 Pop 11 Pop

← SP

The Stack – p. 6/9

slide-18
SLIDE 18

Operation of The Stack

1 Stack Empty 2 Push A 3 Push B 4 Push C 5 Push D 6 Pop 7 Push E 8 Pop 9 Pop 10 Pop 11 Pop Stack Empty

← SP

The Stack – p. 6/9

slide-19
SLIDE 19

Stack as Temporary Storage

  • Push: Save register on the stack

STRcc Rs, [SP], #-4

  • Pop: Recover register from stack

LDRcc Rd, [SP, #4]!

The Stack – p. 7/9

slide-20
SLIDE 20

Stack as Temporary Storage

  • Push: Save register on the stack

STRcc Rs, [SP], #-4

cc: MBR ← Rs cc: MAR ← SP cc: SP ← SP - 4 cc: M(MAR) ← MBR

  • Pop: Recover register from stack

LDRcc Rd, [SP, #4]!

The Stack – p. 7/9

slide-21
SLIDE 21

Stack as Temporary Storage

  • Push: Save register on the stack

STRcc Rs, [SP], #-4

cc: MBR ← Rs cc: MAR ← SP cc: SP ← SP - 4 cc: M(MAR) ← MBR

  • Pop: Recover register from stack

LDRcc Rd, [SP, #4]!

cc: SP ← SP + 4 cc: MAR ← SP cc: MBR ← M(MAR) cc: Rd ← MBR

The Stack – p. 7/9

slide-22
SLIDE 22

Push/Pop a Set of Register

  • Push: Save a set of registers on the stack

STMccmode SP!, { Register List }

  • Pop: Recover the set of registers

LDMccmode 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

slide-23
SLIDE 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