-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from yaacov/stack-ops
Add stack opcodes
- Loading branch information
Showing
11 changed files
with
227 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
; Program to calculate factorial revursively | ||
|
||
; Init the stack pointer (base pointer) | ||
SETBP STACK | ||
|
||
; Call recursive factorial method | ||
PUSH NUM1 | ||
CALL FUNC | ||
POP TEMP ; clear stack | ||
|
||
; Move result from register B to memory | ||
STOREB RESULT | ||
|
||
; End program | ||
END | ||
|
||
NUM1: DATA 0x06 | ||
RESULT: DATA 0x00 | ||
|
||
ZERO: DATA 0x00 | ||
MINUSONE: DATA 0xFF | ||
TEMP: DATA 0x00 | ||
|
||
; Recursive factorial | ||
FUNC: | ||
; Load argument to register A | ||
LOADA [BP + 1] ; [base pointer + 1] is the first function argument | ||
|
||
; Add the number to register B | ||
STOREA TEMP | ||
ADDB TEMP | ||
|
||
; Use register A to check for termination condition (arg == 0) | ||
JZA RET | ||
|
||
; Put (arg - 1) in temp | ||
ADDA MINUSONE | ||
STOREA TEMP | ||
|
||
; Call recursively with num - 1 | ||
PUSH TEMP | ||
CALL FUNC | ||
POP TEMP ; clear stack | ||
RET: RET | ||
|
||
; Start of stack | ||
STACK: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
; Program to swap two numbers using the stack | ||
|
||
; Init the stack pointer (base pointer) | ||
SETBP STACK | ||
|
||
; Push two numbers in order | ||
PUSH NUM1 | ||
PUSH NUM2 | ||
|
||
; Pop the numbers out of order | ||
POP NUM1 | ||
POP NUM2 | ||
|
||
; End program | ||
END | ||
|
||
NUM1: DATA 0xF0 | ||
NUM2: DATA 0x0F | ||
|
||
; start of stack memory | ||
STACK: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters