2020-04-27 05:59:59 +00:00
|
|
|
## compute the factorial of 5, and print the result
|
2018-09-01 17:04:58 +00:00
|
|
|
#
|
2019-07-31 22:28:23 +00:00
|
|
|
# To run:
|
2021-03-30 01:47:52 +00:00
|
|
|
# $ bootstrap/bootstrap translate [01]*.subx factorial.subx -o factorial
|
2021-03-04 08:24:24 +00:00
|
|
|
# $ bootstrap/bootstrap run factorial
|
2018-09-01 17:04:58 +00:00
|
|
|
# Expected result:
|
|
|
|
# $ echo $?
|
|
|
|
# 120
|
2018-09-22 06:52:48 +00:00
|
|
|
#
|
2018-09-24 04:01:28 +00:00
|
|
|
# You can also run the automated test suite:
|
2021-03-04 08:24:24 +00:00
|
|
|
# $ bootstrap/bootstrap run factorial test
|
2018-09-22 06:52:48 +00:00
|
|
|
# Expected output:
|
2018-09-24 04:01:28 +00:00
|
|
|
# ........
|
2018-09-22 06:52:48 +00:00
|
|
|
# Every '.' indicates a passing test. Failing tests get a 'F'.
|
2018-09-01 17:04:58 +00:00
|
|
|
|
2018-09-01 22:58:53 +00:00
|
|
|
== code
|
2018-11-30 19:10:44 +00:00
|
|
|
# instruction effective address register displacement immediate
|
|
|
|
# . op subop mod rm32 base index scale r32
|
|
|
|
# . 1-3 bytes 3 bits 2 bits 3 bits 3 bits 3 bits 2 bits 2 bits 0/1/2/4 bytes 0/1/2/4 bytes
|
2018-09-01 17:04:58 +00:00
|
|
|
|
2020-11-02 06:02:13 +00:00
|
|
|
factorial: # n: int -> _/eax: int
|
2020-10-14 20:42:03 +00:00
|
|
|
# . prologue
|
|
|
|
55/push-ebp
|
|
|
|
89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
|
2020-11-02 06:02:13 +00:00
|
|
|
51/push-ecx
|
2020-10-14 20:42:03 +00:00
|
|
|
# if (n <= 1) return 1
|
|
|
|
b8/copy-to-eax 1/imm32
|
|
|
|
81 7/subop/compare 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 1/imm32 # compare *(ebp+8)
|
|
|
|
7e/jump-if-<= $factorial:end/disp8
|
2020-11-02 06:02:13 +00:00
|
|
|
# var ecx: int = n-1
|
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/ebp . . 1/r32/ecx 8/disp8 . # copy *(ebp+8) to ecx
|
|
|
|
49/decrement-ecx
|
2020-10-14 20:42:03 +00:00
|
|
|
# var eax: int = factorial(n-1)
|
|
|
|
# . . push args
|
2020-11-02 06:02:13 +00:00
|
|
|
51/push-ecx
|
2020-10-14 20:42:03 +00:00
|
|
|
# . . call
|
|
|
|
e8/call factorial/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
|
|
|
|
# return n * factorial(n-1)
|
|
|
|
f7 4/subop/multiply 1/mod/*+disp8 5/rm32/ebp . . 8/disp8 . # multiply *(ebp+8) into eax
|
|
|
|
# TODO: check for overflow
|
|
|
|
$factorial:end:
|
2020-11-02 06:02:13 +00:00
|
|
|
# restore registers
|
|
|
|
59/pop-to-ecx
|
2020-10-14 20:42:03 +00:00
|
|
|
# . epilogue
|
|
|
|
89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
|
|
|
|
5d/pop-to-ebp
|
|
|
|
c3/return
|
|
|
|
|
|
|
|
test-factorial:
|
|
|
|
# factorial(5)
|
|
|
|
# . . push args
|
|
|
|
68/push 5/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call factorial/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
|
|
|
|
# check-ints-equal(eax, 120, msg)
|
|
|
|
# . . push args
|
|
|
|
68/push "F - test-factorial"/imm32
|
|
|
|
68/push 0x78/imm32/expected-120
|
|
|
|
50/push-eax
|
|
|
|
# . . call
|
|
|
|
e8/call check-ints-equal/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
|
|
|
|
# end
|
|
|
|
c3/return
|
|
|
|
|
2019-02-19 05:48:19 +00:00
|
|
|
Entry: # run tests if necessary, compute `factorial(5)` if not
|
2019-10-16 02:35:19 +00:00
|
|
|
# . prologue
|
2019-09-01 05:18:24 +00:00
|
|
|
89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
|
|
|
|
|
2020-11-02 06:02:13 +00:00
|
|
|
# initialize heap (needed by tests elsewhere)
|
2019-09-19 23:32:10 +00:00
|
|
|
# . Heap = new-segment(Heap-size)
|
2019-05-26 06:47:49 +00:00
|
|
|
# . . push args
|
|
|
|
68/push Heap/imm32
|
2019-11-30 19:14:49 +00:00
|
|
|
ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Heap-size/disp32 # push *Heap-size
|
2019-05-26 06:47:49 +00:00
|
|
|
# . . call
|
|
|
|
e8/call new-segment/disp32
|
|
|
|
# . . discard args
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
|
2019-02-19 05:48:19 +00:00
|
|
|
|
2020-11-02 06:02:13 +00:00
|
|
|
# if (argc <= 1) return factorial(5)
|
2018-10-06 02:49:47 +00:00
|
|
|
$run-main:
|
2020-11-02 06:02:13 +00:00
|
|
|
81 7/subop/compare 1/mod/*+disp8 5/rm32/ebp . . . . 0/disp8 1/imm32 # compare *ebp
|
|
|
|
7f/jump-if-> $main:run-tests/disp8
|
2019-09-02 06:57:29 +00:00
|
|
|
# eax = factorial(5)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2018-11-30 18:54:42 +00:00
|
|
|
68/push 5/imm32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . call
|
2018-11-30 18:54:42 +00:00
|
|
|
e8/call factorial/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
|
2020-04-27 05:59:59 +00:00
|
|
|
# var buffer/ecx: (stream byte 10) # number of decimal digits a 32-bit number can have
|
|
|
|
81 5/subop/subtract 3/mod/direct 4/rm32/esp . . . . . 0xa/imm32 # subtract from esp
|
|
|
|
68/push 0xa/imm32/decimal-digits-in-32bit-number
|
|
|
|
68/push 0/imm32/read
|
|
|
|
68/push 0/imm32/write
|
|
|
|
89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx
|
2020-06-30 01:31:17 +00:00
|
|
|
# write-int32-decimal(buffer, eax)
|
2020-04-27 05:59:59 +00:00
|
|
|
# . . push args
|
|
|
|
50/push-eax
|
|
|
|
51/push-ecx
|
|
|
|
# . . call
|
2020-06-30 01:31:17 +00:00
|
|
|
e8/call write-int32-decimal/disp32
|
2020-04-27 05:59:59 +00:00
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
|
|
|
|
# write-stream(stderr, buffer)
|
|
|
|
# . . push args
|
|
|
|
51/push-ecx
|
|
|
|
68/push 2/imm32/stderr
|
|
|
|
# . . call
|
|
|
|
e8/call write-stream/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
|
|
|
|
# write(stderr, Newline)
|
|
|
|
# . . push args
|
|
|
|
68/push Newline/imm32
|
|
|
|
68/push 2/imm32/stderr
|
|
|
|
# . . call
|
|
|
|
e8/call write/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
|
2020-10-14 20:42:03 +00:00
|
|
|
#
|
2019-08-26 18:55:26 +00:00
|
|
|
89/copy 3/mod/direct 3/rm32/ebx . . . 0/r32/eax . . # copy eax to ebx
|
2020-11-02 06:02:13 +00:00
|
|
|
eb/jump $main:end/disp8
|
|
|
|
$main:run-tests:
|
|
|
|
# otherwise if first arg is "test", then return run_tests()
|
|
|
|
# if (!kernel-string-equal?(argv[1], "test")) goto do-nothing
|
|
|
|
# . eax = kernel-string-equal?(argv[1], "test")
|
|
|
|
# . . push args
|
|
|
|
68/push "test"/imm32
|
|
|
|
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
|
|
|
|
# . . call
|
|
|
|
e8/call kernel-string-equal?/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
|
|
|
|
# . if (eax == false) goto do-nothing
|
|
|
|
3d/compare-eax-and 0/imm32/false
|
|
|
|
74/jump-if-= $main:do-nothing/disp8
|
|
|
|
# run-tests()
|
|
|
|
e8/call run-tests/disp32
|
|
|
|
# exit(*Num-test-failures)
|
|
|
|
8b/copy 0/mod/indirect 5/rm32/.disp32 . . 3/r32/ebx Num-test-failures/disp32 # copy *Num-test-failures to ebx
|
|
|
|
eb/jump $main:end/disp8
|
|
|
|
$main:do-nothing:
|
|
|
|
bb/copy-to-ebx 0/imm32
|
2019-09-01 06:22:49 +00:00
|
|
|
$main:end:
|
2020-06-11 06:09:30 +00:00
|
|
|
e8/call syscall_exit/disp32
|
2018-09-01 17:04:58 +00:00
|
|
|
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . vim:nowrap:textwidth=0
|