1 ## compute the factorial of 5, and return the result in the exit code
 2 #
 3 # To run:
 4 #   $ subx translate apps/factorial.subx apps/factorial
 5 #   $ subx run apps/factorial
 6 # Expected result:
 7 #   $ echo $?
 8 #   120
 9 
10 == code
11 # instruction                     effective address                                                   operand     displacement    immediate
12 # op          subop               mod             rm32          base        index         scale       r32
13 # 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
14 
15 # main:
16   # prepare to make a call
17   55/push                         .               .             .           .             .           .           .               .                 # push EBP
18   89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
19   # factorial(5)
20   68/push                         .               .             .           .             .           .           .               5/imm32           # push 5
21   e8/call                         .               .             .           .             .           .           factorial/disp32
22   # discard arg
23   5a/pop                          .               .             .           .             .           .           .               .                 # pop into EDX
24   # clean up after call
25   89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
26   5d/pop                          .               .             .           .             .           .           .               .                 # pop to EBP
27 
28   # exit(EAX)
29   89/copy                         3/mod/direct    3/rm32/EBX    .           .             .           0/r32/EAX   .               .                 # copy EAX to EBX
30   b8/copy                         .               .             .           .             .           .           .               1/imm32           # copy 1 to EAX
31   cd/syscall                      .               .             .           .             .           .           .               0x80/imm8         # int 80h
32 
33 # factorial(n)
34 factorial:
35   # initialize EAX to 1 (base case)
36   b8/copy                         .               .             .           .             .           .           .               1/imm32           # copy 1 to EAX
37   # if (n <= 1) jump exit
38   81          7/subop/compare     1/mod/*+disp8   4/rm32/sib    4/base/ESP  4/index/none  .           .           4/disp8         1/imm32           # compare *(ESP+4) with 1
39   7e/jump-if-<=                   .               .             .           .             .           .           $factorial:exit/disp8             # jump if <= to $factorial:exit
40   # EBX: n-1
41   8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/ESP  4/index/none              3/r32/EBX   4/disp8         .                 # copy *(ESP+4) to EBX
42   81          5/subop/subtract    3/mod/direct    3/rm32/EBX    .           .             .           .           .               1/imm32           # subtract 1 from EBX
43   # prepare call
44   55/push                         .               .             .           .             .           .           .               .                 # push EBP
45   89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
46   # EAX: factorial(n-1)
47   53/push                         .               .             .           .             .           .           .               .                 # push EBX
48   e8/call                         .               .             .           .             .           .           factorial/disp32
49   # discard arg
50   5e/pop                          .               .             .           .             .           .           .               .                 # pop into ESI
51   # clean up after call
52   89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
53   5d/pop                          .               .             .           .             .           .           .               .                 # pop to EBP
54   # refresh n
55   8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/ESP  4/index/none              2/r32/EDX   4/disp8         .                 # copy *(ESP+4) to EDX
56   # return n * factorial(n-1)
57   f7          4/subop/multiply    1/mod/*+disp8   4/rm32/sib    4/base/ESP  4/index/none                          4/disp8         .                 # multiply *(ESP+4) (n) into EAX (factorial(n-1))
58   # TODO: check for overflow
59 $factorial:exit:
60   c3/return
61 
62 # vim:ft=subx:nowrap:so=0