This commit is contained in:
Kartik Agaram 2020-06-29 18:31:17 -07:00
parent 690fa191f1
commit 1afc882890
18 changed files with 48 additions and 48 deletions

View File

@ -5,7 +5,7 @@
# . 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
print-int32-decimal: # out: (addr stream byte), n: int32
write-int32-decimal: # out: (addr stream byte), n: int32
# works by generating characters from lowest to highest and pushing them
# to the stack, before popping them one by one into the stream
#
@ -51,10 +51,10 @@ print-int32-decimal: # out: (addr stream byte), n: int32
# var eax: int = abs(n)
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0xc/disp8 . # copy *(ebp+12) to eax
3d/compare-eax-with 0/imm32
7d/jump-if->= $print-int32-decimal:read-loop/disp8
$print-int32-decimal:negative:
7d/jump-if->= $write-int32-decimal:read-loop/disp8
$write-int32-decimal:negative:
f7 3/subop/negate 3/mod/direct 0/rm32/eax . . . . . . # negate eax
$print-int32-decimal:read-loop:
$write-int32-decimal:read-loop:
# eax, edx = eax / 10, eax % 10
99/sign-extend-eax-into-edx
f7 7/subop/idiv 3/mod/direct 1/rm32/ecx . . . . . . # divide edx:eax by ecx, storing quotient in eax and remainder in edx
@ -64,14 +64,14 @@ $print-int32-decimal:read-loop:
52/push-edx
# if (eax == 0) break
3d/compare-eax-and 0/imm32
7f/jump-if-> $print-int32-decimal:read-loop/disp8
$print-int32-decimal:read-break:
7f/jump-if-> $write-int32-decimal:read-loop/disp8
$write-int32-decimal:read-break:
# if (n < 0) push('-')
81 7/subop/compare 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 0/imm32 # compare *(ebp+12)
7d/jump-if->= $print-int32-decimal:write/disp8
$print-int32-decimal:push-negative:
7d/jump-if->= $write-int32-decimal:write/disp8
$write-int32-decimal:push-negative:
68/push 0x2d/imm32/-
$print-int32-decimal:write:
$write-int32-decimal:write:
# edi = out
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 7/r32/edi 8/disp8 . # copy *(ebp+8) to edi
# var w/edx: int = out->write
@ -81,27 +81,27 @@ $print-int32-decimal:write:
# var max/ebx: (addr byte) = &out->data[out->size]
8b/copy 1/mod/*+disp8 7/rm32/edi . . . 3/r32/ebx 8/disp8 . # copy *(edi+8) to ebx
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
$print-int32-decimal:write-loop:
$write-int32-decimal:write-loop:
# pop into eax
58/pop-to-eax
# if (eax == sentinel) break
3d/compare-eax-and 0/imm32/sentinel
74/jump-if-= $print-int32-decimal:write-break/disp8
74/jump-if-= $write-int32-decimal:write-break/disp8
# if (curr >= max) abort
39/compare 3/mod/direct 1/rm32/ecx . . . 3/r32/ebx . . # compare ecx with ebx
73/jump-if-addr>= $print-int32-decimal:abort/disp8
$print-int32-decimal:write-char:
73/jump-if-addr>= $write-int32-decimal:abort/disp8
$write-int32-decimal:write-char:
# *curr = AL
88/copy-byte 0/mod/indirect 1/rm32/ecx . . . 0/r32/AL . . # copy AL to byte at *ecx
# ++curr
41/increment-ecx
# ++w
42/increment-edx
eb/jump $print-int32-decimal:write-loop/disp8
$print-int32-decimal:write-break:
eb/jump $write-int32-decimal:write-loop/disp8
$write-int32-decimal:write-break:
# out->write = w
89/copy 0/mod/indirect 7/rm32/edi . . . 2/r32/edx . . # copy edx to *edi
$print-int32-decimal:end:
$write-int32-decimal:end:
# . restore registers
5f/pop-to-edi
5b/pop-to-ebx
@ -113,10 +113,10 @@ $print-int32-decimal:end:
5d/pop-to-ebp
c3/return
$print-int32-decimal:abort:
$write-int32-decimal:abort:
# . _write(2/stderr, error)
# . . push args
68/push "print-int32-decimal: out of space\n"/imm32
68/push "write-int32-decimal: out of space\n"/imm32
68/push 2/imm32/stderr
# . . call
e8/call _write/disp32
@ -127,7 +127,7 @@ $print-int32-decimal:abort:
e8/call syscall_exit/disp32
# never gets here
test-print-int32-decimal:
test-write-int32-decimal:
# - check that a single-digit number converts correctly
# setup
# . clear-stream(_test-stream)
@ -137,17 +137,17 @@ test-print-int32-decimal:
e8/call clear-stream/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# print-int32-decimal(_test-stream, 9)
# write-int32-decimal(_test-stream, 9)
# . . push args
68/push 9/imm32
68/push _test-stream/imm32
# . . call
e8/call print-int32-decimal/disp32
e8/call write-int32-decimal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# check-stream-equal(_test-stream, "9", msg)
# . . push args
68/push "F - test-print-int32-decimal"/imm32
68/push "F - test-write-int32-decimal"/imm32
68/push "9"/imm32
68/push _test-stream/imm32
# . . call
@ -157,7 +157,7 @@ test-print-int32-decimal:
# . end
c3/return
test-print-int32-decimal-zero:
test-write-int32-decimal-zero:
# - check that 0 converts correctly
# setup
# . clear-stream(_test-stream)
@ -167,17 +167,17 @@ test-print-int32-decimal-zero:
e8/call clear-stream/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# print-int32-decimal(_test-stream, 0)
# write-int32-decimal(_test-stream, 0)
# . . push args
68/push 0/imm32
68/push _test-stream/imm32
# . . call
e8/call print-int32-decimal/disp32
e8/call write-int32-decimal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# check-stream-equal(_test-stream, "0", msg)
# . . push args
68/push "F - test-print-int32-decimal-zero"/imm32
68/push "F - test-write-int32-decimal-zero"/imm32
68/push "0"/imm32
68/push _test-stream/imm32
# . . call
@ -187,7 +187,7 @@ test-print-int32-decimal-zero:
# . end
c3/return
test-print-int32-decimal-multiple-digits:
test-write-int32-decimal-multiple-digits:
# - check that a multi-digit number converts correctly
# setup
# . clear-stream(_test-stream)
@ -197,17 +197,17 @@ test-print-int32-decimal-multiple-digits:
e8/call clear-stream/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# print-int32-decimal(_test-stream, 10)
# write-int32-decimal(_test-stream, 10)
# . . push args
68/push 0xa/imm32
68/push _test-stream/imm32
# . . call
e8/call print-int32-decimal/disp32
e8/call write-int32-decimal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# check-stream-equal(_test-stream, "10", msg)
# . . push args
68/push "F - test-print-int32-decimal-multiple-digits"/imm32
68/push "F - test-write-int32-decimal-multiple-digits"/imm32
68/push "10"/imm32
68/push _test-stream/imm32
# . . call
@ -217,7 +217,7 @@ test-print-int32-decimal-multiple-digits:
# . end
c3/return
test-print-int32-decimal-negative:
test-write-int32-decimal-negative:
# - check that a negative single-digit number converts correctly
# setup
# . clear-stream(_test-stream)
@ -227,12 +227,12 @@ test-print-int32-decimal-negative:
e8/call clear-stream/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# print-int32-decimal(_test-stream, -9)
# write-int32-decimal(_test-stream, -9)
# . . push args
68/push -9/imm32
68/push _test-stream/imm32
# . . call
e8/call print-int32-decimal/disp32
e8/call write-int32-decimal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # dump _test-stream {{{
@ -263,7 +263,7 @@ test-print-int32-decimal-negative:
#? # }}}
# check-stream-equal(_test-stream, "-9", msg)
# . . push args
68/push "F - test-print-int32-decimal-negative"/imm32
68/push "F - test-write-int32-decimal-negative"/imm32
68/push "-9"/imm32
68/push _test-stream/imm32
# . . call
@ -273,7 +273,7 @@ test-print-int32-decimal-negative:
# . end
c3/return
test-print-int32-decimal-negative-multiple-digits:
test-write-int32-decimal-negative-multiple-digits:
# - check that a multi-digit number converts correctly
# setup
# . clear-stream(_test-stream)
@ -283,17 +283,17 @@ test-print-int32-decimal-negative-multiple-digits:
e8/call clear-stream/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# print-int32-decimal(_test-stream, -10)
# write-int32-decimal(_test-stream, -10)
# . . push args
68/push -0xa/imm32
68/push _test-stream/imm32
# . . call
e8/call print-int32-decimal/disp32
e8/call write-int32-decimal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# check-stream-equal(_test-stream, "-10", msg)
# . . push args
68/push "F - test-print-int32-decimal-negative-multiple-digits"/imm32
68/push "F - test-write-int32-decimal-negative-multiple-digits"/imm32
68/push "-10"/imm32
68/push _test-stream/imm32
# . . call

View File

@ -103,9 +103,9 @@ move-cursor: # row: int, column: int
# construct directive in buf
(write %ecx Esc)
(write %ecx "[")
(print-int32-decimal %ecx *(ebp+8))
(write-int32-decimal %ecx *(ebp+8))
(write %ecx ";")
(print-int32-decimal %ecx *(ebp+0xc))
(write-int32-decimal %ecx *(ebp+0xc))
(write %ecx "H")
# flush
(write-stream 2 %ecx)
@ -198,12 +198,12 @@ start-color: # fg: int, bg: int
# . set fg
(write %ecx Esc)
(write %ecx "[38;5;")
(print-int32-decimal %ecx *(ebp+8))
(write-int32-decimal %ecx *(ebp+8))
(write %ecx "m")
# . set bg
(write %ecx Esc)
(write %ecx "[48;5;")
(print-int32-decimal %ecx *(ebp+0xc))
(write-int32-decimal %ecx *(ebp+0xc))
(write %ecx "m")
# flush
(write-stream 2 %ecx)

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -329,12 +329,12 @@ process-string-literal: # string-literal: (addr slice), out: (addr buffered-fil
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . print-int32-decimal(out-segment, *Next-string-literal)
# . write-int32-decimal(out-segment, *Next-string-literal)
# . . push args
ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Next-string-literal/disp32 # push *Next-string-literal
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0x10/disp8 . # push *(ebp+16)
# . . call
e8/call print-int32-decimal/disp32
e8/call write-int32-decimal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . write(out-segment, ":\n")
@ -370,12 +370,12 @@ process-string-literal: # string-literal: (addr slice), out: (addr buffered-fil
e8/call write-buffered/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . print-int32-decimal(int32-stream, *Next-string-literal)
# . write-int32-decimal(int32-stream, *Next-string-literal)
# . . push args
ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Next-string-literal/disp32 # push *Next-string-literal
51/push-ecx
# . . call
e8/call print-int32-decimal/disp32
e8/call write-int32-decimal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . write-stream-data(out, int32-stream)

Binary file not shown.

View File

@ -68,12 +68,12 @@ $run-main:
68/push 0/imm32/read
68/push 0/imm32/write
89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx
# print-int32-decimal(buffer, eax)
# write-int32-decimal(buffer, eax)
# . . push args
50/push-eax
51/push-ecx
# . . call
e8/call print-int32-decimal/disp32
e8/call write-int32-decimal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# write-stream(stderr, buffer)

BIN
apps/hex

Binary file not shown.

BIN
apps/mu

Binary file not shown.

BIN
apps/pack

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.