Professional Documents
Culture Documents
Topic 8 Call, Stack & Subroutine (ISMAIL - SKE - 2019)
Topic 8 Call, Stack & Subroutine (ISMAIL - SKE - 2019)
.include “m32def.inc”
LDI R16,LOW(RAMEND) Note:
RAMEND = 0x85F
OUT SPL,R16 if ATMEGA32
LDI R16,HIGH(RAMEND)
OUT SPH,R16
SP Upper Limit
o As mentioned earlier, we can define the stack anywhere in the
general purpose memory.
o In AVR, the stack can as big as its RAM.
o Note that we must not define the stack in the register memory,
nor in the I/O memory.
o So, the SP must be set to point above 0x60.
o Stack content is important as it used to store information when
we calling a subroutine.
o Stack overflow will occur when the content of the stack is exceed
the upper limit.
Stack Instructions
o The are two types of stack operations involving SP:
o PUSH – the storing of CPU information on the stack such address
from program counter
o POP – the loading of stack contents back into a CPU register.
Pushing On The Stack
o The SP always points to the top of the
stack.
o As we push data to the stack, the data
are saved where SP points to, and the SP
is decremented by one.
o Other words, SP is decremented when
data is pushed onto the stack.
o To push a register onto stack we use PUSH instruction.
000B POP R0
Memory
Subroutine
(Function)
Subroutine & Call
o In AVR, a software normally
comprises of a main program
and numbers of subroutines.
o Subroutine is a useful modular
program that appears many
times in a software program. A
subroutine code always ends
with a RET instruction.
o A reasonable name is given to
the subroutine.
o AVR program control operation
is conducted by Program
Counter (PC).
Subroutine
• Why use subroutines?
o Code re-use
o Easier to understand code (readability)
o Divide and conquer
Complex tasks are easier when broken down into smaller tasks
o Simplify the code debugging process.
• How do we call a subroutine in assembly?
o Place the parameters somewhere known
o CALL instruction to jump to the subroutine
o RET instruction to return
• Examples of subroutines:
o Time Delay
o Perform output to 7-segment display
o Convert binary to ASCII
o Convert Fahrenheit to Celsius
Subroutine: Role of Stack
o Stack is used to temporarily store address when CPU execute the
CALL instruction.
o This is how the CPU know where to resume when it return from the
called subroutine.
o Hence, we must very careful when manipulating the stack contents.
o For AVRs whose program counter is not longer than 16 bits (e.g.
ATmega32), the value of the program counter is broken into 2 bytes.
o The higher byte is pushed onto the stack first, and then the lower byte
is pushed.
o For AVRs whose program counter is longer than 16 bit but shorter
than 24 bit, the value of the program counter is broken up into 3
bytes.
o The highest byte is pushed first, then the middle byte is pushed, and
finally the lowest byte is push.
CALL & Subroutine Operation
o Before Subroutine operation,
o PC is having the address of Call
instruction of normal program.
o Stack Pointer is at normal address and
Stack area is empty
o During Subroutine operation
o the AVR first saves the address of the
next instruction sequence after CALL of
the normal program onto the stack and
Stack Pointer is decremented
accordingly.
o PC get the address of subroutine and run
the subroutine.
o After Subroutine operation
o PC receives the saved address of the next
instruction sequence from STACK during
RET operation. Stack Pointer is
incremented accordingly and continue
normal program operation
Subroutine: CALL Instruction
o CALL is long call to subroutine.
o It calls to a subroutine within the entire Program memory.
o The return address (to the instruction after the CALL) will be
stored onto the Stack.
Address Code
.INCLUDE "M32DEF.INC"
.ORG 0
0000 LDI R16, HIGH(RAMEND)
0001 OUT SPH, R16
0002 LDI R16, LOW(RAMEND)
0003 OUT SPL, R16
0004 LDI R16, 0x01
0005 LDI R17, 0x02
0006 LDI R18, 0x03
0007 PUSH R16
0008 PUSH R17
0009 PUSH R18
000A CALL COMPUTE
000C POP R16
000D POP R17
000E POP R18
000F EXIT: JMP EXIT
0011 COMPUTE:
0011 ADD R16,R17
0012 ADD R16,R18
0013 RET
Exercise
State the contents of the following after this code is executed right after the
instruction Exit: JMP EXIT. The uppermost memory location is 0x085F.