aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-11-01 18:12:55 +0000
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-11-01 18:12:55 +0000
commitbfa0a1f85c1efbb8d88840f9138850ed534ff2d8 (patch)
tree2040413b3b6f733d0a3ae8824e8e792e64649df0
parentd521871b98c2da215615b2b9fc5b90cad0223df4 (diff)
downloadovm-bfa0a1f85c1efbb8d88840f9138850ed534ff2d8.tar.gz
ovm-bfa0a1f85c1efbb8d88840f9138850ed534ff2d8.tar.bz2
ovm-bfa0a1f85c1efbb8d88840f9138850ed534ff2d8.zip
Implemented a factorial program in the assembly
Very cool, easy, and reads well
-rw-r--r--examples/factorial.asm43
1 files changed, 43 insertions, 0 deletions
diff --git a/examples/factorial.asm b/examples/factorial.asm
new file mode 100644
index 0000000..08c3c13
--- /dev/null
+++ b/examples/factorial.asm
@@ -0,0 +1,43 @@
+;;; factorai.asm: A program that generates the factorials of each
+;;; number from 1 to 24 (24!~=UINT64_MAX). Using the registers to
+;;; store `n` and `n!`.
+
+ ;; Setup initial REG[0] = 1 and REG[1] = 1
+ push.word 1
+ mov.word 0
+ push.word 1
+ mov.word 1
+
+ ;; Print `REG[0]: REG[1]`
+ ;; Here is the loop back point `#`
+ push.byte '\t'
+ print.char
+ push.reg.word 0
+ print.word
+ push.byte ':'
+ print.char
+ push.byte ' '
+ print.char
+ push.reg.word 1
+ print.word
+ push.byte '\n'
+ print.char
+
+ ;; REG[0] += 1
+ push.reg.word 0
+ push.word 1
+ plus.word
+ mov.word 0
+
+ ;; REG[1] *= REG[0]
+ push.reg.word 0
+ push.reg.word 1
+ mult.word
+ mov.word 1
+
+ push.word 24
+ push.reg.word 0
+ gte.word
+ ;; Jump to `#`
+ jump.if.byte 4
+ halt