ECE 2560 Introduction to Microcontroller Based Systems

ECE 2560 Introduction to Microcontroller-Based Systems
ECE 2560 Introduction to Microcontroller-Based Systems – Irem Eryilmaz
Lecture 14

Last Time: Subroutine Calling Sequence Sequence o events after
call #div_by_16
• Current PC is saved on the stack
• This will be the return address
• The address of the subroutine is
loaded into the PC
• The subroutine is executed
• With ret, the return address is restored from the stack into PC
• Execution continues from this point in the calling function
ECE 2560 Introduction to Microcontroller-Based Systems – Irem Eryilmaz

Static vs. Dynamic Allocation
So far we have used the RAM for storing program data initialized or reserved
at compilation time – using compiler directives .word .byte .space
Word Address
0x1C02 0x1C04
Assembler directives allocate data at the top of the RAM
This allocation is static – it does not change during runtime
Þ Static allocation
ECE 2560 Introduction to Microcontroller-Based Systems – Irem Eryilmaz

The stack is a data structure that is managed at the end of the RAM
Word RAM Address
0x1C02 0x1C04
0x2400 – not in RAM
managed using SP, push and pop
Subroutine calls and interrupts use the stack to save critical registers (PC and SR) before execution and restore these with ret/reti
We can use the stack to save/restore additional registers (R4 – R15) during subroutine calls and interrupts
We can create variables during runtime without initializing/reserving them at compile time
The stack enables dynamic data allocation Stack starts here
ECE 2560 Introduction to Microcontroller-Based Systems – Irem Eryilmaz

The stack starts empty and is managed dynamically during runtime i.e., we can add new data to the stack and remove it
Word Address
0x23F6 0x23F8
0x23FC 0x23FE
Always add to the top and remove from the top
ECE 2560 Introduction to Microcontroller-Based Systems – Irem Eryilmaz
Programming Help
Stack – Adding and Removing Data
To add data onto the stack we use To remove data from the stack
push.w src pop.w dst
0x23F6 0x23F8
0x23FC 0x23FE
push.w #0xAAAA push.w #0xBBBB push.w #0xCCCC
pop.w R4 pop.w R5 pop.w R6
The stack is a last-in first-out data structure: the last element that is added onto the stack (i.e., pushed) is the first element removed (i.e., popped)
ECE 2560 Introduction to Microcontroller-Based Systems – Irem Eryilmaz

Top of Stack – Stack Pointer (SP)
New elements are added onto the top of stack and removed from there
To manage the stack we only need to know the address of the top of stack Core register R1 is dedicated for this task: Stack Pointer (SP)
At the beginning of each program the stack pointer is initialized
0x23F8 0x23FA
0x23FC 0x23FE
The stack pointer is always aligned with even addresses
SP = 0x2400
SP points here ECE 2560 Introduction to Microcontroller-Based Systems – Irem Eryilmaz
0x2400 – not in RAM !

程序代写 CS代考 加微信: cstutorcs
Top of Stack – Stack Pointer (SP)
The stack pointer SP is decremented as we push elements onto stack incremented as we pop elements from stack
The SP operates using a pre-decrement, post-increment scheme
0x23FC 0x23FE
push.w #0xAAAA push.w #0xBBBB push.w #0xCCCC
pop.w R4 pop.w R5 pop.w R6
SP = 0x23FA SP = 0x23FC
SP = 0x23FE SP = 0x2400
SP = 0x23FE SP = 0x23FC SP = 0x23FA
SP = 0x23FC SP = 0x23FE SP = 0x2400
R4 = 0xCCCC
R5 = 0xBBBB
R6 = 0xAAAA
ECE 2560 Introduction to Microcontroller-Based Systems – Irem Eryilmaz
Code Help, Add WeChat: cstutorcs
Saving/Restoring Registers using the Stack Often subroutine contracts have restrictions on using core registers
We will use the stack to save core registers at the beginning of a subroutine and restore them before returning
mind the order of push and pop! ECE 2560 Introduction to Microcontroller-Based Systems – Irem Eryilmaz

Not so efficient x_times_y
ECE 2560 Introduction to Microcontroller-Based Systems – Irem Eryilmaz