about summary refs log tree commit diff stats
path: root/linux/119error-byte.subx
blob: 0e4f1c59eba439eb10bc2f8b122169960ed58ba4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Print an error message followed by the text representation of a byte. Then exit.

== 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

#? Entry:  # manual test
#?     # . var ed/eax: exit-descriptor
#?     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # subtract from esp
#?     89/copy                         3/mod/direct    0/rm32/eax    .           .             .           4/r32/esp   .               .                 # copy esp to eax
#?     # . configure ed to really exit()
#?     # . . ed->target = 0
#?     c7          0/subop/copy        0/mod/direct    0/rm32/eax    .           .             .           .           .               0/imm32           # copy to *eax
#?     # . error-byte(ed, Stdout, msg, 34)
#?     68/push  0x34/imm32
#?     68/push  "abc"/imm32
#?     68/push  Stderr/imm32
#?     50/push-eax
#?     e8/call  error-byte/disp32
#?     # . 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
#?     e8/call  syscall_exit/disp32

# write(out, "Error: "+msg+": "+byte) then stop(ed, 1)
error-byte:  # ed: (addr exit-descriptor), out: (addr buffered-file), msg: (addr array byte), n: byte
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # write-buffered(out, "Error: ")
    # . . push args
    68/push  "Error: "/imm32
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
    # . . call
    e8/call  write-buffered/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # write-buffered(out, msg)
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
    # . . call
    e8/call  write-buffered/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # write-buffered(out, ": ")
    # . . push args
    68/push  ": "/imm32
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
    # . . call
    e8/call  write-buffered/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # write-byte-hex-buffered(out, byte)
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x14/disp8      .                 # push *(ebp+20)
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
    # . . call
    e8/call  write-byte-hex-buffered/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # write-buffered(out, Newline)
    # . . push args
    68/push  Newline/imm32
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
    # . . call
    e8/call  write-buffered/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # flush(out)
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
    # . . call
    e8/call  flush/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # stop(ed, 1)
    # . . push args
    68/push  1/imm32
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
    # . . call
    e8/call  stop/disp32
    # should never get past this point
$error-byte:dead-end:
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

# . . vim:nowrap:textwidth=0
tend/ebx: (addr byte) = out + n 8b/-> *(ebp+0x10) 3/r32/ebx 8d/copy-address *(edx+ebx) 3/r32/ebx # eax = in 8b/-> *(ebp+0xc) 0/r32/eax # var inend/ecx: (addr byte) = in + n 8b/-> *(ebp+0x10) 1/r32/ecx 8d/copy-address *(eax+ecx) 1/r32/ecx # (_append-4 %edx %ebx %eax %ecx) # => eax # s->write += n 8b/-> *(ebp+0x10) 1/r32/ecx 01/add-to *edi 1/r32/ecx $write-to-stream:end: # . restore registers 5f/pop-to-edi 5b/pop-to-ebx 5a/pop-to-edx 59/pop-to-ecx 58/pop-to-eax # . epilogue 89/<- %esp 5/r32/ebp 5d/pop-to-ebp c3/return $write-to-stream:abort: (write-buffered Stderr "write-to-stream: stream full\n") (flush Stderr) bb/copy-to-ebx 1/imm32 (syscall_exit) # never gets here read-from-stream: # s: (addr stream _), out: (addr byte), n: int # . prologue 55/push-ebp 89/<- %ebp 4/r32/esp # . save registers 50/push-eax 51/push-ecx 52/push-edx 53/push-ebx 56/push-esi # esi = s 8b/-> *(ebp+8) 6/r32/esi # var sread/edx: int = s->read 8b/-> *(esi+4) 2/r32/edx # if (sread + n > s->write) abort 8b/-> *(ebp+0x10) 1/r32/ecx 01/add-to %ecx 2/r32/edx 3b/compare 1/r32/ecx *esi 0f 8f/jump-if-> $read-from-stream:abort/disp32 # var in/edx: (addr byte) = s->data + s->read 8d/copy-address *(esi+edx+0xc) 2/r32/edx # var inend/ebx: (addr byte) = in + n 8b/-> *(ebp+0x10) 3/r32/ebx 8d/copy-address *(edx+ebx) 3/r32/ebx # eax = out 8b/-> *(ebp+0xc) 0/r32/eax # var outend/ecx: (addr byte) = out + n 8b/-> *(ebp+0x10) 1/r32/ecx 8d/copy-address *(eax+ecx) 1/r32/ecx # (_append-4 %eax %ecx %edx %ebx) # => eax # s->read += n 8b/-> *(ebp+0x10) 1/r32/ecx 01/add-to *(esi+4) 1/r32/ecx $read-from-stream:end: # . restore registers 5e/pop-to-esi 5b/pop-to-ebx 5a/pop-to-edx 59/pop-to-ecx 58/pop-to-eax # . epilogue 89/<- %esp 5/r32/ebp 5d/pop-to-ebp c3/return $read-from-stream:abort: (write-buffered Stderr "read-from-stream: stream empty\n") (flush Stderr) bb/copy-to-ebx 1/imm32 (syscall_exit) # never gets here stream-first: # s: (addr stream byte) -> result/eax: byte # . prologue 55/push-ebp 89/<- %ebp 4/r32/esp # . save registers 51/push-ecx 56/push-esi # result = false b8/copy-to-eax 0/imm32 # esi = s 8b/-> *(ebp+8) 6/r32/esi # var idx/ecx: int = s->read 8b/-> *(esi+4) 1/r32/ecx # if idx >= s->write return 0 3b/compare-with 1/r32/ecx *esi 7d/jump-if->= $stream-first:end/disp8 # result = s->data[idx] 8a/byte-> *(esi+ecx+0xc) 0/r32/AL $stream-first:end: # . restore registers 5e/pop-to-esi 59/pop-to-ecx # . epilogue 89/<- %esp 5/r32/ebp 5d/pop-to-ebp c3/return stream-final: # s: (addr stream byte) -> result/eax: byte # . prologue 55/push-ebp 89/<- %ebp 4/r32/esp # . save registers 51/push-ecx 56/push-esi # result = false b8/copy-to-eax 0/imm32 # esi = s 8b/-> *(ebp+8) 6/r32/esi # var max/ecx: int = s->write 8b/-> *esi 1/r32/ecx # if s->read >= max return 0 39/compare-with *(esi+4) 1/r32/ecx 7d/jump-if->= $stream-final:end/disp8 # var idx/ecx: int = max - 1 49/decrement-ecx # result = s->data[idx] 8a/byte-> *(esi+ecx+0xc) 0/r32/AL $stream-final:end: # . restore registers 5e/pop-to-esi 59/pop-to-ecx # . epilogue 89/<- %esp 5/r32/ebp 5d/pop-to-ebp c3/return