mima-tools/examples/call_ret_stack.mimasm
2019-11-10 18:34:52 +00:00

71 lines
1.1 KiB
Text

IAR = main
ACC = 0
RA = 0
SP = 0xfffff
; 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
100:
main:
; set counter bit 0
LDV counter
ADC 0x01
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 0x02
STV counter
HALT
200:
sub-1:
; Set counter bit 2
LDV counter
ADC 0x04
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 0x08
STV counter
RET
300:
sub-2:
; Set counter bit 4
LDV counter
ADC 0x10
STV counter
RET