.reg IAR main .reg SP -1 ; the last addressable address ; In this example, the stack pointer points to the next free address ; below the stack. The stack grows downwards from large to small ; addresses. The stack has no stack frames to simplify the program. counter: .lit 0 .org 100 main: ; set counter bit 0 LDV counter ADC 0b00001 STV counter ; Since we're top-level, we don't need to (re-)store our RA when calling CALL sub-1 ; set counter bit 1 LDV counter ADC 0b00010 STV counter HALT .org 200 sub-1: ; Set counter bit 2 LDV counter ADC 0b00100 STV counter ;; Store the current RA on the stack ; Store the RA at current position of stack pointer LDRA STRS 0 ; Move stack pointer by 1 since the stack grew LDSP ADC -1 STSP ; Call the subfunction CALL sub-2 ;; Pop and restore the RA from the stack ; Read RA from the top of the stack LDRS 1 STRA ; Remove top element from stack LDSP ADC 1 STSP ; Set counter bit 3 LDV counter ADC 0b01000 STV counter RET .org 300 sub-2: ; Set counter bit 4 LDV counter ADC 0b10000 STV counter RET