2019-05-03 06:32:51 +00:00
|
|
|
# write-byte-buffered: add a single byte to a buffered-file.
|
2018-12-04 01:08:04 +00:00
|
|
|
# flush: write out any buffered writes to disk.
|
|
|
|
#
|
|
|
|
# TODO: Come up with a way to signal failure to write to disk. This is hard
|
|
|
|
# since the failure may impact previous calls that were buffered.
|
|
|
|
|
|
|
|
== data
|
|
|
|
|
|
|
|
# The buffered file for standard output.
|
|
|
|
Stdout:
|
|
|
|
# file descriptor or (address stream)
|
2019-02-16 01:20:57 +00:00
|
|
|
1/imm32 # standard output
|
2018-12-04 01:08:04 +00:00
|
|
|
# current write index
|
2019-02-16 01:20:57 +00:00
|
|
|
0/imm32
|
2018-12-04 01:08:04 +00:00
|
|
|
# current read index
|
2019-02-16 01:20:57 +00:00
|
|
|
0/imm32
|
|
|
|
# length
|
|
|
|
8/imm32
|
2018-12-04 01:08:04 +00:00
|
|
|
# data
|
|
|
|
00 00 00 00 00 00 00 00 # 8 bytes
|
|
|
|
|
|
|
|
# TODO: 8 bytes is too small. We'll need to grow the buffer for efficiency. But
|
2018-12-06 06:34:59 +00:00
|
|
|
# I don't want to type in 1024 bytes here.
|
2018-12-04 01:08:04 +00:00
|
|
|
|
|
|
|
== 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
|
|
|
|
|
|
|
|
# Write lower byte of 'n' to 'f'.
|
2019-05-03 06:32:51 +00:00
|
|
|
write-byte-buffered: # f : (address buffered-file), n : int -> <void>
|
2018-12-04 01:08:04 +00:00
|
|
|
# . prolog
|
|
|
|
55/push-EBP
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
|
|
|
# . save registers
|
|
|
|
51/push-ECX
|
2018-12-04 18:40:01 +00:00
|
|
|
57/push-EDI
|
|
|
|
# EDI = f
|
2018-12-29 21:36:06 +00:00
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 7/r32/EDI 8/disp8 . # copy *(EBP+8) to EDI
|
2018-12-04 01:08:04 +00:00
|
|
|
# ECX = f->write
|
2018-12-04 18:40:01 +00:00
|
|
|
8b/copy 1/mod/*+disp8 7/rm32/EDI . . . 1/r32/ECX 4/disp8 . # copy *(EDI+4) to ECX
|
2018-12-04 17:59:42 +00:00
|
|
|
# if (f->write >= f->length) flush and clear f's stream
|
2018-12-04 18:40:01 +00:00
|
|
|
3b/compare 1/mod/*+disp8 7/rm32/EDI . . . 1/r32/ECX 0xc/disp8 . # compare ECX with *(EDI+12)
|
2019-05-03 06:32:51 +00:00
|
|
|
7c/jump-if-lesser $write-byte-buffered:to-stream/disp8
|
2018-12-04 17:57:40 +00:00
|
|
|
# . flush(f)
|
2018-12-04 01:08:04 +00:00
|
|
|
# . . push args
|
2018-12-04 18:40:01 +00:00
|
|
|
57/push-EDI
|
2018-12-04 01:08:04 +00:00
|
|
|
# . . call
|
|
|
|
e8/call flush/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
|
|
|
# . clear-stream(stream = f+4)
|
|
|
|
# . . push args
|
2018-12-04 18:40:01 +00:00
|
|
|
8d/copy-address 1/mod/*+disp8 7/rm32/EDI . . . 0/r32/EAX 4/disp8 . # copy EDI+4 to EAX
|
2018-12-04 01:08:04 +00:00
|
|
|
50/push-EAX
|
|
|
|
# . . call
|
|
|
|
e8/call clear-stream/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
2019-01-11 01:13:47 +00:00
|
|
|
# . f->write must now be 0; update its cache at ECX
|
2019-01-11 01:06:38 +00:00
|
|
|
31/xor 3/mod/direct 1/rm32/ECX . . . 1/r32/ECX . . # clear ECX
|
2019-05-03 06:32:51 +00:00
|
|
|
$write-byte-buffered:to-stream:
|
2018-12-04 01:08:04 +00:00
|
|
|
# write to stream
|
2019-01-06 20:51:49 +00:00
|
|
|
# f->data[f->write] = LSB(n)
|
2018-12-04 01:08:04 +00:00
|
|
|
31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX
|
2018-12-29 21:36:06 +00:00
|
|
|
8a/copy-byte 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/AL 0xc/disp8 . # copy byte at *(EBP+12) to AL
|
2018-12-04 18:40:01 +00:00
|
|
|
88/copy-byte 1/mod/*+disp8 4/rm32/sib 7/base/EDI 1/index/ECX . 0/r32/AL 0x10/disp8 . # copy AL to *(EDI+ECX+16)
|
2019-01-06 20:51:49 +00:00
|
|
|
# ++f->write
|
2018-12-04 18:40:01 +00:00
|
|
|
ff 0/subop/increment 1/mod/*+disp8 7/rm32/EDI . . . . 4/disp8 . # increment *(EDI+4)
|
2019-05-03 06:32:51 +00:00
|
|
|
$write-byte-buffered:end:
|
2018-12-04 01:08:04 +00:00
|
|
|
# . restore registers
|
2018-12-04 18:40:01 +00:00
|
|
|
5f/pop-to-EDI
|
2018-12-04 01:08:04 +00:00
|
|
|
59/pop-to-ECX
|
|
|
|
# . epilog
|
|
|
|
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
|
|
|
|
5d/pop-to-EBP
|
|
|
|
c3/return
|
|
|
|
|
|
|
|
flush: # f : (address buffered-file) -> <void>
|
|
|
|
# . prolog
|
|
|
|
55/push-EBP
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
|
|
|
# . save registers
|
|
|
|
50/push-EAX
|
|
|
|
51/push-ECX
|
|
|
|
# EAX = f
|
2018-12-29 21:36:06 +00:00
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 8/disp8 . # copy *(EBP+8) to EAX
|
2018-12-04 01:08:04 +00:00
|
|
|
# write-stream(f->fd, data = f+4)
|
|
|
|
# . . push args
|
|
|
|
8d/copy-address 1/mod/*+disp8 0/rm32/EAX . . . 1/r32/ECX 4/disp8 . # copy EAX+4 to ECX
|
|
|
|
51/push-ECX
|
|
|
|
ff 6/subop/push 0/mod/indirect 0/rm32/EAX . . . . . . # push *EAX
|
|
|
|
# . . call
|
|
|
|
e8/call write-stream/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
2018-12-04 08:29:06 +00:00
|
|
|
$flush:end:
|
2018-12-04 01:08:04 +00:00
|
|
|
# . restore registers
|
|
|
|
59/pop-to-ECX
|
|
|
|
58/pop-to-EAX
|
|
|
|
# . epilog
|
|
|
|
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
|
|
|
|
5d/pop-to-EBP
|
|
|
|
c3/return
|
|
|
|
|
2019-05-03 06:32:51 +00:00
|
|
|
test-write-byte-buffered-single:
|
|
|
|
# - check that write-byte-buffered writes to first byte of 'file'
|
2018-12-04 01:08:04 +00:00
|
|
|
# setup
|
|
|
|
# . clear-stream(_test-stream)
|
|
|
|
# . . push args
|
|
|
|
68/push _test-stream/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call clear-stream/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
|
|
|
# . clear-stream(_test-buffered-file+4)
|
|
|
|
# . . push args
|
|
|
|
b8/copy-to-EAX _test-buffered-file/imm32
|
|
|
|
05/add-to-EAX 4/imm32
|
|
|
|
50/push-EAX
|
|
|
|
# . . call
|
|
|
|
e8/call clear-stream/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
2019-05-03 06:32:51 +00:00
|
|
|
# write-byte-buffered(_test-buffered-file, 'A')
|
2018-12-04 01:08:04 +00:00
|
|
|
# . . push args
|
|
|
|
68/push 0x41/imm32
|
|
|
|
68/push _test-buffered-file/imm32
|
|
|
|
# . . call
|
2019-05-03 06:32:51 +00:00
|
|
|
e8/call write-byte-buffered/disp32
|
2018-12-04 01:08:04 +00:00
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# flush(_test-buffered-file)
|
|
|
|
# . . push args
|
|
|
|
68/push _test-buffered-file/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call flush/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
2019-02-04 07:31:03 +00:00
|
|
|
# check-stream-equal(_test-stream, "A", msg)
|
2018-12-04 01:08:04 +00:00
|
|
|
# . . push args
|
2019-05-03 06:32:51 +00:00
|
|
|
68/push "F - test-write-byte-buffered-single"/imm32
|
2019-02-04 07:31:03 +00:00
|
|
|
68/push "A"/imm32
|
|
|
|
68/push _test-stream/imm32
|
2018-12-04 01:08:04 +00:00
|
|
|
# . . call
|
2019-02-04 07:31:03 +00:00
|
|
|
e8/call check-stream-equal/disp32
|
2018-12-04 01:08:04 +00:00
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
|
|
|
# . end
|
|
|
|
c3/return
|
|
|
|
|
2019-05-03 06:32:51 +00:00
|
|
|
test-write-byte-buffered-multiple-flushes:
|
|
|
|
# - check that write-byte-buffered correctly flushes buffered data
|
2019-01-11 01:06:38 +00:00
|
|
|
# setup
|
|
|
|
# . clear-stream(_test-stream)
|
|
|
|
# . . push args
|
|
|
|
68/push _test-stream/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call clear-stream/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
|
|
|
# . clear-stream(_test-buffered-file+4)
|
|
|
|
# . . push args
|
|
|
|
b8/copy-to-EAX _test-buffered-file/imm32
|
|
|
|
05/add-to-EAX 4/imm32
|
|
|
|
50/push-EAX
|
|
|
|
# . . call
|
|
|
|
e8/call clear-stream/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
|
|
|
# fill up the buffer for _test-buffered-file
|
|
|
|
# . write(_test-buffered-file+4, 'abcdef')
|
|
|
|
# . . push args
|
|
|
|
68/push "abcdef"/imm32
|
|
|
|
b8/copy-to-EAX _test-buffered-file/imm32
|
|
|
|
05/add-to-EAX 4/imm32
|
|
|
|
50/push-EAX
|
|
|
|
# . . call
|
|
|
|
e8/call write/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
2019-05-03 06:32:51 +00:00
|
|
|
# write-byte-buffered(_test-buffered-file, 'g')
|
2019-01-11 01:06:38 +00:00
|
|
|
# . . push args
|
|
|
|
68/push 0x67/imm32
|
|
|
|
68/push _test-buffered-file/imm32
|
|
|
|
# . . call
|
2019-05-03 06:32:51 +00:00
|
|
|
e8/call write-byte-buffered/disp32
|
2019-01-11 01:06:38 +00:00
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# flush(_test-buffered-file)
|
|
|
|
# . . push args
|
|
|
|
68/push _test-buffered-file/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call flush/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
2019-02-04 07:31:03 +00:00
|
|
|
# check-stream-equal(_test-stream, "abcdef", msg)
|
2019-01-11 01:06:38 +00:00
|
|
|
# . . push args
|
2019-05-03 06:32:51 +00:00
|
|
|
68/push "F - test-write-byte-buffered-multiple-flushes: 1"/imm32
|
2019-02-04 07:31:03 +00:00
|
|
|
68/push "abcdefg"/imm32
|
|
|
|
68/push _test-stream/imm32
|
2019-01-11 01:06:38 +00:00
|
|
|
# . . call
|
2019-02-04 07:31:03 +00:00
|
|
|
e8/call check-stream-equal/disp32
|
2019-01-11 01:06:38 +00:00
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
|
|
|
# . end
|
|
|
|
c3/return
|
|
|
|
|
2019-05-03 06:44:43 +00:00
|
|
|
# - variant without buffering
|
|
|
|
|
|
|
|
# Write lower byte of 'n' to 'f'.
|
|
|
|
append-byte: # f : (address stream), n : int -> <void>
|
|
|
|
# . prolog
|
|
|
|
55/push-EBP
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
|
|
|
# . save registers
|
|
|
|
51/push-ECX
|
|
|
|
57/push-EDI
|
|
|
|
# EDI = f
|
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 7/r32/EDI 8/disp8 . # copy *(EBP+8) to EDI
|
|
|
|
# ECX = f->write
|
|
|
|
8b/copy 0/mod/indirect 7/rm32/EDI . . . 1/r32/ECX . . # copy *EDI to ECX
|
|
|
|
# if (f->write >= f->length) abort
|
|
|
|
3b/compare 1/mod/*+disp8 7/rm32/EDI . . . 1/r32/ECX 8/disp8 . # compare ECX with *(EDI+8)
|
|
|
|
7d/jump-if-greater-or-equal $append-byte:abort/disp8
|
|
|
|
$append-byte:to-stream:
|
|
|
|
# write to stream
|
|
|
|
# f->data[f->write] = LSB(n)
|
|
|
|
31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX
|
|
|
|
8a/copy-byte 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/AL 0xc/disp8 . # copy byte at *(EBP+12) to AL
|
|
|
|
88/copy-byte 1/mod/*+disp8 4/rm32/sib 7/base/EDI 1/index/ECX . 0/r32/AL 0xc/disp8 . # copy AL to *(EDI+ECX+12)
|
|
|
|
# ++f->write
|
|
|
|
ff 0/subop/increment 0/mod/indirect 7/rm32/EDI . . . . . . # increment *EDI
|
|
|
|
$append-byte:end:
|
|
|
|
# . restore registers
|
|
|
|
5f/pop-to-EDI
|
|
|
|
59/pop-to-ECX
|
|
|
|
# . epilog
|
|
|
|
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
|
|
|
|
5d/pop-to-EBP
|
|
|
|
c3/return
|
|
|
|
|
|
|
|
$append-byte:abort:
|
|
|
|
# . _write(2/stderr, error)
|
|
|
|
# . . push args
|
|
|
|
68/push "append-byte: out of space"/imm32
|
|
|
|
68/push 2/imm32/stderr
|
|
|
|
# . . call
|
|
|
|
e8/call _write/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# . syscall(exit, 1)
|
|
|
|
bb/copy-to-EBX 1/imm32
|
|
|
|
b8/copy-to-EAX 1/imm32/exit
|
|
|
|
cd/syscall 0x80/imm8
|
|
|
|
# never gets here
|
|
|
|
|
|
|
|
test-append-byte-single:
|
|
|
|
# - check that append-byte writes to first byte of 'file'
|
|
|
|
# setup
|
|
|
|
# . clear-stream(_test-stream)
|
|
|
|
# . . push args
|
|
|
|
68/push _test-stream/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call clear-stream/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
|
|
|
# append-byte(_test-stream, 'A')
|
|
|
|
# . . push args
|
|
|
|
68/push 0x41/imm32
|
|
|
|
68/push _test-stream/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call append-byte/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# check-stream-equal(_test-stream, "A", msg)
|
|
|
|
# . . push args
|
|
|
|
68/push "F - test-append-byte-single"/imm32
|
|
|
|
68/push "A"/imm32
|
|
|
|
68/push _test-stream/imm32
|
|
|
|
# . . call
|
|
|
|
e8/call check-stream-equal/disp32
|
|
|
|
# . . discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
|
|
|
# . end
|
|
|
|
c3/return
|
|
|
|
|
2018-12-04 01:08:04 +00:00
|
|
|
# . . vim:nowrap:textwidth=0
|