Use constants in examples where possible
Stuff like numeric limits can be codified in constants which act self documenting.
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
;;; 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 24 (24!~=UINT64_MAX). Using the registers to
|
||||||
;;; store `n` and `n!`.
|
;;; store `n` and `n!`.
|
||||||
|
;; Constants
|
||||||
|
%const(limit) 22 %end
|
||||||
|
|
||||||
;; Setup entrypoint
|
;; Setup entrypoint
|
||||||
global main
|
global main
|
||||||
@@ -38,7 +40,7 @@ loopback:
|
|||||||
mult.word
|
mult.word
|
||||||
mov.word 1
|
mov.word 1
|
||||||
|
|
||||||
push.word 24
|
push.word $limit
|
||||||
push.reg.word 0
|
push.reg.word 0
|
||||||
gte.word
|
gte.word
|
||||||
;; Jump to `loopback`
|
;; Jump to `loopback`
|
||||||
|
|||||||
@@ -4,6 +4,30 @@
|
|||||||
;;; memory is necessary for this program to function, unlike a pure
|
;;; memory is necessary for this program to function, unlike a pure
|
||||||
;;; stack version.
|
;;; stack version.
|
||||||
|
|
||||||
|
;; Constants
|
||||||
|
%const(limit) 12200160415121876738 %end
|
||||||
|
%const(increment_i)
|
||||||
|
push.reg.word 2
|
||||||
|
push.word 1
|
||||||
|
plus.word
|
||||||
|
mov.word 2
|
||||||
|
%end
|
||||||
|
|
||||||
|
%const(print_i)
|
||||||
|
push.reg.word 2
|
||||||
|
print.word
|
||||||
|
%end
|
||||||
|
|
||||||
|
%const(print_reg_0)
|
||||||
|
push.reg.word 0
|
||||||
|
print.word
|
||||||
|
%end
|
||||||
|
|
||||||
|
%const(print_reg_1)
|
||||||
|
push.reg.word 1
|
||||||
|
print.word
|
||||||
|
%end
|
||||||
|
|
||||||
;; Setup entrypoint
|
;; Setup entrypoint
|
||||||
global main
|
global main
|
||||||
main:
|
main:
|
||||||
@@ -32,7 +56,7 @@ loopback:
|
|||||||
plus.word
|
plus.word
|
||||||
mov.word 1
|
mov.word 1
|
||||||
|
|
||||||
push.word 12200160415121876738
|
push.word $limit
|
||||||
push.reg.word 1
|
push.reg.word 1
|
||||||
gte.word
|
gte.word
|
||||||
;; Jump to `loopback`
|
;; Jump to `loopback`
|
||||||
@@ -42,46 +66,24 @@ loopback:
|
|||||||
print_pair:
|
print_pair:
|
||||||
push.byte '\t'
|
push.byte '\t'
|
||||||
print.char
|
print.char
|
||||||
call print_i
|
$print_i
|
||||||
push.byte ':'
|
push.byte ':'
|
||||||
print.char
|
print.char
|
||||||
push.byte ' '
|
push.byte ' '
|
||||||
print.char
|
print.char
|
||||||
call print_reg_0
|
$print_reg_0
|
||||||
push.byte '\n'
|
push.byte '\n'
|
||||||
print.char
|
print.char
|
||||||
call increment_i
|
$increment_i
|
||||||
push.byte '\t'
|
push.byte '\t'
|
||||||
print.char
|
print.char
|
||||||
call print_i
|
$print_i
|
||||||
push.byte ':'
|
push.byte ':'
|
||||||
print.char
|
print.char
|
||||||
push.byte ' '
|
push.byte ' '
|
||||||
print.char
|
print.char
|
||||||
call print_reg_1
|
$print_reg_1
|
||||||
push.byte '\n'
|
push.byte '\n'
|
||||||
print.char
|
print.char
|
||||||
call increment_i
|
$increment_i
|
||||||
ret
|
|
||||||
|
|
||||||
increment_i:
|
|
||||||
push.reg.word 2
|
|
||||||
push.word 1
|
|
||||||
plus.word
|
|
||||||
mov.word 2
|
|
||||||
ret
|
|
||||||
|
|
||||||
print_i:
|
|
||||||
push.reg.word 2
|
|
||||||
print.word
|
|
||||||
ret
|
|
||||||
|
|
||||||
print_reg_0:
|
|
||||||
push.reg.word 0
|
|
||||||
print.word
|
|
||||||
ret
|
|
||||||
|
|
||||||
print_reg_1:
|
|
||||||
push.reg.word 1
|
|
||||||
print.word
|
|
||||||
ret
|
ret
|
||||||
|
|||||||
Reference in New Issue
Block a user