Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 3430 – Intro to Microcomputer Systems

Similar presentations


Presentation on theme: "ECE 3430 – Intro to Microcomputer Systems"— Presentation transcript:

1 ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Introduction to Microcomputer Systems University of Colorado at Colorado Springs Lecture #11 Agenda Today The Stack (Push, Pop) and the Stack Pointer. Subroutine introduction and the Link Register. Lecture #11 ECE 3430 – Intro to Microcomputer Systems Fall 2015

2 The Stack - This is just managed RAM. The “stack” can live anywhere in RAM. - This is a last in, first out (LIFO) data structure. - This is a first in, last out (FILO) data structure. - Items can only be added or removed from the top of the stack. - Conceptually like a stack of cafeteria trays or a PEZ dispenser. PUSH – inserting something onto the TOP of the STACK POP – removing something from the TOP of the STACK Ex) PUSH 0x0011, 0x0022, 0x POP, we will receive 0x0033, 0x0022, 0x0011 in that order TOP Last In This structure is useful because the order of data is inherently kept. We don’t need to worry about setting up dedicated memory for each data item. This is also a necessary structure for subroutines to work. First In BOTTOM TOP 0x0033 0x0022 0x0011 Lecture #11 ECE 3430 – Intro to Microcomputer Systems Fall 2015

3 ECE 3430 – Intro to Microcomputer Systems
The Stack The size of the each argument pushed to the stack (in ARM) are always 32-bit. All ARM pop operations deallocate 32-bits of data into the destination per argument. Lecture #11 ECE 3430 – Intro to Microcomputer Systems Fall 2015

4 The Stack What the stack really is: - A section of memory with an address pointer (stack pointer = SP in CPU). - The SP contains the address of the top element of the stack. - In ARM, the SP is pre-decremented as information is PUSHED. - In ARM, the SP is post-incremented as information is POPPED. - We define where to place the stack data structure (using a MOV instruction or a SP vector). - The stack is variable in size and only limited by RAM availability. - The standard is to place the STACK at the end of RAM. - The lab tools will initialize they stack where they please. - How do we initialize the stack? On reset, the CPU fetches the initial value of the SP from memory location 0x0. (The initial value of PC comes from the reset vector at memory location 0x4). RAM Keep global variables close to the beginning of RAM 0x2000_0000 : 0x3FFF_FFFF Initialize stack at the end of RAM 0x4000_0000 <no RAM here> Lecture #11 ECE 3430 – Intro to Microcomputer Systems Fall 2015

5 The Stack Stack Overflow - If we push too much information onto the stack, it may start overriding data stored in pre-defined variables. - Since there is no operating system running on our MSP432, so you have to be the memory manager! ARM Stack Instructions PUSH {register list} ; example: PUSH {R4} or PUSH {R4-R7, R10} POP {register list} ; example: POP {R5} or POP {R5-R8, R11} RAM Variables 0x2000_0000 : 0x4000_0000 Stack creeps backwards through RAM as data is pushed onto it Lecture #11 ECE 3430 – Intro to Microcomputer Systems Fall 2015

6 ECE 3430 – Intro to Microcomputer Systems
The Stack Example (swapping register values using the stack) Remember that the SP points to the top word on the stack! MOV SP, #0x MOV R4, #0xAA LDR R5, =0xBBCC PUSH {R4} PUSH {R5} POP {R4} ; R4 = 0xBBCC POP {R5} ; R5 = 0xAA NOTE: The data in RAM is not actually erased! NOTE: Each cell is 32-bit (4 bytes). Data is stored in little endian. 0x4000_0000 SP ? 0x3FFF_FFFC 0x4000_0000 SP 0x0000_00AA ? 0x3FFF_FFF8 0x3FFF_FFFC 0x4000_0000 SP 0x0000_BBCC 0x0000_00AA ? 0x3FFF_FFCC 0x4000_0000 SP 0x0000_00AA ? 0x4000_0000 SP ? Lecture #11 ECE 3430 – Intro to Microcomputer Systems Fall 2015

