Subroutine
;subroutine template(2 args)
subroutine
;;Stack Buildup
ADD R6, R6, -4
STR R7, R6, 2
STR R5, R6, 1
ADD R5, R6, 0
ADD R6, R6, -5
STR R0, R5, -1
STR R1, R5, -2
STR R2, R5, -3
STR R3, R5, -4
STR R4, R5, -5
;perform subroutine operation here
;recursive call to subroutine.
ADD R6, R6, -1 ; Push 1 on the stack
STR RX, R6, 0 ; RX is the arg2 to push
ADD R6, R6, -1 ;Push 1 on the stack
STR RX, R6, 0 ;RX is the arg1 to push
JSR subroutine ;Call subroutine
LDR RX, R6, 0 ;Store return value to RX
ADD R6, R6, 3 ;Pop the two pushed and the return value
STR RX, R5, 0 ;Store return value to local variable in current stack
TEARDOWN
;;Stack Teardown
LDR R0, R5, 0 ; R0 = Local variable that holds the return value
STR R0, R5, 3 ; RV = R0
LDR R4, R5, -5 ; Restore R4
LDR R3, R5, -4 ; Restore R3
LDR R2, R5, -3 ; Restore R2
LDR R1, R5, -2 ; Restore R0
LDR R0, R5, -1 ; Restore R1
ADD R6, R5, 0 ; Restore SP
LDR R5, R6, 1 ; Restore FP
LDR R7, R6, 2 ; Restore RA
ADD R6, R6, 3 ; Pop ra,fp,lv1
RET
Array Addressing
;;Fetching Array Nearby
LEA R1, ARRAY ;R1 is the address of ARRAY[0]
LD R2, I ;R2 is the index number
ADD R1, R1, R2 ;R1 = address of ARRAY[I]
LDR R1, R1, 0 ;R1 = value at ARRAY[I]
;;Storing Array Nearby
LEA R1, ARRAY ;R1 is the address of ARRAY[0]
LD R2, I ;R2 is the index number
ADD R1, R1, R2 ;R1 = address of ARRAY[I]
STR RX, R1, 0 ;Store value of RX to ARRAY[I]
;;Fetching Array far away
LD R1, ARRAY ;R1 is the address of ARRAY[0]
LD R2, I ;R2 is the index number
ADD R1, R1, R2 ;R1 = address of ARRAY[I]
LDR R1, R1, 0 ;R1 = value at ARRAY[I]
(BR SK) ; Don't execute the address
AD .fill ARRAY
SK NOP
;;Storing Array far away
LD R1, ARRAY ;R1 is the address of ARRAY[0]
LD R2, I ;R2 is the index number
ADD R1, R1, R2 ;R1 = address of ARRAY[I]
STR RX, R1, 0 ;Store value of RX to ARRAY[I]
(BR SK) ; Don't execute the address
AD .fill ARRAY
SK NOP
if
ADD R1, R1, 0 ;condition variablee
BR(nzp) ELSE1 ;end if
;;if body
BR ENDIF1
ELSE1
;;else body
ENDIF1
while
WHILE1 ;Loop
ADD R1, R1, 0 ;condition variable
BR(nzp) ENDW1 ;end loop if condition met
;;while body
ENDW1
for
//for (i = 0; i < val; i++) {
// body
//}
AND R1, R1, 0 ;;R1 = i = 0
FOR1
LD RX, VALUE ;;RX = VALUE
NOT RX, RX
ADD RX, RX, 1 ;;R2 = - VALUE
ADD RX, RX, R1 ;;i - val
BRzp ENDFOR1 ;;End for if i - val < 0
;;for body
ADD R1, R1, 1 ;;i++
BR FOR1 ;;continue for loop
ENDFOR1 ;;end for loop
VALUE .fill xFFFF
//for (I = 0; I < 10; I++) {
// body
//}
AND R1, R1, 0 ;;R1 = i = 0
ST R1, I ;;Store value of R1 to I
FOR
LD R1, I ;;Load value of R1 to I
ADD R1, R1, -10 ;;R1 = i - 10
BRzp ENDFOR1 ;;End for if i - 10 >= 0
;;body
LD R1, I ;;Load value of I to R1
ADD R1, R1, 1 ;;I++
ST R1, I ;;Store value of I back to R1
BR FOR1 ;;continue for loop
ENDFOR1 ;;end for loop