From 2655a4a91d1d2e98cc9ab9c54e70d4eb49c735d6 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Tue, 30 Jun 2020 14:54:30 -0700 Subject: 6602 --- html/075write-int-decimal.subx.html | 153 +++++++++++++++++++++++++----------- 1 file changed, 108 insertions(+), 45 deletions(-) (limited to 'html/075write-int-decimal.subx.html') diff --git a/html/075write-int-decimal.subx.html b/html/075write-int-decimal.subx.html index 4227ba21..97059ffa 100644 --- a/html/075write-int-decimal.subx.html +++ b/html/075write-int-decimal.subx.html @@ -2,7 +2,7 @@ -Mu - 075print-int-decimal.subx +Mu - 075write-int-decimal.subx @@ -20,6 +20,8 @@ a { color:inherit; } .subxFunction { color: #af5f00; text-decoration: underline; } .LineNr { } .subxS1Comment { color: #0000af; } +.CommentedCode { color: #8a8a8a; } +.SpecialChar { color: #d70000; } .Normal { color: #000000; background-color: #c6c6c6; padding-bottom: 1px; } .Folded { color: #080808; background-color: #949494; } .Constant { color: #008787; } @@ -57,7 +59,7 @@ if ('onhashchange' in window) { -https://github.com/akkartik/mu/blob/master/075print-int-decimal.subx +https://github.com/akkartik/mu/blob/master/075write-int-decimal.subx
   1 # Helper to print an int32 in decimal.
   2 
@@ -66,7 +68,7 @@ if ('onhashchange' in window) {
   5 # . op          subop               mod             rm32          base        index         scale       r32
   6 # . 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
   7 
-  8 print-int32-decimal:  # out: (addr stream byte), n: int32
+  8 write-int32-decimal:  # out: (addr stream byte), n: int32
   9     # works by generating characters from lowest to highest and pushing them
  10     # to the stack, before popping them one by one into the stream
  11     #
@@ -112,10 +114,10 @@ if ('onhashchange' in window) {
  51     # var eax: int = abs(n)
  52     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   0xc/disp8       .                 # copy *(ebp+12) to eax
  53     3d/compare-eax-with  0/imm32
- 54     7d/jump-if->=  $print-int32-decimal:read-loop/disp8
- 55 $print-int32-decimal:negative:
+ 54     7d/jump-if->=  $write-int32-decimal:read-loop/disp8
+ 55 $write-int32-decimal:negative:
  56     f7          3/subop/negate      3/mod/direct    0/rm32/eax    .           .             .           .           .               .                 # negate eax
- 57 $print-int32-decimal:read-loop:
+ 57 $write-int32-decimal:read-loop:
  58     # eax, edx = eax / 10, eax % 10
  59     99/sign-extend-eax-into-edx
  60     f7          7/subop/idiv        3/mod/direct    1/rm32/ecx    .           .             .           .           .               .                 # divide edx:eax by ecx, storing quotient in eax and remainder in edx
@@ -125,14 +127,14 @@ if ('onhashchange' in window) {
  64     52/push-edx
  65     # if (eax == 0) break
  66     3d/compare-eax-and  0/imm32
- 67     7f/jump-if->  $print-int32-decimal:read-loop/disp8
- 68 $print-int32-decimal:read-break:
+ 67     7f/jump-if->  $write-int32-decimal:read-loop/disp8
+ 68 $write-int32-decimal:read-break:
  69     # if (n < 0) push('-')
  70     81          7/subop/compare     1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       0/imm32           # compare *(ebp+12)
- 71     7d/jump-if->=  $print-int32-decimal:write/disp8
- 72 $print-int32-decimal:push-negative:
+ 71     7d/jump-if->=  $write-int32-decimal:write/disp8
+ 72 $write-int32-decimal:push-negative:
  73     68/push  0x2d/imm32/-
- 74 $print-int32-decimal:write:
+ 74 $write-int32-decimal:write:
  75     # edi = out
  76     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           7/r32/edi   8/disp8         .                 # copy *(ebp+8) to edi
  77     # var w/edx: int = out->write
@@ -142,27 +144,27 @@ if ('onhashchange' in window) {
  81     # var max/ebx: (addr byte) = &out->data[out->size]
  82     8b/copy                         1/mod/*+disp8   7/rm32/edi    .           .             .           3/r32/ebx   8/disp8         .                 # copy *(edi+8) to ebx
  83     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    7/base/edi  3/index/ebx   .           3/r32/ebx   0xc/disp8       .                 # copy edi+ebx+12 to ebx
- 84 $print-int32-decimal:write-loop:
+ 84 $write-int32-decimal:write-loop:
  85     # pop into eax
  86     58/pop-to-eax
  87     # if (eax == sentinel) break
  88     3d/compare-eax-and  0/imm32/sentinel
- 89     74/jump-if-=  $print-int32-decimal:write-break/disp8
+ 89     74/jump-if-=  $write-int32-decimal:write-break/disp8
  90     # if (curr >= max) abort
  91     39/compare                      3/mod/direct    1/rm32/ecx    .           .             .           3/r32/ebx   .               .                 # compare ecx with ebx
- 92     73/jump-if-addr>=  $print-int32-decimal:abort/disp8
- 93 $print-int32-decimal:write-char:
+ 92     73/jump-if-addr>=  $write-int32-decimal:abort/disp8
+ 93 $write-int32-decimal:write-char:
  94     # *curr = AL
  95     88/copy-byte                    0/mod/indirect  1/rm32/ecx    .           .             .           0/r32/AL    .               .                 # copy AL to byte at *ecx
  96     # ++curr
  97     41/increment-ecx
  98     # ++w
  99     42/increment-edx
-100     eb/jump  $print-int32-decimal:write-loop/disp8
-101 $print-int32-decimal:write-break:
+100     eb/jump  $write-int32-decimal:write-loop/disp8
+101 $write-int32-decimal:write-break:
 102     # out->write = w
 103     89/copy                         0/mod/indirect  7/rm32/edi    .           .             .           2/r32/edx   .               .                 # copy edx to *edi
-104 $print-int32-decimal:end:
+104 $write-int32-decimal:end:
 105     # . restore registers
 106     5f/pop-to-edi
 107     5b/pop-to-ebx
@@ -174,10 +176,10 @@ if ('onhashchange' in window) {
 113     5d/pop-to-ebp
 114     c3/return
 115 
-116 $print-int32-decimal:abort:
+116 $write-int32-decimal:abort:
 117     # . _write(2/stderr, error)
 118     # . . push args
-119     68/push  "print-int32-decimal: out of space\n"/imm32
+119     68/push  "write-int32-decimal: out of space\n"/imm32
 120     68/push  2/imm32/stderr
 121     # . . call
 122     e8/call  _write/disp32
@@ -188,7 +190,7 @@ if ('onhashchange' in window) {
 127     e8/call  syscall_exit/disp32
 128     # never gets here
 129 
-130 test-print-int32-decimal:
+130 test-write-int32-decimal:
 131     # - check that a single-digit number converts correctly
 132     # setup
 133     # . clear-stream(_test-stream)
@@ -198,17 +200,17 @@ if ('onhashchange' in window) {
 137     e8/call  clear-stream/disp32
 138     # . . discard args
 139     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
-140     # print-int32-decimal(_test-stream, 9)
+140     # write-int32-decimal(_test-stream, 9)
 141     # . . push args
 142     68/push  9/imm32
 143     68/push  _test-stream/imm32
 144     # . . call
-145     e8/call  print-int32-decimal/disp32
+145     e8/call  write-int32-decimal/disp32
 146     # . . discard args
 147     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 148     # check-stream-equal(_test-stream, "9", msg)
 149     # . . push args
-150     68/push  "F - test-print-int32-decimal"/imm32
+150     68/push  "F - test-write-int32-decimal"/imm32
 151     68/push  "9"/imm32
 152     68/push  _test-stream/imm32
 153     # . . call
@@ -218,7 +220,7 @@ if ('onhashchange' in window) {
 157     # . end
 158     c3/return
 159 
-160 test-print-int32-decimal-zero:
+160 test-write-int32-decimal-zero:
 161     # - check that 0 converts correctly
 162     # setup
 163     # . clear-stream(_test-stream)
@@ -228,17 +230,17 @@ if ('onhashchange' in window) {
 167     e8/call  clear-stream/disp32
 168     # . . discard args
 169     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
-170     # print-int32-decimal(_test-stream, 0)
+170     # write-int32-decimal(_test-stream, 0)
 171     # . . push args
 172     68/push  0/imm32
 173     68/push  _test-stream/imm32
 174     # . . call
-175     e8/call  print-int32-decimal/disp32
+175     e8/call  write-int32-decimal/disp32
 176     # . . discard args
 177     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 178     # check-stream-equal(_test-stream, "0", msg)
 179     # . . push args
-180     68/push  "F - test-print-int32-decimal-zero"/imm32
+180     68/push  "F - test-write-int32-decimal-zero"/imm32
 181     68/push  "0"/imm32
 182     68/push  _test-stream/imm32
 183     # . . call
@@ -248,7 +250,7 @@ if ('onhashchange' in window) {
 187     # . end
 188     c3/return
 189 
-190 test-print-int32-decimal-multiple-digits:
+190 test-write-int32-decimal-multiple-digits:
 191     # - check that a multi-digit number converts correctly
 192     # setup
 193     # . clear-stream(_test-stream)
@@ -258,17 +260,17 @@ if ('onhashchange' in window) {
 197     e8/call  clear-stream/disp32
 198     # . . discard args
 199     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
-200     # print-int32-decimal(_test-stream, 10)
+200     # write-int32-decimal(_test-stream, 10)
 201     # . . push args
 202     68/push  0xa/imm32
 203     68/push  _test-stream/imm32
 204     # . . call
-205     e8/call  print-int32-decimal/disp32
+205     e8/call  write-int32-decimal/disp32
 206     # . . discard args
 207     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 208     # check-stream-equal(_test-stream, "10", msg)
 209     # . . push args
-210     68/push  "F - test-print-int32-decimal-multiple-digits"/imm32
+210     68/push  "F - test-write-int32-decimal-multiple-digits"/imm32
 211     68/push  "10"/imm32
 212     68/push  _test-stream/imm32
 213     # . . call
@@ -278,7 +280,7 @@ if ('onhashchange' in window) {
 217     # . end
 218     c3/return
 219 
-220 test-print-int32-decimal-negative:
+220 test-write-int32-decimal-negative:
 221     # - check that a negative single-digit number converts correctly
 222     # setup
 223     # . clear-stream(_test-stream)
@@ -288,18 +290,18 @@ if ('onhashchange' in window) {
 227     e8/call  clear-stream/disp32
 228     # . . discard args
 229     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
-230     # print-int32-decimal(_test-stream, -9)
+230     # write-int32-decimal(_test-stream, -9)
 231     # . . push args
 232     68/push  -9/imm32
 233     68/push  _test-stream/imm32
 234     # . . call
-235     e8/call  print-int32-decimal/disp32
+235     e8/call  write-int32-decimal/disp32
 236     # . . discard args
 237     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 238 +-- 26 lines: #?     # dump _test-stream ------------------------------------------------------------------------------------------------------------------------------------------------
 264     # check-stream-equal(_test-stream, "-9", msg)
 265     # . . push args
-266     68/push  "F - test-print-int32-decimal-negative"/imm32
+266     68/push  "F - test-write-int32-decimal-negative"/imm32
 267     68/push  "-9"/imm32
 268     68/push  _test-stream/imm32
 269     # . . call
@@ -309,7 +311,7 @@ if ('onhashchange' in window) {
 273     # . end
 274     c3/return
 275 
-276 test-print-int32-decimal-negative-multiple-digits:
+276 test-write-int32-decimal-negative-multiple-digits:
 277     # - check that a multi-digit number converts correctly
 278     # setup
 279     # . clear-stream(_test-stream)
@@ -319,17 +321,17 @@ if ('onhashchange' in window) {
 283     e8/call  clear-stream/disp32
 284     # . . discard args
 285     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
-286     # print-int32-decimal(_test-stream, -10)
+286     # write-int32-decimal(_test-stream, -10)
 287     # . . push args
 288     68/push  -0xa/imm32
 289     68/push  _test-stream/imm32
 290     # . . call
-291     e8/call  print-int32-decimal/disp32
+291     e8/call  write-int32-decimal/disp32
 292     # . . discard args
 293     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 294     # check-stream-equal(_test-stream, "-10", msg)
 295     # . . push args
-296     68/push  "F - test-print-int32-decimal-negative-multiple-digits"/imm32
+296     68/push  "F - test-write-int32-decimal-negative-multiple-digits"/imm32
 297     68/push  "-10"/imm32
 298     68/push  _test-stream/imm32
 299     # . . call
@@ -370,7 +372,7 @@ if ('onhashchange' in window) {
 334     # . . push args
 335     68/push  0x2f/imm32
 336     # . . call
-337     e8/call  is-decimal-digit?/disp32
+337     e8/call  is-decimal-digit?/disp32
 338     # . . discard args
 339     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 340     # check-ints-equal(eax, 0, msg)
@@ -389,7 +391,7 @@ if ('onhashchange' in window) {
 353     # . . push args
 354     68/push  0x30/imm32
 355     # . . call
-356     e8/call  is-decimal-digit?/disp32
+356     e8/call  is-decimal-digit?/disp32
 357     # . . discard args
 358     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 359     # check-ints-equal(eax, 1, msg)
@@ -405,7 +407,7 @@ if ('onhashchange' in window) {
 369     # . . push args
 370     68/push  0x39/imm32
 371     # . . call
-372     e8/call  is-decimal-digit?/disp32
+372     e8/call  is-decimal-digit?/disp32
 373     # . . discard args
 374     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 375     # check-ints-equal(eax, 1, msg)
@@ -424,7 +426,7 @@ if ('onhashchange' in window) {
 388     # . . push args
 389     68/push  0x3a/imm32
 390     # . . call
-391     e8/call  is-decimal-digit?/disp32
+391     e8/call  is-decimal-digit?/disp32
 392     # . . discard args
 393     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 394     # check-ints-equal(eax, 0, msg)
@@ -438,7 +440,68 @@ if ('onhashchange' in window) {
 402     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
 403     c3/return
 404 
-405 # . . vim:nowrap:textwidth=0
+405 to-decimal-digit:  # in: byte -> out/eax: int
+406     # . prologue
+407     55/push-ebp
+408     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
+409     # eax = in
+410     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   8/disp8         .                 # copy *(ebp+8) to eax
+411 $to-decimal-digit:check0:
+412     # if (eax < '0') goto abort
+413     3d/compare-eax-with  0x30/imm32/0
+414     7c/jump-if-<  $to-decimal-digit:abort/disp8
+415 $to-decimal-digit:check1:
+416     # if (eax > '9') goto abort
+417     3d/compare-eax-with  0x39/imm32/f
+418     7f/jump-if->  $to-decimal-digit:abort/disp8
+419 $to-decimal-digit:digit:
+420     # return eax - '0'
+421     2d/subtract-from-eax  0x30/imm32/0
+422 $to-decimal-digit:end:
+423     # . epilogue
+424     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
+425     5d/pop-to-ebp
+426     c3/return
+427 
+428 $to-decimal-digit:abort:
+429     # . write-buffered(stderr, error)
+430     # . . push args
+431     68/push  "to-decimal-digit: not a digit character: "/imm32
+432     68/push  Stderr/imm32
+433     # . . call
+434     e8/call  write-buffered/disp32
+435     # . . discard args
+436     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
+437     # . write-byte-buffered(stderr, %eax)
+438     # . . push args
+439     50/push-eax
+440     68/push  Stderr/imm32
+441     # . . call
+442 #?     e8/call  write-byte-buffered/disp32
+443     e8/call  write-int32-hex-buffered/disp32
+444     # . . discard args
+445     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
+446     # . write-buffered(stderr, "\n")
+447     # . . push args
+448     68/push  Newline/imm32
+449     68/push  Stderr/imm32
+450     # . . call
+451     e8/call  write-buffered/disp32
+452     # . . discard args
+453     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
+454     # . flush(Stderr)
+455     # . . push args
+456     68/push  Stderr/imm32
+457     # . . call
+458     e8/call  flush/disp32
+459     # . . discard args
+460     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
+461     # . syscall(exit, 1)
+462     bb/copy-to-ebx  1/imm32
+463     e8/call  syscall_exit/disp32
+464     # never gets here
+465 
+466 # . . vim:nowrap:textwidth=0
 
-- cgit 1.4.1-2-gfad0