diff options
Diffstat (limited to 'subx/apps/factorial.subx')
-rw-r--r-- | subx/apps/factorial.subx | 22 |
1 files changed, 11 insertions, 11 deletions
diff --git a/subx/apps/factorial.subx b/subx/apps/factorial.subx index 9b014313..d223b692 100644 --- a/subx/apps/factorial.subx +++ b/subx/apps/factorial.subx @@ -23,7 +23,7 @@ # prolog 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP # if (argc > 1) - 81 7/subop/compare 1/mod/*+disp8 4/rm32/SIB 5/base/EBP 4/index/none . . 0/disp8 1/imm32 # compare *EBP with 1 + 81 7/subop/compare 1/mod/*+disp8 4/rm32/SIB 5/base/EBP 4/index/none . . 0/disp8 1/imm32 # compare *EBP 7e/jump-if-lesser-or-equal $run_main/disp8 # and if (argv[1] == "test") # push args @@ -32,9 +32,9 @@ # call e8/call kernel_string_equal/disp32 # discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add 8 to ESP + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP # check result - 3d/compare . . . . . . . 1/imm32 # compare EAX with 1 + 3d/compare-EAX 1/imm32 75/jump-if-not-equal $run_main/disp8 # then e8/call run_tests/disp32 @@ -46,11 +46,11 @@ $run_main: # EAX <- call e8/call factorial/disp32 # discard arg - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP $main_exit: # exit(EAX) 89/copy 3/mod/direct 3/rm32/EBX . . . 0/r32/EAX . . # copy EAX to EBX - b8/copy . . . . . . . 1/imm32 # copy 1 to EAX + b8/copy . . . . . . . 1/imm32 # copy to EAX cd/syscall 0x80/imm8 # factorial(n) @@ -60,18 +60,18 @@ factorial: 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP 53/push-EBX # initialize EAX to 1 (base case) - b8/copy . . . . . . . 1/imm32 # copy 1 to EAX + b8/copy . . . . . . . 1/imm32 # copy to EAX # if (n <= 1) jump exit - 81 7/subop/compare 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none . . 8/disp8 1/imm32 # compare *(EBP+8) with 1 + 81 7/subop/compare 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none . . 8/disp8 1/imm32 # compare *(EBP+8) 7e/jump-if-<= $factorial:exit/disp8 # EBX: n-1 8b/copy 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none 3/r32/EBX 8/disp8 . # copy *(EBP+8) to EBX - 81 5/subop/subtract 3/mod/direct 3/rm32/EBX . . . . . 1/imm32 # subtract 1 from EBX + 81 5/subop/subtract 3/mod/direct 3/rm32/EBX . . . . . 1/imm32 # subtract from EBX # EAX: factorial(n-1) 53/push-EBX e8/call . . . . . . factorial/disp32 # discard arg - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP + 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 4/rm32/sib 5/base/EBP 4/index/none 8/disp8 . # multiply *(EBP+8) into EAX # TODO: check for overflow @@ -89,7 +89,7 @@ test_factorial: # call e8/call factorial/disp32 # discard arg - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add 4 to ESP + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP # check_ints_equal(EAX, 120, failure message) # push args 68/push "F - test_factorial"/imm32 @@ -98,7 +98,7 @@ test_factorial: # call e8/call check_ints_equal/disp32 # discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add 12 to ESP + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP # end c3/return |