2018-11-24 22:00:55 +00:00
|
|
|
# write-stream: like write, but write streams rather than strings
|
|
|
|
|
|
|
|
== code
|
2018-11-30 19:10:44 +00:00
|
|
|
# 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
|
2018-11-24 22:00:55 +00:00
|
|
|
|
|
|
|
# main:
|
2018-11-30 18:54:42 +00:00
|
|
|
# manual test
|
|
|
|
#? # write-stream(stdout, _test-stream2)
|
|
|
|
#? 68/push _test-stream2/imm32
|
|
|
|
#? 68/push 1/imm32/stdout
|
|
|
|
#? e8/call write-stream/disp32
|
|
|
|
# automatic test
|
|
|
|
#? e8/call test-write-stream-appends/disp32
|
|
|
|
e8/call run-tests/disp32 # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
|
|
|
|
# syscall(exit, Num-test-failures)
|
|
|
|
8b/copy 0/mod/indirect 5/rm32/.disp32 . . 3/r32/EBX Num-test-failures/disp32 # copy *Num-test-failures to EBX
|
2018-12-28 16:56:21 +00:00
|
|
|
b8/copy-to-EAX 1/imm32/exit
|
2018-11-30 18:54:42 +00:00
|
|
|
cd/syscall 0x80/imm8
|
2018-11-24 22:00:55 +00:00
|
|
|
|
|
|
|
write-stream: # f : fd or (address stream), s : (address stream) -> <void>
|
2018-11-30 19:10:44 +00:00
|
|
|
# . prolog
|
2018-11-30 18:54:42 +00:00
|
|
|
55/push-EBP
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
|
|
|
# if (f < 0x08000000) _write-stream(f, s), return # f can't be a user-mode address, so treat it as a kernel file descriptor
|
|
|
|
81 7/subop/compare 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none . . 8/disp8 0x08000000/imm32 # compare *(EBP+8)
|
|
|
|
7d/jump-if-greater-or-equal $write-stream:fake/disp8
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2018-11-30 18:54:42 +00:00
|
|
|
ff 6/subop/push 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none . . 0xc/disp8 . # push *(EBP+12)
|
|
|
|
ff 6/subop/push 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none . . 8/disp8 . # push *(EBP+8)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . call
|
2018-11-30 18:54:42 +00:00
|
|
|
e8/call _write-stream/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2018-11-30 18:54:42 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
eb/jump $write-stream:end/disp8
|
2018-11-24 22:00:55 +00:00
|
|
|
$write-stream:fake:
|
2018-11-30 18:54:42 +00:00
|
|
|
# otherwise, treat 'f' as a stream to append to
|
2018-11-30 19:10:44 +00:00
|
|
|
# . save registers
|
2018-11-30 18:54:42 +00:00
|
|
|
50/push-EAX
|
|
|
|
56/push-ESI
|
|
|
|
57/push-EDI
|
2018-12-10 17:55:46 +00:00
|
|
|
# EDI = f
|
|
|
|
8b/copy 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none 7/r32/EDI 8/disp8 . # copy *(EBP+8) to EDI
|
|
|
|
# ESI = s
|
|
|
|
8b/copy 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none 6/r32/ESI 0xc/disp8 . # copy *(EBP+12) to ESI
|
2018-11-30 18:54:42 +00:00
|
|
|
# EAX = _append-4(&f->data[f->write], &f->data[f->length], &s->data[s->read], &s->data[s->write])
|
2018-12-01 00:45:15 +00:00
|
|
|
# . . push &s->data[s->write]
|
2018-12-10 17:55:46 +00:00
|
|
|
8b/copy 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # copy *ESI to EAX
|
|
|
|
8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/ESI 0/index/EAX . 0/r32/EAX 0xc/disp8 . # copy ESI+EAX+12 to EAX
|
2018-11-30 18:54:42 +00:00
|
|
|
50/push-EAX
|
2018-12-01 00:45:15 +00:00
|
|
|
# . . push &s->data[s->read]
|
2018-12-10 17:55:46 +00:00
|
|
|
8b/copy 1/mod/*+disp8 6/rm32/ESI . . . 0/r32/EAX 4/disp8 . # copy *(ESI+4) to EAX
|
|
|
|
8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/ESI 0/index/EAX . 0/r32/EAX 0xc/disp8 . # copy ESI+EAX+12 to EAX
|
2018-11-30 18:54:42 +00:00
|
|
|
50/push-EAX
|
2018-12-01 00:45:15 +00:00
|
|
|
# . . push &f->data[f->length]
|
2018-12-10 17:55:46 +00:00
|
|
|
8b/copy 1/mod/*+disp8 7/rm32/EDI . . . 0/r32/EAX 8/disp8 . # copy *(EDI+8) to EAX
|
|
|
|
8d/copy-address 1/mod/*+disp8 4/rm32/sib 7/base/EDI 0/index/EAX . 0/r32/EAX 0xc/disp8 . # copy EDI+EAX+12 to EAX
|
2018-11-30 18:54:42 +00:00
|
|
|
50/push-EAX
|
2018-12-01 00:45:15 +00:00
|
|
|
# . . push &f->data[f->write]
|
2018-12-10 17:55:46 +00:00
|
|
|
8b/copy 0/mod/indirect 7/rm32/EDI . . . 0/r32/EAX . . # copy *EDI to EAX
|
|
|
|
8d/copy-address 1/mod/*+disp8 4/rm32/sib 7/base/EDI 0/index/EAX . 0/r32/EAX 0xc/disp8 . # copy EDI+EAX+12 to EAX
|
2018-11-30 18:54:42 +00:00
|
|
|
50/push-EAX
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . call
|
2018-11-30 18:54:42 +00:00
|
|
|
e8/call _append-4/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2018-11-30 18:54:42 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP
|
|
|
|
# f->write += EAX
|
2018-12-10 17:55:46 +00:00
|
|
|
01/add 0/mod/indirect 7/rm32/EDI . . . 0/r32/EAX . . # add EAX to *EDI
|
2018-11-30 18:54:42 +00:00
|
|
|
# s->read += EAX
|
2018-12-10 17:55:46 +00:00
|
|
|
01/add 1/mod/*+disp8 6/rm32/ESI . . . 0/r32/EAX 4/disp8 . # add EAX to *(ESI+4)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . restore registers
|
2018-11-30 18:54:42 +00:00
|
|
|
5f/pop-to-EDI
|
|
|
|
5e/pop-to-ESI
|
|
|
|
58/pop-to-EAX
|
2018-11-24 22:00:55 +00:00
|
|
|
$write-stream:end:
|
2018-11-30 19:10:44 +00:00
|
|
|
# . epilog
|
2018-11-30 18:54:42 +00:00
|
|
|
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
|
|
|
|
5d/pop-to-EBP
|
|
|
|
c3/return
|
2018-11-24 22:00:55 +00:00
|
|
|
|
|
|
|
_write-stream: # fd : int, s : (address stream) -> <void>
|
2018-11-30 19:10:44 +00:00
|
|
|
# . prolog
|
2018-11-30 18:54:42 +00:00
|
|
|
55/push-EBP
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
2018-11-30 19:10:44 +00:00
|
|
|
# . save registers
|
2018-11-30 18:54:42 +00:00
|
|
|
50/push-EAX
|
|
|
|
51/push-ECX
|
|
|
|
52/push-EDX
|
|
|
|
53/push-EBX
|
|
|
|
56/push-ESI
|
|
|
|
57/push-EDI
|
|
|
|
# ESI = s
|
|
|
|
8b/copy 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none . 6/r32/ESI 0xc/disp8 . # copy *(EBP+12) to ESI
|
|
|
|
# EDI = s->read
|
|
|
|
8b/copy 1/mod/*+disp8 6/rm32/ESI . . . 7/r32/EDI 4/disp8 . # copy *(ESI+4) to EDI
|
|
|
|
# EDX = s->write
|
|
|
|
8b/copy 0/mod/indirect 6/rm32/ESI . . . 2/r32/EDX . . # copy *ESI to EDX
|
|
|
|
# syscall(write, fd, &s->data[s->read], s->write-s->read)
|
2018-12-01 00:45:15 +00:00
|
|
|
# . . fd : EBX
|
2018-11-30 18:54:42 +00:00
|
|
|
8b/copy 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none . 3/r32/EBX 8/disp8 . # copy *(EBP+8) to EBX
|
2018-12-01 00:45:15 +00:00
|
|
|
# . . data : ECX = &s->data[s->read]
|
2018-11-30 18:54:42 +00:00
|
|
|
8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/ESI 7/index/EDI . 1/r32/ECX 0xc/disp8 . # copy ESI+EDI+12 to ECX
|
2018-12-01 00:45:15 +00:00
|
|
|
# . . size : EDX = s->write - s->read
|
2018-11-30 18:54:42 +00:00
|
|
|
29/subtract 3/mod/direct 2/rm32/EDX . . . 7/r32/EDI . . # subtract EDI from EDX
|
2018-12-01 00:45:15 +00:00
|
|
|
# . . syscall
|
2018-11-30 18:54:42 +00:00
|
|
|
b8/copy-to-EAX 4/imm32/write
|
|
|
|
cd/syscall 0x80/imm8
|
2018-11-30 19:10:44 +00:00
|
|
|
# . restore registers
|
2018-11-30 18:54:42 +00:00
|
|
|
5f/pop-to-EDI
|
|
|
|
5e/pop-to-ESI
|
|
|
|
5b/pop-to-EBX
|
|
|
|
5a/pop-to-EDX
|
|
|
|
59/pop-to-ECX
|
|
|
|
58/pop-to-EAX
|
2018-11-30 19:10:44 +00:00
|
|
|
# . epilog
|
2018-11-30 18:54:42 +00:00
|
|
|
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
|
|
|
|
5d/pop-to-EBP
|
|
|
|
c3/return
|
2018-11-24 22:00:55 +00:00
|
|
|
|
|
|
|
test-write-stream-single:
|
2018-12-01 00:45:15 +00:00
|
|
|
# setup
|
|
|
|
# . clear-stream(_test-stream)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2018-11-30 18:54:42 +00:00
|
|
|
68/push _test-stream/imm32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . call
|
2018-11-30 18:54:42 +00:00
|
|
|
e8/call clear-stream/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2018-11-30 18:54:42 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
2018-12-01 00:45:15 +00:00
|
|
|
# . clear-stream(_test-stream2)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2018-11-30 18:54:42 +00:00
|
|
|
68/push _test-stream2/imm32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . call
|
2018-11-30 18:54:42 +00:00
|
|
|
e8/call clear-stream/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2018-11-30 18:54:42 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
2018-12-01 00:45:15 +00:00
|
|
|
# . write(_test-stream2, "Ab")
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2018-11-30 18:54:42 +00:00
|
|
|
68/push "Ab"/imm32
|
|
|
|
68/push _test-stream2/imm32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . call
|
2018-11-30 18:54:42 +00:00
|
|
|
e8/call write/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2018-11-30 18:54:42 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
|
|
|
# write-stream(_test-stream, _test-stream2)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2018-11-30 18:54:42 +00:00
|
|
|
68/push _test-stream2/imm32
|
|
|
|
68/push _test-stream/imm32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . call
|
2018-11-30 18:54:42 +00:00
|
|
|
e8/call write-stream/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2018-11-30 18:54:42 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
2018-12-02 22:41:21 +00:00
|
|
|
# check-ints-equal(*_test-stream->data, 41/A 62/b 00 00, msg)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2018-11-30 18:54:42 +00:00
|
|
|
68/push "F - test-write-stream-single"/imm32
|
|
|
|
68/push 0x006241/imm32/Ab
|
2018-12-02 22:41:21 +00:00
|
|
|
# . . push *_test-stream->data
|
2018-11-30 18:54:42 +00:00
|
|
|
b8/copy-to-EAX _test-stream/imm32
|
|
|
|
ff 6/subop/push 1/mod/*+disp8 0/rm32/EAX . . . . 0xc/disp8 . # push *(EAX+12)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . call
|
2018-11-30 18:54:42 +00:00
|
|
|
e8/call check-ints-equal/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2018-11-30 18:54:42 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
2018-12-01 00:45:15 +00:00
|
|
|
# . end
|
2018-11-30 18:54:42 +00:00
|
|
|
c3/return
|
2018-11-24 22:00:55 +00:00
|
|
|
|
|
|
|
test-write-stream-appends:
|
2018-12-01 00:45:15 +00:00
|
|
|
# setup
|
|
|
|
# . clear-stream(_test-stream)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2018-11-30 18:54:42 +00:00
|
|
|
68/push _test-stream/imm32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . call
|
2018-11-30 18:54:42 +00:00
|
|
|
e8/call clear-stream/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2018-11-30 18:54:42 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
2018-12-01 00:45:15 +00:00
|
|
|
# . clear-stream(_test-stream2)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2018-11-30 18:54:42 +00:00
|
|
|
68/push _test-stream2/imm32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . call
|
2018-11-30 18:54:42 +00:00
|
|
|
e8/call clear-stream/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2018-11-30 18:54:42 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
2018-12-01 00:45:15 +00:00
|
|
|
# . write(_test-stream2, "C")
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2018-11-30 18:54:42 +00:00
|
|
|
68/push "C"/imm32
|
|
|
|
68/push _test-stream2/imm32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . call
|
2018-11-30 18:54:42 +00:00
|
|
|
e8/call write/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2018-11-30 18:54:42 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
2018-12-01 00:45:15 +00:00
|
|
|
# first write
|
|
|
|
# . write-stream(_test-stream, _test-stream2)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2018-11-30 18:54:42 +00:00
|
|
|
68/push _test-stream2/imm32
|
|
|
|
68/push _test-stream/imm32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . call
|
2018-11-30 18:54:42 +00:00
|
|
|
e8/call write-stream/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2018-11-30 18:54:42 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
2018-12-01 00:45:15 +00:00
|
|
|
# second write
|
|
|
|
# . write(_test-stream2, "D")
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2018-11-30 18:54:42 +00:00
|
|
|
68/push "D"/imm32
|
|
|
|
68/push _test-stream2/imm32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . call
|
2018-11-30 18:54:42 +00:00
|
|
|
e8/call write/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2018-11-30 18:54:42 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
2018-12-01 00:45:15 +00:00
|
|
|
# . write-stream(_test-stream, _test-stream2)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2018-11-30 18:54:42 +00:00
|
|
|
68/push _test-stream2/imm32
|
|
|
|
68/push _test-stream/imm32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . call
|
2018-11-30 18:54:42 +00:00
|
|
|
e8/call write-stream/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2018-11-30 18:54:42 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
2018-12-02 22:41:21 +00:00
|
|
|
# check-ints-equal(*_test-stream->data, 43/C 44/D 00 00, msg)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2018-11-30 18:54:42 +00:00
|
|
|
68/push "F - test-write-stream-appends"/imm32
|
|
|
|
68/push 0x00004443/imm32/C-D
|
2018-12-02 22:41:21 +00:00
|
|
|
# . . push *_test-stream->data
|
2018-11-30 18:54:42 +00:00
|
|
|
b8/copy-to-EAX _test-stream/imm32
|
|
|
|
ff 6/subop/push 1/mod/*+disp8 0/rm32/EAX . . . . 0xc/disp8 . # push *(EAX+12)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . call
|
2018-11-30 18:54:42 +00:00
|
|
|
e8/call check-ints-equal/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2018-11-30 18:54:42 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
2018-12-01 00:45:15 +00:00
|
|
|
# . end
|
2018-11-30 18:54:42 +00:00
|
|
|
c3/return
|
2018-11-24 22:00:55 +00:00
|
|
|
|
|
|
|
== data
|
|
|
|
|
|
|
|
_test-stream2:
|
2018-11-30 18:54:42 +00:00
|
|
|
# current write index
|
|
|
|
04 00 00 00
|
|
|
|
# current read index
|
|
|
|
01 00 00 00
|
|
|
|
# length (= 8)
|
|
|
|
08 00 00 00
|
|
|
|
# data
|
|
|
|
41 42 43 44 00 00 00 00 # 8 bytes
|
2018-11-24 22:00:55 +00:00
|
|
|
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . vim:nowrap:textwidth=0
|