snapshot: stupid debugging session

I spent a while building a little keyboard scancode printer:

  $ ./translate ex1.mu &&  qemu-system-i386 disk.img

..and wondering why up-arrow was 0x48 in hex but 724 in decimal. I ended
up paranoidly poking at a bunch of crap (though there _is_ a cool chromatography-based
debugging technique in 126write-int-decimal.subx) before I realized:

  - 724 just has one extra digit over the correct answer
  - the 0xe0 scan code is a 3-digit number in decimal -- and the final digit is '4'

There's nothing actually wrong.
This commit is contained in:
Kartik K. Agaram 2021-04-05 21:10:45 -07:00
parent 463878a4a4
commit 928fd47d68
6 changed files with 152 additions and 123 deletions

View File

@ -43,18 +43,24 @@ write-int32-decimal: # out: (addr stream byte), n: int
51/push-ecx
52/push-edx
53/push-ebx
56/push-esi
57/push-edi
be/copy-to-esi 3/imm32/x
# const ten/ecx = 10
b9/copy-to-ecx 0xa/imm32
# push sentinel
68/push 0/imm32/sentinel
# var eax: int = abs(n)
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0xc/disp8 . # copy *(ebp+12) to eax
(draw-vertical-line-on-real-screen %eax 0x40 0x50 %eax)
81 0/subop/add %esi 3/imm32
3d/compare-eax-with 0/imm32
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
$write-int32-decimal:read-loop:
(draw-vertical-line-on-real-screen %esi 0x80 0x90 %esi)
81 0/subop/add %esi 3/imm32
# 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
@ -104,6 +110,7 @@ $write-int32-decimal:write-break:
$write-int32-decimal:end:
# . restore registers
5f/pop-to-edi
5e/pop-to-esi
5b/pop-to-ebx
5a/pop-to-edx
59/pop-to-ecx

View File

@ -105,6 +105,15 @@ fn draw-text-rightward screen: (addr screen), text: (addr array byte), x: int, x
return xcurr
}
fn draw-int32-decimal-rightward screen: (addr screen), n: int, x: int, xmax: int, y: int, color: int, background-color: int -> _/eax: int {
var stream-storage: (stream byte 0x100)
var stream/esi: (addr stream byte) <- address stream-storage
write-int32-decimal stream, n
#? write-int32-hex stream, n
var xcurr/eax: int <- draw-stream-rightward screen, stream, x, xmax, y, color, background-color
return xcurr
}
# draw a single-line stream from x, y to xmax
# return the next 'x' coordinate
# if there isn't enough space, truncate
@ -244,23 +253,9 @@ fn draw-int32-hex-wrapping-right-then-down screen: (addr screen), n: int, xmin:
var stream-storage: (stream byte 0x100)
var stream/esi: (addr stream byte) <- address stream-storage
write-int32-hex stream, n
var xcurr/edx: int <- copy x
var xcurr/eax: int <- copy x
var ycurr/ecx: int <- copy y
{
var g/eax: grapheme <- read-grapheme stream
compare g, 0xffffffff/end-of-file
break-if-=
draw-grapheme screen, g, xcurr, ycurr, color, background-color
xcurr <- increment
compare xcurr, xmax
{
break-if-<
xcurr <- copy xmin
ycurr <- increment
}
loop
}
set-cursor-position screen, xcurr, ycurr
xcurr, ycurr <- draw-stream-wrapping-right-then-down screen, stream, xmin, ymin, xmax, ymax, x, y, color, background-color
return xcurr, ycurr
}
@ -297,24 +292,17 @@ fn draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen screen:
fn draw-int32-decimal-wrapping-right-then-down screen: (addr screen), n: int, xmin: int, ymin: int, xmax: int, ymax: int, x: int, y: int, color: int, background-color: int -> _/eax: int, _/ecx: int {
var stream-storage: (stream byte 0x100)
var stream/esi: (addr stream byte) <- address stream-storage
#? compare n, 0x48
#? {
#? break-if-!=
#? abort "aa"
#? }
write-int32-decimal stream, n
var xcurr/edx: int <- copy x
var ycurr/ecx: int <- copy y
{
var g/eax: grapheme <- read-grapheme stream
compare g, 0xffffffff/end-of-file
break-if-=
draw-grapheme screen, g, xcurr, ycurr, color, background-color
xcurr <- increment
compare xcurr, xmax
{
break-if-<
xcurr <- copy xmin
ycurr <- increment
}
loop
}
set-cursor-position screen, xcurr, ycurr
#? write-int32-decimal stream, 0x48
#? write-int32-hex stream, n
var xcurr/eax: int <- copy 0
var ycurr/ecx: int <- copy 0
xcurr, ycurr <- draw-stream-wrapping-right-then-down screen, stream, xmin, ymin, xmax, ymax, x, y, color, background-color
return xcurr, ycurr
}

