From 518429b1cbb193d31416e02e660908469b96a459 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Sat, 31 Aug 2019 22:34:56 -0700 Subject: 5598 --- apps/factorial.subx | 3 +- html/apps/factorial.subx.html | 89 +++++++++++++++++++++---------------------- 2 files changed, 45 insertions(+), 47 deletions(-) diff --git a/apps/factorial.subx b/apps/factorial.subx index 34309546..12fc3b0c 100644 --- a/apps/factorial.subx +++ b/apps/factorial.subx @@ -73,9 +73,8 @@ factorial: # n : int -> int/eax 55/push-ebp 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp 53/push-ebx - # eax = 1 (base case) + # if (n <= 1) return 1 b8/copy-to-eax 1/imm32 - # if (n <= 1) return 81 7/subop/compare 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 1/imm32 # compare *(ebp+8) 7e/jump-if-<= $factorial:end/disp8 # ebx = n-1 diff --git a/html/apps/factorial.subx.html b/html/apps/factorial.subx.html index f8e53ba6..67bfaf84 100644 --- a/html/apps/factorial.subx.html +++ b/html/apps/factorial.subx.html @@ -134,52 +134,51 @@ if ('onhashchange' in window) { 73 55/push-ebp 74 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp 75 53/push-ebx - 76 # eax = 1 (base case) + 76 # if (n <= 1) return 1 77 b8/copy-to-eax 1/imm32 - 78 # if (n <= 1) return - 79 81 7/subop/compare 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 1/imm32 # compare *(ebp+8) - 80 7e/jump-if-<= $factorial:end/disp8 - 81 # ebx = n-1 - 82 8b/copy 1/mod/*+disp8 5/rm32/ebp . . 3/r32/ebx 8/disp8 . # copy *(ebp+8) to ebx - 83 81 5/subop/subtract 3/mod/direct 3/rm32/ebx . . . . . 1/imm32 # subtract from ebx - 84 # eax = factorial(n-1) - 85 # . . push args - 86 53/push-ebx - 87 # . . call - 88 e8/call factorial/disp32 - 89 # . . discard args - 90 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp - 91 # return n * factorial(n-1) - 92 f7 4/subop/multiply 1/mod/*+disp8 5/rm32/ebp . . 8/disp8 . # multiply *(ebp+8) into eax - 93 # TODO: check for overflow - 94 $factorial:end: - 95 # . epilog - 96 5b/pop-to-ebx - 97 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp - 98 5d/pop-to-ebp - 99 c3/return -100 -101 test-factorial: -102 # factorial(5) -103 # . . push args -104 68/push 5/imm32 -105 # . . call -106 e8/call factorial/disp32 -107 # . . discard args -108 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp -109 # check-ints-equal(eax, 120, msg) -110 # . . push args -111 68/push "F - test-factorial"/imm32 -112 68/push 0x78/imm32/expected-120 -113 50/push-eax -114 # . . call -115 e8/call check-ints-equal/disp32 -116 # . . discard args -117 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp -118 # end -119 c3/return -120 -121 # . . vim:nowrap:textwidth=0 + 78 81 7/subop/compare 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 1/imm32 # compare *(ebp+8) + 79 7e/jump-if-<= $factorial:end/disp8 + 80 # ebx = n-1 + 81 8b/copy 1/mod/*+disp8 5/rm32/ebp . . 3/r32/ebx 8/disp8 . # copy *(ebp+8) to ebx + 82 81 5/subop/subtract 3/mod/direct 3/rm32/ebx . . . . . 1/imm32 # subtract from ebx + 83 # eax = factorial(n-1) + 84 # . . push args + 85 53/push-ebx + 86 # . . call + 87 e8/call factorial/disp32 + 88 # . . discard args + 89 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + 90 # return n * factorial(n-1) + 91 f7 4/subop/multiply 1/mod/*+disp8 5/rm32/ebp . . 8/disp8 . # multiply *(ebp+8) into eax + 92 # TODO: check for overflow + 93 $factorial:end: + 94 # . epilog + 95 5b/pop-to-ebx + 96 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 97 5d/pop-to-ebp + 98 c3/return + 99 +100 test-factorial: +101 # factorial(5) +102 # . . push args +103 68/push 5/imm32 +104 # . . call +105 e8/call factorial/disp32 +106 # . . discard args +107 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp +108 # check-ints-equal(eax, 120, msg) +109 # . . push args +110 68/push "F - test-factorial"/imm32 +111 68/push 0x78/imm32/expected-120 +112 50/push-eax +113 # . . call +114 e8/call check-ints-equal/disp32 +115 # . . discard args +116 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp +117 # end +118 c3/return +119 +120 # . . vim:nowrap:textwidth=0 -- cgit 1.4.1-2-gfad0