From 480fd9958abbd9b3b8443190ece755504cacdf9c Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Wed, 14 Oct 2020 13:42:03 -0700 Subject: 7032 --- html/apps/factorial2.subx.html | 200 +++++++++++++++++++++-------------------- 1 file changed, 101 insertions(+), 99 deletions(-) (limited to 'html/apps/factorial2.subx.html') diff --git a/html/apps/factorial2.subx.html b/html/apps/factorial2.subx.html index cf2478f8..295d7e0b 100644 --- a/html/apps/factorial2.subx.html +++ b/html/apps/factorial2.subx.html @@ -16,11 +16,11 @@ a { color:inherit; } * { font-size:12pt; font-size: 1em; } .subxComment { color: #005faf; } .subxS2Comment { color: #8a8a8a; } -.subxFunction { color: #af5f00; text-decoration: underline; } +.SpecialChar { color: #d70000; } .LineNr { } .subxTest { color: #5f8700; } .subxS1Comment { color: #0000af; } -.SpecialChar { color: #d70000; } +.subxFunction { color: #af5f00; text-decoration: underline; } .Constant { color: #008787; } .subxH1Comment { color: #005faf; text-decoration: underline; } --> @@ -75,106 +75,108 @@ if ('onhashchange' in window) { 15 # Expected output: 16 # ........ 17 # Every '.' indicates a passing test. Failing tests get a 'F'. - 18 - 19 == code + 18 # + 19 # Compare apps/factorial.subx 20 - 21 Entry: # run tests if necessary, compute `factorial(5)` if not - 22 # . prologue - 23 89/<- %ebp 4/r32/esp - 24 - 25 # initialize heap - 26 # . Heap = new-segment(Heap-size) - 27 # . . push args - 28 68/push Heap/imm32 - 29 ff 6/subop/push *Heap-size - 30 # . . call - 31 e8/call new-segment/disp32 - 32 # . . discard args - 33 81 0/subop/add %esp 8/imm32 - 34 - 35 # - if argc > 1 and argv[1] == "test", then return run_tests() - 36 # if (argc <= 1) goto run-main - 37 81 7/subop/compare *ebp 1/imm32 - 38 7e/jump-if-<= $run-main/disp8 - 39 # if (!kernel-string-equal?(argv[1], "test")) goto run-main - 40 # . eax = kernel-string-equal?(argv[1], "test") - 41 # . . push args - 42 68/push "test"/imm32 - 43 ff 6/subop/push *(ebp+8) - 44 # . . call - 45 e8/call kernel-string-equal?/disp32 - 46 # . . discard args - 47 81 0/subop/add %esp 8/imm32 - 48 # . if (eax == false) goto run-main - 49 3d/compare-eax-and 0/imm32/false - 50 74/jump-if-= $run-main/disp8 - 51 # run-tests() - 52 e8/call run-tests/disp32 - 53 # syscall(exit, *Num-test-failures) - 54 8b/-> *Num-test-failures 3/r32/ebx - 55 eb/jump $main:end/disp8 - 56 $run-main: - 57 # - otherwise return factorial(5) - 58 # eax = factorial(5) - 59 # . . push args - 60 68/push 5/imm32 - 61 # . . call - 62 e8/call factorial/disp32 - 63 # . . discard args - 64 81 0/subop/add %esp 4/imm32 - 65 # syscall(exit, eax) - 66 89/<- %ebx 0/r32/eax - 67 $main:end: - 68 e8/call syscall_exit/disp32 - 69 - 70 factorial: # n: int -> int/eax - 71 # . prologue - 72 55/push-ebp - 73 89/<- %ebp 4/r32/esp - 74 53/push-ebx - 75 # if (n <= 1) return 1 - 76 b8/copy-to-eax 1/imm32 - 77 81 7/subop/compare *(ebp+8) 1/imm32 - 78 7e/jump-if-<= $factorial:end/disp8 - 79 # var ebx: int = n-1 - 80 8b/-> *(ebp+8) 3/r32/ebx - 81 4b/decrement-ebx - 82 # var eax: int = factorial(n-1) - 83 # . . push args - 84 53/push-ebx - 85 # . . call - 86 e8/call factorial/disp32 - 87 # . . discard args - 88 81 0/subop/add %esp 4/imm32 - 89 # return n * factorial(n-1) - 90 f7 4/subop/multiply-into-eax *(ebp+8) - 91 # TODO: check for overflow - 92 $factorial:end: - 93 # . epilogue - 94 5b/pop-to-ebx - 95 89/<- %esp 5/r32/ebp - 96 5d/pop-to-ebp - 97 c3/return - 98 - 99 test-factorial: -100 # factorial(5) -101 # . . push args -102 68/push 5/imm32 -103 # . . call -104 e8/call factorial/disp32 -105 # . . discard args -106 81 0/subop/add %esp 4/imm32 -107 # check-ints-equal(eax, 120, msg) -108 # . . push args -109 68/push "F - test-factorial"/imm32 -110 68/push 0x78/imm32/expected-120 -111 50/push-eax + 21 == code + 22 + 23 factorial: # n: int -> int/eax + 24 # . prologue + 25 55/push-ebp + 26 89/<- %ebp 4/r32/esp + 27 53/push-ebx + 28 # if (n <= 1) return 1 + 29 b8/copy-to-eax 1/imm32 + 30 81 7/subop/compare *(ebp+8) 1/imm32 + 31 7e/jump-if-<= $factorial:end/disp8 + 32 # var ebx: int = n-1 + 33 8b/-> *(ebp+8) 3/r32/ebx + 34 4b/decrement-ebx + 35 # var eax: int = factorial(n-1) + 36 # . . push args + 37 53/push-ebx + 38 # . . call + 39 e8/call factorial/disp32 + 40 # . . discard args + 41 81 0/subop/add %esp 4/imm32 + 42 # return n * factorial(n-1) + 43 f7 4/subop/multiply-into-eax *(ebp+8) + 44 # TODO: check for overflow + 45 $factorial:end: + 46 # . epilogue + 47 5b/pop-to-ebx + 48 89/<- %esp 5/r32/ebp + 49 5d/pop-to-ebp + 50 c3/return + 51 + 52 test-factorial: + 53 # factorial(5) + 54 # . . push args + 55 68/push 5/imm32 + 56 # . . call + 57 e8/call factorial/disp32 + 58 # . . discard args + 59 81 0/subop/add %esp 4/imm32 + 60 # check-ints-equal(eax, 120, msg) + 61 # . . push args + 62 68/push "F - test-factorial"/imm32 + 63 68/push 0x78/imm32/expected-120 + 64 50/push-eax + 65 # . . call + 66 e8/call check-ints-equal/disp32 + 67 # . . discard args + 68 81 0/subop/add %esp 0xc/imm32 + 69 # end + 70 c3/return + 71 + 72 Entry: # run tests if necessary, compute `factorial(5)` if not + 73 # . prologue + 74 89/<- %ebp 4/r32/esp + 75 + 76 # initialize heap + 77 # . Heap = new-segment(Heap-size) + 78 # . . push args + 79 68/push Heap/imm32 + 80 ff 6/subop/push *Heap-size + 81 # . . call + 82 e8/call new-segment/disp32 + 83 # . . discard args + 84 81 0/subop/add %esp 8/imm32 + 85 + 86 # - if argc > 1 and argv[1] == "test", then return run_tests() + 87 # if (argc <= 1) goto run-main + 88 81 7/subop/compare *ebp 1/imm32 + 89 7e/jump-if-<= $run-main/disp8 + 90 # if (!kernel-string-equal?(argv[1], "test")) goto run-main + 91 # . eax = kernel-string-equal?(argv[1], "test") + 92 # . . push args + 93 68/push "test"/imm32 + 94 ff 6/subop/push *(ebp+8) + 95 # . . call + 96 e8/call kernel-string-equal?/disp32 + 97 # . . discard args + 98 81 0/subop/add %esp 8/imm32 + 99 # . if (eax == false) goto run-main +100 3d/compare-eax-and 0/imm32/false +101 74/jump-if-= $run-main/disp8 +102 # run-tests() +103 e8/call run-tests/disp32 +104 # syscall(exit, *Num-test-failures) +105 8b/-> *Num-test-failures 3/r32/ebx +106 eb/jump $main:end/disp8 +107 $run-main: +108 # - otherwise return factorial(5) +109 # ebx = factorial(5) +110 # . . push args +111 68/push 5/imm32 112 # . . call -113 e8/call check-ints-equal/disp32 +113 e8/call factorial/disp32 114 # . . discard args -115 81 0/subop/add %esp 0xc/imm32 -116 # end -117 c3/return +115 81 0/subop/add %esp 4/imm32 +116 # +117 89/<- %ebx 0/r32/eax +118 $main:end: +119 e8/call syscall_exit/disp32 -- cgit 1.4.1-2-gfad0