# write: like _write, but also support in-memory streams in addition to file # descriptors. # # Our first dependency-injected and testable primitive. We can pass it either # a file descriptor or an address to a stream. If a file descriptor is passed # in, we _write to it using the right syscall. If a 'fake file descriptor' or # stream is passed in, we append to the stream. This lets us redirect output # in tests and check it later. # # We assume our data segment will never begin at an address shorter than # 0x08000000, so any smaller arguments are assumed to be real file descriptors. # # A stream looks like this: # read: int # index at which to read next # write: int # index at which writes go # data: (array byte) # prefixed by size as usual == 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 # TODO: come up with a way to signal when a write to disk fails write: # f: fd or (addr stream byte), s: (addr array byte) # . prologue 55/push-ebp 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp # if (s == 0) return 81 7/subop/compare 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 0/imm32 # compare *(ebp+12) 74/jump-if-= $write:end/disp8 # if (f < 0x08000000) _write(f, s) and return # f can't be a user-mode address, so treat it as a kernel file descriptor 81 7/subop/compare 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 0x08000000/imm32 # compare *(ebp+8) 73/jump-if-addr>= $write:fake/disp8 # . . push args ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12) ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8) # . . call e8/call _write/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp eb/jump $write:end/disp8 $write:fake: # otherwise, treat 'f' as a stream to append to # . save registers 50/push-eax 51/push-ecx 52/push-edx 53/push-ebx # ecx = f 8b/copy 1/mod/*+disp8 5/rm32/ebp . . 1/r32/ecx 8/disp8 . # copy *(ebp+8) to ecx # edx = f->write 8b/copy 0/mod/indirect 1/rm32/ecx . . . 2/r32/edx . . # copy *ecx to edx # ebx = f->size 8b/copy 1/mod/*+disp8 1/rm32/ecx . . . 3/r32/ebx 8/disp8 . # copy *(ecx+8) to ebx # eax = _append-3(&f->data[f->write], &f->data[f->size], s) # . . push s ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12) # . . push &f->data[f->size] 8d/copy-address 1/mod/*+disp8 4/rm32/sib 1/base/e