Subroutines

A Dr. Taylor Tutorial

initstack:                                ;  r16   SP
           ldi  r16, HIGH(RAMEND-0x20)    ; 0x08  ----
           out  SPH, r16                  ; 0x08  08--
           ldi  r16, LOW(RAMEND-0x20)     ; 0x3f  08--
           out  SPL, r16                  ; 0x3f  083f

Example Program

The following program shows an example of calling a delay subroutine. Can you walk through the code and keep track of the registers R16-R18, the program counter, the stack and the stack pointer?

.cseg
.org 0x0
         rjmp initStack

.org 0x2a
initStack:
           ldi   r16, HIGH(RAMEND-0x20)
           out   SPH, r16
           ldi   r16, LOW(RAMEND-0x20)
           out   SPL, r16

start:
           ldi   r16, 0
           rcall delay
           dec   r16
           rjmp  start

delay:
           push  r16
           push  r17
           push  r18
           ldi   r16, 20
loop1:
           ldi   r17, 255
loop2:
           ldi   r18, 255
loop3:
           dec   r18
           brne  loop3
           dec   r17
           brne  loop2
           dec   r16
           brne  loop1
           pop   r18
           pop   r17
           pop   r16
           ret

General Stack Usage Rules

Tutorials for Other Courses

Additional Links

Site Mirror

This is a mirror to be used when http://myweb.msoe.edu/taylor/, (EDU instead of US) is not available.