












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













If you “google” the word stack, one of the definitions you will get is: If you “google” the word stack, one of the definitions you will get is:
A reserved area of memory used to keep track of a program's internal operations, including functions, return addresses, passed parameters, etc. A stack is A reserved area of memory used to keep track of a program's internal operations, including functions, return addresses, passed parameters, etc. A stack is
usually maintained as a "last in, first out" ( usually maintained as a "last in, first out" (LIFOLIFO) data structure, so that the last item added to the structure is the first item used.) data structure, so that the last item added to the structure is the first item used.
Sometimes is useful to have a region of memory for temporary storage, Sometimes is useful to have a region of memory for temporary storage,
which which does not have to be allocated as named variables.does not have to be allocated as named variables.
When you use subroutines and interrupts it will be essential to have such a storage
When you use subroutines and interrupts it will be essential to have such a storage
region.
region.
Such region is called a Such region is called a StackStack
The
The Stack Pointer
Stack Pointer (SP) register is used to indicate the location of the last item put onto
(SP) register is used to indicate the location of the last item put onto
the stack. the stack.
When you PUT something ONTO the stack ( When you PUT something ONTO the stack (PUSHPUSH onto the stack), the SP is decrementedonto the stack), the SP is decremented
before
before the item is placed on the stack.
the item is placed on the stack.
When you take something OFF of the stack (
When you take something OFF of the stack ( PULL
from the stack), the SP is incremented
from the stack), the SP is incremented
after the item is pulled from the stack. after the item is pulled from the stack.
Before you can use a stack you have to initialize the SP to point to one value higher than Before you can use a stack you have to initialize the SP to point to one value higher than
the highest memory location in the stack.
the highest memory location in the stack.
For the HC12 use a block of memory from about $3B00 to $3BFF for the stack.
For the HC12 use a block of memory from about $3B00 to $3BFF for the stack.
For this region of memory, initialize the stack pointer to For this region of memory, initialize the stack pointer to $3C00$3C00.. Use LDS (Load StackUse LDS (Load Stack
Pointer) to initialize the stack pointer. Pointer) to initialize the stack pointer.
The stack pointer is initialized only one time in the program.
The stack pointer is initialized only one time in the program.
0x3BFA
0x3BFB
0x3BFC
0x3BFD
0x3BFE
0x3BFF
0x3C
Memory
used
by MCU
(Debug
Data)
SP points to location last item placed in block SP points to location last item placed in block
SP SP decreasesdecreases when you put an item on the stackwhen you put an item on the stack
increases
increases when you pull the item from the stack
when you pull the item from the stack
For the HC12, use
For the HC12, use 0x3c
0x3c as initial SP
as initial SP
0x3BFA
0x3BFB
0x3BFC
0x3BFD
0x3BFE
0x3BFF
0x3C
Memory
used
by MCU
0x3BF
0x3BF
0x3BF
0x3BF
A subroutine is a section of code which performs a specific task, usually a task which needs to be executed by different parts of the program. A subroutine is a section of code which performs a specific task, usually a task which needs to be executed by different parts of the program.
Example: Example:
org $
org $ -Math functions, such as square root (sqrt)
-Math functions, such as square root (sqrt)
Because a subroutine can be called from different places in a program, you cannot get
Because a subroutine can be called from different places in a program, you cannot get
out of a subroutine with an instruction such as
out of a subroutine with an instruction such as
call sqrt
call sqrt
call sqrt call sqrt Because you would need to jump to different places depending upon which section ofBecause you would need to jump to different places depending upon which section of
: : the code called the subroutine.the code called the subroutine.
swi
swi When you want to call the subroutine your code has to save the address where
When you want to call the subroutine your code has to save the address where the
the
subroutine should return to subroutine should return to.. It does this by saving the return address on theIt does this by saving the return address on the stackstack..
sqrt: sqrt: compute square rootcompute square root
:: - This is done automatically for you when you get to the subroutine by- This is done automatically for you when you get to the subroutine by
:: usingusing JSRJSR (Jump to Subroutine) or BSR (Branch to Subroutine)(Jump to Subroutine) or BSR (Branch to Subroutine)
jmp label jmp label instruction.instruction. This instruction pushes the address of the instructionThis instruction pushes the address of the instruction
following the JSR (BSR) instruction on the stack following the JSR (BSR) instruction on the stack
After the subroutine is done executing its code, it needs to After the subroutine is done executing its code, it needs to return to the address savedreturn to the address saved
on the on the stackstack..
using RTS (Return from Subroutine) instruction. using RTS (Return from Subroutine) instruction. This instruction pullsThis instruction pulls
the return address off the stack and loads it into the PC.
the return address off the stack and loads it into the PC.
0x3BFA
0x3BFB
0x3BFC
0x3BFD
0x3BFE
0x3BFF
0x3C
Memory
used
by MCU
0x3BF
0x3BF
0x3BF
0x3BF
Using a subroutine to wait for an event to occur then take action Using a subroutine to wait for an event to occur then take action
Wait until bit 7 of address $00CC is set. Wait until bit 7 of address $00CC is set.
Write the value of ACCA to address $00CF
Write the value of ACCA to address $00CF
; This routine waits until the HC12 serial port is ready, then send a byte of data to the serial port
; This routine waits until the HC12 serial port is ready, then send a byte of data to the serial port
putchar:
putchar: brclr
brclr $00CC,#$80,putchar
$00CC,#$80,putchar ; Data Terminal Equip. ready
; Data Terminal Equip. ready
staa
staa $00CF
; Send char
; Send char
rts
rts
; Program to send the word “hello” to the HC12 serial port
; Program to send the word “hello” to the HC12 serial port
ldx ldx #str#str
loop: loop: ldaaldaa 1,x+1,x+
beq beq donedone
jsr jsr putcharputchar
bra bra looploop
done: done: swiswi
str: str: fccfcc “hello”“hello” ; form constant character; form constant character
dc.b dc.b $0a,$0d,0$0a,$0d,0 ; CR-LF; CR-LF
Want to look for a particular pattern on 4 input pins Want to look for a particular pattern on 4 input pins
-For example want to do something if pattern on -For example want to do something if pattern on PB3-PB0 is 0110PB3-PB0 is 0110
Don’t know or care what are on the other 4 pins (PB7-PB4)
Don’t know or care what are on the other 4 pins (PB7-PB4)
Here is the wrong way to do it:
Here is the wrong way to do it:
ldaa ldaa PORTBPORTB
cmpa cmpa #b0110#b
beq
beq task
task
If PB7-PB4 are anything other than 0000, you will not execute the task.
If PB7-PB4 are anything other than 0000, you will not execute the task.
You need to mask out the Don’t Care bits before checking for the pattern on the bits you are interested in You need to mask out the Don’t Care bits before checking for the pattern on the bits you are interested in
ldaa ldaa PORTBPORTB
andaa
andaa #b
#b
cmpa
cmpa #b
#b
beq
beq task
task
Now, whatever pattern appears on PB7-4 is ignored Now, whatever pattern appears on PB7-4 is ignored
Connect an output port from the HC12 to an LED. Connect an output port from the HC12 to an LED.
Using an output port to control an LED Using an output port to control an LED
; Program to display patterns
prog: equ $1000 delay: psha
data: equ $2000 pshx
stack: equ $3C00 ldaa #
PTH: equ $0260 Loop2: ldx #
DDRH: equ $0262 Loop1: dex
org prog bne Loop
lds #stack deca
ldaa #$ff bne Loop
staa DDRH pulx
L1: ldx #table pula
L2: ldaa 1,x+ rts
staa PTH
jsr delay
cpx #table_end org data
bls L2 table: dc.b $3f
bra L1 dc.b $5b
dc.b $
dc.b $7d
table_end: dc.b $7f