1 ## compute the factorial of 5, and return the result in the exit code
 2 #
 3 # To run:
 4 #   $ subx translate ex7.subx ex7
 5 #   $ subx run ex7
 6 # Expected result:
 7 #   $ echo $?
 8 #   120
 9 
10 == 0x08048054  # code segment, after leaving room for ELF header
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 n
36   8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/ESP  4/index/none              2/r32/EDX   4/disp8         .                 # copy *(ESP+4) to EDX
37   # initialize EAX to 1 (base case)
38   b8/copy                         .               .             .           .             .           .           .               1/imm32           # copy 1 to EAX
39   # if (n <= 1) jump exit
40   81          7/subop/compare     3/mod/direct    2/rm32/EDX    .           .             .           .           .               1/imm32           # compare EDX with 1
41   7e/jump-if-<=                   .               .             .           .             .           .           $factorial:exit/disp8             # jump if <= to $factorial:exit
42   # EBX: n-1
43   89/copy                         3/mod/direct    3/rm32/EBX    .           .             .           2/r32/EDX   .               .                 # copy EDX to EBX
44   81          5/subop/subtract    3/mod/direct    3/rm32/EBX    .           .             .           .           .               1/imm32           # subtract 1 from EBX
45   # prepare call
46   55/push                         .               .             .           .             .           .           .               .                 # push EBP
47   89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
48   # EAX: factorial(n-1)
49   53/push                         .               .             .           .             .           .           .               .                 # push EBX
50   e8/call                         .               .             .           .             .           .           factorial/disp32
51   # discard arg
52   5e/pop                          .               .             .           .             .           .           .               .                 # pop into ESI
53   # clean up after call
54   89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
55   5d/pop                          .               .             .           .             .           .           .               .                 # pop to EBP
56   # refresh n
57   8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/ESP  4/index/none              2/r32/EDX   4/disp8         .                 # copy *(ESP+4) to EDX
58   # return n * factorial(n-1)
59   0f af/multiply                  3/mod/direct    2/rm32/EDX    .           .             .           0/r32/EAX   .               .                 # multiply EDX (n) into EAX (factorial(n-1))
60   # TODO: check for overflow
61 $factorial:exit:
62   c3/return
63 
64 # vim:ft=subx:nowrap:so=0