Easier to read documentation in examples
This commit is contained in:
@@ -1,19 +1,20 @@
|
|||||||
;;; factorial.asm: A program that generates the factorials of each
|
;;; factorial.asm: A program that generates the factorials of each
|
||||||
;;; number from 1 to 24 (24!~=UINT64_MAX). Using the registers to
|
;;; number from 1 to 22 (22!~=UINT64_MAX). Using the registers to
|
||||||
;;; store `n` and `n!`.
|
;;; store `n` and `n!`.
|
||||||
|
|
||||||
;; Constants
|
;; Constants
|
||||||
%const(limit) 22 %end
|
%const(limit) 22 %end
|
||||||
|
|
||||||
;; Setup entrypoint
|
;; Setup entrypoint
|
||||||
global main
|
global main
|
||||||
main:
|
main:
|
||||||
;; Setup initial REG[0] = 1 and REG[1] = 1
|
;; $I -> W[0] = 1, $J -> W[1] = 1
|
||||||
push.word 1
|
push.word 1
|
||||||
mov.word 0
|
mov.word 0
|
||||||
push.word 1
|
push.word 1
|
||||||
mov.word 1
|
mov.word 1
|
||||||
|
|
||||||
;; Print `REG[0]: REG[1]`
|
;; Print `$I: $J`
|
||||||
loopback:
|
loopback:
|
||||||
push.byte '\t'
|
push.byte '\t'
|
||||||
print.char
|
print.char
|
||||||
@@ -28,21 +29,23 @@ loopback:
|
|||||||
push.byte '\n'
|
push.byte '\n'
|
||||||
print.char
|
print.char
|
||||||
|
|
||||||
;; REG[0] += 1
|
;; $I += 1
|
||||||
push.reg.word 0
|
push.reg.word 0
|
||||||
push.word 1
|
push.word 1
|
||||||
plus.word
|
plus.word
|
||||||
mov.word 0
|
mov.word 0
|
||||||
|
|
||||||
;; REG[1] *= REG[0]
|
;; $J *= $I
|
||||||
push.reg.word 0
|
push.reg.word 0
|
||||||
push.reg.word 1
|
push.reg.word 1
|
||||||
mult.word
|
mult.word
|
||||||
mov.word 1
|
mov.word 1
|
||||||
|
|
||||||
|
;; IF $I >= $LIMIT ...
|
||||||
push.word $limit
|
push.word $limit
|
||||||
push.reg.word 0
|
push.reg.word 0
|
||||||
gte.word
|
gte.word
|
||||||
;; Jump to `loopback`
|
;; THEN jump to `loopback`
|
||||||
jump.if.byte loopback
|
jump.if.byte loopback
|
||||||
|
;; ELSE halt
|
||||||
halt
|
halt
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
;; Constants
|
;; Constants
|
||||||
%const(limit) 12200160415121876738 %end
|
%const(limit) 12200160415121876738 %end
|
||||||
|
|
||||||
%const(increment_i)
|
%const(increment_i)
|
||||||
push.reg.word 2
|
push.reg.word 2
|
||||||
push.word 1
|
push.word 1
|
||||||
@@ -31,36 +32,38 @@
|
|||||||
;; Setup entrypoint
|
;; Setup entrypoint
|
||||||
global main
|
global main
|
||||||
main:
|
main:
|
||||||
;; Setup iterator i
|
;; Setup iterator I
|
||||||
push.word 1
|
push.word 1
|
||||||
mov.word 2
|
mov.word 2
|
||||||
;; Setup initial REG[0] = 1 and REG[1] = 1
|
;; Setup initial A -> W[0] = 1 and B -> W[1] = 1
|
||||||
push.word 1
|
push.word 1
|
||||||
mov.word 0
|
mov.word 0
|
||||||
push.word 1
|
push.word 1
|
||||||
mov.word 1
|
mov.word 1
|
||||||
|
|
||||||
;; Print REG[0] and REG[1]
|
;; Print "$I: $A" and "($I + 1): $B"
|
||||||
loopback:
|
loopback:
|
||||||
call print_pair
|
call print_pair
|
||||||
|
|
||||||
;; REG[0] += REG[1]
|
;; $A += $B
|
||||||
push.reg.word 0
|
push.reg.word 0
|
||||||
push.reg.word 1
|
push.reg.word 1
|
||||||
plus.word
|
plus.word
|
||||||
mov.word 0
|
mov.word 0
|
||||||
|
|
||||||
;; REG[1] += REG[0]
|
;; $B += $A
|
||||||
push.reg.word 0
|
push.reg.word 0
|
||||||
push.reg.word 1
|
push.reg.word 1
|
||||||
plus.word
|
plus.word
|
||||||
mov.word 1
|
mov.word 1
|
||||||
|
|
||||||
|
;; IF $B >= $LIMIT ...
|
||||||
push.word $limit
|
push.word $limit
|
||||||
push.reg.word 1
|
push.reg.word 1
|
||||||
gte.word
|
gte.word
|
||||||
;; Jump to `loopback`
|
;; THEN jump to `loopback`
|
||||||
jump.if.byte loopback
|
jump.if.byte loopback
|
||||||
|
;; ELSE halt
|
||||||
halt
|
halt
|
||||||
|
|
||||||
print_pair:
|
print_pair:
|
||||||
|
|||||||
Reference in New Issue
Block a user