2018-11-18 08:17:29 +00:00
|
|
|
# read: analogously to write, support reading from in-memory streams in
|
|
|
|
# addition to file descriptors.
|
|
|
|
#
|
|
|
|
# We can pass it either a file descriptor or an address to a stream. If a
|
|
|
|
# file descriptor is passed in, we _read from it using the right syscall. If a
|
|
|
|
# stream is passed in (a fake file descriptor), we read from it instead. This
|
|
|
|
# lets us initialize input for tests.
|
|
|
|
#
|
|
|
|
# A little counter-intuitively, the output of 'read' ends up in.. a stream. So
|
|
|
|
# tests end up doing a redundant copy. Why? Well, consider the alternatives:
|
|
|
|
#
|
|
|
|
# a) Reading into a string, and returning a pointer to the end of the read
|
|
|
|
# region, or a count of bytes written. Now this count or end pointer must be
|
|
|
|
# managed separately by the caller, which can be error-prone.
|
|
|
|
#
|
|
|
|
# b) Having 'read' return a buffer that it allocates. But there's no way to
|
|
|
|
# know in advance how large to make the buffer. If you read less than the
|
|
|
|
# size of the buffer you again end up needing to manage initialized vs
|
|
|
|
# uninitialized memory.
|
|
|
|
#
|
|
|
|
# c) Creating more helpful variants like 'read-byte' or 'read-until' which
|
|
|
|
# also can take a file descriptor or stream, just like 'write'. But such
|
|
|
|
# primitives don't exist in the Linux kernel, so we'd be implementing them
|
|
|
|
# somehow, either with more internal buffering or by making multiple
|
|
|
|
# syscalls.
|
|
|
|
#
|
|
|
|
# Reading into a stream avoids these problems. The buffer is externally
|
|
|
|
# provided and the caller has control over where it's allocated, its lifetime,
|
|
|
|
# and so on. The buffer's read and write pointers are internal to it so it's
|
|
|
|
# easier to keep in a consistent state. And it can now be passed directly to
|
|
|
|
# helpers like 'read-byte' or 'read-until' that only need to support streams,
|
|
|
|
# never file descriptors.
|
|
|
|
#
|
|
|
|
# Like with 'write', 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.
|
|
|
|
#
|
|
|
|
# As a reminder, a stream looks like this:
|
|
|
|
# write: int # index at which to write to next
|
|
|
|
# read: int # index at which to read next
|
2020-04-03 19:35:53 +00:00
|
|
|
# data: (array byte) # prefixed by size as usual
|
2018-11-18 08:17:29 +00:00
|
|
|
|
|
|
|
== 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-18 08:17:29 +00:00
|
|
|
|
2020-01-27 08:36:44 +00:00
|
|
|
read: # f: fd or (addr stream byte), s: (addr stream byte) -> num-bytes-read/eax: int
|
2019-10-16 02:35:19 +00:00
|
|
|
# . prologue
|
2019-08-26 18:55:26 +00:00
|
|
|
55/push-ebp
|
|
|
|
89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
|
2018-12-01 00:45:15 +00:00
|
|
|
# if (f < 0x08000000) return _read(f, s) # f can't be a user-mode address, so treat it as a kernel file descriptor
|
2019-08-26 18:55:26 +00:00
|
|
|
81 7/subop/compare 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 0x08000000/imm32 # compare *(ebp+8)
|
2020-01-17 02:31:12 +00:00
|
|
|
73/jump-if-addr>= $read:fake/disp8
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2019-08-26 18:55:26 +00:00
|
|
|
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)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . call
|
2018-11-30 18:54:42 +00:00
|
|
|
e8/call _read/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
|
2018-11-30 18:54:42 +00:00
|
|
|
# return
|
|
|
|
eb/jump $read:end/disp8
|
2018-11-18 08:17:29 +00:00
|
|
|
$read:fake:
|
2018-12-01 00:45:15 +00:00
|
|
|
# otherwise, treat 'f' as a stream to scan from
|
2018-11-30 19:10:44 +00:00
|
|
|
# . save registers
|
2019-08-26 18:55:26 +00:00
|
|
|
56/push-esi
|
|
|
|
57/push-edi
|
|
|
|
# esi = f
|
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi
|
|
|
|
# edi = s
|
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 7/r32/edi 0xc/disp8 . # copy *(ebp+12) to esi
|
2020-04-03 19:35:53 +00:00
|
|
|
# eax = _buffer-4(out = &s->data[s->write], outend = &s->data[s->size],
|
2018-11-30 18:54:42 +00:00
|
|
|
# in = &f->data[f->read], inend = &f->data[f->write])
|
2018-12-01 00:45:15 +00:00
|
|
|
# . . push &f->data[f->write]
|
2019-08-26 18:55:26 +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
|
|
|
|
50/push-eax
|
2018-12-01 00:45:15 +00:00
|
|
|
# . . push &f->data[f->read]
|
2019-08-26 18:55:26 +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
|
|
|
|
50/push-eax
|
2020-04-03 19:35:53 +00:00
|
|
|
# . . push &s->data[s->size]
|
2019-08-26 18:55:26 +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
|
|
|
|
50/push-eax
|
2018-12-01 00:45:15 +00:00
|
|
|
# . . push &s->data[s->write]
|
2019-08-26 18:55:26 +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
|
|
|
|
50/push-eax
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . call
|
2019-04-11 00:22:00 +00:00
|
|
|
e8/call _buffer-4/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp
|
|
|
|
# s->write += eax
|
|
|
|
01/add 0/mod/indirect 7/rm32/edi . . . 0/r32/eax . . # add eax to *edi
|
|
|
|
# f->read += eax
|
|
|
|
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
|
2019-08-26 18:55:26 +00:00
|
|
|
5f/pop-to-edi
|
|
|
|
5e/pop-to-esi
|
2018-11-18 08:17:29 +00:00
|
|
|
$read:end:
|
2019-10-16 02:35:19 +00:00
|
|
|
# . epilogue
|
2019-08-26 18:55:26 +00:00
|
|
|
89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
|
|
|
|
5d/pop-to-ebp
|
2018-11-30 18:54:42 +00:00
|
|
|
c3/return
|
2018-11-18 08:17:29 +00:00
|
|
|
|
2018-12-01 00:45:15 +00:00
|
|
|
# - helpers
|
2018-11-18 08:17:29 +00:00
|
|
|
|
2019-04-11 00:22:00 +00:00
|
|
|
# '_buffer' is like '_append', but silently stops instead of aborting when it runs out of space
|
|
|
|
|
|
|
|
# 3-argument variant of _buffer
|
2020-01-27 08:36:44 +00:00
|
|
|
_buffer-3: # out: address, outend: address, s: (array byte) -> num_bytes_buffered/eax
|
2019-10-16 02:35:19 +00:00
|
|
|
# . prologue
|
2019-08-26 18:55:26 +00:00
|
|
|
55/push-ebp
|
|
|
|
89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
|
2019-04-11 00:22:00 +00:00
|
|
|
# . save registers
|
2019-08-26 18:55:26 +00:00
|
|
|
51/push-ecx
|
2020-04-03 19:35:53 +00:00
|
|
|
# eax = _buffer-4(out, outend, &s->data[0], &s->data[s->size])
|
|
|
|
# . . push &s->data[s->size]
|
2019-08-26 18:55:26 +00:00
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/ebp . . 0/r32/eax 0x10/disp8 . # copy *(ebp+16) to eax
|
|
|
|
8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx
|
|
|
|
8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx
|
|
|
|
51/push-ecx
|
2019-04-11 00:22:00 +00:00
|
|
|
# . . push &s->data[0]
|
2019-08-26 18:55:26 +00:00
|
|
|
8d/copy-address 1/mod/*+disp8 0/rm32/eax . . . 1/r32/ecx 4/disp8 . # copy eax+4 to ecx
|
|
|
|
51/push-ecx
|
2019-04-11 00:22:00 +00:00
|
|
|
# . . push outend
|
2019-08-26 18:55:26 +00:00
|
|
|
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12)
|
2019-04-11 00:22:00 +00:00
|
|
|
# . . push out
|
2019-08-26 18:55:26 +00:00
|
|
|
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
|
2019-04-11 00:22:00 +00:00
|
|
|
# . . call
|
|
|
|
e8/call _buffer-4/disp32
|
|
|
|
# . . discard args
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp
|
2019-04-11 00:22:00 +00:00
|
|
|
$_buffer-3:end:
|
|
|
|
# . restore registers
|
2019-08-26 18:55:26 +00:00
|
|
|
59/pop-to-ecx
|
2019-10-16 02:35:19 +00:00
|
|
|
# . epilogue
|
2019-08-26 18:55:26 +00:00
|
|
|
89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
|
|
|
|
5d/pop-to-ebp
|
2019-04-11 00:22:00 +00:00
|
|
|
c3/return
|
|
|
|
|
|
|
|
# 4-argument variant of _buffer
|
2020-01-27 08:36:44 +00:00
|
|
|
_buffer-4: # out: address, outend: address, in: address, inend: address -> num_bytes_buffered/eax
|
2019-10-16 02:35:19 +00:00
|
|
|
# . prologue
|
2019-08-26 18:55:26 +00:00
|
|
|
55/push-ebp
|
|
|
|
89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
|
2019-04-11 00:22:00 +00:00
|
|
|
# . save registers
|
2019-08-26 18:55:26 +00:00
|
|
|
51/push-ecx
|
|
|
|
52/push-edx
|
|
|
|
53/push-ebx
|
|
|
|
56/push-esi
|
|
|
|
57/push-edi
|
|
|
|
# eax/num_bytes_buffered = 0
|
|
|
|
b8/copy-to-eax 0/imm32
|
|
|
|
# edi = out
|
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 7/r32/edi 8/disp8 . # copy *(ebp+8) to edi
|
|
|
|
# edx = outend
|
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 2/r32/edx 0xc/disp8 . # copy *(ebp+12) to edx
|
|
|
|
# esi = in
|
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 0x10/disp8 . # copy *(ebp+16) to esi
|
|
|
|
# ecx = inend
|
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 1/r32/ecx 0x14/disp8 . # copy *(ebp+20) to ecx
|
2019-04-11 00:22:00 +00:00
|
|
|
$_buffer-4:loop:
|
|
|
|
# if (in >= inend) break
|
2019-08-26 18:55:26 +00:00
|
|
|
39/compare 3/mod/direct 6/rm32/esi . . . 1/r32/ecx . . # compare esi with ecx
|
2020-01-17 02:31:12 +00:00
|
|
|
73/jump-if-addr>= $_buffer-4:end/disp8
|
2019-04-11 00:22:00 +00:00
|
|
|
# if (out >= outend) break # for now silently ignore filled up buffer
|
2019-08-26 18:55:26 +00:00
|
|
|
39/compare 3/mod/direct 7/rm32/edi . . . 2/r32/edx . . # compare edi with edx
|
2020-01-17 02:31:12 +00:00
|
|
|
73/jump-if-addr>= $_buffer-4:end/disp8
|
2019-04-11 00:22:00 +00:00
|
|
|
# *out = *in
|
2019-08-26 18:55:26 +00:00
|
|
|
8a/copy-byte 0/mod/indirect 6/rm32/esi . . . 3/r32/BL . . # copy byte at *esi to BL
|
|
|
|
88/copy-byte 0/mod/indirect 7/rm32/edi . . . 3/r32/BL . . # copy byte at BL to *edi
|
2019-04-11 00:22:00 +00:00
|
|
|
# ++num_bytes_buffered
|
2019-08-26 18:55:26 +00:00
|
|
|
40/increment-eax
|
2019-04-11 00:22:00 +00:00
|
|
|
# ++in
|
2019-08-26 18:55:26 +00:00
|
|
|
46/increment-esi
|
2019-04-11 00:22:00 +00:00
|
|
|
# ++out
|
2019-08-26 18:55:26 +00:00
|
|
|
47/increment-edi
|
2019-04-11 00:22:00 +00:00
|
|
|
eb/jump $_buffer-4:loop/disp8
|
|
|
|
$_buffer-4:end:
|
|
|
|
# . restore registers
|
2019-08-26 18:55:26 +00:00
|
|
|
5f/pop-to-edi
|
|
|
|
5e/pop-to-esi
|
|
|
|
5b/pop-to-ebx
|
|
|
|
5a/pop-to-edx
|
|
|
|
59/pop-to-ecx
|
2019-10-16 02:35:19 +00:00
|
|
|
# . epilogue
|
2019-08-26 18:55:26 +00:00
|
|
|
89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
|
|
|
|
5d/pop-to-ebp
|
2019-04-11 00:22:00 +00:00
|
|
|
c3/return
|
|
|
|
|
2018-12-02 22:41:21 +00:00
|
|
|
# idea: a clear-if-empty method on streams that clears only if f->read == f->write
|
2018-11-18 08:17:29 +00:00
|
|
|
# Unclear how I'd use it, though. Callers seem to need the check anyway.
|
|
|
|
# Maybe a better helper would be 'empty-stream?'
|
|
|
|
|
2020-01-27 08:36:44 +00:00
|
|
|
_read: # fd: int, s: (addr stream byte) -> num-bytes-read/eax: int
|
2019-10-16 02:35:19 +00:00
|
|
|
# . prologue
|
2019-08-26 18:55:26 +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
|
2019-08-26 18:55:26 +00:00
|
|
|
51/push-ecx
|
|
|
|
52/push-edx
|
|
|
|
53/push-ebx
|
|
|
|
56/push-esi
|
|
|
|
# esi = s
|
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 0xc/disp8 . # copy *(ebp+12) to esi
|
|
|
|
# eax = s->write
|
|
|
|
8b/copy 0/mod/indirect 6/rm32/esi . . . 0/r32/eax . . # copy *esi to eax
|
2020-04-03 19:35:53 +00:00
|
|
|
# edx = s->size
|
2019-08-26 18:55:26 +00:00
|
|
|
8b/copy 1/mod/*+disp8 6/rm32/esi . . . 2/r32/edx 8/disp8 . # copy *(esi+8) to edx
|
2021-07-20 14:30:59 +00:00
|
|
|
# syscall_read(fd, &s->data[s->write], s->size - s->write)
|
2020-01-27 08:36:44 +00:00
|
|
|
# . . fd: ebx
|
2019-08-26 18:55:26 +00:00
|
|
|
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 3/r32/ebx 8/disp8 . # copy *(ebp+8) to ebx
|
2020-01-27 08:36:44 +00:00
|
|
|
# . . data: ecx = &s->data[s->write]
|
2019-08-26 18:55:26 +00:00
|
|
|
8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/esi 0/index/eax . 1/r32/ecx 0xc/disp8 . # copy esi+eax+12 to ecx
|
2020-04-03 19:35:53 +00:00
|
|
|
# . . size: edx = s->size - s->write
|
2019-08-26 18:55:26 +00:00
|
|
|
29/subtract 3/mod/direct 2/rm32/edx . . . 0/r32/eax . . # subtract eax from edx
|
2018-12-01 00:45:15 +00:00
|
|
|
# . . syscall
|
2020-06-11 06:09:30 +00:00
|
|
|
e8/call syscall_read/disp32
|
2019-08-26 18:55:26 +00:00
|
|
|
# add the result eax to s->write
|
|
|
|
01/add 0/mod/indirect 6/rm32/esi . . . 0/r32/eax . . # add eax to *esi
|
2018-12-04 08:29:06 +00:00
|
|
|
$_read:end:
|
2018-11-30 19:10:44 +00:00
|
|
|
# . restore registers
|
2019-08-26 18:55:26 +00:00
|
|
|
5e/pop-to-esi
|
|
|
|
5b/pop-to-ebx
|
|
|
|
5a/pop-to-edx
|
|
|
|
59/pop-to-ecx
|
2019-10-16 02:35:19 +00:00
|
|
|
# . epilogue
|
2019-08-26 18:55:26 +00:00
|
|
|
89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
|
|
|
|
5d/pop-to-ebp
|
2018-11-30 18:54:42 +00:00
|
|
|
c3/return
|
2018-11-18 08:17:29 +00:00
|
|
|
|
2018-11-30 18:54:42 +00:00
|
|
|
# Two options:
|
|
|
|
# 1 (what we have above):
|
2019-08-26 18:55:26 +00:00
|
|
|
# ecx = s
|
|
|
|
# eax = s->write
|
2020-04-03 19:35:53 +00:00
|
|
|
# edx = s->size
|
2018-11-30 18:54:42 +00:00
|
|
|
# # syscall
|
2019-08-26 18:55:26 +00:00
|
|
|
# ecx = lea ecx+eax+12
|
|
|
|
# edx = sub edx eax
|
2018-11-30 18:54:42 +00:00
|
|
|
#
|
|
|
|
# 2:
|
2019-08-26 18:55:26 +00:00
|
|
|
# ecx = s
|
2020-04-03 19:35:53 +00:00
|
|
|
# edx = s->size
|
2019-08-26 18:55:26 +00:00
|
|
|
# ecx = &s->data
|
2018-11-30 18:54:42 +00:00
|
|
|
# # syscall
|
2019-08-26 18:55:26 +00:00
|
|
|
# ecx = add ecx, s->write
|
|
|
|
# edx = sub edx, s->write
|
2018-11-30 18:54:42 +00:00
|
|
|
#
|
|
|
|
# Not much to choose between the two? Option 2 performs a duplicate load to
|
2019-08-26 18:55:26 +00:00
|
|
|
# use one less register, but doesn't increase the amount of spilling (ecx
|
|
|
|
# and edx must be used, and eax must be clobbered anyway).
|
2018-11-18 08:17:29 +00:00
|
|
|
|
2018-12-01 00:45:15 +00:00
|
|
|
# - tests
|
2018-11-18 08:17:29 +00:00
|
|
|
|
|
|
|
test-read-single:
|
2019-04-10 22:00:57 +00:00
|
|
|
# - write a single character into _test-stream, then read from it
|
2018-11-30 18:54:42 +00:00
|
|
|
# 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
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
|
2019-02-11 00:54:20 +00:00
|
|
|
# clear-stream(_test-tmp-stream)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2019-02-11 00:54:20 +00:00
|
|
|
68/push _test-tmp-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
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
|
2018-11-30 18:54:42 +00:00
|
|
|
# write(_test-stream, "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-stream/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
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
|
|
|
|
# eax = read(_test-stream, _test-tmp-stream)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2019-02-11 00:54:20 +00:00
|
|
|
68/push _test-tmp-stream/imm32
|
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 read/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
|
|
|
|
# check-ints-equal(eax, 2, msg)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2019-08-26 18:54:29 +00:00
|
|
|
68/push "F - test-read-single: return value"/imm32
|
2018-11-30 18:54:42 +00:00
|
|
|
68/push 2/imm32
|
2019-08-26 18:55:26 +00:00
|
|
|
50/push-eax
|
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
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
|
2019-02-11 00:54:20 +00:00
|
|
|
# check-stream-equal(_test-tmp-stream, "Ab", msg)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2018-11-30 18:54:42 +00:00
|
|
|
68/push "F - test-read-single"/imm32
|
2019-02-04 07:31:03 +00:00
|
|
|
68/push "Ab"/imm32
|
2019-02-11 00:54:20 +00:00
|
|
|
68/push _test-tmp-stream/imm32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . call
|
2019-02-04 07:31:03 +00:00
|
|
|
e8/call check-stream-equal/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
|
2018-11-30 18:54:42 +00:00
|
|
|
# end
|
|
|
|
c3/return
|
2018-11-18 08:17:29 +00:00
|
|
|
|
|
|
|
test-read-is-stateful:
|
2018-12-01 00:45:15 +00:00
|
|
|
# - make two consecutive reads, check that their results are appended
|
2018-11-30 18:54:42 +00:00
|
|
|
# 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
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
|
2019-02-11 00:54:20 +00:00
|
|
|
# clear-stream(_test-tmp-stream)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2019-02-11 00:54:20 +00:00
|
|
|
68/push _test-tmp-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
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
|
2018-11-30 18:54:42 +00:00
|
|
|
# write(_test-stream, "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-stream/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
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
|
2019-02-11 00:54:20 +00:00
|
|
|
# read(_test-stream, _test-tmp-stream)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2019-02-11 00:54:20 +00:00
|
|
|
68/push _test-tmp-stream/imm32
|
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 read/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
|
2018-11-30 18:54:42 +00:00
|
|
|
# write(_test-stream, "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-stream/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
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
|
2019-02-11 00:54:20 +00:00
|
|
|
# read(_test-stream, _test-tmp-stream)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2019-02-11 00:54:20 +00:00
|
|
|
68/push _test-tmp-stream/imm32
|
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 read/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
|
2019-02-11 00:54:20 +00:00
|
|
|
# check-stream-equal(_test-tmp-stream, "CD", msg)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2018-11-30 18:54:42 +00:00
|
|
|
68/push "F - test-read-is-stateful"/imm32
|
2019-02-04 07:31:03 +00:00
|
|
|
68/push "CD"/imm32
|
2019-02-11 00:54:20 +00:00
|
|
|
68/push _test-tmp-stream/imm32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . call
|
2019-02-04 07:31:03 +00:00
|
|
|
e8/call check-stream-equal/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
|
2018-11-30 18:54:42 +00:00
|
|
|
# end
|
|
|
|
c3/return
|
2018-11-18 08:17:29 +00:00
|
|
|
|
|
|
|
test-read-returns-0-on-end-of-file:
|
2018-12-01 00:45:15 +00:00
|
|
|
# - read after hitting end-of-file, check that result is 0
|
|
|
|
# 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
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
|
2019-02-11 00:54:20 +00:00
|
|
|
# . clear-stream(_test-tmp-stream)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2019-02-11 00:54:20 +00:00
|
|
|
68/push _test-tmp-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
|
2019-08-26 18:55:26 +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-stream, "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-stream/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
|
2019-08-26 18:55:26 +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 read gets to end-of-file
|
2019-02-11 00:54:20 +00:00
|
|
|
# . read(_test-stream, _test-tmp-stream)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2019-02-11 00:54:20 +00:00
|
|
|
68/push _test-tmp-stream/imm32
|
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 read/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2019-08-26 18:55:26 +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 read
|
2019-02-11 00:54:20 +00:00
|
|
|
# . read(_test-stream, _test-tmp-stream)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2019-02-11 00:54:20 +00:00
|
|
|
68/push _test-tmp-stream/imm32
|
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 read/disp32
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . discard args
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
|
|
|
|
# check-ints-equal(eax, 0, msg)
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . push args
|
2018-11-30 18:54:42 +00:00
|
|
|
68/push "F - test-read-returns-0-on-end-of-file"/imm32
|
|
|
|
68/push 0/imm32
|
2019-08-26 18:55:26 +00:00
|
|
|
50/push-eax
|
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
|
2019-08-26 18:55:26 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
|
2018-11-30 18:54:42 +00:00
|
|
|
# end
|
|
|
|
c3/return
|
2018-11-18 08:17:29 +00:00
|
|
|
|
|
|
|
== data
|
|
|
|
|
2020-01-12 20:11:15 +00:00
|
|
|
_test-tmp-stream: # (stream byte)
|
2018-11-30 18:54:42 +00:00
|
|
|
# current write index
|
2019-02-16 01:20:57 +00:00
|
|
|
0/imm32
|
2018-11-30 18:54:42 +00:00
|
|
|
# current read index
|
2019-02-16 01:20:57 +00:00
|
|
|
0/imm32
|
2020-04-03 19:35:53 +00:00
|
|
|
# size
|
2019-02-16 01:20:57 +00:00
|
|
|
8/imm32
|
2018-11-30 18:54:42 +00:00
|
|
|
# data
|
|
|
|
00 00 00 00 00 00 00 00 # 8 bytes
|
2018-11-22 06:25:26 +00:00
|
|
|
|
2018-11-30 19:10:44 +00:00
|
|
|
# . . vim:nowrap:textwidth=0
|