7 ECE 3430 – Intro to Microcomputer Systems
The Stack Example (preserving and restoring a register set—using multiple items at once) PUSH {R4,R5} POP {R4,R5} NOTE: R4 and R5 were not swapped! When using register lists (more than one register specified), do not assume anything about the order in which they are pushed and popped. Technically, regardless of what order the registers are specified, the highest register number is pushed first and onward to the lowest. During a pop, the lowest is popped first and onwards towards the highest. 0x4000_0000 SP ? 0x3FFF_FFF8 0x3FFF_FFFC 0x4000_0000 SP 0x0000_BBCC 0x0000_00AA ? 0x4000_0000 SP ? Lecture #11 ECE 3430 – Intro to Microcomputer Systems Fall 2015

8 ECE 3430 – Intro to Microcomputer Systems
The Stack The reason the push and pop order cannot be controlled in a register list is because the machine encoding contains no specification for order (just a bit mask indicating which registers are to be pushed or what registers should be affected on a pop. Multi-item push and pop operations require multiple bus accesses—so they still take longer than single-item operations (but still faster than individual push and pops). The initial SP value must be word-aligned (i.e. a multiple of 4 bytes). The least-significant two bits of the SP are always zero! Since the PC is required to be half-word aligned (i.e. a multiple of 2 bytes), the least-significant one bit of the PC is normally always zero… Lecture #11 ECE 3430 – Intro to Microcomputer Systems Fall 2015

9 PC least significant bit and machine state
However, the least-significant bit of the PC indicates the initial operating mode of the processor: ARM mode or Thumb mode. Since our ARM Cortex-M4F is always operating in Thumb mode, the assembler will set the least-significant bit of the Reset vector to 1. After the initial PC value is loaded into the register, you don’t see the one. The ‘T’ bit in the PSR indicates the current state. Lecture #11 ECE 3430 – Intro to Microcomputer Systems Fall 2015

10 Subroutine introduction
A subroutine is just a chunk of code that you want executed like a function (in a higher-level language). You expect to call it by name, have it finish, and then return back to the caller. In ARM, this is accomplished using the link register (LR). Traditional architectures will use a special instruction to call a subroutine and a special instruction to return from a subroutine. In these architectures, the special “call” instruction will push the return address to the stack. The special “return” instruction will pop the top value off the stack and put it into the PC to return to the caller. ARM uses the LR instead to avoid using the stack for this purpose—so long as the subroutine call stack is only one function deep. Lecture #11 ECE 3430 – Intro to Microcomputer Systems Fall 2015

11 Subroutine introduction
To create a subroutine, label the top line. To call a subroutine, use the “branch with link” instruction: BL MySubroutine Inside MySubroutine, return to the caller by using the “branch indirect” instruction to branch back to the address held in the link register: BX LR The BL instruction will put the return address into the link register and then set the PC equal to the address of the subroutine. The BL instruction also marks the current processor execution state (ARM or Thumb) in the least-significant bit of the LR. For us, this is always 1—and, hence, the LR value is always odd. Lecture #11 ECE 3430 – Intro to Microcomputer Systems Fall 2015

12 Subroutine introduction
When the BX instruction is executed, it masks off the least-significant bit and set the PC to that value. The least-significant bit gets placed in the ‘T’ bit in the PSR to put the machine back in the correct operating mode. All this is done so ARM instruction codes and Thumb instruction codes can be mixed across subroutine boundaries—provided the processor understands both modes. When a subroutine needs to call another subroutine, the current LR must be pushed to the stack before calling BL to branch to the next subroutine. Upon return, the caller must restore the LR before calling BX LR. In summary, the link register accelerates the execution time of single-level subroutine calls by eliminating the extra accesses to memory. Lecture #11 ECE 3430 – Intro to Microcomputer Systems Fall 2015

13 Summary So far, we see that the stack pointer is useful for swapping registers and preserving/restoring register sets. When programming and you run out of general purpose registers, push a copy of those you aren’t using at that moment in time and then restore them later. This is a cleaner (and thread-safe) approach to extending the programming model. We also see that the stack can be used to facilitate subroutine calling and returning. In the next lecture, we’ll learn what else we can use the stack to do! Lecture #11 ECE 3430 – Intro to Microcomputer Systems Fall 2015


Download ppt "ECE 3430 – Intro to Microcomputer Systems"

Similar presentations


Ads by Google