about summary refs log tree commit diff stats
path: root/075print-int-decimal.subx
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-06-14 00:24:47 -0700
committerKartik Agaram <vc@akkartik.com>2020-06-14 00:28:23 -0700
commitad61776f498e8117756e4794eac840fab60cfac6 (patch)
tree73510284e749eab8c207aecef90084d0a5c57101 /075print-int-decimal.subx
parentead3d08e774529f3026f8cd6f93b8a0d7c87ed08 (diff)
downloadmu-ad61776f498e8117756e4794eac840fab60cfac6.tar.gz
6520 - new app: parse-int
Several bugs fixed in the process, and expectation of further bugs is growing.
I'd somehow started assuming I don't need to have separate cases for rm32
as a register vs mem. That's not right. We might need more reg-reg Primitives.
Diffstat (limited to '075print-int-decimal.subx')
-rw-r--r--075print-int-decimal.subx61
1 files changed, 61 insertions, 0 deletions
diff --git a/075print-int-decimal.subx b/075print-int-decimal.subx
index 62d9b6e0..38edd445 100644
--- a/075print-int-decimal.subx
+++ b/075print-int-decimal.subx
@@ -402,4 +402,65 @@ test-is-decimal-digit-above-9:
     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
     c3/return
 
+to-decimal-digit:  # in: byte -> out/eax: int
+    # . prologue
+    55/push-ebp
+    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
+    # eax = in
+    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   8/disp8         .                 # copy *(ebp+8) to eax
+$to-decimal-digit:check0:
+    # if (eax < '0') goto abort
+    3d/compare-eax-with  0x30/imm32/0
+    7c/jump-if-<  $to-decimal-digit:abort/disp8
+$to-decimal-digit:check1:
+    # if (eax > '9') goto abort
+    3d/compare-eax-with  0x39/imm32/f
+    7f/jump-if->  $to-decimal-digit:abort/disp8
+$to-decimal-digit:digit:
+    # return eax - '0'
+    2d/subtract-from-eax  0x30/imm32/0
+$to-decimal-digit:end:
+    # . epilogue
+    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
+    5d/pop-to-ebp
+    c3/return
+
+$to-decimal-digit:abort:
+    # . write-buffered(stderr, error)
+    # . . push args
+    68/push  "to-decimal-digit: not a digit character: "/imm32
+    68/push  Stderr/imm32
+    # . . call
+    e8/call  write-buffered/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
+    # . write-byte-buffered(stderr, %eax)
+    # . . push args
+    50/push-eax
+    68/push  Stderr/imm32
+    # . . call
+#?     e8/call  write-byte-buffered/disp32
+    e8/call  print-int32-buffered/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
+    # . write-buffered(stderr, "\n")
+    # . . push args
+    68/push  Newline/imm32
+    68/push  Stderr/imm32
+    # . . call
+    e8/call  write-buffered/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
+    # . flush(Stderr)
+    # . . push args
+    68/push  Stderr/imm32
+    # . . call
+    e8/call  flush/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
+    # . syscall(exit, 1)
+    bb/copy-to-ebx  1/imm32
+    e8/call  syscall_exit/disp32
+    # never gets here
+
 # . . vim:nowrap:textwidth=0