From 9428990bd6e61988df23811205504a5c63a348e1 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Sat, 14 Mar 2020 01:06:27 -0700 Subject: 6143 --- apps/factorial.mu | 2 + apps/factorial4.subx | 12 +++--- html/apps/factorial.mu.html | 90 +++++++++++++++++++++--------------------- html/apps/factorial4.subx.html | 12 +++--- 4 files changed, 60 insertions(+), 56 deletions(-) diff --git a/apps/factorial.mu b/apps/factorial.mu index 128ac9ad..722c9e6c 100644 --- a/apps/factorial.mu +++ b/apps/factorial.mu @@ -2,6 +2,8 @@ # ./translate_mu apps/factorial.mu # ./a.elf test # to run tests # ./a.elf # to run factorial(5) +# +# Compare apps/factorial4.subx fn factorial n: int -> result/eax: int { compare n 1 diff --git a/apps/factorial4.subx b/apps/factorial4.subx index 9ab7038f..3aae0dd3 100644 --- a/apps/factorial4.subx +++ b/apps/factorial4.subx @@ -60,7 +60,7 @@ factorial: # n: int -> int/eax 55/push-ebp 89/<- %ebp 4/r32/esp # save registers - 53/push-ebx + 51/push-ecx # if (n <= 1) return 1 81 7/subop/compare *(ebp+8) 1/imm32 { @@ -70,14 +70,14 @@ factorial: # n: int -> int/eax # if (n > 1) return n * factorial(n-1) { 7e/jump-if-<= break/disp8 - # var ebx: int = n-1 - 8b/-> *(ebp+8) 3/r32/ebx - 4b/decrement-ebx - (factorial %ebx) # => eax + # var tmp/ecx: int = n-1 + 8b/-> *(ebp+8) 1/r32/ecx + 49/decrement-ecx + (factorial %ecx) # => eax f7 4/subop/multiply-into-eax *(ebp+8) } # restore registers - 5b/pop-to-ebx + 59/pop-to-ecx # . epilogue 89/<- %esp 5/r32/ebp 5d/pop-to-ebp diff --git a/html/apps/factorial.mu.html b/html/apps/factorial.mu.html index fa2ce7a3..2b8d1f0c 100644 --- a/html/apps/factorial.mu.html +++ b/html/apps/factorial.mu.html @@ -60,50 +60,52 @@ if ('onhashchange' in window) { 2 # ./translate_mu apps/factorial.mu 3 # ./a.elf test # to run tests 4 # ./a.elf # to run factorial(5) - 5 - 6 fn factorial n: int -> result/eax: int { - 7 compare n 1 - 8 { - 9 break-if-> -10 result <- copy 1 -11 } -12 { -13 break-if-<= -14 var tmp/ecx: int <- copy n -15 tmp <- decrement -16 result <- factorial tmp -17 result <- multiply n -18 } -19 } -20 -21 fn test-factorial { -22 var result/eax: int <- factorial 5 -23 check-ints-equal result 0x78 "F - test-factorial" -24 } -25 -26 fn main args: (addr array kernel-string) -> exit-status/ebx: int { -27 var a/eax: (addr array kernel-string) <- copy args -28 var tmp/ecx: int <- length a -29 $main-body: { -30 compare tmp, 1 -31 # if (len(args) == 1) factorial(5) -32 { -33 break-if-!= -34 var tmp/eax: int <- factorial 5 -35 exit-status <- copy tmp -36 break $main-body -37 } -38 # if (args[1] == "test") run-tests() -39 var tmp2/ecx: (addr kernel-string) <- index a, 1 -40 var tmp3/eax: boolean <- kernel-string-equal? *tmp2, "test" -41 compare tmp3, 0 -42 { -43 break-if-= -44 run-tests -45 exit-status <- copy 0 # TODO: get at Num-test-failures somehow -46 } -47 } -48 } + 5 # + 6 # Compare apps/factorial4.subx + 7 + 8 fn factorial n: int -> result/eax: int { + 9 compare n 1 +10 { +11 break-if-> +12 result <- copy 1 +13 } +14 { +15 break-if-<= +16 var tmp/ecx: int <- copy n +17 tmp <- decrement +18 result <- factorial tmp +19 result <- multiply n +20 } +21 } +22 +23 fn test-factorial { +24 var result/eax: int <- factorial 5 +25 check-ints-equal result 0x78 "F - test-factorial" +26 } +27 +28 fn main args: (addr array kernel-string) -> exit-status/ebx: int { +29 var a/eax: (addr array kernel-string) <- copy args +30 var tmp/ecx: int <- length a +31 $main-body: { +32 compare tmp, 1 +33 # if (len(args) == 1) factorial(5) +34 { +35 break-if-!= +36 var tmp/eax: int <- factorial 5 +37 exit-status <- copy tmp +38 break $main-body +39 } +40 # if (args[1] == "test") run-tests() +41 var tmp2/ecx: (addr kernel-string) <- index a, 1 +42 var tmp3/eax: boolean <- kernel-string-equal? *tmp2, "test" +43 compare tmp3, 0 +44 { +45 break-if-= +46 run-tests +47 exit-status <- copy 0 # TODO: get at Num-test-failures somehow +48 } +49 } +50 } diff --git a/html/apps/factorial4.subx.html b/html/apps/factorial4.subx.html index bd41739e..fcbde291 100644 --- a/html/apps/factorial4.subx.html +++ b/html/apps/factorial4.subx.html @@ -119,7 +119,7 @@ if ('onhashchange' in window) { 60 55/push-ebp 61 89/<- %ebp 4/r32/esp 62 # save registers -63 53/push-ebx +63 51/push-ecx 64 # if (n <= 1) return 1 65 81 7/subop/compare *(ebp+8) 1/imm32 66 { @@ -129,14 +129,14 @@ if ('onhashchange' in window) { 70 # if (n > 1) return n * factorial(n-1) 71 { 72 7e/jump-if-<= break/disp8 -73 # var ebx: int = n-1 -74 8b/-> *(ebp+8) 3/r32/ebx -75 4b/decrement-ebx -76 (factorial %ebx) # => eax +73 # var tmp/ecx: int = n-1 +74 8b/-> *(ebp+8) 1/r32/ecx +75 49/decrement-ecx +76 (factorial %ecx) # => eax 77 f7 4/subop/multiply-into-eax *(ebp+8) 78 } 79 # restore registers -80 5b/pop-to-ebx +80 59/pop-to-ecx 81 # . epilogue 82 89/<- %esp 5/r32/ebp 83 5d/pop-to-ebp -- cgit 1.4.1-2-gfad0