Skip to content

Commit

Permalink
refactor-example
Browse files Browse the repository at this point in the history
Signed-off-by: yzamir <[email protected]>
  • Loading branch information
yaacov committed Oct 6, 2023
1 parent 9f35022 commit e030155
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions examples/recursive-triangular.asm
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ SETBP STACK
LOADA NUM1
PUSHA
CALL FUNC
POPA ; clear stack into regA
POPA ; Get return value into regA

; Move result from register B to memory
STOREB RESULT
; Move result from register A to memory
STOREA RESULT

; End program
END
Expand All @@ -23,22 +23,28 @@ MINUSONE: DATA 0xFF
TEMP: DATA 0x00

; Recursive triangular
; f(n) = n + f(n-1)
FUNC:
; Load arg to register A
LOADA [BP + 1] ; [base pointer + 1] is the first function argument

; Add the arg to register B using TEMP
STOREA TEMP
ADDB TEMP

; Check for termination condition, arg == 0
JZA RET

; Call recursively with (arg - 1)
ADDA MINUSONE
PUSHA
CALL FUNC
POPA ; clear stack into regA
POPA ; Get return value into regA

; Add the retrun value to arg:
STOREA TEMP ; TEMP = return value
LOADA [BP + 1] ; regA = arg
ADDA TEMP ; regA + TEMP

; Store return value in arg
STOREA [BP + 1]

RET: RET

; Start of stack
Expand Down

0 comments on commit e030155

Please sign in to comment.