Professional Documents
Culture Documents
Embedded Systems/Microcontroller
Laboratory
Lecture 5
Stack, Subroutines, and Interrupts
Professor Rahul Mangharam
Electrical and Systems Engineering
University of Pennsylvania
Stack
A stack is a data structure type that uses Last In-First Out (LIFO) to
access its contents.
Key characteristic:
Items are accessible only from one end-the top.
2
Stack Structure & Operation
BOTTOM
3
MC9S12C128 Stack (in Memory)
CPU Register:
XX SP Stack Pointer (SP)
Lower $20
Addresses $00 A 16-bit stack pointer (SP) points to the
location above the top byte of the stack.
Set in hardware 4
Push and Pull Instructions
- PSHA: Push A onto the stack
- PSHB: Push B onto the stack
- PSHX: Push X onto the stack (low order byte is pushed first)
- PSHY: Push Y onto the stack (low order byte is pushed first)
- PULA: Pull A from the stack
- PULB: Pull B from the stack
- PULX: Pull X from the stack (high order byte is pulled first)
- PULY: Pull Y from the stack (high order byte is pulled first)
PUSH (PSHA, PSHB, PSHX, PSHY)
Put contents of register onto stack, then decrement SP by 1 for
Accumulator A, B or 2 for Index registers X, Y.
PULL (PULA, PULB, PULX, PULY)
Increment SP by 1 for Accumulator A, B or 2 for Index registers X, Y
then load contents onto register.
5
Stack Pointer Instructions
[<label>] DES [<comment>] decrements the contents of the stack pointer by 1
[<label>] INS [<comment>] increments the contents of the stack pointer by 1
6
Stack Example
Example: With SP=$3FFF,
LDAA #$10
PSHA
LDAA #$20
PSHA
PULB
7
Stack Example-Diagram
$3FFF XX SP $3F $FF
$3FFF $10 8
Subroutine
- Sequence of instructions that can be called from other places in program
- Allows the same operation to be performed with different parameters
- Simplifies the design of a complex program by using the divide-and-
conquer approach
Assembly instructions related subroutine calls
where
<rel> is the offset to the subroutine (Relative Addressing Mode limit range -128 to 127 bytes*)
<opr> is the address of the subroutine and is specified in the DIR, EXT, or INDexed
addressing mode.
Main program
Main program
<call> sub_x
.
Subroutine 1 Subroutine 2 Subroutine 3 .
.
sub_x: .
Subroutine 1.1 Subroutine 2.1 Subroutine 3.1
.
.
Subroutine <return>
Subroutine 2.1.1 Subroutine 2.1.2
10
Subroutine Program 1
C Language Assembly
$8000
void func1(){
PORTB|=0x02;
}
$8010 JSR $9000
main(){
$8020 JSR $9000
func1();
func1();
$9000 BSET PORTB,X $02
$9003 RTS
}
11
Subroutine Program 2-Nested
C Language Assembly
$8000
void func2(){
func1(); $8030 JSR $A000
}
12
Subroutine Relation to Stack
JSR pushes the “current” Program Counter (PC)* onto the stack then
jump to the subroutine
RTS pops old PC from stack and continues execution
Program 1 Program 2
$8030 JSR $A000
$8010 JSR $9000
$A100 JSR $9000
$9000 BSET PORTB,X $02
XX SP
$A1
XX SP
$03
$80
$80
$13
$33
A special event that requires the CPU to stop normal program execution and
perform some service related to the event. Examples of interrupts include
I/O completion, timer time-out, illegal opcodes, arithmetic overflow, divide-by-0,
etc.
Functions of Interrupts
Interrupt Maskability
- Interrupts that can be ignored by the CPU are called maskable interrupts. A
maskable interrupt must be enabled before it can interrupt the CPU. An interrupt is
enabled by setting an enable flag.
- Interrupts that can’t be ignored by the CPU are called nonmaskable interrupts. 15
Interrupt Priority & Service
Interrupt priority
The order in which the CPU will service interrupts when all of them occur at the
same time.
Interrupt Service
The CPU provides service to an interrupt by executing a program called the
interrupt service routine (ISR).
SP after interrupt
Low address
CCR
B
A
XH
XL
YH
YL
PCH
High address PCL SP before interrupt
19
Interrupt Programming
Interrupt Vector
Starting address of the interrupt service routine
20
Writing the ISR
Assembly Language:
xxx_ISR …
…
RTI
C Language:
void xxx_ISR ( )
{
…
}
21
Enable Interrupts
Simply include the following line:
EnableInterrupts;
22