mu/baremetal/112read-byte.subx
Kartik Agaram 0d246473c5 7524 - bring back some abort messages
Our infrastructure for displaying errors is far more rudimentary in
baremetal. Many ways things can go wrong. But making the attempt seems
better than not.

I'm also making some effort to keep it easy to see what has been copied
over from the top-level, by not modifying copied code to use syntax
sugar and so on. It may not be an important enough reason to mix
notations in a single file.
2021-01-15 21:27:52 -08:00

49 lines
2.6 KiB
Plaintext

# Read a single byte from a stream.
#
# We need to do this in machine code because streams need to be opaque types,
# and we don't yet support opaque types in Mu.
== code
# instruction effective address register 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
# Return next byte value in eax, with top 3 bytes cleared.
# Abort on reaching end of stream.
read-byte: # s: (addr stream byte) -> result/eax: byte
# . prologue
55/push-ebp
89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
# . save registers
51/push-ecx
56/push-esi
# esi = s
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi
# ecx = s->read
8b/copy 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # copy *(esi+4) to ecx
# if (f->read >= f->write) abort
3b/compare 0/mod/indirect 6/rm32/esi . . . 1/r32/ecx . . # compare ecx with *esi
0f 8d/jump-if->= $read-byte:abort/disp32
# result = f->data[f->read]
31/xor 3/mod/direct 0/rm32/eax . . . 0/r32/eax . . # clear eax
8a/copy-byte 1/mod/*+disp8 4/rm32/sib 6/base/esi 1/index/ecx . 0/r32/AL 0xc/disp8 . # copy byte at *(esi+ecx+12) to AL
# ++f->read
ff 0/subop/increment 1/mod/*+disp8 6/rm32/esi . . . . 4/disp8 . # increment *(esi+4)
$read-byte:end:
# . restore registers
5e/pop-to-esi
59/pop-to-ecx
# . epilogue
89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
5d/pop-to-ebp
c3/return
$read-byte:abort:
(draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "read-byte: empty stream" 3) # 3=cyan
{
eb/jump loop/disp8
}
# never gets here
# . . vim:nowrap:textwidth=0