207
boot.subx
View File

@ -359,96 +359,125 @@ keyboard-interrupt-handler:
3c/compare-al-and 0/imm8
75/jump-if-!= $keyboard-interrupt-handler:epilogue/disp8
# - read keycode
31/xor %eax 0/r32/eax
e4/read-port-into-al 0x60/imm8
# - key released
# if (al == 0xaa) shift = false # left shift is being lifted
{
3c/compare-al-and 0xaa/imm8
75/jump-if-!= break/disp8
# *shift = 0
c7 0/subop/copy *Keyboard-shift-pressed? 0/imm32
}
# if (al == 0xb6) shift = false # right shift is being lifted
{
3c/compare-al-and 0xb6/imm8
75/jump-if-!= break/disp8
# *shift = 0
c7 0/subop/copy *Keyboard-shift-pressed? 0/imm32
}
# if (al == 0x9d) ctrl = false # ctrl is being lifted
{
3c/compare-al-and 0x9d/imm8
75/jump-if-!= break/disp8
# *ctrl = 0
c7 0/subop/copy *Keyboard-ctrl-pressed? 0/imm32
}
# if (al & 0x80) a key is being lifted; return
50/push-eax
24/and-al-with 0x80/imm8
3c/compare-al-and 0/imm8
58/pop-to-eax
75/jump-if-!= $keyboard-interrupt-handler:epilogue/disp8
# - key pressed
# if (al == 0x2a) shift = true, return # left shift pressed
{
3c/compare-al-and 0x2a/imm8
75/jump-if-!= break/disp8
# *shift = 1
c7 0/subop/copy *Keyboard-shift-pressed? 1/imm32
# return
eb/jump $keyboard-interrupt-handler:epilogue/disp8
}
# if (al == 0x36) shift = true, return # right shift pressed
{
3c/compare-al-and 0x36/imm8
75/jump-if-!= break/disp8
# *shift = 1
c7 0/subop/copy *Keyboard-shift-pressed? 1/imm32
# return
eb/jump $keyboard-interrupt-handler:epilogue/disp8
}
# if (al == 0x1d) ctrl = true, return
{
3c/compare-al-and 0x1d/imm8
75/jump-if-!= break/disp8
# *ctrl = 1
c7 0/subop/copy *Keyboard-ctrl-pressed? 1/imm32
# return
eb/jump $keyboard-interrupt-handler:epilogue/disp8
}
# - convert key to character
# if (shift) use keyboard shift map
{
81 7/subop/compare *Keyboard-shift-pressed? 0/imm32
74/jump-if-= break/disp8
# sigils don't currently support labels inside *(eax+label)
05/add-to-eax Keyboard-shift-map/imm32
8a/byte-> *eax 0/r32/al
eb/jump $keyboard-interrupt-handler:select-map-done/disp8
}
# if (ctrl) al = *(ctrl map + al)
{
81 7/subop/compare *Keyboard-ctrl-pressed? 0/imm32
74/jump-if-= break/disp8
05/add-to-eax Keyboard-ctrl-map/imm32
8a/byte-> *eax 0/r32/al
eb/jump $keyboard-interrupt-handler:select-map-done/disp8
}
# otherwise al = *(normal map + al)
05/add-to-eax Keyboard-normal-map/imm32
8a/byte-> *eax 0/r32/al
$keyboard-interrupt-handler:select-map-done:
# - if there's no character mapping, return
{
3c/compare-al-and 0/imm8
74/jump-if-= break/disp8
# - store al in keyboard buffer
88/<- *ecx 0/r32/al
# increment index
fe/increment-byte *Keyboard-buffer:write
# clear top nibble of index (keyboard buffer is circular)
80 4/subop/and-byte *Keyboard-buffer:write 0x0f/imm8
}
#
#? 50/push-eax
#? 51/push-ecx
#? (draw-int32-hex-wrapping-right-then-down-over-full-screen 0 %eax 0 0 7 0)
#? 59/pop-to-ecx
#? 58/pop-to-eax
#
#? 50/push-eax
#? 51/push-ecx
#? (draw-int32-decimal-wrapping-right-then-down 0 %eax 0 2 0x400 2 0 2 7 0)
89/<- %ecx 0/r32/eax
# . . push args
68/push 0/imm32/bg
68/push 7/imm32/fg
68/push 2/imm32/y
68/push 0x400/imm32/xmax
68/push 0/imm32/xmin
#? 68/push 0x48/imm32/n
51/push-ecx
68/push 0/imm32/screen
# . . call
#? e8/call draw-int32-hex-wrapping-right-then-down/disp32
#? e8/call draw-int32-decimal-wrapping-right-then-down/disp32
e8/call draw-int32-decimal-rightward/disp32
# . . discard args
81 0/subop/add %esp 0x1c/imm32
#? 59/pop-to-ecx
#? 58/pop-to-eax
#? # - key released
#? # if (al == 0xaa) shift = false # left shift is being lifted
#? {
#? 3c/compare-al-and 0xaa/imm8
#? 75/jump-if-!= break/disp8
#? # *shift = 0
#? c7 0/subop/copy *Keyboard-shift-pressed? 0/imm32
#? }
#? # if (al == 0xb6) shift = false # right shift is being lifted
#? {
#? 3c/compare-al-and 0xb6/imm8
#? 75/jump-if-!= break/disp8
#? # *shift = 0
#? c7 0/subop/copy *Keyboard-shift-pressed? 0/imm32
#? }
#? # if (al == 0x9d) ctrl = false # ctrl is being lifted
#? {
#? 3c/compare-al-and 0x9d/imm8
#? 75/jump-if-!= break/disp8
#? # *ctrl = 0
#? c7 0/subop/copy *Keyboard-ctrl-pressed? 0/imm32
#? }
#? # if (al & 0x80) a key is being lifted; return
#? 50/push-eax
#? 24/and-al-with 0x80/imm8
#? 3c/compare-al-and 0/imm8
#? 58/pop-to-eax
#? 75/jump-if-!= $keyboard-interrupt-handler:epilogue/disp8
#? # - key pressed
#? # if (al == 0x2a) shift = true, return # left shift pressed
#? {
#? 3c/compare-al-and 0x2a/imm8
#? 75/jump-if-!= break/disp8
#? # *shift = 1
#? c7 0/subop/copy *Keyboard-shift-pressed? 1/imm32
#? # return
#? eb/jump $keyboard-interrupt-handler:epilogue/disp8
#? }
#? # if (al == 0x36) shift = true, return # right shift pressed
#? {
#? 3c/compare-al-and 0x36/imm8
#? 75/jump-if-!= break/disp8
#? # *shift = 1
#? c7 0/subop/copy *Keyboard-shift-pressed? 1/imm32
#? # return
#? eb/jump $keyboard-interrupt-handler:epilogue/disp8
#? }
#? # if (al == 0x1d) ctrl = true, return
#? {
#? 3c/compare-al-and 0x1d/imm8
#? 75/jump-if-!= break/disp8
#? # *ctrl = 1
#? c7 0/subop/copy *Keyboard-ctrl-pressed? 1/imm32
#? # return
#? eb/jump $keyboard-interrupt-handler:epilogue/disp8
#? }
#? # - convert key to character
#? # if (shift) use keyboard shift map
#? {
#? 81 7/subop/compare *Keyboard-shift-pressed? 0/imm32
#? 74/jump-if-= break/disp8
#? # sigils don't currently support labels inside *(eax+label)
#? 05/add-to-eax Keyboard-shift-map/imm32
#? 8a/byte-> *eax 0/r32/al
#? eb/jump $keyboard-interrupt-handler:select-map-done/disp8
#? }
#? # if (ctrl) al = *(ctrl map + al)
#? {
#? 81 7/subop/compare *Keyboard-ctrl-pressed? 0/imm32
#? 74/jump-if-= break/disp8
#? 05/add-to-eax Keyboard-ctrl-map/imm32
#? 8a/byte-> *eax 0/r32/al
#? eb/jump $keyboard-interrupt-handler:select-map-done/disp8
#? }
#? # otherwise al = *(normal map + al)
#? 05/add-to-eax Keyboard-normal-map/imm32
#? 8a/byte-> *eax 0/r32/al
#? $keyboard-interrupt-handler:select-map-done:
#? # - if there's no character mapping, return
#? {
#? 3c/compare-al-and 0/imm8
#? 74/jump-if-= break/disp8
#? # - store al in keyboard buffer
#? 88/<- *ecx 0/r32/al
#? # increment index
#? fe/increment-byte *Keyboard-buffer:write
#? # clear top nibble of index (keyboard buffer is circular)
#? 80 4/subop/and-byte *Keyboard-buffer:write 0x0f/imm8
#? }
$keyboard-interrupt-handler:epilogue:
# epilogue
9d/pop-flags

3
ex1.mu Normal file
View File

@ -0,0 +1,3 @@
fn main screen: (addr screen), keyboard: (addr keyboard), data-disk: (addr disk) {
loop
}

View File

@ -167,9 +167,10 @@ test-write-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
b8/copy-to-eax 0x48/imm32
# write-int32-decimal(_test-stream, 0)
# . . push args
68/push 0/imm32
50/push-eax
68/push _test-stream/imm32
# . . call
e8/call write-int32-decimal/disp32

View File

@ -11,6 +11,7 @@
== code
Entry:
e8/call test-write-int32-decimal-zero/disp32
# exit(42)
bb/copy-to-ebx 0x2a/imm32 # 42 in hex
e8/call syscall_exit/disp32