Calling subroutines in assembly And using the Stack

Calling subroutines in assembly And using the Stack CS-280 Dr. Mark L. Hornick 1

CS 2851 Review: the Queue: a traditional CS data structure l Classical semantics (behavior) are: l l Elements can only be inserted at the back and removed from the front of the collection This is known as First-In, First-Out (FIFO) CS-280 Dr. Mark L. Hornick 2

JCF Queue – behavioral methods l offer() – place an element at the back of the queue l poll() or remove() – return and remove an element from the front of the queue l l l poll() returns null if the queue is empty remove() throws an exception if the queue is empty peek() or element() – return (without removing) the element at the front of the queue l l peek() returns null if the queue is empty element() throws an exception if the queue is empty CS-280 Dr. Mark L. Hornick 3

CS 2851 Review: Stack Another traditional CS data structure l Classical semantics (behavior) l l Elements can only be inserted and removed from the front of the collection This is known as Last-In, First-Out (LIFO) l l Less commonly: First-Out, Last-In (FILO) Random-access is not defined CS-280 Dr. Mark L. Hornick 4

JCF Stack – behavioral methods l l The naming for the structure and the methods is an analogy for how the data elements within the structure accessed Principal methods that define behavior: l l l push() – place an element on (top of) the stack pop() – return and remove an element from the (top of the) stack peek() – return the top element of the stack l Without removing (popping) it CS-280 Dr. Mark L. Hornick 5

Most CPUs have a built-in implementation of Stack l l The actual stack data structure is just an ordered collection of bytes The stack “data structure” is maintained in Data Memory A special CPU register (SP) is used to keep track of the front of the stack Dedicated instructions are used to push data (bytes) onto and pull items from the stack CS-280 Dr. Mark L. Hornick 6

By convention, the stack starts at (or very near) the end of data memory l On our Atmega 32 systems, the high address in Data Memory is 0 x 85 F l Because we have 2048 (0 x 800) bytes of SRAM l And the first 96 (0 x 60) bytes are assigned to Registers and IO Ports l The address 0 x 085 F is. EQU’d in m 32 def. inc as RAMEND CS-280 Dr. Mark L. Hornick Mapped to Registers and IO 0 x 0000 to 0 x 005 F Usable SRAM 0 x 0060 to 0 x 085 F 7

The special register (SP) used to maintain the stack is called the Stack Pointer l l Like X, Y, and Z, the SP is a 16 -bit register divided into two 8 bit registers If SP contains the address 0 x 085 F: l l . . . SPL contains the low byte of the address of the front of the stack (e. g. 0 x 5 F) SPH contains the high byte (e. g. 0 x 08) CS-280 Dr. Mark L. Hornick 8

The SP registers are located in the IO address space l In the IO address space l l SPL address is 0 x 3 D SPH address is 0 x 3 E These are. DEF’d in m 32 def. inc . . . 0 x 085 B SP registers are set using the OUT instruction l ldi out 0 x 085 C similar to how PORTB is written to TEMP, LOW(RAMEND) SPL, TEMP, HIGH(RAMEND) SPH, TEMP 0 x 085 D 0 x 085 E SP CS-280 Dr. Mark L. Hornick 0 x 085 F 9

The stack starts out with a size of 0 Data (bytes) are pushed onto the front of the stack l . . The front of the stack grows “downward” towards lower memory address Front of Stack grows downward . 0 x 085 B 0 x 085 C 0 x 085 D 0 x 085 E SP start CS-280 Dr. Mark L. Hornick 0 x 085 F 10

Using the Stack – pushing a byte onto the stack LDI PUSH INC PUSH R 20, 5 R 20 R 20 ; load 5 to R 20 ; push 5 to stack ; push 6 to stack 0 x 085 B ; push 7 to stack 0 x 085 C SP (after 3) l SP is automatically decremented by each PUSH instruction SP (after 2) 7 0 x 085 D SP (after 1) 6 0 x 085 E SP (before) 5 0 x 085 F CS-280 Dr. Mark L. Hornick 11

Using the Stack – pulling a byte from the stack POP POP l l R 21 R 22 R 23 ; pull 7 from stack ; pull 6 from stack ; pull 5 from stack SP is automatically incremented by each POP instruction Although the SP is incremented, the values in Data Memory are not actually removed 0 x 085 B 0 x 085 C SP (before) SP (after 1) 7 0 x 085 D SP (after 2) 6 0 x 085 E SP (after 3) 5 0 x 085 F CS-280 Dr. Mark L. Hornick 12

OK, so how do you actually use the stack to do something useful? l Demo CS-280 Dr. Mark L. Hornick 13

Instructions for calling a subroutine l RCALL l l Like RJMP, where the call target address must be no more than +/- 2 K from the call origin Operand can be a raw address (e. g. 0 x 0123) or a label that represents an address CALL l l <far_address> ; long call Like JMP, where the call target address can be any address 0 x 0 -0 x. FFFF ICALL l <close_address> ; relative call ; indirect call Call target address is contained indirectly in the Z register can contain any address 0 x 0 -0 x. FFFF The address of the next instruction following RCALL, or ICALL is automatically CS-280 pushed onto the Stack Dr. Mark L. Hornick 14

The address of the next instruction following RCALL, or ICALL is automatically pushed onto the Stack 0 x 002 A ldi 0 x 002 B RCALL 0 x 002 C CLR 1. 2. 3. r 0, 1 sub 1 r 0 ; sample instruction ; call subroutine ; sample instruction SP is automatically decremented by RCALL by 2 bytes (2 -byte address) The low byte is pushed first, followed by the high byte Program execution jumps to the first instruction in sub 1 0 x 085 B 0 x 085 C 0 x 085 D SP (after RCALL) SP (before RCALL) CS-280 Dr. Mark L. Hornick 0 x 00 0 x 085 E 0 x 2 C 0 x 085 F 15

Only one instruction is used for returning from a subroutine l RET ; return from subroutine l l l RET is the ONLY way to return from a subroutine l l The return address (the address of the instruction that immediately followed the RCALL, or ICALL) is popped from the Stack and placed in the PC SP is pre-decremented during execution of RET NEVER use RJMP or JMP to return l SP will not get decremented without RET Never use JMP, RJMP, or IJMP to call a subroutine l l The Stack will not get incremented Use only CALL, RCALL, or ICALL CS-280 Dr. Mark L. Hornick 16

When the RET statement in the subroutine is executed, the return address is popped from the Stack and placed in the PC 0 x 002 A ldi 0 x 002 B RCALL 0 x 002 C CLR l l l r 0, 1 sub 1 r 0 ; sample instruction ; call subroutine ; sample instruction SP is automatically incremented by RETby 2 bytes (2 -byte address) The high byte is popped first, followed by the low byte Program execution resumes at address 0 x 002 C after the RET from sub 1 0 x 085 B 0 x 085 C 0 x 085 D SP (before RET) SP (after RET) CS-280 Dr. Mark L. Hornick 0 x 00 0 x 085 E 0 x 2 C 0 x 085 F 17

Some more uses for the stack l Short term data storage l vs. regular Data Memory storage l l Preserving registers during calculations l l Easy to just PUSH and POP data on/off the stack vs. LD/ST from memory PUSH a register value onto the Stack to save it Do some other work involving that register POP the value off the Stack back into the register Preserving registers prior to a subroutine call l PUSH register values onto the Stack to save them Call subroutine(s) that might use those registers POP the values off the Stack back into the registers after the subroutine call returns 18
- Slides: 18