about summary refs log tree commit diff stats
path: root/subx/apps
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2018-09-01 10:04:58 -0700
committerKartik Agaram <vc@akkartik.com>2018-09-01 10:54:20 -0700
commit6ff9ce26e84f686a96ada723f63ce95879216cca (patch)
treebb8562b4f5f65a3f3478a9237e0253038d59c241 /subx/apps
parentea7f869856a614d14de31c680c8dd290de1cc113 (diff)
downloadmu-6ff9ce26e84f686a96ada723f63ce95879216cca.tar.gz
4530 - create an apps/ directory
Diffstat (limited to 'subx/apps')
-rwxr-xr-xsubx/apps/factorialbin0 -> 156 bytes
-rw-r--r--subx/apps/factorial.subx64
2 files changed, 64 insertions, 0 deletions
diff --git a/subx/apps/factorial b/subx/apps/factorial
new file mode 100755
index 00000000..daea2cf4
--- /dev/null
+++ b/subx/apps/factorial
Binary files differdiff --git a/subx/apps/factorial.subx b/subx/apps/factorial.subx
new file mode 100644
index 00000000..de9953bf
--- /dev/null
+++ b/subx/apps/factorial.subx
@@ -0,0 +1,64 @@
+## compute the factorial of 5, and return the result in the exit code
+#
+# To run:
+#   $ subx translate apps/factorial.subx apps/factorial
+#   $ subx run apps/factorial
+# Expected result:
+#   $ echo $?
+#   120
+
+== 0x08048054  # code segment, after leaving room for ELF header
+# instruction                     effective address                                                   operand     displacement    immediate
+# op          subop               mod             rm32          base        index         scale       r32
+# 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
+
+# main:
+  # prepare to make a call
+  55/push                         .               .             .           .             .           .           .               .                 # push EBP
+  89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
+  # factorial(5)
+  68/push                         .               .             .           .             .           .           .               5/imm32           # push 5
+  e8/call                         .               .             .           .             .           .           factorial/disp32
+  # discard arg
+  5a/pop                          .               .             .           .             .           .           .               .                 # pop into EDX
+  # clean up after call
+  89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
+  5d/pop                          .               .             .           .             .           .           .               .                 # pop to EBP
+
+  # 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
+  cd/syscall                      .               .             .           .             .           .           .               0x80/imm8         # int 80h
+
+# factorial(n)
+factorial:
+  # initialize n
+  8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/ESP  4/index/none              2/r32/EDX   4/disp8         .                 # copy *(ESP+4) to EDX
+  # initialize EAX to 1 (base case)
+  b8/copy                         .               .             .           .             .           .           .               1/imm32           # copy 1 to EAX
+  # if (n <= 1) jump exit
+  81          7/subop/compare     3/mod/direct    2/rm32/EDX    .           .             .           .           .               1/imm32           # compare EDX with 1
+  7e/jump-if-<=                   .               .             .           .             .           .           $factorial:exit/disp8             # jump if <= to $factorial:exit
+  # EBX: n-1
+  89/copy                         3/mod/direct    3/rm32/EBX    .           .             .           2/r32/EDX   .               .                 # copy EDX to EBX
+  81          5/subop/subtract    3/mod/direct    3/rm32/EBX    .           .             .           .           .               1/imm32           # subtract 1 from EBX
+  # prepare call
+  55/push                         .               .             .           .             .           .           .               .                 # push EBP
+  89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
+  # EAX: factorial(n-1)
+  53/push                         .               .             .           .             .           .           .               .                 # push EBX
+  e8/call                         .               .             .           .             .           .           factorial/disp32
+  # discard arg
+  5e/pop                          .               .             .           .             .           .           .               .                 # pop into ESI
+  # clean up after call
+  89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
+  5d/pop                          .               .             .           .             .           .           .               .                 # pop to EBP
+  # refresh n
+  8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/ESP  4/index/none              2/r32/EDX   4/disp8         .                 # copy *(ESP+4) to EDX
+  # return n * factorial(n-1)
+  0f af/multiply                  3/mod/direct    2/rm32/EDX    .           .             .           0/r32/EAX   .               .                 # multiply EDX (n) into EAX (factorial(n-1))
+  # TODO: check for overflow
+$factorial:exit:
+  c3/return
+
+# vim:ft=subx:nowrap:so=0