https://github.com/akkartik/mu/blob/master/apps/survey.subx
   1 # Assign addresses (co-ordinates) to instructions (landmarks) in a program
   2 # (landscape).
   3 # Use the addresses assigned to:
   4 #   a) replace labels
   5 #   b) add segment headers with addresses and offsets correctly filled in
   6 #
   7 # To build:
   8 #   $ ./bootstrap translate init.linux 0*.subx apps/subx-params.subx apps/survey.subx  -o apps/survey
   9 #
  10 # The expected input is a stream of bytes with segment headers, comments and
  11 # some interspersed labels.
  12 #   $ cat x
  13 #   == code 0x1
  14 #   l1:
  15 #   aa bb l1/imm8
  16 #   cc dd l2/disp32
  17 #   l2:
  18 #   ee foo/imm32
  19 #   == data 0x10
  20 #   foo:
  21 #     00
  22 #
  23 # The output is the stream of bytes without segment headers or label definitions,
  24 # and with label references replaced with numeric values/displacements.
  25 #
  26 #   $ cat x  |./bootstrap run apps/survey
  27 #   ...ELF header bytes...
  28 #   # ELF header above will specify that code segment begins at this offset
  29 #   aa bb nn  # some computed address
  30 #   cc dd nn nn nn nn  # some computed displacement
  31 #   ee nn nn nn nn  # some computed address
  32 #   # ELF header above will specify that data segment begins at this offset
  33 #   00
  34 #
  35 # The ELF format has some persnickety constraints on the starting addresses of
  36 # segments, so input headers are treated as guidelines and adjusted in the
  37 # output.
  38 
  39 == code
  40 #   instruction                     effective address                                                   register    displacement    immediate
  41 # . op          subop               mod             rm32          base        index         scale       r32
  42 # . 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
  43 
  44 Entry:  # run tests if necessary, convert stdin if not
  45     # . prologue
  46     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
  47 
  48     # Heap = new-segment(Heap-size)
  49     # . . push args
  50     68/push  Heap/imm32
  51     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Heap-size/disp32                  # push *Heap-size
  52     # . . call
  53     e8/call  new-segment/disp32
  54     # . . discard args
  55     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
  56     # initialize-trace-stream(Trace-size)
  57     # . . push args
  58     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-size/disp32                 # push *Heap-size
  59     # . . call
  60     e8/call  initialize-trace-stream/disp32
  61     # . . discard args
  62     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
  63 
  64     # - if argc > 1 and argv[1] == "test", then return run_tests()
  65     # if (argc <= 1) goto interactive
  66     81          7/subop/compare     1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0/disp8         1/imm32           # compare *ebp
  67     7e/jump-if-<=  $subx-survey-main:interactive/disp8
  68     # if (!kernel-string-equal?(argv[1], "test")) goto interactive
  69     # . eax = kernel-string-equal?(argv[1], "test")
  70     # . . push args
  71     68/push  "test"/imm32
  72     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
  73     # . . call
  74     e8/call  kernel-string-equal?/disp32
  75     # . . discard args
  76     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
  77     # . if (eax == false) goto interactive
  78     3d/compare-eax-and  0/imm32/false
  79     74/jump-if-=  $subx-survey-main:interactive/disp8
  80     # run-tests()
  81     e8/call  run-tests/disp32
  82     # syscall(exit, *Num-test-failures)
  83     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/ebx   Num-test-failures/disp32          # copy *Num-test-failures to ebx
  84     eb/jump  $subx-survey-main:end/disp8
  85 $subx-survey-main:interactive:
  86     # - otherwise convert stdin
  87     # subx-survey(Stdin, Stdout)
  88     # . . push args
  89     68/push  Stdout/imm32
  90     68/push  Stdin/imm32
  91     # . . call
  92     e8/call  subx-survey/disp32
  93     # . . discard args
  94     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
  95 #?     # . write-stream(2/stderr, Trace-stream)
  96 #?     # . . push args
  97 #?     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
  98 #?     68/push  2/imm32/stderr
  99 #?     # . . call
 100 #?     e8/call  write-stream/disp32
 101 #?     # . . discard args
 102 #?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 103     # syscall(exit, 0)
 104     bb/copy-to-ebx  0/imm32
 105 $subx-survey-main:end:
 106     e8/call  syscall_exit/disp32
 107 
 108 # data structures:
 109 #   segment-info: {address, file-offset, size}                                  (12 bytes)
 110 #   segments: (addr stream {(handle array byte), segment-info})                 (20 bytes per row)
 111 #   label-info: {segment-name: (handle array byte), segment-offset, address}    (16 bytes)
 112 #   labels: (addr stream {(handle array byte), label-info})                     (24 bytes per row)
 113 # these are all inefficient, using sequential scans for lookups
 114 
 115 subx-survey:  # infile: (addr buffered-file), out: (addr buffered-file)
 116     # pseudocode
 117     #   var in: (stream byte Input-size)
 118     #   slurp(infile, in)
 119     #   var segments: (stream {segment-name, segment-info})
 120     #   var labels: (stream {label-name, label-info} Max-labels)
 121     #   compute-offsets(in, segments, labels)
 122     #   compute-addresses(segments, labels)
 123     #   rewind-stream(in)
 124     #   emit-output(in, out, segments, labels)
 125     #
 126     # . prologue
 127     55/push-ebp
 128     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 129     # . save registers
 130     51/push-ecx
 131     52/push-edx
 132     56/push-esi
 133     # var segments/ecx: (stream {string, segment-info} 200)   # 10 rows * 20 bytes/row
 134     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc8/imm32        # subtract from esp
 135     68/push  0xc8/imm32/size
 136     68/push  0/imm32/read
 137     68/push  0/imm32/write
 138     89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
 139     # var labels/edx: (stream label-info Max-labels)
 140     # . data
 141     2b/subtract                     0/mod/indirect  5/rm32/.disp32            .             .           4/r32/esp   Max-labels/disp32                 # subtract *Max-labels from esp
 142     # . size
 143     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Max-labels/disp32                 # push *Max-labels
 144     # . read
 145     68/push  0/imm32/read
 146     # . write
 147     68/push  0/imm32/write
 148     89/copy                         3/mod/direct    2/rm32/edx    .           .             .           4/r32/esp   .               .                 # copy esp to edx
 149     # var in/esi: (stream byte Input-size)
 150     # . data
 151     2b/subtract                     0/mod/indirect  5/rm32/.disp32            .             .           4/r32/esp   Input-size/disp32                 # subtract *Input-size from esp
 152     # . size
 153     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Input-size/disp32                 # push *Input-size
 154     # . read
 155     68/push  0/imm32/read
 156     # . write
 157     68/push  0/imm32/write
 158     89/copy                         3/mod/direct    6/rm32/esi    .           .             .           4/r32/esp   .               .                 # copy esp to esi
 159     # slurp(infile, in)
 160     # . . push args
 161     56/push-esi
 162     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
 163     # . . call
 164     e8/call  slurp/disp32
 165     # . . discard args
 166     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 167     # compute-offsets(in, segments, labels)
 168     # . . push args
 169     52/push-edx
 170     51/push-ecx
 171     56/push-esi
 172     # . . call
 173     e8/call  compute-offsets/disp32
 174     # . . discard args
 175     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
 176     # compute-addresses(segments, labels)
 177     # . . push args
 178     52/push-edx
 179     51/push-ecx
 180     # . . call
 181     e8/call  compute-addresses/disp32
 182     # . . discard args
 183     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 184     # rewind-stream(in)
 185     # . . push args
 186     56/push-esi
 187     # . . call
 188     e8/call  rewind-stream/disp32
 189     # . . discard args
 190     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 191     # emit-output(in, out, segments, labels)
 192     # . . push args
 193     52/push-edx
 194     51/push-ecx
 195     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
 196     56/push-esi
 197     # . . call
 198     e8/call  emit-output/disp32
 199     # . . discard args
 200     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
 201     # flush(out)
 202     # . . push args
 203     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
 204     # . . call
 205     e8/call  flush/disp32
 206     # . . discard args
 207     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 208 $subx-survey:end:
 209     # . reclaim locals
 210     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xec/imm32        # add to esp
 211     03/add                          0/mod/indirect  5/rm32/.disp32            .             .           4/r32/esp   Max-labels/disp32                 # add *Max-labels to esp
 212     03/add                          0/mod/indirect  5/rm32/.disp32            .             .           4/r32/esp   Input-size/disp32                 # add *Input-size to esp
 213     # . restore registers
 214     5e/pop-to-esi
 215     5a/pop-to-edx
 216     59/pop-to-ecx
 217     # . epilogue
 218     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
 219     5d/pop-to-ebp
 220     c3/return
 221 
 222 test-subx-survey-computes-addresses:
 223     # input:
 224     #   == code 0x1
 225     #   Entry:
 226     #   ab x/imm32
 227     #   == data 0x1000
 228     #   x:
 229     #     01
 230     #
 231     # trace contains (in any order):
 232     #   label x is at address 0x1079
 233     #   segment code starts at address 0x74
 234     #   segment code has size 5
 235     #   segment data starts at address 0x1079
 236     #
 237     # . prologue
 238     55/push-ebp
 239     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 240     # setup
 241     # . clear-stream(_test-input-stream)
 242     # . . push args
 243     68/push  _test-input-stream/imm32
 244     # . . call
 245     e8/call  clear-stream/disp32
 246     # . . discard args
 247     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 248     # . clear-stream($_test-input-buffered-file->buffer)
 249     # . . push args
 250     68/push  $_test-input-buffered-file->buffer/imm32
 251     # . . call
 252     e8/call  clear-stream/disp32
 253     # . . discard args
 254     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 255     # . clear-stream(_test-output-stream)
 256     # . . push args
 257     68/push  _test-output-stream/imm32
 258     # . . call
 259     e8/call  clear-stream/disp32
 260     # . . discard args
 261     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 262     # . clear-stream($_test-output-buffered-file->buffer)
 263     # . . push args
 264     68/push  $_test-output-buffered-file->buffer/imm32
 265     # . . call
 266     e8/call  clear-stream/disp32
 267     # . . discard args
 268     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 269     # initialize input
 270     # . write(_test-input-stream, "== code 0x1\n")
 271     # . . push args
 272     68/push  "== code 0x1\n"/imm32
 273     68/push  _test-input-stream/imm32
 274     # . . call
 275     e8/call  write/disp32
 276     # . . discard args
 277     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 278     # . write(_test-input-stream, "Entry:\n")
 279     # . . push args
 280     68/push  "Entry:\n"/imm32
 281     68/push  _test-input-stream/imm32
 282     # . . call
 283     e8/call  write/disp32
 284     # . . discard args
 285     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 286     # . write(_test-input-stream, "ab x/imm32\n")
 287     # . . push args
 288     68/push  "ab x/imm32\n"/imm32
 289     68/push  _test-input-stream/imm32
 290     # . . call
 291     e8/call  write/disp32
 292     # . . discard args
 293     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 294     # . write(_test-input-stream, "== data 0x1000\n")
 295     # . . push args
 296     68/push  "== data 0x1000\n"/imm32
 297     68/push  _test-input-stream/imm32
 298     # . . call
 299     e8/call  write/disp32
 300     # . . discard args
 301     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 302     # . write(_test-input-stream, "x:\n")
 303     # . . push args
 304     68/push  "x:\n"/imm32
 305     68/push  _test-input-stream/imm32
 306     # . . call
 307     e8/call  write/disp32
 308     # . . discard args
 309     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 310     # . write(_test-input-stream, "01\n")
 311     # . . push args
 312     68/push  "01\n"/imm32
 313     68/push  _test-input-stream/imm32
 314     # . . call
 315     e8/call  write/disp32
 316     # . . discard args
 317     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 318     # subx-survey(_test-input-buffered-file, _test-output-buffered-file)
 319     # . . push args
 320     68/push  _test-output-buffered-file/imm32
 321     68/push  _test-input-buffered-file/imm32
 322     # . . call
 323     e8/call  subx-survey/disp32
 324     # . . discard args
 325     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 326     # check trace
 327 +-- 26 lines: #?     # dump *Trace-stream -----------------------------------------------------------------------------------------------------------------------------------------------
 353     # . check-trace-contains("label 'x' is at address 0x00001079.", msg)
 354     # . . push args
 355     68/push  "F - test-subx-survey-computes-addresses/0"/imm32
 356     68/push  "label 'x' is at address 0x00001079."/imm32
 357     # . . call
 358     e8/call  check-trace-contains/disp32
 359     # . . discard args
 360     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 361     # . check-trace-contains("segment 'code' starts at address 0x00000074.", msg)
 362     # . . push args
 363     68/push  "F - test-subx-survey-computes-addresses/1"/imm32
 364     68/push  "segment 'code' starts at address 0x00000074."/imm32
 365     # . . call
 366     e8/call  check-trace-contains/disp32
 367     # . . discard args
 368     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 369     # . check-trace-contains("segment 'code' has size 0x00000005.", msg)
 370     # . . push args
 371     68/push  "F - test-subx-survey-computes-addresses/2"/imm32
 372     68/push  "segment 'code' has size 0x00000005."/imm32
 373     # . . call
 374     e8/call  check-trace-contains/disp32
 375     # . . discard args
 376     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 377     # . check-trace-contains("segment 'data' starts at address 0x00001079.", msg)
 378     # . . push args
 379     68/push  "F - test-subx-survey-computes-addresses/3"/imm32
 380     68/push  "segment 'data' starts at address 0x00001079."/imm32
 381     # . . call
 382     e8/call  check-trace-contains/disp32
 383     # . . discard args
 384     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 385     # . epilogue
 386     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
 387     5d/pop-to-ebp
 388     c3/return
 389 
 390 # global scratch space for compute-offsets in the data segment
 391 == data
 392 
 393 compute-offsets:file-offset:  # int
 394   0/imm32
 395 compute-offsets:segment-offset:  # int
 396   0/imm32
 397 compute-offsets:segment-tmp:  # slice
 398   0/imm32/start
 399   0/imm32/end
 400 
 401 == code
 402 
 403 # write segments->file-offset,
 404 #       segments->size,
 405 #       labels->segment-name, and
 406 #       labels->segment-offset
 407 compute-offsets:  # in: (addr stream byte), segments: (addr stream {(handle array byte), segment-info}), labels: (addr stream {(handle array byte), label-info})
 408     # skeleton:
 409     #   for lines in 'in'
 410     #     for words in line
 411     #       switch word
 412     #         case 1
 413     #         case 2
 414     #         ...
 415     #         default
 416     #
 417     # pseudocode:
 418     #   var curr-segment-name: (handle array byte)
 419     #   var file-offset = 0
 420     #   var segment-offset = 0
 421     #   var line: (stream byte 512)
 422     #   var sinfo: (addr segment-info)
 423     #   var linfo: (addr label-info)
 424     #   while true                                  # line loop
 425     #     clear-stream(line)
 426     #     read-line(in, line)
 427     #     if (line->write == 0) break               # end of file
 428     #     while true                                # word loop
 429     #       word-slice = next-word(line)
 430     #       if slice-empty?(word-slice)             # end of line
 431     #         break
 432     #       else if slice-starts-with?(word-slice, "#")  # comment
 433     #         break                                 # end of line
 434     #       else if slice-equal?(word-slice, "==")
 435     #         if *curr-segment-name != 0
 436     #           sinfo = get-or-insert-handle(segments, curr-segment-name)
 437     #           sinfo->size = file-offset - sinfo->file-offset
 438     #           trace("segment '", curr-segment-name, "' has size ", sinfo->size)
 439     #         segment-tmp = next-word(line)
 440     #         if slice-empty?(segment-tmp)
 441     #           abort
 442     #         curr-segment-name = slice-to-string(segment-tmp)
 443     #         segment-tmp = next-word(line)
 444     #         if slice-empty?(segment-tmp)
 445     #           abort
 446     #         sinfo = get-or-insert-handle(segments, curr-segment-name)
 447     #         sinfo->starting-address = parse-hex-int-from-slice(segment-tmp)
 448     #         sinfo->file-offset = file-offset
 449     #         trace("segment '", curr-segment-name, "' is at file offset ", sinfo->file-offset)
 450     #         segment-offset = 0
 451     #         break  (next line)
 452     #       else if is-label?(word-slice)
 453     #         strip trailing ':' from word-slice
 454     #         linfo: (addr label-info) = get-or-insert-slice(labels, word-slice)
 455     #         linfo->segment-name = curr-segment-name
 456     #         trace("label '", word-slice, "' is in segment '", curr-segment-name, "'.")
 457     #         linfo->segment-offset = segment-offset
 458     #         trace("label '", word-slice, "' is at segment offset ", segment-offset, ".")
 459     #         # labels occupy no space, so no need to increment offsets
 460     #       else
 461     #         width = compute-width-of-slice(word-slice)
 462     #         segment-offset += width
 463     #         file-offset += width
 464     #   sinfo = get-or-insert-handle(segments, curr-segment-name)
 465     #   sinfo->size = file-offset - sinfo->file-offset
 466     #
 467     # . prologue
 468     55/push-ebp
 469     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 470     # . save registers
 471     50/push-eax
 472     51/push-ecx
 473     52/push-edx
 474     53/push-ebx
 475     56/push-esi
 476     57/push-edi
 477     # var curr-segment-name/esi: (handle array byte)
 478     68/push  0/imm32
 479     68/push  0/imm32
 480     89/copy                         3/mod/direct    6/rm32/esi    .           .             .           4/r32/esp   .               .                 # copy esp to esi
 481     # file-offset = 0
 482     c7          0/subop/copy        0/mod/indirect  5/rm32/.disp32            .             .           .           compute-offsets:file-offset/disp32  0/imm32  # copy to *compute-offsets:file-offset
 483     # segment-offset = 0
 484     c7          0/subop/copy        0/mod/indirect  5/rm32/.disp32            .             .           .           compute-offsets:segment-offset/disp32  0/imm32  # copy to *compute-offsets:segment-offset
 485     # var line/ecx: (stream byte 512)
 486     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               0x200/imm32       # subtract from esp
 487     68/push  0x200/imm32/size
 488     68/push  0/imm32/read
 489     68/push  0/imm32/write
 490     89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
 491     # var word-slice/edx: (addr slice)
 492     68/push  0/imm32
 493     68/push  0/imm32
 494     89/copy                         3/mod/direct    2/rm32/edx    .           .             .           4/r32/esp   .               .                 # copy esp to edx
 495 $compute-offsets:line-loop:
 496     # clear-stream(line)
 497     51/push-ecx
 498     e8/call  clear-stream/disp32
 499     # . discard args
 500     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 501     # read-line(in, line)
 502     51/push-ecx
 503     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
 504     e8/call  read-line/disp32
 505     # . discard args
 506     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 507     # if (line->write == 0) break
 508     8b/copy                         0/mod/indirect  1/rm32/ecx    .           .             .           0/r32/eax   .               .                 # copy *ecx to eax
 509     3d/compare-eax-and  0/imm32
 510     0f 84/jump-if-=  $compute-offsets:break-line-loop/disp32
 511 +-- 33 lines: #?     # dump line --------------------------------------------------------------------------------------------------------------------------------------------------------
 544 $compute-offsets:word-loop:
 545     # next-word(line, word-slice)
 546     52/push-edx
 547     51/push-ecx
 548     e8/call  next-word/disp32
 549     # . discard args
 550     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 551 $compute-offsets:case-empty:
 552     # if slice-empty?(word-slice) break
 553     # . eax = slice-empty?(word-slice)
 554     52/push-edx
 555     e8/call  slice-empty?/disp32
 556     # . . discard args
 557     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 558     # . if (eax != false) break
 559     3d/compare-eax-and  0/imm32/false
 560     0f 85/jump-if-!=  $compute-offsets:line-loop/disp32
 561 $compute-offsets:case-comment:
 562     # if slice-starts-with?(word-slice, "#") continue
 563     68/push  "#"/imm32
 564     52/push-edx
 565     e8/call  slice-starts-with?/disp32
 566     # . . discard args
 567     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 568     # . if (eax != false) break
 569     3d/compare-eax-and  0/imm32/false
 570     0f 85/jump-if-!=  $compute-offsets:line-loop/disp32
 571 $compute-offsets:case-segment-header:
 572     # if (!slice-equal?(word-slice, "==")) goto next case
 573     # . eax = slice-equal?(word-slice, "==")
 574     68/push  "=="/imm32
 575     52/push-edx
 576     e8/call  slice-equal?/disp32
 577     # . . discard args
 578     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 579     # . if (eax == false) goto next case
 580     3d/compare-eax-and  0/imm32/false
 581     0f 84/jump-if-=  $compute-offsets:case-label/disp32
 582     # if (*curr-segment-name == 0) goto construct-next-segment
 583     81          7/subop/compare     0/mod/indirect  6/rm32/esi    .           .             .           .           .               0/imm32           # compare *esi
 584     74/jump-if-=  $compute-offsets:construct-next-segment/disp8
 585     # sinfo/edi = get-or-insert-handle(segments, curr-segment-name, row-size=16)
 586     # . eax = get-or-insert-handle(segments, curr-segment-name, row-size=16)
 587     # . . push args
 588     68/push  0x14/imm32/row-size
 589     ff          6/subop/push        1/mod/*+disp8   6/rm32/esi    .           .             .           .           4/disp8         .                 # push *(esi+4)
 590     ff          6/subop/push        0/mod/indirect  6/rm32/esi    .           .             .           .           .               .                 # push *esi
 591     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
 592     # . . call
 593     e8/call  get-or-insert-handle/disp32
 594     # . . discard args
 595     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
 596     # . edi = eax
 597     89/copy                         3/mod/direct    7/rm32/edi    .           .             .           0/r32/eax   .               .                 # copy eax to edi
 598     # sinfo->size = file-offset - sinfo->file-offset
 599     # . save ecx
 600     51/push-ecx
 601     # . ebx = *file-offset
 602     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/ebx   compute-offsets:file-offset/disp32  # copy *file-offset to ebx
 603     # . ecx = sinfo->file-offset
 604     8b/copy                         1/mod/*+disp8   7/rm32/edi    .           .             .           1/r32/ecx   4/disp8         .                 # copy *(edi+4) to ecx
 605     # . ebx -= ecx
 606     29/subtract                     3/mod/direct    3/rm32/ebx    .           .             .           1/r32/ecx   .               .                 # subtract ecx from ebx
 607     # . sinfo->size = ebx
 608     89/copy                         1/mod/*+disp8   7/rm32/edi    .           .             .           3/r32/ebx   8/disp8         .                 # copy ebx to *(edi+8)
 609     # . restore ecx
 610     59/pop-to-ecx
 611     # trace-sssns("segment '", curr-segment-name, "' has size ", sinfo->size, ".")
 612     # . eax = lookup(curr-segment-name)
 613     # . . push args
 614     ff          6/subop/push        1/mod/*+disp8   6/rm32/esi    .           .             .           .           4/disp8         .                 # push *(esi+4)
 615     ff          6/subop/push        0/mod/indirect  6/rm32/esi    .           .             .           .           .               .                 # push *esi
 616     # . . call
 617     e8/call  lookup/disp32
 618     # . . discard args
 619     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 620     # . trace-sssns("segment '", eax, "' has size ", sinfo->size, ".")
 621     # . . push args
 622     68/push  "."/imm32
 623     53/push-ebx
 624     68/push  "' has size "/imm32
 625     50/push-eax
 626     68/push  "segment '"/imm32
 627     # . . call
 628     e8/call  trace-sssns/disp32
 629     # . . discard args
 630     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x14/imm32        # add to esp
 631 $compute-offsets:construct-next-segment:
 632     # next-word(line, segment-tmp)
 633     68/push  compute-offsets:segment-tmp/imm32
 634     51/push-ecx
 635     e8/call  next-word/disp32
 636     # . discard args
 637     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 638     # if slice-empty?(segment-tmp) abort
 639     # . eax = slice-empty?(segment-tmp)
 640     # . . push args
 641     68/push  compute-offsets:segment-tmp/imm32
 642     # . . call
 643     e8/call  slice-empty?/disp32
 644     # . . discard args
 645     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 646     # . if (eax != false) abort
 647     3d/compare-eax-and  0/imm32/false
 648     0f 85/jump-if-!=  $compute-offsets:abort/disp32
 649 $compute-offsets:update-curr-segment-name:
 650     # slice-to-string(Heap, segment-tmp, curr-segment-name)
 651     # . . push args
 652     56/push-esi
 653     68/push  compute-offsets:segment-tmp/imm32
 654     68/push  Heap/imm32
 655     # . . call
 656     e8/call  slice-to-string/disp32
 657     # . . discard args
 658     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
 659     # next-word(line, segment-tmp)
 660     68/push  compute-offsets:segment-tmp/imm32
 661     51/push-ecx
 662     e8/call  next-word/disp32
 663     # . discard args
 664     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 665     # if slice-empty?(segment-tmp) abort
 666     # . eax = slice-empty?(segment-tmp)
 667     # . . push args
 668     68/push  compute-offsets:segment-tmp/imm32
 669     # . . call
 670     e8/call  slice-empty?/disp32
 671     # . . discard args
 672     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 673     # . if (eax != false) abort
 674     3d/compare-eax-and  0/imm32/false
 675     0f 85/jump-if-!=  $compute-offsets:abort/disp32
 676     # sinfo/edi = get-or-insert-handle(segments, curr-segment-name, row-size=16)
 677     # . eax = get-or-insert-handle(segments, curr-segment-name, row-size=16)
 678     # . . push args
 679     68/push  0x14/imm32/row-size
 680     ff          6/subop/push        1/mod/*+disp8   6/rm32/esi    .           .             .           .           4/disp8         .                 # push *(esi+4)
 681     ff          6/subop/push        0/mod/indirect  6/rm32/esi    .           .             .           .           .               .                 # push *esi
 682     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
 683     # . . call
 684     e8/call  get-or-insert-handle/disp32
 685     # . . discard args
 686     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
 687     # . edi = eax
 688     89/copy                         3/mod/direct    7/rm32/edi    .           .             .           0/r32/eax   .               .                 # copy eax to edi
 689     # sinfo->address = parse-hex-int-from-slice(segment-tmp)
 690     # . eax = parse-hex-int-from-slice(segment-tmp)
 691     # . . push args
 692     68/push  compute-offsets:segment-tmp/imm32
 693     # . . call
 694     e8/call  parse-hex-int-from-slice/disp32
 695     # . . discard args
 696     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 697     # . sinfo->address = eax
 698     89/copy                         0/mod/indirect  7/rm32/edi    .           .             .           0/r32/eax   .               .                 # copy eax to *edi
 699     # sinfo->file-offset = *file-offset
 700     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   compute-offsets:file-offset/disp32  # copy *file-offset to eax
 701     89/copy                         1/mod/*+disp8   7/rm32/edi    .           .             .           0/r32/eax   4/disp8         .                 # copy eax to *(edi+4)
 702     # trace-sssns("segment '", curr-segment-name, "' is at file offset ", sinfo->file-offset, "")
 703     # . eax = lookup(curr-segment-name)
 704     # . . push args
 705     ff          6/subop/push        1/mod/*+disp8   6/rm32/esi    .           .             .           .           4/disp8         .                 # push *(esi+4)
 706     ff          6/subop/push        0/mod/indirect  6/rm32/esi    .           .             .           .           .               .                 # push *esi
 707     # . . call
 708     e8/call  lookup/disp32
 709     # . . discard args
 710     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 711     # . trace-sssns("segment '", eax, "' is at file offset ", file-offset, ".")
 712     # . . push args
 713     68/push  "."/imm32
 714     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           compute-offsets:file-offset/disp32  # push *file-offset
 715     68/push  "' is at file offset "/imm32
 716     50/push-eax
 717     68/push  "segment '"/imm32
 718     # . . call
 719     e8/call  trace-sssns/disp32
 720     # . . discard args
 721     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x14/imm32        # add to esp
 722     # segment-offset = 0
 723     c7          0/subop/copy        0/mod/indirect  5/rm32/.disp32            .             .           .     compute-offsets:segment-offset/disp32  0/imm32  # copy to *segment-offset
 724     # break
 725     e9/jump $compute-offsets:line-loop/disp32
 726 $compute-offsets:case-label:
 727     # if (!is-label?(word-slice)) goto next case
 728     # . eax = is-label?(word-slice)
 729     # . . push args
 730     52/push-edx
 731     # . . call
 732     e8/call  is-label?/disp32
 733     # . . discard args
 734     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 735     # . if (eax == false) goto next case
 736     3d/compare-eax-and  0/imm32/false
 737     0f 84/jump-if-=  $compute-offsets:case-default/disp32
 738     # strip trailing ':' from word-slice
 739     ff          1/subop/decrement   1/mod/*+disp8   2/rm32/edx    .           .             .           .           4/disp8         .                 # decrement *(edx+4)
 740     # linfo/edi = get-or-insert-slice(labels, word-slice, row-size=24)
 741     # . eax = get-or-insert-slice(labels, word-slice, row-size=24)
 742     # . . push args
 743     68/push  Heap/imm32
 744     68/push  0x18/imm32/row-size
 745     52/push-edx
 746     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
 747     # . . call
 748     e8/call  get-or-insert-slice/disp32
 749     # . . discard args
 750     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
 751     # . edi = eax
 752     89/copy                         3/mod/direct    7/rm32/edi    .           .             .           0/r32/eax   .               .                 # copy eax to edi
 753 $compute-offsets:save-label-offset:
 754     # linfo->segment-name = curr-segment-name
 755     8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           0/r32/eax   .               .                 # copy *esi to eax
 756     89/copy                         0/mod/indirect  7/rm32/edi    .           .             .           0/r32/eax   .               .                 # copy eax to *edi
 757     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   4/disp8         .                 # copy *(esi+4) to eax
 758     89/copy                         1/mod/*+disp8   7/rm32/edi    .           .             .           0/r32/eax   4/disp8         .                 # copy eax to *(edi+4)
 759     # trace-slsss("label '" word-slice "' is in segment '" current-segment-name "'.")
 760     # . eax = lookup(curr-segment-name)
 761     # . . push args
 762     ff          6/subop/push        1/mod/*+disp8   6/rm32/esi    .           .             .           .           4/disp8         .                 # push *(esi+4)
 763     ff          6/subop/push        0/mod/indirect  6/rm32/esi    .           .             .           .           .               .                 # push *esi
 764     # . . call
 765     e8/call  lookup/disp32
 766     # . . discard args
 767     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 768     # . trace-slsss("label '" word-slice "' is in segment '" eax "'.")
 769     # . . push args
 770     68/push  "'."/imm32
 771     50/push-eax
 772     68/push  "' is in segment '"/imm32
 773     52/push-edx
 774     68/push  "label '"/imm32
 775     # . . call
 776     e8/call  trace-slsss/disp32
 777     # . . discard args
 778     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x14/imm32        # add to esp
 779     # linfo->segment-offset = segment-offset
 780     # . ebx = segment-offset
 781     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/ebx   compute-offsets:segment-offset/disp32  # copy *segment-offset to ebx
 782     # . linfo->segment-offset = ebx
 783     89/copy                         1/mod/*+disp8   7/rm32/edi    .           .             .           3/r32/ebx   8/disp8         .                 # copy ebx to *(edi+8)
 784     # trace-slsns("label '" word-slice "' is at segment offset " *segment-offset/eax ".")
 785     # . . push args
 786     68/push  "."/imm32
 787     53/push-ebx
 788     68/push  "' is at segment offset "/imm32
 789     52/push-edx
 790     68/push  "label '"/imm32
 791     # . . call
 792     e8/call  trace-slsns/disp32
 793     # . . discard args
 794     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x14/imm32        # add to esp
 795     # continue
 796     e9/jump  $compute-offsets:word-loop/disp32
 797 $compute-offsets:case-default:
 798     # width/eax = compute-width-of-slice(word-slice)
 799     # . . push args
 800     52/push-edx
 801     # . . call
 802     e8/call compute-width-of-slice/disp32
 803     # . . discard args
 804     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 805     # segment-offset += width
 806     01/add                          0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   compute-offsets:segment-offset/disp32  # add eax to *segment-offset
 807     # file-offset += width
 808     01/add                          0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   compute-offsets:file-offset/disp32  # add eax to *file-offset
 809 +-- 41 lines: #?     # dump segment-offset ----------------------------------------------------------------------------------------------------------------------------------------------
 850     e9/jump $compute-offsets:word-loop/disp32
 851 $compute-offsets:break-line-loop:
 852     # sinfo/edi = get-or-insert-handle(segments, curr-segment-name, row-size=16)
 853     # . eax = get-or-insert-handle(segments, curr-segment-name, row-size=16)
 854     # . . push args
 855     68/push  0x14/imm32/row-size
 856     ff          6/subop/push        1/mod/*+disp8   6/rm32/esi    .           .             .           .           4/disp8         .                 # push *(esi+4)
 857     ff          6/subop/push        0/mod/indirect  6/rm32/esi    .           .             .           .           .               .                 # push *esi
 858     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
 859     # . . call
 860     e8/call  get-or-insert-handle/disp32
 861     # . . discard args
 862     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
 863     # . edi = eax
 864     89/copy                         3/mod/direct    7/rm32/edi    .           .             .           0/r32/eax   .               .                 # copy eax to edi
 865     # sinfo->size = file-offset - sinfo->file-offset
 866     # . save ecx
 867     51/push-ecx
 868     # . ebx = *file-offset
 869     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/ebx   compute-offsets:file-offset/disp32  # copy *file-offset to ebx
 870     # . ecx = sinfo->file-offset
 871     8b/copy                         1/mod/*+disp8   7/rm32/edi    .           .             .           1/r32/ecx   4/disp8         .                 # copy *(edi+4) to ecx
 872     # . ebx -= ecx
 873     29/subtract                     3/mod/direct    3/rm32/ebx    .           .             .           1/r32/ecx   .               .                 # subtract ecx from ebx
 874     # . sinfo->size = ebx
 875     89/copy                         1/mod/*+disp8   7/rm32/edi    .           .             .           3/r32/ebx   8/disp8         .                 # copy ebx to *(edi+8)
 876     # . restore ecx
 877     59/pop-to-ecx
 878     # trace-sssns("segment '", curr-segment-name, "' has size ", sinfo->size, ".")
 879     # . eax = lookup(curr-segment-name)
 880     # . . push args
 881     ff          6/subop/push        1/mod/*+disp8   6/rm32/esi    .           .             .           .           4/disp8         .                 # push *(esi+4)
 882     ff          6/subop/push        0/mod/indirect  6/rm32/esi    .           .             .           .           .               .                 # push *esi
 883     # . . call
 884     e8/call  lookup/disp32
 885     # . . discard args
 886     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 887     # . trace-sssns("segment '", eax, "' has size ", ebx, ".")
 888     # . . push args
 889     68/push  "."/imm32
 890     53/push-ebx
 891     68/push  "' has size "/imm32
 892     50/push-eax
 893     68/push  "segment '"/imm32
 894     # . . call
 895     e8/call  trace-sssns/disp32
 896     # . . discard args
 897     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x14/imm32        # add to esp
 898 $compute-offsets:end:
 899     # . reclaim locals
 900     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x21c/imm32       # add to esp
 901     # . restore registers
 902     5f/pop-to-edi
 903     5e/pop-to-esi
 904     5b/pop-to-ebx
 905     5a/pop-to-edx
 906     59/pop-to-ecx
 907     58/pop-to-eax
 908     # . epilogue
 909     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
 910     5d/pop-to-ebp
 911     c3/return
 912 
 913 $compute-offsets:abort:
 914     # . _write(2/stderr, error)
 915     # . . push args
 916     68/push  "'==' must be followed by segment name and segment-start\n"/imm32
 917     68/push  2/imm32/stderr
 918     # . . call
 919     e8/call  _write/disp32
 920     # . . discard args
 921     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 922     # . syscall(exit, 1)
 923     bb/copy-to-ebx  1/imm32
 924     e8/call  syscall_exit/disp32
 925     # never gets here
 926 
 927 test-compute-offsets:
 928     # input:
 929     #   == code 0x1
 930     #   ab x/imm32  # skip comment
 931     #   == data 0x1000
 932     #   00
 933     #   x:
 934     #     34
 935     #
 936     # trace contains (in any order):
 937     #   segment 'code' is at file offset 0x0.
 938     #   segment 'code' has size 0x5.
 939     #   segment 'data' is at file offset 0x5.
 940     #   segment 'data' has size 0x2.
 941     #   label 'x' is in segment 'data'.
 942     #   label 'x' is at segment offset 0x1.
 943     #
 944     # . prologue
 945     55/push-ebp
 946     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 947     # setup
 948     # . clear-stream(_test-input-stream)
 949     # . . push args
 950     68/push  _test-input-stream/imm32
 951     # . . call
 952     e8/call  clear-stream/disp32
 953     # . . discard args
 954     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 955     # var segments/ecx: (stream byte 2*20)
 956     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               0x28/imm32        # subtract from esp
 957     68/push  0x28/imm32/size
 958     68/push  0/imm32/read
 959     68/push  0/imm32/write
 960     89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
 961     # var labels/edx: (stream byte 2*24)
 962     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               0x30/imm32        # subtract from esp
 963     68/push  0x30/imm32/size
 964     68/push  0/imm32/read
 965     68/push  0/imm32/write
 966     89/copy                         3/mod/direct    2/rm32/edx    .           .             .           4/r32/esp   .               .                 # copy esp to edx
 967     # initialize input
 968     # . write(_test-input-stream, "== code 0x1\n")
 969     # . . push args
 970     68/push  "== code 0x1\n"/imm32
 971     68/push  _test-input-stream/imm32
 972     # . . call
 973     e8/call  write/disp32
 974     # . . discard args
 975     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 976     # . write(_test-input-stream, "ab x/imm32  # skip comment\n")
 977     # . . push args
 978     68/push  "ab x/imm32  # skip comment\n"/imm32
 979     68/push  _test-input-stream/imm32
 980     # . . call
 981     e8/call  write/disp32
 982     # . . discard args
 983     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 984     # . write(_test-input-stream, "== data 0x1000\n")
 985     # . . push args
 986     68/push  "== data 0x1000\n"/imm32
 987     68/push  _test-input-stream/imm32
 988     # . . call
 989     e8/call  write/disp32
 990     # . . discard args
 991     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 992     # . write(_test-input-stream, "00\n")
 993     # . . push args
 994     68/push  "00\n"/imm32
 995     68/push  _test-input-stream/imm32
 996     # . . call
 997     e8/call  write/disp32
 998     # . . discard args
 999     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1000     # . write(_test-input-stream, "x:\n")
1001     # . . push args
1002     68/push  "x:\n"/imm32
1003     68/push  _test-input-stream/imm32
1004     # . . call
1005     e8/call  write/disp32
1006     # . . discard args
1007     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1008     # . write(_test-input-stream, "34\n")
1009     # . . push args
1010     68/push  "34\n"/imm32
1011     68/push  _test-input-stream/imm32
1012     # . . call
1013     e8/call  write/disp32
1014     # . . discard args
1015     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1016     # compute-offsets(_test-input-stream, segments, labels)
1017     # . . push args
1018     52/push-edx
1019     51/push-ecx
1020     68/push  _test-input-stream/imm32
1021     # . . call
1022     e8/call  compute-offsets/disp32
1023     # . . discard args
1024     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32        # add to esp
1025 +-- 26 lines: #?     # dump *Trace-stream -----------------------------------------------------------------------------------------------------------------------------------------------
1051     # check trace
1052     # . check-trace-contains("segment 'code' is at file offset 0x00000000.", msg)
1053     # . . push args
1054     68/push  "F - test-compute-offsets/0"/imm32
1055     68/push  "segment 'code' is at file offset 0x00000000."/imm32
1056     # . . call
1057     e8/call  check-trace-contains/disp32
1058     # . . discard args
1059     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1060     # . check-trace-contains("segment 'code' has size 0x00000005", msg)
1061     # . . push args
1062     68/push  "F - test-compute-offsets/1"/imm32
1063     68/push  "segment 'code' has size 0x00000005."/imm32
1064     # . . call
1065     e8/call  check-trace-contains/disp32
1066     # . . discard args
1067     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1068     # . check-trace-contains("segment 'data' is at file offset 0x00000005.", msg)
1069     # . . push args
1070     68/push  "F - test-compute-offsets/2"/imm32
1071     68/push  "segment 'data' is at file offset 0x00000005."/imm32
1072     # . . call
1073     e8/call  check-trace-contains/disp32
1074     # . . discard args
1075     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1076     # . check-trace-contains("segment 'data' has size 0x00000002.", msg)
1077     # . . push args
1078     68/push  "F - test-compute-offsets/3"/imm32
1079     68/push  "segment 'data' has size 0x00000002."/imm32
1080     # . . call
1081     e8/call  check-trace-contains/disp32
1082     # . . discard args
1083     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1084     # . check-trace-contains("label 'x' is in segment 'data'.", msg)
1085     # . . push args
1086     68/push  "F - test-compute-offsets/4"/imm32
1087     68/push  "label 'x' is in segment 'data'."/imm32
1088     # . . call
1089     e8/call  check-trace-contains/disp32
1090     # . . discard args
1091     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1092     # . check-trace-contains("label 'x' is at segment offset 0x00000001.", msg)
1093     # . . push args
1094     68/push  "F - test-compute-offsets/5"/imm32
1095     68/push  "label 'x' is at segment offset 0x00000001."/imm32
1096     # . . call
1097     e8/call  check-trace-contains/disp32
1098     # . . discard args
1099     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1100     # . check-ints-equal(labels->write, 0x18, msg)
1101     # . . push args
1102     68/push  "F - test-compute-offsets-maintains-labels-write-index"/imm32
1103     68/push  0x18/imm32/1-entry
1104     ff          6/subop/push        0/mod/indirect  2/rm32/edx    .           .             .           .           .               .                 # push *edx
1105     # . . call
1106     e8/call  check-ints-equal/disp32
1107     # . . discard args
1108     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
1109     # . epilogue
1110     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
1111     5d/pop-to-ebp
1112     c3/return
1113 
1114 # write segments->file-offset,
1115 #       segments->address, and
1116 #       labels->address
1117 compute-addresses:  # segments: (addr stream {(handle array byte), segment-info}), labels: (addr stream {(handle array byte), label-info})
1118     # pseudocode:
1119     #   var srow: (addr segment-row) = segments->data
1120     #   var max: (addr byte) = &segments->data[segments->write]
1121     #   var num-segments: int = segments->write / 20
1122     #   var starting-offset: int = 0x34 + (num-segments * 0x20)
1123     #   while true
1124     #     if (srow >= max) break
1125     #     srow->file-offset += starting-offset
1126     #     srow->address &= 0xfffff000  # clear last 12 bits for p_align
1127     #     srow->address += (srow->file-offset & 0x00000fff)
1128     #     trace-sssns("segment " srow->key " starts at address " srow->address)
1129     #     srow += 20  # row-size
1130     #   var lrow: (addr label-row) = labels->data
1131     #   max = &labels->data[labels->write]
1132     #   while true
1133     #     if (lrow >= max) break
1134     #     var seg-name: (addr array byte) = lookup(lrow->segment-name)
1135     #     var label-seg: (addr segment-info) = get(segments, seg-name)
1136     #     lrow->address = label-seg->address + lrow->segment-offset
1137     #     trace-sssns("label " lrow->key " is at address " lrow->address)
1138     #     lrow += 24  # row-size
1139     #
1140     # . prologue
1141     55/push-ebp
1142     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
1143     # . save registers
1144     50/push-eax
1145     51/push-ecx
1146     52/push-edx
1147     53/push-ebx
1148     56/push-esi
1149     57/push-edi
1150     # esi = segments
1151     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   8/disp8         .                 # copy *(ebp+8) to esi
1152     # var num-segments/edi: int = segments->write / 20 (row-size)
1153     # . eax = segments->write
1154     8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           0/r32/eax   .               .                 # copy *esi to eax
1155     # . edx = 0
1156     ba/copy-to-edx  0/imm32
1157     # . ecx = 20 (row-size)
1158     b9/copy-to-ecx  0x14/imm32/row-size
1159     # . eax /= ecx (clobbering edx)
1160     f7          7/subop/divide      3/mod/direct    1/rm32/ecx    .           .             .           .           .               .                 # divide eax by ecx
1161     # . edi = eax
1162     89/copy                         3/mod/direct    7/rm32/edi    .           .             .           0/r32/eax   .               .                 # copy eax to edi
1163     # var starting-offset/edi: int = 0x34 + (num-segments * 0x20)  # make room for ELF headers
1164     c1/shift    4/subop/left        3/mod/direct    7/rm32/edi    .           .             .           .           .               5/imm8            # shift edi left by 5 bits
1165     81          0/subop/add         3/mod/direct    7/rm32/edi    .           .             .           .           .               0x34/imm32        # add to edi
1166     # var max/ecx: (addr byte) = &segments->data[segments->write]
1167     8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           1/r32/ecx   .               .                 # copy *esi to ecx
1168     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    6/base/esi  1/index/ecx   .           1/r32/ecx   0xc/disp8       .                 # copy esi+ecx+12 to ecx
1169     # var srow/esi: (addr segment-row) = segments->data
1170     8d/copy-address                 1/mod/*+disp8   6/rm32/esi    .           .             .           6/r32/esi   0xc/disp8       .                 # copy esi+12 to esi
1171 $compute-addresses:segment-loop:
1172     # if (srow >= max) break
1173     39/compare                      3/mod/direct    6/rm32/esi    .           .             .           1/r32/ecx   .               .                 # compare esi with ecx
1174     73/jump-if-addr>=  $compute-addresses:segment-break/disp8
1175     # srow->file-offset += starting-offset
1176     01/add                          1/mod/*+disp8   6/rm32/esi    .           .             .           7/r32/edi   0xc/disp8       .                 # add edi to *(esi+12)
1177     # clear last 12 bits of srow->address for p_align=0x1000
1178     # . edx = srow->address
1179     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           2/r32/edx   8/disp8         .                 # copy *(esi+8) to edx
1180     # . edx &= 0xfffff000
1181     81          4/subop/and         3/mod/direct    2/rm32/edx    .           .             .           .           .               0xfffff000/imm32  # bitwise and of edx
1182     # update last 12 bits from srow->file-offset
1183     # . ebx = srow->file-offset
1184     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           3/r32/ebx   0xc/disp8       .                 # copy *(esi+12) to ebx
1185     # . ebx &= 0xfff
1186     81          4/subop/and         3/mod/direct    3/rm32/ebx    .           .             .           .           .               0x00000fff/imm32  # bitwise and of ebx
1187     # . srow->address = edx | ebx
1188     09/or                           3/mod/direct    2/rm32/edx    .           .             .           3/r32/ebx   .               .                 # edx = bitwise OR with ebx
1189     89/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           2/r32/edx   8/disp8         .                 # copy edx to *(esi+8)
1190     # trace-sssns("segment " srow " starts at address " srow->address ".")
1191     # . eax = lookup(*srow)
1192     # . . push args
1193     ff          6/subop/push        1/mod/*+disp8   6/rm32/esi    .           .             .           .           4/disp8         .                 # push *(esi+4)
1194     ff          6/subop/push        0/mod/indirect  6/rm32/esi    .           .             .           .           .               .                 # push *esi
1195     # . . call
1196     e8/call  lookup/disp32
1197     # . . discard args
1198     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1199     # . trace-sssns("segment " eax " starts at address " srow->address ".")
1200     # . . push args
1201     68/push  "."/imm32
1202     52/push-edx
1203     68/push  "' starts at address "/imm32
1204     50/push-eax
1205     68/push  "segment '"/imm32
1206     # . . call
1207     e8/call  trace-sssns/disp32
1208     # . . discard args
1209     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x14/imm32        # add to esp
1210     # srow += 20  # size of row
1211     81          0/subop/add         3/mod/direct    6/rm32/esi    .           .             .           .           .               0x14/imm32        # add to esi
1212     eb/jump  $compute-addresses:segment-loop/disp8
1213 $compute-addresses:segment-break:
1214 +-- 26 lines: #?     # dump *Trace-stream -----------------------------------------------------------------------------------------------------------------------------------------------
1240     # esi = labels
1241     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   0xc/disp8       .                 # copy *(ebp+12) to esi
1242     # var max/ecx: (addr byte) = &labels->data[labels->write]
1243     8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           1/r32/ecx   .               .                 # copy *esi to ecx
1244     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    6/base/esi  1/index/ecx   .           1/r32/ecx   0xc/disp8       .                 # copy esi+ecx+12 to ecx
1245     # var lrow/esi: (addr label-row) = labels->data
1246     8d/copy-address                 1/mod/*+disp8   6/rm32/esi    .           .             .           6/r32/esi   0xc/disp8       .                 # copy esi+12 to esi
1247 $compute-addresses:label-loop:
1248     # if (lrow >= max) break
1249     39/compare                      3/mod/direct    6/rm32/esi    .           .             .           1/r32/ecx   .               .                 # compare esi with ecx
1250     0f 83/jump-if-addr>=  $compute-addresses:end/disp32
1251 +-- 26 lines: #?     # dump lrow->key ---------------------------------------------------------------------------------------------------------------------------------------------------
1277     # var seg-name/edx: (addr array byte) = lookup(lrow->segment-name)
1278     # . eax = lookup(lrow->segment-name)
1279     # . . push args
1280     ff          6/subop/push        1/mod/*+disp8   6/rm32/esi    .           .             .           .           0xc/disp8       .                 # push *(esi+12)
1281     ff          6/subop/push        1/mod/*+disp8   6/rm32/esi    .           .             .           .           8/disp8         .                 # push *(esi+8)
1282     # . . call
1283     e8/call  lookup/disp32
1284     # . . discard args
1285     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1286     # . edx = eax
1287     89/copy                         3/mod/direct    2/rm32/edx    .           .             .           0/r32/eax   .               .                 # copy eax to edx
1288 +-- 26 lines: #?     # dump seg-name ----------------------------------------------------------------------------------------------------------------------------------------------------
1314     # var label-seg/edx: (addr segment-info) = get(segments, seg-name, row-size=20, "segment table")
1315     # . eax = get(segments, seg-name, row-size=20)
1316     # . . push args
1317     68/push  "segment table"/imm32
1318     68/push  0x14/imm32/row-size
1319     52/push-edx
1320     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
1321     # . . call
1322     e8/call  get/disp32
1323     # . . discard args
1324     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
1325     # . edx = eax
1326     89/copy                         3/mod/direct    2/rm32/edx    .           .             .           0/r32/eax   .               .                 # copy eax to edx
1327     # ebx = label-seg->address
1328     8b/copy                         0/mod/indirect  2/rm32/edx    .           .             .           3/r32/ebx   .               .                 # copy *edx to ebx
1329     # ebx += lrow->segment-offset
1330     03/add                          1/mod/*+disp8   6/rm32/esi    .           .             .           3/r32/ebx   0x10/disp8      .                 # add *(esi+16) to ebx
1331     # lrow->address = ebx
1332     89/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           3/r32/ebx   0x14/disp8      .                 # copy ebx to *(esi+20)
1333     # trace-sssns("label " lrow->key " is at address " lrow->address ".")
1334     # . eax = lookup(lrow->key)
1335     # . . push args
1336     ff          6/subop/push        1/mod/*+disp8   6/rm32/esi    .           .             .           .           4/disp8         .                 # push *(esi+4)
1337     ff          6/subop/push        0/mod/indirect  6/rm32/esi    .           .             .           .           .               .                 # push *esi
1338     # . . call
1339     e8/call  lookup/disp32
1340     # . . discard args
1341     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1342     # . trace-sssns("label " eax " is at address " lrow->address ".")
1343     # . . push args
1344     68/push  "."/imm32
1345     53/push-ebx
1346     68/push  "' is at address "/imm32
1347     50/push-eax
1348     68/push  "label '"/imm32
1349     # . . call
1350     e8/call  trace-sssns/disp32
1351     # . . discard args
1352     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x14/imm32        # add to esp
1353     # lrow += 24  # size of row
1354     81          0/subop/add         3/mod/direct    6/rm32/esi    .           .             .           .           .               0x18/imm32        # add to esi
1355     e9/jump  $compute-addresses:label-loop/disp32
1356 $compute-addresses:end:
1357     # . restore registers
1358     5f/pop-to-edi
1359     5e/pop-to-esi
1360     5b/pop-to-ebx
1361     5a/pop-to-edx
1362     59/pop-to-ecx
1363     58/pop-to-eax
1364     # . epilogue
1365     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
1366     5d/pop-to-ebp
1367     c3/return
1368 
1369 test-compute-addresses:
1370     # input:
1371     #   segments:
1372     #     - 'a': {0x1000, 0, 5}
1373     #     - 'b': {0x2018, 5, 1}
1374     #     - 'c': {0x5444, 6, 12}
1375     #   labels:
1376     #     - 'l1': {'a', 3, 0}
1377     #     - 'l2': {'b', 0, 0}
1378     #
1379     # trace contains in any order (comments in parens):
1380     #   segment 'a' starts at address 0x00001094.  (0x34 + 0x20 for each segment)
1381     #   segment 'b' starts at address 0x00002099.  (0x018 discarded)
1382     #   segment 'c' starts at address 0x0000509a.  (0x444 discarded)
1383     #   label 'l1' is at address 0x00001097.       (0x1094 + segment-offset 3)
1384     #   label 'l2' is at address 0x00002099.       (0x2099 + segment-offset 0)
1385     #
1386     # . prologue
1387     55/push-ebp
1388     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
1389     # setup
1390     # . var segments/ecx: (stream byte 10*20)
1391     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc8/imm32        # subtract from esp
1392     68/push  0xc8/imm32/size
1393     68/push  0/imm32/read
1394     68/push  0/imm32/write
1395     89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
1396     # . var labels/edx: (stream byte 8*24)
1397     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc0/imm32        # subtract from esp
1398     68/push  0xc0/imm32/size
1399     68/push  0/imm32/read
1400     68/push  0/imm32/write
1401     89/copy                         3/mod/direct    2/rm32/edx    .           .             .           4/r32/esp   .               .                 # copy esp to edx
1402     # . var h/ebx: (handle array byte)
1403     68/push  0/imm32
1404     68/push  0/imm32
1405     89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           4/r32/esp   .               .                 # copy esp to ebx
1406     # . h = copy-array(Heap, "a")
1407     # . . push args
1408     53/push-ebx
1409     68/push  "a"/imm32
1410     68/push  Heap/imm32
1411     # . . call
1412     e8/call  copy-array/disp32
1413     # . . discard args
1414     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
1415     # . stream-add5(segments, "a", 0x1000, 0, 5)
1416     # . . push args
1417     68/push  5/imm32/segment-size
1418     68/push  0/imm32/file-offset
1419     68/push  0x1000/imm32/start-address
1420     ff          6/subop/push        1/mod/*+disp8   3/rm32/ebx    .           .             .           .           4/disp8         .                 # push *(ebx+4)
1421     ff          6/subop/push        0/mod/indirect  3/rm32/ebx    .           .             .           .           .               .                 # push *ebx
1422     51/push-ecx
1423     # . . call
1424     e8/call  stream-add5/disp32
1425     # . . discard args
1426     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x18/imm32        # add to esp
1427     # . h = copy-array(Heap, "b")
1428     # . . push args
1429     53/push-ebx
1430     68/push  "b"/imm32
1431     68/push  Heap/imm32
1432     # . . call
1433     e8/call  copy-array/disp32
1434     # . . discard args
1435     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
1436     # . stream-add5(segments, "b", 0x2018, 5, 1)
1437     # . . push args
1438     68/push  1/imm32/segment-size
1439     68/push  5/imm32/file-offset
1440     68/push  0x2018/imm32/start-address
1441     ff          6/subop/push        1/mod/*+disp8   3/rm32/ebx    .           .             .           .           4/disp8         .                 # push *(ebx+4)
1442     ff          6/subop/push        0/mod/indirect  3/rm32/ebx    .           .             .           .           .               .                 # push *ebx
1443     51/push-ecx
1444     # . . call
1445     e8/call  stream-add5/disp32
1446     # . . discard args
1447     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x18/imm32        # add to esp
1448     # . h = copy-array(Heap, "c")
1449     # . . push args
1450     53/push-ebx
1451     68/push  "c"/imm32
1452     68/push  Heap/imm32
1453     # . . call
1454     e8/call  copy-array/disp32
1455     # . . discard args
1456     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
1457     # . stream-add5(segments, "c", 0x5444, 6, 12)
1458     68/push  0xc/imm32/segment-size
1459     68/push  6/imm32/file-offset
1460     68/push  0x5444/imm32/start-address
1461     ff          6/subop/push        1/mod/*+disp8   3/rm32/ebx    .           .             .           .           4/disp8         .                 # push *(ebx+4)
1462     ff          6/subop/push        0/mod/indirect  3/rm32/ebx    .           .             .           .           .               .                 # push *ebx
1463     51/push-ecx
1464     # . . call
1465     e8/call  stream-add5/disp32
1466     # . . discard args
1467     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x18/imm32        # add to esp
1468     # . stream-add6(labels, "l1", "a", 3, 0)
1469     # . . push args
1470     68/push  0/imm32/label-address
1471     68/push  3/imm32/segment-offset
1472     # . . push "a"
1473     53/push-ebx
1474     68/push  "a"/imm32
1475     68/push  Heap/imm32
1476     e8/call  copy-array/disp32
1477     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
1478     ff          6/subop/push        1/mod/*+disp8   3/rm32/ebx    .           .             .           .           4/disp8         .                 # push *(ebx+4)
1479     ff          6/subop/push        0/mod/indirect  3/rm32/ebx    .           .             .           .           .               .                 # push *ebx
1480     # . . push "l1"
1481     53/push-ebx
1482     68/push  "l1"/imm32
1483     68/push  Heap/imm32
1484     e8/call  copy-array/disp32
1485     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
1486     ff          6/subop/push        1/mod/*+disp8   3/rm32/ebx    .           .             .           .           4/disp8         .                 # push *(ebx+4)
1487     ff          6/subop/push        0/mod/indirect  3/rm32/ebx    .           .             .           .           .               .                 # push *ebx
1488     # . . push labels
1489     52/push-edx
1490     # . . call
1491     e8/call  stream-add6/disp32
1492     # . . discard args
1493     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x1c/imm32        # add to esp
1494     # . stream-add6(labels, "l2", "b", 0, 0)
1495     # . . push args
1496     68/push  0/imm32/label-address
1497     68/push  0/imm32/segment-offset
1498     # . . push "b"
1499     53/push-ebx
1500     68/push  "b"/imm32
1501     68/push  Heap/imm32
1502     e8/call  copy-array/disp32
1503     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
1504     ff          6/subop/push        1/mod/*+disp8   3/rm32/ebx    .           .             .           .           4/disp8         .                 # push *(ebx+4)
1505     ff          6/subop/push        0/mod/indirect  3/rm32/ebx    .           .             .           .           .               .                 # push *ebx
1506     # . . push "l2"
1507     53/push-ebx
1508     68/push  "l2"/imm32
1509     68/push  Heap/imm32
1510     e8/call  copy-array/disp32
1511     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
1512     ff          6/subop/push        1/mod/*+disp8   3/rm32/ebx    .           .             .           .           4/disp8         .                 # push *(ebx+4)
1513     ff          6/subop/push        0/mod/indirect  3/rm32/ebx    .           .             .           .           .               .                 # push *ebx
1514     # . . push labels
1515     52/push-edx
1516     # . . call
1517     e8/call  stream-add6/disp32
1518     # . . discard args
1519     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x1c/imm32        # add to esp
1520     # component under test
1521     # . compute-addresses(segments, labels)
1522     # . . push args
1523     52/push-edx
1524     51/push-ecx
1525     # . . call
1526     e8/call  compute-addresses/disp32
1527     # . . discard args
1528     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1529     # checks
1530 +-- 26 lines: #?     # dump *Trace-stream -----------------------------------------------------------------------------------------------------------------------------------------------
1556     # . check-trace-contains("segment 'a' starts at address 0x00001094.", msg)
1557     # . . push args
1558     68/push  "F - test-compute-addresses/0"/imm32
1559     68/push  "segment 'a' starts at address 0x00001094."/imm32
1560     # . . call
1561     e8/call  check-trace-contains/disp32
1562     # . . discard args
1563     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1564     # . check-trace-contains("segment 'b' starts at address 0x00002099.", msg)
1565     # . . push args
1566     68/push  "F - test-compute-addresses/1"/imm32
1567     68/push  "segment 'b' starts at address 0x00002099."/imm32
1568     # . . call
1569     e8/call  check-trace-contains/disp32
1570     # . . discard args
1571     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1572     # . check-trace-contains("segment 'c' starts at address 0x0000509a.", msg)
1573     # . . push args
1574     68/push  "F - test-compute-addresses/2"/imm32
1575     68/push  "segment 'c' starts at address 0x0000509a."/imm32
1576     # . . call
1577     e8/call  check-trace-contains/disp32
1578     # . . discard args
1579     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1580     # . check-trace-contains("label 'l1' is at address 0x00001097.", msg)
1581     # . . push args
1582     68/push  "F - test-compute-addresses/3"/imm32
1583     68/push  "label 'l1' is at address 0x00001097."/imm32
1584     # . . call
1585     e8/call  check-trace-contains/disp32
1586     # . . discard args
1587     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1588     # . check-trace-contains("label 'l2' is at address 0x00002099.", msg)
1589     # . . push args
1590     68/push  "F - test-compute-addresses/4"/imm32
1591     68/push  "label 'l2' is at address 0x00002099."/imm32
1592     # . . call
1593     e8/call  check-trace-contains/disp32
1594     # . . discard args
1595     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1596     # . check-ints-equal(labels->write, 0x30, msg)
1597     # . . push args
1598     68/push  "F - test-compute-addresses/maintains-labels-write-index"/imm32
1599     68/push  0x30/imm32/2-entries
1600     ff          6/subop/push        0/mod/indirect  2/rm32/edx    .           .             .           .           .               .                 # push *edx
1601     # . . call
1602     e8/call  check-ints-equal/disp32
1603     # . . discard args
1604     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
1605     # . epilogue
1606     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
1607     5d/pop-to-ebp
1608     c3/return
1609 
1610 test-compute-addresses-large-segments:
1611     # input:
1612     #   segments:
1613     #     - 'a': {0x1000, 0, 0x5604}
1614     #     - 'b': {0x2018, 0x5604, 1}
1615     #   labels:
1616     #     - 'l1': {'a', 3, 0}
1617     #
1618     # trace contains in any order (comments in parens):
1619     #   segment 'a' starts at address 0x00001074.  (0x34 + 0x20 for each segment)
1620     #   segment 'b' starts at address 0x00002678.  (0x018 discarded; last 3 nibbles from 0x1074 + 0x5604)
1621     #   label 'l1' is at address 0x00001077.       (0x1074 + segment-offset 3)
1622     #
1623     # . prologue
1624     55/push-ebp
1625     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
1626     # setup
1627     # . var segments/ecx: (stream byte 10*20)
1628     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc8/imm32        # subtract from esp
1629     68/push  0xc8/imm32/size
1630     68/push  0/imm32/read
1631     68/push  0/imm32/write
1632     89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
1633     # . var labels/edx: (stream byte 8*24)
1634     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc0/imm32        # subtract from esp
1635     68/push  0xc0/imm32/size
1636     68/push  0/imm32/read
1637     68/push  0/imm32/write
1638     89/copy                         3/mod/direct    2/rm32/edx    .           .             .           4/r32/esp   .               .                 # copy esp to edx
1639     # . var h/ebx: (handle array byte)
1640     68/push  0/imm32
1641     68/push  0/imm32
1642     89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           4/r32/esp   .               .                 # copy esp to ebx
1643     # . h = copy-array(Heap, "a")
1644     # . . push args
1645     53/push-ebx
1646     68/push  "a"/imm32
1647     68/push  Heap/imm32
1648     # . . call
1649     e8/call  copy-array/disp32
1650     # . . discard args
1651     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
1652     # . stream-add5(segments, "a", 0x1000, 0, 0x5604)
1653     68/push  0x5604/imm32/segment-size
1654     68/push  0/imm32/file-offset
1655     68/push  0x1000/imm32/start-address
1656     ff          6/subop/push        1/mod/*+disp8   3/rm32/ebx    .           .             .           .           4/disp8         .                 # push *(ebx+4)
1657     ff          6/subop/push        0/mod/indirect  3/rm32/ebx    .           .             .           .           .               .                 # push *ebx
1658     51/push-ecx
1659     # . . call
1660     e8/call  stream-add5/disp32
1661     # . . discard args
1662     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x18/imm32        # add to esp
1663     # . h = copy-array(Heap, "b")
1664     # . . push args
1665     53/push-ebx
1666     68/push  "b"/imm32
1667     68/push  Heap/imm32
1668     # . . call
1669     e8/call  copy-array/disp32
1670     # . . discard args
1671     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
1672     # . stream-add5(segments, "b", 0x2018, 0x5604, 1)
1673     68/push  1/imm32/segment-size
1674     68/push  0x5604/imm32/file-offset
1675     68/push  0x2018/imm32/start-address
1676     ff          6/subop/push        1/mod/*+disp8   3/rm32/ebx    .           .             .           .           4/disp8         .                 # push *(ebx+4)
1677     ff          6/subop/push        0/mod/indirect  3/rm32/ebx    .           .             .           .           .               .                 # push *ebx
1678     51/push-ecx
1679     # . . call
1680     e8/call  stream-add5/disp32
1681     # . . discard args
1682     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x18/imm32        # add to esp
1683     # . stream-add6(labels, "l1", "a", 3, 0)
1684     68/push  0/imm32/label-address
1685     68/push  3/imm32/segment-offset
1686     # . . push "a"
1687     53/push-ebx
1688     68/push  "a"/imm32
1689     68/push  Heap/imm32
1690     e8/call  copy-array/disp32
1691     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
1692     ff          6/subop/push        1/mod/*+disp8   3/rm32/ebx    .           .             .           .           4/disp8         .                 # push *(ebx+4)
1693     ff          6/subop/push        0/mod/indirect  3/rm32/ebx    .           .             .           .           .               .                 # push *ebx
1694     # . . push "l1"
1695     53/push-ebx
1696     68/push  "l1"/imm32
1697     68/push  Heap/imm32
1698     e8/call  copy-array/disp32
1699     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
1700     ff          6/subop/push        1/mod/*+disp8   3/rm32/ebx    .           .             .           .           4/disp8         .                 # push *(ebx+4)
1701     ff          6/subop/push        0/mod/indirect  3/rm32/ebx    .           .             .           .           .               .                 # push *ebx
1702     # . . push labels
1703     52/push-edx
1704     # . . call
1705     e8/call  stream-add6/disp32
1706     # . . discard args
1707     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x1c/imm32        # add to esp
1708     # component under test
1709     # . compute-addresses(segments, labels)
1710     # . . push args
1711     52/push-edx
1712     51/push-ecx
1713     # . . call
1714     e8/call  compute-addresses/disp32
1715     # . . discard args
1716     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1717     # checks
1718 +-- 26 lines: #?     # dump *Trace-stream -----------------------------------------------------------------------------------------------------------------------------------------------
1744     # . check-trace-contains("segment 'a' starts at address 0x00001074.", msg)
1745     # . . push args
1746     68/push  "F - test-compute-addresses-large-segments/0"/imm32
1747     68/push  "segment 'a' starts at address 0x00001074."/imm32
1748     # . . call
1749     e8/call  check-trace-contains/disp32
1750     # . . discard args
1751     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1752     # . check-trace-contains("segment 'b' starts at address 0x00002678.", msg)
1753     # . . push args
1754     68/push  "F - test-compute-addresses-large-segments/1"/imm32
1755     68/push  "segment 'b' starts at address 0x00002678."/imm32
1756     # . . call
1757     e8/call  check-trace-contains/disp32
1758     # . . discard args
1759     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1760     # . check-trace-contains("label 'l1' is at address 0x00001077.", msg)
1761     # . . push args
1762     68/push  "F - test-compute-addresses-large-segments/3"/imm32
1763     68/push  "label 'l1' is at address 0x00001077."/imm32
1764     # . . call
1765     e8/call  check-trace-contains/disp32
1766     # . . discard args
1767     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1768     # . epilogue
1769     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
1770     5d/pop-to-ebp
1771     c3/return
1772 
1773 emit-output:  # in: (addr stream byte), out: (addr buffered-file), segments: (addr stream {(handle array byte), segment-info}), labels: (addr stream {(handle array byte), label-info})
1774     # pseudocode:
1775     #   emit-headers(out, segments, labels)
1776     #   emit-segments(in, out, labels)
1777     #
1778     # . prologue
1779     55/push-ebp
1780     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
1781 +--  9 lines: #?     # write(2/stderr, "emit-headers\n") --------------------------------------------------------------------------------------------------------------------------------
1790     # emit-headers(out, segments, labels)
1791     # . . push args
1792     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x14/disp8       .                # push *(ebp+20)
1793     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8       .                # push *(ebp+16)
1794     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8        .                # push *(ebp+12)
1795     # . . call
1796     e8/call  emit-headers/disp32
1797     # . . discard args
1798     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
1799 +--  9 lines: #?     # write(2/stderr, "emit-segments\n") -------------------------------------------------------------------------------------------------------------------------------
1808     # emit-segments(in, out, labels)
1809     # . . push args
1810     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x14/disp8      .                 # push *(ebp+20)
1811     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
1812     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
1813     # . . call
1814     e8/call  emit-segments/disp32
1815     # . . discard args
1816     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
1817 $emit-output:end:
1818     # . epilogue
1819     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
1820     5d/pop-to-ebp
1821     c3/return
1822 
1823 emit-segments:  # in: (addr stream byte), out: (addr buffered-file), labels: (addr stream {(handle array byte), label-info})
1824     # pseudocode:
1825     #   var offset-of-next-instruction = 0
1826     #   var line: (stream byte 512)
1827     #   line-loop:
1828     #   while true
1829     #     clear-stream(line)
1830     #     read-line(in, line)
1831     #     if (line->write == 0) break               # end of file
1832     #     offset-of-next-instruction += num-bytes(line)
1833     #     while true
1834     #       var word-slice = next-word(line)
1835     #       if slice-empty?(word-slice)             # end of line
1836     #         break
1837     #       if slice-starts-with?(word-slice, "#")  # comment
1838     #         break
1839     #       if is-label?(word-slice)                # no need for label declarations anymore
1840     #         goto line-loop                        # don't insert empty lines
1841     #       if slice-equal?(word-slice, "==")       # no need for segment header lines
1842     #         goto line-loop                        # don't insert empty lines
1843     #       if length(word-slice) == 2
1844     #         write-slice-buffered(out, word-slice)
1845     #         write-buffered(out, " ")
1846     #         continue
1847     #       var datum: (addr slice) = next-token-from-slice(word-slice->start, word-slice->end, "/")
1848     #       var info: (addr label-info) = get-slice(labels, datum)
1849     #       var curr-segment-name: (addr array byte) = lookup(info->segment-name)
1850     #       if !string-equal?(info->segment-name, "code")
1851     #         if has-metadata?(word-slice, "disp8")
1852     #           abort
1853     #         if has-metadata?(word-slice, "imm8")
1854     #           abort
1855     #         emit(out, info->address, 4)  # global variables always translate init.linux to absolute addresses
1856     #       # code segment cases
1857     #       else if has-metadata?(word-slice, "imm8")
1858     #         abort  # label should never go to imm8
1859     #       else if has-metadata?(word-slice, "imm32")
1860     #         emit(out, info->address, 4)
1861     #       else if has-metadata?(word-slice, "disp8")
1862     #         value = info->offset - offset-of-next-instruction
1863     #         emit(out, value, 1)
1864     #       else if has-metadata?(word-slice, "disp32")
1865     #         value = info->offset - offset-of-next-instruction
1866     #         emit(out, value, 4)
1867     #       else
1868     #         abort
1869     #     write-buffered(out, "\n")
1870     #
1871     # registers:
1872     #   line: ecx
1873     #   word-slice: edx
1874     #   offset-of-next-instruction: ebx
1875     #   datum: edi
1876     #   info: esi (inner loop only)
1877     #   temporaries: eax, esi (outer loop)
1878     #
1879     # . prologue
1880     55/push-ebp
1881     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
1882     # . save registers
1883     50/push-eax
1884     51/push-ecx
1885     52/push-edx
1886     53/push-ebx
1887     56/push-esi
1888     57/push-edi
1889     # var line/ecx: (stream byte 512)
1890     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               0x200/imm32       # subtract from esp
1891     68/push  0x200/imm32/size
1892     68/push  0/imm32/read
1893     68/push  0/imm32/write
1894     89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
1895     # var word-slice/edx: slice
1896     68/push  0/imm32/end
1897     68/push  0/imm32/start
1898     89/copy                         3/mod/direct    2/rm32/edx    .           .             .           4/r32/esp   .               .                 # copy esp to edx
1899     # var datum/edi: slice
1900     68/push  0/imm32/end
1901     68/push  0/imm32/start
1902     89/copy                         3/mod/direct    7/rm32/edi    .           .             .           4/r32/esp   .               .                 # copy esp to edi
1903     # offset-of-next-instruction/ebx = 0
1904     31/xor                          3/mod/direct    3/rm32/ebx    .           .             .           3/r32/ebx   .               .                 # clear ebx
1905 $emit-segments:line-loop:
1906     # clear-stream(line)
1907     # . . push args
1908     51/push-ecx
1909     # . . call
1910     e8/call  clear-stream/disp32
1911     # . . discard args
1912     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
1913     # read-line(in, line)
1914     # . . push args
1915     51/push-ecx
1916     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
1917     # . . call
1918     e8/call  read-line/disp32
1919     # . . discard args
1920     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1921 +-- 33 lines: #?     # dump line --------------------------------------------------------------------------------------------------------------------------------------------------------
1954 $emit-segments:check-for-end-of-input:
1955     # if (line->write == 0) break
1956     81          7/subop/compare     0/mod/indirect  1/rm32/ecx    .           .             .           .           .               0/imm32           # compare *ecx
1957     0f 84/jump-if-=  $emit-segments:end/disp32
1958     # offset-of-next-instruction += num-bytes(line)
1959     # . eax = num-bytes(line)
1960     # . . push args
1961     51/push-ecx
1962     # . . call
1963     e8/call  num-bytes/disp32
1964     # . . discard args
1965     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
1966     # . ebx += eax
1967     01/add                          3/mod/direct    3/rm32/ebx    .           .             .           0/r32/eax   .               .                 # add eax to ebx
1968 $emit-segments:word-loop:
1969     # next-word(line, word-slice)
1970     # . . push args
1971     52/push-edx
1972     51/push-ecx
1973     # . . call
1974     e8/call  next-word/disp32
1975     # . . discard args
1976     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
1977 +-- 33 lines: #?     # dump word-slice --------------------------------------------------------------------------------------------------------------------------------------------------
2010 $emit-segments:check-for-end-of-line:
2011     # if (slice-empty?(word-slice)) break
2012     # . eax = slice-empty?(word-slice)
2013     # . . push args
2014     52/push-edx
2015     # . . call
2016     e8/call  slice-empty?/disp32
2017     # . . discard args
2018     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
2019     # . if (eax != 0) break
2020     3d/compare-eax-and  0/imm32/false
2021     0f 85/jump-if-!=  $emit-segments:next-line/disp32
2022 $emit-segments:check-for-comment:
2023     # if (slice-starts-with?(word-slice, "#")) break
2024     # . start/esi = word-slice->start
2025     8b/copy                         0/mod/indirect  2/rm32/edx    .           .             .           6/r32/esi   .               .                 # copy *edx to esi
2026     # . c/eax = *start
2027     31/xor                          3/mod/direct    0/rm32/eax    .           .             .           0/r32/eax   .               .                 # clear eax
2028     8a/copy-byte                    0/mod/indirect  6/rm32/esi    .           .             .           0/r32/AL    .               .                 # copy byte at *esi to AL
2029     # . if (eax == '#') break
2030     3d/compare-eax-and  0x23/imm32/hash
2031     0f 84/jump-if-=  $emit-segments:next-line/disp32
2032 $emit-segments:check-for-label:
2033     # if is-label?(word-slice) break
2034     # . eax = is-label?(word-slice)
2035     # . . push args
2036     52/push-edx
2037     # . . call
2038     e8/call  is-label?/disp32
2039     # . . discard args
2040     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
2041     # . if (eax != false) break
2042     3d/compare-eax-and  0/imm32/false
2043     0f 85/jump-if-!=  $emit-segments:line-loop/disp32
2044 $emit-segments:check-for-segment-header:
2045     # if (slice-equal?(word-slice, "==")) break
2046     # . eax = slice-equal?(word-slice, "==")
2047     # . . push args
2048     68/push  "=="/imm32
2049     52/push-edx
2050     # . . call
2051     e8/call  slice-equal?/disp32
2052     # . . discard args
2053     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2054     # . if (eax != false) break
2055     3d/compare-eax-and  0/imm32/false
2056     0f 85/jump-if-!=  $emit-segments:line-loop/disp32
2057 $emit-segments:2-character:
2058     # if (size(word-slice) != 2) goto next check
2059     # . eax = size(word-slice)
2060     8b/copy                         1/mod/*+disp8   2/rm32/edx    .           .             .           0/r32/eax   4/disp8         .                 # copy *(edx+4) to eax
2061     2b/subtract                     0/mod/indirect  2/rm32/edx    .           .             .           0/r32/eax   .               .                 # subtract *edx from eax
2062     # . if (eax != 2) goto next check
2063     3d/compare-eax-and  2/imm32
2064     75/jump-if-!=  $emit-segments:check-metadata/disp8
2065     # write-slice-buffered(out, word-slice)
2066     # . . push args
2067     52/push-edx
2068     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
2069     # . . call
2070     e8/call  write-slice-buffered/disp32
2071     # . . discard args
2072     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2073     # write-buffered(out, " ")
2074     # . . push args
2075     68/push  Space/imm32
2076     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
2077     # . . call
2078     e8/call  write-buffered/disp32
2079     # . . discard args
2080     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2081     # continue
2082     e9/jump  $emit-segments:word-loop/disp32
2083 $emit-segments:check-metadata:
2084     # - if we get here, 'word-slice' must be a label to be looked up
2085     # datum/edi = next-token-from-slice(word-slice->start, word-slice->end, "/")
2086     # . . push args
2087     57/push-edi
2088     68/push  0x2f/imm32/slash
2089     ff          6/subop/push        1/mod/*+disp8   2/rm32/edx    .           .             .           .           4/disp8         .                 # push *(edx+4)
2090     ff          6/subop/push        0/mod/indirect  2/rm32/edx    .           .             .           .           .               .                 # push *edx
2091     # . . call
2092     e8/call  next-token-from-slice/disp32
2093     # . . discard args
2094     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
2095 +-- 33 lines: #?     # dump datum -------------------------------------------------------------------------------------------------------------------------------------------------------
2128     # info/esi = get-slice(labels, datum, row-size=24, "label table")
2129     # . eax = get-slice(labels, datum, row-size=24, "label table")
2130     # . . push args
2131     68/push  "label table"/imm32
2132     68/push  0x18/imm32/row-size
2133     57/push-edi
2134     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
2135     # . . call
2136     e8/call  get-slice/disp32
2137     # . . discard args
2138     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
2139     # . esi = eax
2140     89/copy                         3/mod/direct    6/rm32/esi    .           .             .           0/r32/eax   .               .                 # copy eax to esi
2141 $emit-segments:check-global-variable:
2142     # if string-equal?(info->segment-name, "code") goto code label checks
2143     # . eax = lookup(info->segment-name)
2144     # . . push args
2145     ff          6/subop/push        1/mod/*+disp8   6/rm32/esi    .           .             .           .           4/disp8         .                 # push *(esi+4)
2146     ff          6/subop/push        0/mod/indirect  6/rm32/esi    .           .             .           .           .               .                 # push *esi
2147     # . . call
2148     e8/call  lookup/disp32
2149     # . . discard args
2150     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2151     # . eax = string-equal?(eax, "code")
2152     # . . push args
2153     68/push  "code"/imm32
2154     50/push-eax
2155     # . . call
2156     e8/call  string-equal?/disp32
2157     # . . discard args
2158     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2159     # . if (eax != false) goto code label checks
2160     3d/compare-eax-and  0/imm32/false
2161     0f 85/jump-if-!=  $emit-segments:check-code-label-for-imm8/disp32
2162 $emit-segments:check-global-variable-for-disp8:
2163     # if has-metadata?(word-slice, "disp8") abort
2164     # . eax = has-metadata?(word-slice, "disp8")
2165     # . . push args
2166     68/push  "disp8"/imm32
2167     52/push-edx
2168     # . . call
2169     e8/call  has-metadata?/disp32
2170     # . . discard args
2171     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2172     # . if (eax != false) abort
2173     3d/compare-eax-and  0/imm32/false
2174     0f 85/jump-if-!=  $emit-segments:global-variable-abort/disp32
2175 $emit-segments:check-global-variable-for-imm8:
2176     # if has-metadata?(word-slice, "imm8") abort
2177     # . eax = has-metadata?(word-slice, "imm8")
2178     # . . push args
2179     68/push  "imm8"/imm32
2180     52/push-edx
2181     # . . call
2182     e8/call  has-metadata?/disp32
2183     # . . discard args
2184     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2185     # . if (eax != false) abort
2186     3d/compare-eax-and  0/imm32/false
2187     0f 85/jump-if-!=  $emit-segments:global-variable-abort/disp32
2188 $emit-segments:emit-global-variable:
2189     # emit-hex(out, info->address, 4)
2190     # . . push args
2191     68/push  4/imm32
2192     ff          6/subop/push        1/mod/*+disp8   6/rm32/esi    .           .             .           .           0xc/disp8       .                 # push *(esi+12)
2193     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
2194     # . . call
2195     e8/call  emit-hex/disp32
2196     # . . discard args
2197     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
2198     # continue
2199     e9/jump  $emit-segments:word-loop/disp32
2200 $emit-segments:check-code-label-for-imm8:
2201     # if (has-metadata?(word-slice, "imm8")) abort
2202     # . eax = has-metadata?(edx, "imm8")
2203     # . . push args
2204     68/push  "imm8"/imm32
2205     52/push-edx
2206     # . . call
2207     e8/call  has-metadata?/disp32
2208     # . . discard args
2209     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2210     # . if (eax != false) abort
2211     3d/compare-eax-and  0/imm32/false
2212     0f 85/jump-if-!=  $emit-segments:imm8-abort/disp32
2213 $emit-segments:check-code-label-for-imm32:
2214     # if (!has-metadata?(word-slice, "imm32")) goto next check
2215     # . eax = has-metadata?(edx, "imm32")
2216     # . . push args
2217     68/push  "imm32"/imm32
2218     52/push-edx
2219     # . . call
2220     e8/call  has-metadata?/disp32
2221     # . . discard args
2222     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2223     # . if (eax == false) goto next check
2224     3d/compare-eax-and  0/imm32/false
2225     74/jump-if-=  $emit-segments:check-code-label-for-disp8/disp8
2226 +-- 33 lines: #?     # dump info->address -----------------------------------------------------------------------------------------------------------------------------------------------
2259 $emit-segments:emit-code-label-imm32:
2260     # emit-hex(out, info->address, 4)
2261     # . . push args
2262     68/push  4/imm32
2263     ff          6/subop/push        1/mod/*+disp8   6/rm32/esi    .           .             .           .           0xc/disp8       .                 # push *(esi+12)
2264     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
2265     # . . call
2266     e8/call  emit-hex/disp32
2267     # . . discard args
2268     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
2269     # continue
2270     e9/jump  $emit-segments:word-loop/disp32
2271 $emit-segments:check-code-label-for-disp8:
2272     # if (!has-metadata?(word-slice, "disp8")) goto next check
2273     # . eax = has-metadata?(edx, "disp8")
2274     # . . push args
2275     68/push  "disp8"/imm32
2276     52/push-edx
2277     # . . call
2278     e8/call  has-metadata?/disp32
2279     # . . discard args
2280     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2281     # . if (eax == false) goto next check
2282     3d/compare-eax-and  0/imm32/false
2283     74/jump-if-=  $emit-segments:check-code-label-for-disp32/disp8
2284 $emit-segments:emit-code-label-disp8:
2285     # emit-hex(out, info->offset - offset-of-next-instruction, 1)
2286     # . . push args
2287     68/push  1/imm32
2288     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   8/disp8         .                 # copy *(esi+8) to eax
2289     29/subtract                     3/mod/direct    0/rm32/eax    .           .             .           3/r32/ebx   .               .                 # subtract ebx from eax
2290     50/push-eax
2291     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
2292     # . . call
2293     e8/call  emit-hex/disp32
2294     # . . discard args
2295     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
2296     # continue
2297     e9/jump  $emit-segments:word-loop/disp32
2298 $emit-segments:check-code-label-for-disp32:
2299     # if (!has-metadata?(word-slice, "disp32")) abort
2300     # . eax = has-metadata?(edx, "disp32")
2301     # . . push args
2302     68/push  "disp32"/imm32
2303     52/push-edx
2304     # . . call
2305     e8/call  has-metadata?/disp32
2306     # . . discard args
2307     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2308     # . if (eax == false) abort
2309     3d/compare-eax-and  0/imm32/false
2310     0f 84/jump-if-=  $emit-segments:abort/disp32
2311 $emit-segments:emit-code-label-disp32:
2312     # emit-hex(out, info->offset - offset-of-next-instruction, 4)
2313     # . . push args
2314     68/push  4/imm32
2315     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   8/disp8         .                 # copy *(esi+8) to eax
2316     29/subtract                     3/mod/direct    0/rm32/eax    .           .             .           3/r32/ebx   .               .                 # subtract ebx from eax
2317     50/push-eax
2318     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
2319     # . . call
2320     e8/call  emit-hex/disp32
2321     # . . discard args
2322     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
2323     # continue
2324     e9/jump  $emit-segments:word-loop/disp32
2325 $emit-segments:next-line:
2326     # write-buffered(out, "\n")
2327     # . . push args
2328     68/push  Newline/imm32
2329     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
2330     # . . call
2331     e8/call  write-buffered/disp32
2332     # . . discard args
2333     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2334     # loop
2335     e9/jump  $emit-segments:line-loop/disp32
2336 $emit-segments:end:
2337     # . reclaim locals
2338     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x21c/imm32       # add to esp
2339     # . restore registers
2340     5f/pop-to-edi
2341     5e/pop-to-esi
2342     5b/pop-to-ebx
2343     5a/pop-to-edx
2344     59/pop-to-ecx
2345     58/pop-to-eax
2346     # . epilogue
2347     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
2348     5d/pop-to-ebp
2349     c3/return
2350 
2351 $emit-segments:global-variable-abort:
2352     # . _write(2/stderr, error)
2353     # . . push args
2354     68/push  "emit-segments: must refer to global variables with /disp32 or /imm32"/imm32
2355     68/push  2/imm32/stderr
2356     # . . call
2357     e8/call  _write/disp32
2358     # . . discard args
2359     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2360     # . syscall(exit, 1)
2361     bb/copy-to-ebx  1/imm32
2362     e8/call  syscall_exit/disp32
2363     # never gets here
2364 
2365 $emit-segments:imm8-abort:
2366     # . _write(2/stderr, error)
2367     # . . push args
2368     68/push  "emit-segments: cannot refer to code labels with /imm8"/imm32
2369     68/push  2/imm32/stderr
2370     # . . call
2371     e8/call  _write/disp32
2372     # . . discard args
2373     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2374     # . syscall(exit, 1)
2375     bb/copy-to-ebx  1/imm32
2376     e8/call  syscall_exit/disp32
2377     # never gets here
2378 
2379 $emit-segments:abort:
2380     # print(stderr, "missing metadata in " word-slice)
2381     # . _write(2/stderr, "missing metadata in word ")
2382     # . . push args
2383     68/push  "emit-segments: missing metadata in "/imm32
2384     68/push  2/imm32/stderr
2385     # . . call
2386     e8/call  _write/disp32
2387     # . . discard args
2388     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2389     # . write-slice-buffered(Stderr, word-slice)
2390     # . . push args
2391     52/push-edx
2392     68/push  Stderr/imm32
2393     # . . call
2394     e8/call  write-slice-buffered/disp32
2395     # . . discard args
2396     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2397     # . flush(Stderr)
2398     # . . push args
2399     68/push  Stderr/imm32
2400     # . . call
2401     e8/call  flush/disp32
2402     # . . discard args
2403     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
2404     # . syscall(exit, 1)
2405     bb/copy-to-ebx  1/imm32
2406     e8/call  syscall_exit/disp32
2407     # never gets here
2408 
2409 test-emit-segments-global-variable:
2410     # global variables always convert to absolute addresses, regardless of metadata
2411     #
2412     # input:
2413     #   in:
2414     #     == code 0x1000
2415     #     ab cd ef gh
2416     #     ij x/disp32
2417     #     == data 0x2000
2418     #     00
2419     #     x:
2420     #       34
2421     #   labels:
2422     #     - 'x': {'data', 1, 0x207a}
2423     #
2424     # output:
2425     #   ab cd ef gh
2426     #   ij 7a 20 00 00
2427     #   00
2428     #   34
2429     #
2430     # . prologue
2431     55/push-ebp
2432     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
2433     # setup
2434     # . clear-stream(_test-input-stream)
2435     # . . push args
2436     68/push  _test-input-stream/imm32
2437     # . . call
2438     e8/call  clear-stream/disp32
2439     # . . discard args
2440     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
2441     # . clear-stream(_test-output-stream)
2442     # . . push args
2443     68/push  _test-output-stream/imm32
2444     # . . call
2445     e8/call  clear-stream/disp32
2446     # . . discard args
2447     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
2448     # . clear-stream($_test-output-buffered-file->buffer)
2449     # . . push args
2450     68/push  $_test-output-buffered-file->buffer/imm32
2451     # . . call
2452     e8/call  clear-stream/disp32
2453     # . . discard args
2454     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
2455     # . var labels/edx: (stream byte 8*24)
2456     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc0/imm32        # subtract from esp
2457     68/push  0xc0/imm32/size
2458     68/push  0/imm32/read
2459     68/push  0/imm32/write
2460     89/copy                         3/mod/direct    2/rm32/edx    .           .             .           4/r32/esp   .               .                 # copy esp to edx
2461     # . var h/ebx: (handle array byte)
2462     68/push  0/imm32
2463     68/push  0/imm32
2464     89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           4/r32/esp   .               .                 # copy esp to ebx
2465     # initialize input
2466     # . write(_test-input-stream, "== code 0x1000\n")
2467     # . . push args
2468     68/push  "== code 0x1000\n"/imm32
2469     68/push  _test-input-stream/imm32
2470     # . . call
2471     e8/call  write/disp32
2472     # . . discard args
2473     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2474     # . write(_test-input-stream, "ab cd ef gh\n")
2475     # . . push args
2476     68/push  "ab cd ef gh\n"/imm32
2477     68/push  _test-input-stream/imm32
2478     # . . call
2479     e8/call  write/disp32
2480     # . . discard args
2481     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2482     # . write(_test-input-stream, "ij x/disp32\n")
2483     # . . push args
2484     68/push  "ij x/disp32\n"/imm32
2485     68/push  _test-input-stream/imm32
2486     # . . call
2487     e8/call  write/disp32
2488     # . . discard args
2489     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2490     # . write(_test-input-stream, "== data 0x2000\n")
2491     # . . push args
2492     68/push  "== data 0x2000\n"/imm32
2493     68/push  _test-input-stream/imm32
2494     # . . call
2495     e8/call  write/disp32
2496     # . . discard args
2497     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2498     # . write(_test-input-stream, "00\n")
2499     # . . push args
2500     68/push  "00\n"/imm32
2501     68/push  _test-input-stream/imm32
2502     # . . call
2503     e8/call  write/disp32
2504     # . . discard args
2505     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2506     # . write(_test-input-stream, "x:\n")
2507     # . . push args
2508     68/push  "x:\n"/imm32
2509     68/push  _test-input-stream/imm32
2510     # . . call
2511     e8/call  write/disp32
2512     # . . discard args
2513     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2514     # . write(_test-input-stream, "34\n")
2515     # . . push args
2516     68/push  "34\n"/imm32
2517     68/push  _test-input-stream/imm32
2518     # . . call
2519     e8/call  write/disp32
2520     # . . discard args
2521     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2522     # . stream-add6(labels, "x", "data", 1, 0x207a)
2523     68/push  0x207a/imm32/label-address
2524     68/push  1/imm32/segment-offset
2525     # . . push "data"
2526     53/push-ebx
2527     68/push  "data"/imm32
2528     68/push  Heap/imm32
2529     e8/call  copy-array/disp32
2530     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
2531     ff          6/subop/push        1/mod/*+disp8   3/rm32/ebx    .           .             .           .           4/disp8         .                 # push *(ebx+4)
2532     ff          6/subop/push        0/mod/indirect  3/rm32/ebx    .           .             .           .           .               .                 # push *ebx
2533     # . . push "l1"
2534     53/push-ebx
2535     68/push  "x"/imm32
2536     68/push  Heap/imm32
2537     e8/call  copy-array/disp32
2538     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
2539     ff          6/subop/push        1/mod/*+disp8   3/rm32/ebx    .           .             .           .           4/disp8         .                 # push *(ebx+4)
2540     ff          6/subop/push        0/mod/indirect  3/rm32/ebx    .           .             .           .           .               .                 # push *ebx
2541     # . . push labels
2542     52/push-edx
2543     # . . call
2544     e8/call  stream-add6/disp32
2545     # . . discard args
2546     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x1c/imm32        # add to esp
2547     # component under test
2548     # . emit-segments(_test-input-stream, _test-output-buffered-file, labels)
2549     # . . push args
2550     52/push-edx
2551     68/push  _test-output-buffered-file/imm32
2552     68/push  _test-input-stream/imm32
2553     # . . call
2554     e8/call  emit-segments/disp32
2555     # . . discard args
2556     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
2557     # checks
2558     # . flush(_test-output-buffered-file)
2559     # . . push args
2560     68/push  _test-output-buffered-file/imm32
2561     # . . call
2562     e8/call  flush/disp32
2563     # . . discard args
2564     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
2565 +-- 33 lines: #?     # dump output ------------------------------------------------------------------------------------------------------------------------------------------------------
2598     # . check-next-stream-line-equal(_test-output-stream, "ab cd ef gh ", msg)
2599     # . . push args
2600     68/push  "F - test-emit-segments-global-variable/0"/imm32
2601     68/push  "ab cd ef gh "/imm32
2602     68/push  _test-output-stream/imm32
2603     # . . call
2604     e8/call  check-next-stream-line-equal/disp32
2605     # . . discard args
2606     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
2607     # . check-next-stream-line-equal(_test-output-stream, "ij 7a 20 00 00 ", msg)
2608     # . . push args
2609     68/push  "F - test-emit-segments-global-variable/1"/imm32
2610     68/push  "ij 7a 20 00 00 "/imm32
2611     68/push  _test-output-stream/imm32
2612     # . . call
2613     e8/call  check-next-stream-line-equal/disp32
2614     # . . discard args
2615     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
2616     # . check-next-stream-line-equal(_test-output-stream, "00 ", msg)
2617     # . . push args
2618     68/push  "F - test-emit-segments-global-variable/2"/imm32
2619     68/push  "00 "/imm32
2620     68/push  _test-output-stream/imm32
2621     # . . call
2622     e8/call  check-next-stream-line-equal/disp32
2623     # . . discard args
2624     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
2625     # . check-next-stream-line-equal(_test-output-stream, "34 ", msg)
2626     # . . push args
2627     68/push  "F - test-emit-segments-global-variable/3"/imm32
2628     68/push  "34 "/imm32
2629     68/push  _test-output-stream/imm32
2630     # . . call
2631     e8/call  check-next-stream-line-equal/disp32
2632     # . . discard args
2633     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
2634     # . epilogue
2635     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
2636     5d/pop-to-ebp
2637     c3/return
2638 
2639 test-emit-segments-code-label:
2640     # labels usually convert to displacements
2641     #
2642     # input:
2643     #   in:
2644     #     == code 0x1000
2645     #     ab cd
2646     #     l1:
2647     #       ef gh
2648     #       ij l1/disp32
2649     #   labels:
2650     #     - 'l1': {'code', 2, 0x1056}
2651     #
2652     # output:
2653     #   ab cd
2654     #   ef gh
2655     #   ij f9 ff ff ff  # -7
2656     #
2657     # . prologue
2658     55/push-ebp
2659     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
2660     # setup
2661     # . clear-stream(_test-input-stream)
2662     # . . push args
2663     68/push  _test-input-stream/imm32
2664     # . . call
2665     e8/call  clear-stream/disp32
2666     # . . discard args
2667     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
2668     # . clear-stream(_test-output-stream)
2669     # . . push args
2670     68/push  _test-output-stream/imm32
2671     # . . call
2672     e8/call  clear-stream/disp32
2673     # . . discard args
2674     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
2675     # . clear-stream($_test-output-buffered-file->buffer)
2676     # . . push args
2677     68/push  $_test-output-buffered-file->buffer/imm32
2678     # . . call
2679     e8/call  clear-stream/disp32
2680     # . . discard args
2681     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
2682     # . var labels/edx: (stream byte 8*24)
2683     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc0/imm32        # subtract from esp
2684     68/push  0xc0/imm32/size
2685     68/push  0/imm32/read
2686     68/push  0/imm32/write
2687     89/copy                         3/mod/direct    2/rm32/edx    .           .             .           4/r32/esp   .               .                 # copy esp to edx
2688     # . var h/ebx: (handle array byte)
2689     68/push  0/imm32
2690     68/push  0/imm32
2691     89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           4/r32/esp   .               .                 # copy esp to ebx
2692     # initialize input
2693     # . write(_test-input-stream, "== code 0x1000\n")
2694     # . . push args
2695     68/push  "== code 0x1000\n"/imm32
2696     68/push  _test-input-stream/imm32
2697     # . . call
2698     e8/call  write/disp32
2699     # . . discard args
2700     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2701     # . write(_test-input-stream, "ab cd\n")
2702     # . . push args
2703     68/push  "ab cd\n"/imm32
2704     68/push  _test-input-stream/imm32
2705     # . . call
2706     e8/call  write/disp32
2707     # . . discard args
2708     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2709     # . write(_test-input-stream, "l1:\n")
2710     # . . push args
2711     68/push  "l1:\n"/imm32
2712     68/push  _test-input-stream/imm32
2713     # . . call
2714     e8/call  write/disp32
2715     # . . discard args
2716     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2717     # . write(_test-input-stream, "  ef gh\n")
2718     # . . push args
2719     68/push  "  ef gh\n"/imm32
2720     68/push  _test-input-stream/imm32
2721     # . . call
2722     e8/call  write/disp32
2723     # . . discard args
2724     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2725     # . write(_test-input-stream, "  ij l1/disp32\n")
2726     # . . push args
2727     68/push  "  ij l1/disp32\n"/imm32
2728     68/push  _test-input-stream/imm32
2729     # . . call
2730     e8/call  write/disp32
2731     # . . discard args
2732     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2733     # . stream-add6(labels, "l1", "code", 2, 0x1056)
2734     68/push  0x1056/imm32/label-address
2735     68/push  2/imm32/segment-offset
2736     # . . push "data"
2737     53/push-ebx
2738     68/push  "code"/imm32
2739     68/push  Heap/imm32
2740     e8/call  copy-array/disp32
2741     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
2742     ff          6/subop/push        1/mod/*+disp8   3/rm32/ebx    .           .             .           .           4/disp8         .                 # push *(ebx+4)
2743     ff          6/subop/push        0/mod/indirect  3/rm32/ebx    .           .             .           .           .               .                 # push *ebx
2744     # . . push "l1"
2745     53/push-ebx
2746     68/push  "l1"/imm32
2747     68/push  Heap/imm32
2748     e8/call  copy-array/disp32
2749     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
2750     ff          6/subop/push        1/mod/*+disp8   3/rm32/ebx    .           .             .           .           4/disp8         .                 # push *(ebx+4)
2751     ff          6/subop/push        0/mod/indirect  3/rm32/ebx    .           .             .           .           .               .                 # push *ebx
2752     # . . push labels
2753     52/push-edx
2754     # . . call
2755     e8/call  stream-add6/disp32
2756     # . . discard args
2757     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x1c/imm32        # add to esp
2758     # component under test
2759     # . emit-segments(_test-input-stream, _test-output-buffered-file, labels)
2760     # . . push args
2761     52/push-edx
2762     68/push  _test-output-buffered-file/imm32
2763     68/push  _test-input-stream/imm32
2764     # . . call
2765     e8/call  emit-segments/disp32
2766     # . . discard args
2767     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
2768     # checks
2769     # . flush(_test-output-buffered-file)
2770     # . . push args
2771     68/push  _test-output-buffered-file/imm32
2772     # . . call
2773     e8/call  flush/disp32
2774     # . . discard args
2775     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
2776 +-- 33 lines: #?     # dump output ------------------------------------------------------------------------------------------------------------------------------------------------------
2809     # . check-next-stream-line-equal(_test-output-stream, "ab cd ", msg)
2810     # . . push args
2811     68/push  "F - test-emit-segments-code-label/0"/imm32
2812     68/push  "ab cd "/imm32
2813     68/push  _test-output-stream/imm32
2814     # . . call
2815     e8/call  check-next-stream-line-equal/disp32
2816     # . . discard args
2817     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
2818     # . check-next-stream-line-equal(_test-output-stream, "ef gh ", msg)
2819     # . . push args
2820     68/push  "F - test-emit-segments-code-label/1"/imm32
2821     68/push  "ef gh "/imm32
2822     68/push  _test-output-stream/imm32
2823     # . . call
2824     e8/call  check-next-stream-line-equal/disp32
2825     # . . discard args
2826     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
2827     # . check-next-stream-line-equal(_test-output-stream, "ij f9 ff ff ff ", msg)
2828     # . . push args
2829     68/push  "F - test-emit-segments-code-label/2"/imm32
2830     68/push  "ij f9 ff ff ff "/imm32
2831     68/push  _test-output-stream/imm32
2832     # . . call
2833     e8/call  check-next-stream-line-equal/disp32
2834     # . . discard args
2835     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
2836     # . epilogue
2837     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
2838     5d/pop-to-ebp
2839     c3/return
2840 
2841 test-emit-segments-code-label-absolute:
2842     # labels can also convert to absolute addresses
2843     #
2844     # input:
2845     #   in:
2846     #     == code 0x1000
2847     #     ab cd
2848     #     l1:
2849     #       ef gh
2850     #       ij l1/imm32
2851     #   labels:
2852     #     - 'l1': {'code', 2, 0x1056}
2853     #
2854     # output:
2855     #   ab cd
2856     #   ef gh
2857     #   ij 56 10 00 00
2858     #
2859     # . prologue
2860     55/push-ebp
2861     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
2862     # setup
2863     # . clear-stream(_test-input-stream)
2864     # . . push args
2865     68/push  _test-input-stream/imm32
2866     # . . call
2867     e8/call  clear-stream/disp32
2868     # . . discard args
2869     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
2870     # . clear-stream(_test-output-stream)
2871     # . . push args
2872     68/push  _test-output-stream/imm32
2873     # . . call
2874     e8/call  clear-stream/disp32
2875     # . . discard args
2876     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
2877     # . clear-stream($_test-output-buffered-file->buffer)
2878     # . . push args
2879     68/push  $_test-output-buffered-file->buffer/imm32
2880     # . . call
2881     e8/call  clear-stream/disp32
2882     # . . discard args
2883     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
2884     # . var labels/edx: (stream byte 8*24)
2885     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc0/imm32        # subtract from esp
2886     68/push  0xc0/imm32/size
2887     68/push  0/imm32/read
2888     68/push  0/imm32/write
2889     89/copy                         3/mod/direct    2/rm32/edx    .           .             .           4/r32/esp   .               .                 # copy esp to edx
2890     # . var h/ebx: (handle array byte)
2891     68/push  0/imm32
2892     68/push  0/imm32
2893     89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           4/r32/esp   .               .                 # copy esp to ebx
2894     # initialize input
2895     # . write(_test-input-stream, "== code 0x1000\n")
2896     # . . push args
2897     68/push  "== code 0x1000\n"/imm32
2898     68/push  _test-input-stream/imm32
2899     # . . call
2900     e8/call  write/disp32
2901     # . . discard args
2902     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2903     # . write(_test-input-stream, "ab cd\n")
2904     # . . push args
2905     68/push  "ab cd\n"/imm32
2906     68/push  _test-input-stream/imm32
2907     # . . call
2908     e8/call  write/disp32
2909     # . . discard args
2910     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2911     # . write(_test-input-stream, "l1:\n")
2912     # . . push args
2913     68/push  "l1:\n"/imm32
2914     68/push  _test-input-stream/imm32
2915     # . . call
2916     e8/call  write/disp32
2917     # . . discard args
2918     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2919     # . write(_test-input-stream, "  ef gh\n")
2920     # . . push args
2921     68/push  "  ef gh\n"/imm32
2922     68/push  _test-input-stream/imm32
2923     # . . call
2924     e8/call  write/disp32
2925     # . . discard args
2926     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2927     # . write(_test-input-stream, "  ij l1/imm32\n")
2928     # . . push args
2929     68/push  "  ij l1/imm32\n"/imm32
2930     68/push  _test-input-stream/imm32
2931     # . . call
2932     e8/call  write/disp32
2933     # . . discard args
2934     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
2935     # . stream-add6(labels, "l1", "code", 2, 0x1056)
2936     68/push  0x1056/imm32/label-address
2937     68/push  2/imm32/segment-offset
2938     # . . push "data"
2939     53/push-ebx
2940     68/push  "code"/imm32
2941     68/push  Heap/imm32
2942     e8/call  copy-array/disp32
2943     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
2944     ff          6/subop/push        1/mod/*+disp8   3/rm32/ebx    .           .             .           .           4/disp8         .                 # push *(ebx+4)
2945     ff          6/subop/push        0/mod/indirect  3/rm32/ebx    .           .             .           .           .               .                 # push *ebx
2946     # . . push "l1"
2947     53/push-ebx
2948     68/push  "l1"/imm32
2949     68/push  Heap/imm32
2950     e8/call  copy-array/disp32
2951     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
2952     ff          6/subop/push        1/mod/*+disp8   3/rm32/ebx    .           .             .           .           4/disp8         .                 # push *(ebx+4)
2953     ff          6/subop/push        0/mod/indirect  3/rm32/ebx    .           .             .           .           .               .                 # push *ebx
2954     # . . push labels
2955     52/push-edx
2956     # . . call
2957     e8/call  stream-add6/disp32
2958     # . . discard args
2959     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x1c/imm32        # add to esp
2960     # component under test
2961     # . emit-segments(_test-input-stream, _test-output-buffered-file, labels)
2962     # . . push args
2963     52/push-edx
2964     68/push  _test-output-buffered-file/imm32
2965     68/push  _test-input-stream/imm32
2966     # . . call
2967     e8/call  emit-segments/disp32
2968     # . . discard args
2969     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
2970     # checks
2971     # . flush(_test-output-buffered-file)
2972     # . . push args
2973     68/push  _test-output-buffered-file/imm32
2974     # . . call
2975     e8/call  flush/disp32
2976     # . . discard args
2977     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
2978 +-- 33 lines: #?     # dump output ------------------------------------------------------------------------------------------------------------------------------------------------------
3011     # . check-next-stream-line-equal(_test-output-stream, "ab cd ", msg)
3012     # . . push args
3013     68/push  "F - test-emit-segments-code-label-absolute/0"/imm32
3014     68/push  "ab cd "/imm32
3015     68/push  _test-output-stream/imm32
3016     # . . call
3017     e8/call  check-next-stream-line-equal/disp32
3018     # . . discard args
3019     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
3020     # . check-next-stream-line-equal(_test-output-stream, "ef gh ", msg)
3021     # . . push args
3022     68/push  "F - test-emit-segments-code-label-absolute/1"/imm32
3023     68/push  "ef gh "/imm32
3024     68/push  _test-output-stream/imm32
3025     # . . call
3026     e8/call  check-next-stream-line-equal/disp32
3027     # . . discard args
3028     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
3029     # . check-next-stream-line-equal(_test-output-stream, "ij f9 ff ff ff ", msg)
3030     # . . push args
3031     68/push  "F - test-emit-segments-code-label-absolute/2"/imm32
3032     68/push  "ij 56 10 00 00 "/imm32
3033     68/push  _test-output-stream/imm32
3034     # . . call
3035     e8/call  check-next-stream-line-equal/disp32
3036     # . . discard args
3037     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
3038     # . epilogue
3039     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
3040     5d/pop-to-ebp
3041     c3/return
3042 
3043 emit-headers:  # out: (addr buffered-file), segments: (addr stream {(handle array byte), segment-info}), labels: (addr stream {(handle array byte), label-info})
3044     # pseudocode:
3045     #   emit-elf-header(out, segments, labels)
3046     #   var curr-segment-row: (addr handle array byte) = segments->data
3047     #   max = &segments->data[segments->write]
3048     #   while true
3049     #     if (curr-segment >= max) break
3050     #     emit-elf-program-header-entry(out, curr-segment-row)
3051     #     curr-segment-row += 20                        # size of a row
3052     #
3053     # . prologue
3054     55/push-ebp
3055     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
3056     # . save registers
3057     50/push-eax
3058     51/push-ecx
3059     # emit-elf-header(out, segments, labels)
3060     # . . push args
3061     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
3062     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
3063     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
3064     # . . call
3065     e8/call  emit-elf-header/disp32
3066     # . . discard args
3067     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
3068     # eax = segments
3069     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   0xc/disp8       .                 # copy *(ebp+12) to eax
3070     # ecx = segments->write
3071     8b/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy *eax to ecx
3072     # curr-segment/eax = segments->data
3073     8d/copy-address                 1/mod/*+disp8   0/rm32/eax    .           .             .           0/r32/eax   0xc/disp8       .                 # copy eax+12 to eax
3074     # max/ecx = &segments->data[segments->write]
3075     01/add                          3/mod/direct    1/rm32/ecx    .           .             .           0/r32/eax   .               .                 # add eax to ecx
3076 $emit-headers:loop:
3077     # if (curr-segment >= max) break
3078     39/compare                      3/mod/direct    0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # compare eax with ecx
3079     0f 83/jump-if-addr>=  $emit-headers:end/disp32
3080 +-- 63 lines: #?     # dump curr-segment->name ------------------------------------------------------------------------------------------------------------------------------------------
3143 +--  9 lines: #?     # write(2/stderr, "emit-segment-header\n") -------------------------------------------------------------------------------------------------------------------------
3152     # emit-elf-program-header-entry(out, curr-segment)
3153     # . . push args
3154     50/push-eax
3155     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
3156     # . . call
3157     e8/call  emit-elf-program-header-entry/disp32
3158     # . . discard args
3159     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3160     # curr-segment += 20                        # size of a row
3161     81          0/subop/add         3/mod/direct    0/rm32/eax    .           .             .           .           .               0x14/imm32        # add to eax
3162     e9/jump  $emit-headers:loop/disp32
3163 $emit-headers:end:
3164     # . restore registers
3165     59/pop-to-ecx
3166     58/pop-to-eax
3167     # . epilogue
3168     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
3169     5d/pop-to-ebp
3170     c3/return
3171 
3172 emit-elf-header:  # out: (addr buffered-file), segments: (addr stream {(handle array byte), segment-info}), labels: (addr stream {(handle array byte), label-info})
3173     # pseudocode
3174     #   *$Elf_e_entry = get(labels, "Entry")->address
3175     #   *$Elf_e_phnum = segments->write / 20         # size of a row
3176     #   emit-hex-array(out, Elf_header)
3177     #   write-buffered(out, "\n")
3178     #
3179     # . prologue
3180     55/push-ebp
3181     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
3182     # . save registers
3183     50/push-eax
3184     51/push-ecx
3185     52/push-edx  # just because we need to call idiv
3186     # *$Elf_e_entry = get(labels, "Entry")->address
3187     # . eax = labels
3188     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   0x10/disp8      .                 # copy *(ebp+16) to eax
3189     # . label-info/eax = get(labels, "Entry", row-size=24, "label table")
3190     # . . push args
3191     68/push  "label table"/imm32
3192     68/push  0x18/imm32/row-size
3193     68/push  "Entry"/imm32
3194     50/push-eax
3195     # . . call
3196     e8/call  get/disp32
3197     # . . discard args
3198     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
3199     # . eax = label-info->address
3200     8b/copy                         1/mod/*+disp8   0/rm32/eax    .           .             .           0/r32/eax   0xc/disp8       .                 # copy *(eax+12) to eax
3201     # . *$Elf_e_entry = eax
3202     89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   $Elf_e_entry/disp32               # copy eax to *$Elf_e_entry
3203     # *$Elf_e_phnum = segments->write / 20
3204     # . eax = segments
3205     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   0xc/disp8       .                 # copy *(ebp+12) to eax
3206     # . len/eax = segments->write
3207     8b/copy                         0/mod/indirect  0/rm32/eax    .           .             .           0/r32/eax   .               .                 # copy *eax to eax
3208     # . eax = len / 20  (clobbering ecx and edx)
3209     b9/copy-to-ecx  0x14/imm32
3210     31/xor                          3/mod/direct    2/rm32/edx    .           .             .           2/r32/edx   .               .                 # clear edx
3211     f7          7/subop/idiv        3/mod/direct    1/rm32/ecx    .           .             .           .           .               .                 # divide edx:eax by ecx, storing quotient in eax and remainder in edx
3212     # . *$Elf_e_phnum = eax
3213     89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   $Elf_e_phnum/disp32               # copy eax to *$Elf_e_phnum
3214     # emit-hex-array(out, Elf_header)
3215     # . . push args
3216     68/push  Elf_header/imm32
3217     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
3218     # . . call
3219     e8/call  emit-hex-array/disp32
3220     # . . discard args
3221     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3222     # write-buffered(out, "\n")
3223     # . . push args
3224     68/push  Newline/imm32
3225     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
3226     # . . call
3227     e8/call  write-buffered/disp32
3228     # . . discard args
3229     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3230 $emit-elf-header:end:
3231     # . restore registers
3232     5a/pop-to-edx
3233     59/pop-to-ecx
3234     58/pop-to-eax
3235     # . epilogue
3236     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
3237     5d/pop-to-ebp
3238     c3/return
3239 
3240 # segment-info: {address, file-offset, size}                                  (12 bytes)
3241 # segments: (addr stream {(handle array byte), segment-info})                 (20 bytes per row)
3242 emit-elf-program-header-entry:  # out: (addr buffered-file), curr-segment: (addr {(handle array byte), segment-info})
3243     # pseudocode:
3244     #   *$Elf_p_offset = curr-segment->file-offset
3245     #   *$Elf_p_vaddr = curr-segment->address
3246     #   *$Elf_p_paddr = curr-segment->address
3247     #   *$Elf_p_filesz = curr-segment->size
3248     #   *$Elf_p_memsz = curr-segment->size
3249     #   if curr-segment->name == "code"
3250     #     *$Elf_p_flags = 5  # r-x
3251     #   else
3252     #     *$Elf_p_flags = 6  # rw-
3253     #   emit-hex-array(out, Elf_program_header_entry)
3254     #   write-buffered(out, "\n")
3255     #
3256     # . prologue
3257     55/push-ebp
3258     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
3259     # . save registers
3260     50/push-eax
3261     56/push-esi
3262     # esi = curr-segment
3263     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   0xc/disp8       .                 # copy *(ebp+12) to esi
3264     # *$Elf_p_offset = curr-segment->file-offset
3265     # . eax = curr-segment->file-offset
3266     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   0xc/disp8       .                 # copy *(esi+12) to eax
3267     # . *$Elf_p_offset = eax
3268     89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   $Elf_p_offset/disp32              # copy eax to *$Elf_p_offset
3269     # *$Elf_p_vaddr = curr-segment->address
3270     # . eax = curr-segment->address
3271     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   8/disp8         .                 # copy *(esi+8) to eax
3272     # . *$Elf_p_vaddr = eax
3273     89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   $Elf_p_vaddr/disp32               # copy eax to *$Elf_p_vaddr
3274     # *$Elf_p_paddr = curr-segment->address
3275     89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   $Elf_p_paddr/disp32               # copy eax to *$Elf_p_paddr
3276     # *$Elf_p_filesz = curr-segment->size
3277     # . eax = curr-segment->size
3278     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   0x10/disp8       .                # copy *(esi+16) to eax
3279     # . *$Elf_p_filesz = eax
3280     89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   $Elf_p_filesz/disp32              # copy eax to *$Elf_p_filesz
3281     # *$Elf_p_memsz = curr-segment->size
3282     89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   $Elf_p_memsz/disp32               # copy eax to *$Elf_p_memsz
3283     # if (!string-equal?(name, "code") goto next check
3284     # . var name/eax: (addr array byte) = lookup(curr-segment->name)
3285     # . . push args
3286     ff          6/subop/push        1/mod/*+disp8   6/rm32/esi    .           .             .           .           4/disp8         .                 # push *(esi+4)
3287     ff          6/subop/push        0/mod/indirect  6/rm32/esi    .           .             .           .           .               .                 # push *esi
3288     # . . call
3289     e8/call  lookup/disp32
3290     # . . discard args
3291     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3292     # . eax = string-equal?(name, "code")
3293     # . . push args
3294     68/push  "code"/imm32
3295     50/push-eax
3296     # . . call
3297     e8/call  string-equal?/disp32
3298     # . . discard args
3299     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3300     # . if (eax == false) goto next check
3301     3d/compare-eax-and  0/imm32/false
3302     74/jump-if-=  $emit-elf-program-header-entry:data/disp8
3303     # *$Elf_p_flags = r-x
3304     c7          0/subop/copy        0/mod/indirect  5/rm32/.disp32            .             .           .           $Elf_p_flags/disp32  5/imm32      # copy to *$Elf_p_flags
3305     eb/jump  $emit-elf-program-header-entry:really-emit/disp8
3306 $emit-elf-program-header-entry:data:
3307     # otherwise *$Elf_p_flags = rw-
3308     c7          0/subop/copy        0/mod/indirect  5/rm32/.disp32            .             .           .           $Elf_p_flags/disp32  6/imm32      # copy to *$Elf_p_flags
3309 $emit-elf-program-header-entry:really-emit:
3310     # emit-hex-array(out, Elf_program_header_entry)
3311     # . . push args
3312     68/push  Elf_program_header_entry/imm32
3313     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
3314     # . . call
3315     e8/call  emit-hex-array/disp32
3316     # . . discard args
3317     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3318     # write-buffered(out, "\n")
3319     # . . push args
3320     68/push  Newline/imm32
3321     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
3322     # . . call
3323     e8/call  write-buffered/disp32
3324     # . . discard args
3325     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3326 $emit-elf-program-header-entry:end:
3327     # . restore registers
3328     5e/pop-to-esi
3329     58/pop-to-eax
3330     # . epilogue
3331     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
3332     5d/pop-to-ebp
3333     c3/return
3334 
3335 # - some helpers for tests
3336 
3337 stream-add4:  # in: (addr stream byte), key: addr, val1: addr, val2: addr, val3: addr
3338     # . prologue
3339     55/push-ebp
3340     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
3341     # . save registers
3342     50/push-eax
3343     51/push-ecx
3344     52/push-edx
3345     56/push-esi
3346     # esi = in
3347     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   8/disp8         .                 # copy *(ebp+8) to esi
3348     # curr/eax = &in->data[in->write]
3349     # . eax = in->write
3350     8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           0/r32/eax   .               .                 # copy *esi to eax
3351     # . eax = esi+eax+12
3352     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
3353     # max/edx = &in->data[in->size]
3354     # . edx = in->size
3355     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           2/r32/edx   8/disp8         .                 # copy *(esi+8) to edx
3356     # . edx = esi+edx+12
3357     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    6/base/esi  2/index/edx   .           2/r32/edx   0xc/disp8       .                 # copy esi+edx+12 to edx
3358     # if (curr >= max) abort
3359     39/compare                      3/mod/direct    0/rm32/eax    .           .             .           2/r32/edx   .               .                 # compare eax with edx
3360     73/jump-if-addr>=  $stream-add4:abort/disp8
3361     # *curr = key
3362     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         1/r32/ecx   0xc/disp8       .                 # copy *(ebp+12) to ecx
3363     89/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy ecx to *eax
3364     # curr += 4
3365     05/add-to-eax  4/imm32
3366     # if (curr >= max) abort
3367     39/compare                      3/mod/direct    0/rm32/eax    .           .             .           2/r32/edx   .               .                 # compare eax with edx
3368     73/jump-if-addr>=  $stream-add4:abort/disp8
3369     # *curr = val1
3370     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         1/r32/ecx   0x10/disp8      .                 # copy *(ebp+16) to ecx
3371     89/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy ecx to *eax
3372     # curr += 4
3373     05/add-to-eax  4/imm32
3374     # if (curr >= max) abort
3375     39/compare                      3/mod/direct    0/rm32/eax    .           .             .           2/r32/edx   .               .                 # compare eax with edx
3376     73/jump-if-addr>=  $stream-add4:abort/disp8
3377     # *curr = val2
3378     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         1/r32/ecx   0x14/disp8      .                 # copy *(ebp+20) to ecx
3379     89/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy ecx to *eax
3380     # curr += 4
3381     05/add-to-eax  4/imm32
3382     # if (curr >= max) abort
3383     39/compare                      3/mod/direct    0/rm32/eax    .           .             .           2/r32/edx   .               .                 # compare eax with edx
3384     73/jump-if-addr>=  $stream-add4:abort/disp8
3385     # *curr = val3
3386     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         1/r32/ecx   0x18/disp8      .                 # copy *(ebp+24) to ecx
3387     89/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy ecx to *eax
3388     # in->write += 16
3389     81          0/subop/add         0/mod/indirect  6/rm32/esi    .           .             .           .           .               0x10/imm32        # add to *esi
3390 $stream-add4:end:
3391     # . restore registers
3392     5e/pop-to-esi
3393     5a/pop-to-edx
3394     59/pop-to-ecx
3395     58/pop-to-eax
3396     # . epilogue
3397     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
3398     5d/pop-to-ebp
3399     c3/return
3400 
3401 $stream-add4:abort:
3402     # . _write(2/stderr, error)
3403     # . . push args
3404     68/push  "overflow in stream-add4\n"/imm32
3405     68/push  2/imm32/stderr
3406     # . . call
3407     e8/call  _write/disp32
3408     # . . discard args
3409     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3410     # . syscall(exit, 1)
3411     bb/copy-to-ebx  1/imm32
3412     e8/call  syscall_exit/disp32
3413     # never gets here
3414 
3415 stream-add5:  # in: (addr stream byte), key: handle, val1: addr, val2: addr, val3: addr
3416     # . prologue
3417     55/push-ebp
3418     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
3419     # . save registers
3420     50/push-eax
3421     51/push-ecx
3422     52/push-edx
3423     56/push-esi
3424     # esi = in
3425     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   8/disp8         .                 # copy *(ebp+8) to esi
3426     # curr/eax = &in->data[in->write]
3427     # . eax = in->write
3428     8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           0/r32/eax   .               .                 # copy *esi to eax
3429     # . eax = esi+eax+12
3430     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
3431     # max/edx = &in->data[in->size]
3432     # . edx = in->size
3433     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           2/r32/edx   8/disp8         .                 # copy *(esi+8) to edx
3434     # . edx = esi+edx+12
3435     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    6/base/esi  2/index/edx   .           2/r32/edx   0xc/disp8       .                 # copy esi+edx+12 to edx
3436     # if (curr >= max) abort
3437     39/compare                      3/mod/direct    0/rm32/eax    .           .             .           2/r32/edx   .               .                 # compare eax with edx
3438     73/jump-if-addr>=  $stream-add5:abort/disp8
3439     # *curr = key->alloc-id
3440     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         1/r32/ecx   0xc/disp8       .                 # copy *(ebp+12) to ecx
3441     89/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy ecx to *eax
3442     # curr += 4
3443     05/add-to-eax  4/imm32
3444     # if (curr >= max) abort
3445     39/compare                      3/mod/direct    0/rm32/eax    .           .             .           2/r32/edx   .               .                 # compare eax with edx
3446     73/jump-if-addr>=  $stream-add5:abort/disp8
3447     # *curr = key->payload
3448     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         1/r32/ecx   0x10/disp8      .                 # copy *(ebp+16) to ecx
3449     89/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy ecx to *eax
3450     # curr += 4
3451     05/add-to-eax  4/imm32
3452     # if (curr >= max) abort
3453     39/compare                      3/mod/direct    0/rm32/eax    .           .             .           2/r32/edx   .               .                 # compare eax with edx
3454     73/jump-if-addr>=  $stream-add5:abort/disp8
3455     # *curr = val1
3456     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         1/r32/ecx   0x14/disp8      .                 # copy *(ebp+20) to ecx
3457     89/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy ecx to *eax
3458     # curr += 4
3459     05/add-to-eax  4/imm32
3460     # if (curr >= max) abort
3461     39/compare                      3/mod/direct    0/rm32/eax    .           .             .           2/r32/edx   .               .                 # compare eax with edx
3462     73/jump-if-addr>=  $stream-add5:abort/disp8
3463     # *curr = val2
3464     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         1/r32/ecx   0x18/disp8      .                 # copy *(ebp+24) to ecx
3465     89/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy ecx to *eax
3466     # curr += 4
3467     05/add-to-eax  4/imm32
3468     # if (curr >= max) abort
3469     39/compare                      3/mod/direct    0/rm32/eax    .           .             .           2/r32/edx   .               .                 # compare eax with edx
3470     73/jump-if-addr>=  $stream-add5:abort/disp8
3471     # *curr = val3
3472     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         1/r32/ecx   0x1c/disp8      .                 # copy *(ebp+28) to ecx
3473     89/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy ecx to *eax
3474     # in->write += 20
3475     81          0/subop/add         0/mod/indirect  6/rm32/esi    .           .             .           .           .               0x14/imm32        # add to *esi
3476 $stream-add5:end:
3477     # . restore registers
3478     5e/pop-to-esi
3479     5a/pop-to-edx
3480     59/pop-to-ecx
3481     58/pop-to-eax
3482     # . epilogue
3483     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
3484     5d/pop-to-ebp
3485     c3/return
3486 
3487 $stream-add5:abort:
3488     # . _write(2/stderr, error)
3489     # . . push args
3490     68/push  "overflow in stream-add5\n"/imm32
3491     68/push  2/imm32/stderr
3492     # . . call
3493     e8/call  _write/disp32
3494     # . . discard args
3495     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3496     # . syscall(exit, 1)
3497     bb/copy-to-ebx  1/imm32
3498     e8/call  syscall_exit/disp32
3499     # never gets here
3500 
3501 stream-add6:  # in: (addr stream byte), key: handle, val1: addr, val2: addr, val3: addr, val4: addr
3502     # . prologue
3503     55/push-ebp
3504     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
3505     # . save registers
3506     50/push-eax
3507     51/push-ecx
3508     52/push-edx
3509     56/push-esi
3510     # esi = in
3511     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   8/disp8         .                 # copy *(ebp+8) to esi
3512     # curr/eax = &in->data[in->write]
3513     # . eax = in->write
3514     8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           0/r32/eax   .               .                 # copy *esi to eax
3515     # . eax = esi+eax+12
3516     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
3517     # max/edx = &in->data[in->size]
3518     # . edx = in->size
3519     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           2/r32/edx   8/disp8         .                 # copy *(esi+8) to edx
3520     # . edx = esi+edx+12
3521     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    6/base/esi  2/index/edx   .           2/r32/edx   0xc/disp8       .                 # copy esi+edx+12 to edx
3522     # if (curr >= max) abort
3523     39/compare                      3/mod/direct    0/rm32/eax    .           .             .           2/r32/edx   .               .                 # compare eax with edx
3524     73/jump-if-addr>=  $stream-add6:abort/disp8
3525     # *curr = key->alloc-id
3526     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         1/r32/ecx   0xc/disp8       .                 # copy *(ebp+12) to ecx
3527     89/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy ecx to *eax
3528     # curr += 4
3529     05/add-to-eax  4/imm32
3530     # if (curr >= max) abort
3531     39/compare                      3/mod/direct    0/rm32/eax    .           .             .           2/r32/edx   .               .                 # compare eax with edx
3532     73/jump-if-addr>=  $stream-add6:abort/disp8
3533     # *curr = key->payload
3534     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         1/r32/ecx   0x10/disp8      .                 # copy *(ebp+16) to ecx
3535     89/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy ecx to *eax
3536     # curr += 4
3537     05/add-to-eax  4/imm32
3538     # if (curr >= max) abort
3539     39/compare                      3/mod/direct    0/rm32/eax    .           .             .           2/r32/edx   .               .                 # compare eax with edx
3540     73/jump-if-addr>=  $stream-add6:abort/disp8
3541     # *curr = val1
3542     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         1/r32/ecx   0x14/disp8      .                 # copy *(ebp+20) to ecx
3543     89/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy ecx to *eax
3544     # curr += 4
3545     05/add-to-eax  4/imm32
3546     # if (curr >= max) abort
3547     39/compare                      3/mod/direct    0/rm32/eax    .           .             .           2/r32/edx   .               .                 # compare eax with edx
3548     73/jump-if-addr>=  $stream-add6:abort/disp8
3549     # *curr = val2
3550     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         1/r32/ecx   0x18/disp8      .                 # copy *(ebp+24) to ecx
3551     89/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy ecx to *eax
3552     # curr += 4
3553     05/add-to-eax  4/imm32
3554     # if (curr >= max) abort
3555     39/compare                      3/mod/direct    0/rm32/eax    .           .             .           2/r32/edx   .               .                 # compare eax with edx
3556     73/jump-if-addr>=  $stream-add6:abort/disp8
3557 $aa-write-segment-offset:
3558     # *curr = val3
3559     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         1/r32/ecx   0x1c/disp8      .                 # copy *(ebp+28) to ecx
3560     89/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy ecx to *eax
3561     # curr += 4
3562     05/add-to-eax  4/imm32
3563     # if (curr >= max) abort
3564     39/compare                      3/mod/direct    0/rm32/eax    .           .             .           2/r32/edx   .               .                 # compare eax with edx
3565     73/jump-if-addr>=  $stream-add6:abort/disp8
3566     # *curr = val4
3567     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         1/r32/ecx   0x20/disp8      .                 # copy *(ebp+32) to ecx
3568     89/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy ecx to *eax
3569     # in->write += 24
3570     81          0/subop/add         0/mod/indirect  6/rm32/esi    .           .             .           .           .               0x18/imm32        # add to *esi
3571 $stream-add6:end:
3572     # . restore registers
3573     5e/pop-to-esi
3574     5a/pop-to-edx
3575     59/pop-to-ecx
3576     58/pop-to-eax
3577     # . epilogue
3578     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
3579     5d/pop-to-ebp
3580     c3/return
3581 
3582 $stream-add6:abort:
3583     # . _write(2/stderr, error)
3584     # . . push args
3585     68/push  "overflow in stream-add6\n"/imm32
3586     68/push  2/imm32/stderr
3587     # . . call
3588     e8/call  _write/disp32
3589     # . . discard args
3590     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3591     # . syscall(exit, 1)
3592     bb/copy-to-ebx  1/imm32
3593     e8/call  syscall_exit/disp32
3594     # never gets here
3595 
3596 # some variants of 'trace' that take multiple arguments in different combinations of types:
3597 #   n: int
3598 #   c: character [4-bytes, will eventually be UTF-8]
3599 #   s: (addr array byte)
3600 #   l: (addr slice)
3601 # one gotcha: 's5' must not be empty
3602 
3603 trace-sssns:  # s1: (addr array byte), s2: (addr array byte), s3: (addr array byte), n4: int, s5: (addr array byte)
3604     # . prologue
3605     55/push-ebp
3606     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
3607     # write(*Trace-stream, s1)
3608     # . . push args
3609     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
3610     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
3611     # . . call
3612     e8/call  write/disp32
3613     # . . discard args
3614     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3615     # write(*Trace-stream, s2)
3616     # . . push args
3617     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
3618     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
3619     # . . call
3620     e8/call  write/disp32
3621     # . . discard args
3622     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3623     # write(*Trace-stream, s3)
3624     # . . push args
3625     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
3626     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
3627     # . . call
3628     e8/call  write/disp32
3629     # . . discard args
3630     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3631     # print-int32(*Trace-stream, n4)
3632     # . . push args
3633     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x14/disp8      .                 # push *(ebp+20)
3634     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
3635     # . . call
3636     e8/call  print-int32/disp32
3637     # . . discard args
3638     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3639     # trace(s5)  # implicitly adds a newline and finalizes the trace line
3640     # . . push args
3641     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x18/disp8      .                 # push *(ebp+24)
3642     # . . call
3643     e8/call  trace/disp32
3644     # . . discard args
3645     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
3646 $trace-sssns:end:
3647     # . epilogue
3648     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
3649     5d/pop-to-ebp
3650     c3/return
3651 
3652 test-trace-sssns:
3653     # . prologue
3654     55/push-ebp
3655     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
3656     # setup
3657     # . *Trace-stream->write = 0
3658     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Trace-stream/disp32               # copy *Trace-stream to eax
3659     c7          0/subop/copy        0/mod/direct    0/rm32/eax    .           .             .           .           .               0/imm32           # clear *eax
3660     # trace-sssns("A" "b" "c " 3 " e")
3661     # . . push args
3662     68/push  " e"/imm32
3663     68/push  3/imm32
3664     68/push  "c "/imm32
3665     68/push  "b"/imm32
3666     68/push  "A"/imm32
3667     # . . call
3668     e8/call  trace-sssns/disp32
3669     # . . discard args
3670     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x14/imm32        # add to esp
3671 +-- 26 lines: #?     # dump *Trace-stream -----------------------------------------------------------------------------------------------------------------------------------------------
3697     # check-trace-contains("Abc 0x00000003 e")
3698     # . . push args
3699     68/push  "F - test-trace-sssns"/imm32
3700     68/push  "Abc 0x00000003 e"/imm32
3701     # . . call
3702     e8/call  check-trace-contains/disp32
3703     # . . discard args
3704     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3705     # . epilogue
3706     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
3707     5d/pop-to-ebp
3708     c3/return
3709 
3710 trace-snsns:  # s1: (addr array byte), n2: int, s3: (addr array byte), n4: int, s5: (addr array byte)
3711     # . prologue
3712     55/push-ebp
3713     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
3714     # write(*Trace-stream, s1)
3715     # . . push args
3716     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
3717     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
3718     # . . call
3719     e8/call  write/disp32
3720     # . . discard args
3721     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3722     # print-int32(*Trace-stream, n2)
3723     # . . push args
3724     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
3725     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
3726     # . . call
3727     e8/call  print-int32/disp32
3728     # . . discard args
3729     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3730     # write(*Trace-stream, s3)
3731     # . . push args
3732     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
3733     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
3734     # . . call
3735     e8/call  write/disp32
3736     # . . discard args
3737     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3738     # print-int32(*Trace-stream, n4)
3739     # . . push args
3740     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x14/disp8      .                 # push *(ebp+20)
3741     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
3742     # . . call
3743     e8/call  print-int32/disp32
3744     # . . discard args
3745     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3746     # trace(s5)  # implicitly adds a newline and finalizes the trace line
3747     # . . push args
3748     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x18/disp8      .                 # push *(ebp+24)
3749     # . . call
3750     e8/call  trace/disp32
3751     # . . discard args
3752     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
3753 $trace-snsns:end:
3754     # . epilogue
3755     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
3756     5d/pop-to-ebp
3757     c3/return
3758 
3759 test-trace-snsns:
3760     # . prologue
3761     55/push-ebp
3762     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
3763     # setup
3764     # . *Trace-stream->write = 0
3765     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Trace-stream/disp32               # copy *Trace-stream to eax
3766     c7          0/subop/copy        0/mod/direct    0/rm32/eax    .           .             .           .           .               0/imm32           # clear *eax
3767     # trace-snsns("A " 2 " c " 3 " e")
3768     # . . push args
3769     68/push  " e"/imm32
3770     68/push  3/imm32
3771     68/push  " c "/imm32
3772     68/push  2/imm32
3773     68/push  "A "/imm32
3774     # . . call
3775     e8/call  trace-snsns/disp32
3776     # . . discard args
3777     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x14/imm32        # add to esp
3778 +-- 26 lines: #?     # dump *Trace-stream -----------------------------------------------------------------------------------------------------------------------------------------------
3804     # check-trace-contains("Abc 0x00000003 e")
3805     # . . push args
3806     68/push  "F - test-trace-snsns"/imm32
3807     68/push  "A 0x00000002 c 0x00000003 e"/imm32
3808     # . . call
3809     e8/call  check-trace-contains/disp32
3810     # . . discard args
3811     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3812     # . epilogue
3813     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
3814     5d/pop-to-ebp
3815     c3/return
3816 
3817 trace-slsls:  # s1: (addr array byte), l2: (addr slice), s3: (addr array byte), l4: (addr slice), s5: (addr array byte)
3818     # . prologue
3819     55/push-ebp
3820     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
3821     # write(*Trace-stream, s1)
3822     # . . push args
3823     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
3824     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
3825     # . . call
3826     e8/call  write/disp32
3827     # . . discard args
3828     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3829     # write-slice(*Trace-stream, l2)
3830     # . . push args
3831     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
3832     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
3833     # . . call
3834     e8/call  write-slice/disp32
3835     # . . discard args
3836     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3837     # write(*Trace-stream, s3)
3838     # . . push args
3839     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
3840     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
3841     # . . call
3842     e8/call  write/disp32
3843     # . . discard args
3844     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3845     # write-slice(*Trace-stream, l4)
3846     # . . push args
3847     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x14/disp8      .                 # push *(ebp+20)
3848     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
3849     # . . call
3850     e8/call  write-slice/disp32
3851     # . . discard args
3852     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3853     # trace(s5)  # implicitly adds a newline and finalizes the trace line
3854     # . . push args
3855     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x18/disp8      .                 # push *(ebp+24)
3856     # . . call
3857     e8/call  trace/disp32
3858     # . . discard args
3859     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
3860 $trace-slsls:end:
3861     # . epilogue
3862     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
3863     5d/pop-to-ebp
3864     c3/return
3865 
3866 test-trace-slsls:
3867     # . prologue
3868     55/push-ebp
3869     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
3870     # setup
3871     # . *Trace-stream->write = 0
3872     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Trace-stream/disp32               # copy *Trace-stream to eax
3873     c7          0/subop/copy        0/mod/direct    0/rm32/eax    .           .             .           .           .               0/imm32           # clear *eax
3874     # (eax..ecx) = "b"
3875     b8/copy-to-eax  "b"/imm32
3876     8b/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy *eax to ecx
3877     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
3878     05/add-to-eax  4/imm32
3879     # var b/ebx: slice = {eax, ecx}
3880     51/push-ecx
3881     50/push-eax
3882     89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           4/r32/esp   .               .                 # copy esp to ebx
3883     # (eax..ecx) = "d"
3884     b8/copy-to-eax  "d"/imm32
3885     8b/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy *eax to ecx
3886     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
3887     05/add-to-eax  4/imm32
3888     # var d/edx: slice = {eax, ecx}
3889     51/push-ecx
3890     50/push-eax
3891     89/copy                         3/mod/direct    2/rm32/edx    .           .             .           4/r32/esp   .               .                 # copy esp to edx
3892     # trace-slsls("A" b "c" d "e")
3893     # . . push args
3894     68/push  "e"/imm32
3895     52/push-edx
3896     68/push  "c"/imm32
3897     53/push-ebx
3898     68/push  "A"/imm32
3899     # . . call
3900     e8/call  trace-slsls/disp32
3901     # . . discard args
3902     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x14/imm32        # add to esp
3903 +-- 26 lines: #?     # dump *Trace-stream -----------------------------------------------------------------------------------------------------------------------------------------------
3929     # check-trace-contains("Abcde")
3930     # . . push args
3931     68/push  "F - test-trace-slsls"/imm32
3932     68/push  "Abcde"/imm32
3933     # . . call
3934     e8/call  check-trace-contains/disp32
3935     # . . discard args
3936     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3937     # . epilogue
3938     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
3939     5d/pop-to-ebp
3940     c3/return
3941 
3942 trace-slsns:  # s1: (addr array byte), l2: (addr slice), s3: (addr array byte), n4: int, s5: (addr array byte)
3943     # . prologue
3944     55/push-ebp
3945     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
3946     # write(*Trace-stream, s1)
3947     # . . push args
3948     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
3949     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
3950     # . . call
3951     e8/call  write/disp32
3952     # . . discard args
3953     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3954     # write-slice(*Trace-stream, l2)
3955     # . . push args
3956     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
3957     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
3958     # . . call
3959     e8/call  write-slice/disp32
3960     # . . discard args
3961     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3962     # write(*Trace-stream, s3)
3963     # . . push args
3964     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
3965     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
3966     # . . call
3967     e8/call  write/disp32
3968     # . . discard args
3969     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3970     # print-int32(*Trace-stream, n4)
3971     # . . push args
3972     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x14/disp8      .                 # push *(ebp+20)
3973     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
3974     # . . call
3975     e8/call  print-int32/disp32
3976     # . . discard args
3977     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
3978     # trace(s5)  # implicitly adds a newline and finalizes the trace line
3979     # . . push args
3980     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x18/disp8      .                 # push *(ebp+24)
3981     # . . call
3982     e8/call  trace/disp32
3983     # . . discard args
3984     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
3985 $trace-slsns:end:
3986     # . epilogue
3987     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
3988     5d/pop-to-ebp
3989     c3/return
3990 
3991 test-trace-slsns:
3992     # . prologue
3993     55/push-ebp
3994     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
3995     # setup
3996     # . *Trace-stream->write = 0
3997     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Trace-stream/disp32               # copy *Trace-stream to eax
3998     c7          0/subop/copy        0/mod/direct    0/rm32/eax    .           .             .           .           .               0/imm32           # clear *eax
3999     # (eax..ecx) = "b"
4000     b8/copy-to-eax  "b"/imm32
4001     8b/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy *eax to ecx
4002     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
4003     05/add-to-eax  4/imm32
4004     # var b/ebx: slice = {eax, ecx}
4005     51/push-ecx
4006     50/push-eax
4007     89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           4/r32/esp   .               .                 # copy esp to ebx
4008     # trace-slsls("A" b "c " 3 " e")
4009     # . . push args
4010     68/push  " e"/imm32
4011     68/push  3/imm32
4012     68/push  "c "/imm32
4013     53/push-ebx
4014     68/push  "A"/imm32
4015     # . . call
4016     e8/call  trace-slsns/disp32
4017     # . . discard args
4018     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x14/imm32        # add to esp
4019 +-- 26 lines: #?     # dump *Trace-stream -----------------------------------------------------------------------------------------------------------------------------------------------
4045     # check-trace-contains("Abc 0x00000003 e")
4046     # . . push args
4047     68/push  "F - test-trace-slsls"/imm32
4048     68/push  "Abc 0x00000003 e"/imm32
4049     # . . call
4050     e8/call  check-trace-contains/disp32
4051     # . . discard args
4052     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
4053     # . epilogue
4054     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
4055     5d/pop-to-ebp
4056     c3/return
4057 
4058 trace-slsss:  # s1: (addr array byte), l2: (addr slice), s3: (addr array byte), s4: (addr array byte), s5: (addr array byte)
4059     # . prologue
4060     55/push-ebp
4061     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
4062     # write(*Trace-stream, s1)
4063     # . . push args
4064     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
4065     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
4066     # . . call
4067     e8/call  write/disp32
4068     # . . discard args
4069     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
4070     # write-slice(*Trace-stream, l2)
4071     # . . push args
4072     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
4073     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
4074     # . . call
4075     e8/call  write-slice/disp32
4076     # . . discard args
4077     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
4078     # write(*Trace-stream, s3)
4079     # . . push args
4080     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
4081     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
4082     # . . call
4083     e8/call  write/disp32
4084     # . . discard args
4085     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
4086     # write(*Trace-stream, s4)
4087     # . . push args
4088     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x14/disp8      .                 # push *(ebp+20)
4089     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
4090     # . . call
4091     e8/call  write/disp32
4092     # . . discard args
4093     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
4094     # trace(s5)  # implicitly adds a newline and finalizes the trace line
4095     # . . push args
4096     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x18/disp8      .                 # push *(ebp+24)
4097     # . . call
4098     e8/call  trace/disp32
4099     # . . discard args
4100     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4101 $trace-slsss:end:
4102     # . epilogue
4103     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
4104     5d/pop-to-ebp
4105     c3/return
4106 
4107 test-trace-slsss:
4108     # . prologue
4109     55/push-ebp
4110     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
4111     # setup
4112     # . *Trace-stream->write = 0
4113     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Trace-stream/disp32               # copy *Trace-stream to eax
4114     c7          0/subop/copy        0/mod/direct    0/rm32/eax    .           .             .           .           .               0/imm32           # clear *eax
4115     # (eax..ecx) = "b"
4116     b8/copy-to-eax  "b"/imm32
4117     8b/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy *eax to ecx
4118     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
4119     05/add-to-eax  4/imm32
4120     # var b/ebx: slice = {eax, ecx}
4121     51/push-ecx
4122     50/push-eax
4123     89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           4/r32/esp   .               .                 # copy esp to ebx
4124     # trace-slsss("A" b "c" "d" "e")
4125     # . . push args
4126     68/push  "e"/imm32
4127     68/push  "d"/imm32
4128     68/push  "c"/imm32
4129     53/push-ebx
4130     68/push  "A"/imm32
4131     # . . call
4132     e8/call  trace-slsss/disp32
4133     # . . discard args
4134     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x14/imm32        # add to esp
4135 +-- 26 lines: #?     # dump *Trace-stream -----------------------------------------------------------------------------------------------------------------------------------------------
4161     # check-trace-contains("Abcde")
4162     # . . push args
4163     68/push  "F - test-trace-slsss"/imm32
4164     68/push  "Abcde"/imm32
4165     # . . call
4166     e8/call  check-trace-contains/disp32
4167     # . . discard args
4168     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
4169     # . epilogue
4170     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
4171     5d/pop-to-ebp
4172     c3/return
4173 
4174 num-bytes:  # line: (addr stream byte) -> eax: int
4175     # pseudocode:
4176     #   result = 0
4177     #   while true
4178     #     var word-slice = next-word(line)
4179     #     if slice-empty?(word-slice)             # end of line
4180     #       break
4181     #     if slice-starts-with?(word-slice, "#")  # comment
4182     #       break
4183     #     if is-label?(word-slice)                # no need for label declarations anymore
4184     #       break
4185     #     if slice-equal?(word-slice, "==")
4186     #       break                                 # no need for segment header lines
4187     #     result += compute-width(word-slice)
4188     #   return result
4189     #
4190     # . prologue
4191     55/push-ebp
4192     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
4193     # . save registers
4194     51/push-ecx
4195     52/push-edx
4196     53/push-ebx
4197     # var result/eax = 0
4198     31/xor                          3/mod/direct    0/rm32/eax    .           .             .           0/r32/eax   .               .                 # clear eax
4199     # var word-slice/ecx: slice
4200     68/push  0/imm32/end
4201     68/push  0/imm32/start
4202     89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
4203 +-- 26 lines: #?     # dump line --------------------------------------------------------------------------------------------------------------------------------------------------------
4229     # . rewind-stream(line)
4230     # . . push args
4231     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
4232     # . . call
4233     e8/call  rewind-stream/disp32
4234     # . . discard args
4235     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4236 $num-bytes:loop:
4237     # next-word(line, word-slice)
4238     # . . push args
4239     51/push-ecx
4240     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
4241     # . . call
4242     e8/call  next-word/disp32
4243     # . . discard args
4244     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
4245 +-- 40 lines: #?     # dump word-slice --------------------------------------------------------------------------------------------------------------------------------------------------
4285 $num-bytes:check0:
4286     # if (slice-empty?(word-slice)) break
4287     # . save result
4288     50/push-eax
4289     # . eax = slice-empty?(word-slice)
4290     # . . push args
4291     51/push-ecx
4292     # . . call
4293     e8/call  slice-empty?/disp32
4294     # . . discard args
4295     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4296     # . if (eax != false) break
4297     3d/compare-eax-and  0/imm32/false
4298     # . restore result now that ZF is set
4299     58/pop-to-eax
4300     75/jump-if-!=  $num-bytes:end/disp8
4301 $num-bytes:check-for-comment:
4302     # if (slice-starts-with?(word-slice, "#")) break
4303     # . start/edx = word-slice->start
4304     8b/copy                         0/mod/indirect  1/rm32/ecx    .           .             .           2/r32/edx   .               .                 # copy *ecx to edx
4305     # . c/ebx = *start
4306     31/xor                          3/mod/direct    3/rm32/ebx    .           .             .           3/r32/ebx   .               .                 # clear ebx
4307     8a/copy-byte                    0/mod/indirect  2/rm32/edx    .           .             .           3/r32/BL    .               .                 # copy byte at *edx to BL
4308     # . if (ebx == '#') break
4309     81          7/subop/compare     3/mod/direct    3/rm32/ebx    .           .             .           .           .               0x23/imm32/hash   # compare ebx
4310     74/jump-if-=  $num-bytes:end/disp8
4311 $num-bytes:check-for-label:
4312     # if (slice-ends-with?(word-slice, ":")) break
4313     # . end/edx = word-slice->end
4314     8b/copy                         1/mod/*+disp8   1/rm32/ecx    .           .             .           2/r32/edx   4/disp8         .                 # copy *(ecx+4) to edx
4315     # . c/ebx = *(end-1)
4316     31/xor                          3/mod/direct    3/rm32/ebx    .           .             .           3/r32/ebx   .               .                 # clear ebx
4317     8a/copy-byte                    1/mod/*+disp8   2/rm32/edx    .           .             .           3/r32/BL    -1/disp8        .                 # copy byte at *ecx to BL
4318     # . if (ebx == ':') break
4319     81          7/subop/compare     3/mod/direct    3/rm32/ebx    .           .             .           .           .               0x3a/imm32/colon  # compare ebx
4320     74/jump-if-=  $num-bytes:end/disp8
4321 $num-bytes:check-for-segment-header:
4322     # if (slice-equal?(word-slice, "==")) break
4323     # . push result
4324     50/push-eax
4325     # . eax = slice-equal?(word-slice, "==")
4326     # . . push args
4327     68/push  "=="/imm32
4328     51/push-ecx
4329     # . . call
4330     e8/call  slice-equal?/disp32
4331     # . . discard args
4332     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
4333     # . if (eax != false) break
4334     3d/compare-eax-and  0/imm32/false
4335     # . restore result now that ZF is set
4336     58/pop-to-eax
4337     75/jump-if-!=  $num-bytes:end/disp8
4338 $num-bytes:loop-body:
4339     # result += compute-width-of-slice(word-slice)
4340     # . copy result to edx
4341     89/copy                         3/mod/direct    2/rm32/edx    .           .             .           0/r32/eax   .               .                 # copy eax to edx
4342     # . eax = compute-width-of-slice(word-slice)
4343     # . . push args
4344     51/push-ecx
4345     # . . call
4346     e8/call compute-width-of-slice/disp32
4347     # . . discard args
4348     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4349     # . eax += result
4350     01/add                          3/mod/direct    0/rm32/eax    .           .             .           2/r32/edx   .               .                 # add edx to eax
4351     e9/jump  $num-bytes:loop/disp32
4352 $num-bytes:end:
4353     # . rewind-stream(line)
4354     # . . push args
4355     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
4356     # . . call
4357     e8/call  rewind-stream/disp32
4358     # . . discard args
4359     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4360     # . reclaim locals
4361     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
4362     # . restore registers
4363     5b/pop-to-ebx
4364     5a/pop-to-edx
4365     59/pop-to-ecx
4366     # . epilogue
4367     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
4368     5d/pop-to-ebp
4369     c3/return
4370 
4371 test-num-bytes-handles-empty-string:
4372     # if a line starts with '#', return 0
4373     # . prologue
4374     55/push-ebp
4375     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
4376     # setup
4377     # . clear-stream(_test-input-stream)
4378     # . . push args
4379     68/push  _test-input-stream/imm32
4380     # . . call
4381     e8/call  clear-stream/disp32
4382     # . . discard args
4383     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4384     # . clear-stream(_test-output-stream)
4385     # . . push args
4386     68/push  _test-output-stream/imm32
4387     # . . call
4388     e8/call  clear-stream/disp32
4389     # . . discard args
4390     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4391     # no contents in input
4392     # eax = num-bytes(_test-input-stream)
4393     # . . push args
4394     68/push  _test-input-stream/imm32
4395     # . . call
4396     e8/call  num-bytes/disp32
4397     # . . discard args
4398     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4399     # check-ints-equal(eax, 0, msg)
4400     # . . push args
4401     68/push  "F - test-num-bytes-handles-empty-string"/imm32
4402     68/push  0/imm32/true
4403     50/push-eax
4404     # . . call
4405     e8/call  check-ints-equal/disp32
4406     # . . discard args
4407     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
4408     # . epilogue
4409     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
4410     5d/pop-to-ebp
4411     c3/return
4412 
4413 test-num-bytes-ignores-comments:
4414     # if a line starts with '#', return 0
4415     # . prologue
4416     55/push-ebp
4417     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
4418     # setup
4419     # . clear-stream(_test-input-stream)
4420     # . . push args
4421     68/push  _test-input-stream/imm32
4422     # . . call
4423     e8/call  clear-stream/disp32
4424     # . . discard args
4425     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4426     # . clear-stream(_test-output-stream)
4427     # . . push args
4428     68/push  _test-output-stream/imm32
4429     # . . call
4430     e8/call  clear-stream/disp32
4431     # . . discard args
4432     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4433     # initialize input
4434     # . write(_test-input-stream, "# abcd")
4435     # . . push args
4436     68/push  "# abcd"/imm32
4437     68/push  _test-input-stream/imm32
4438     # . . call
4439     e8/call  write/disp32
4440     # . . discard args
4441     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
4442     # eax = num-bytes(_test-input-stream)
4443     # . . push args
4444     68/push  _test-input-stream/imm32
4445     # . . call
4446     e8/call  num-bytes/disp32
4447     # . . discard args
4448     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4449     # check-ints-equal(eax, 0, msg)
4450     # . . push args
4451     68/push  "F - test-num-bytes-ignores-comments"/imm32
4452     68/push  0/imm32/true
4453     50/push-eax
4454     # . . call
4455     e8/call  check-ints-equal/disp32
4456     # . . discard args
4457     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
4458     # . epilogue
4459     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
4460     5d/pop-to-ebp
4461     c3/return
4462 
4463 test-num-bytes-ignores-labels:
4464     # if the first word ends with ':', return 0
4465     # . prologue
4466     55/push-ebp
4467     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
4468     # setup
4469     # . clear-stream(_test-input-stream)
4470     # . . push args
4471     68/push  _test-input-stream/imm32
4472     # . . call
4473     e8/call  clear-stream/disp32
4474     # . . discard args
4475     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4476     # . clear-stream(_test-output-stream)
4477     # . . push args
4478     68/push  _test-output-stream/imm32
4479     # . . call
4480     e8/call  clear-stream/disp32
4481     # . . discard args
4482     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4483     # initialize input
4484     # . write(_test-input-stream, "ab: # cd")
4485     # . . push args
4486     68/push  "ab: # cd"/imm32
4487     68/push  _test-input-stream/imm32
4488     # . . call
4489     e8/call  write/disp32
4490     # . . discard args
4491     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
4492     # eax = num-bytes(_test-input-stream)
4493     # . . push args
4494     68/push  _test-input-stream/imm32
4495     # . . call
4496     e8/call  num-bytes/disp32
4497     # . . discard args
4498     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4499     # check-ints-equal(eax, 0, msg)
4500     # . . push args
4501     68/push  "F - test-num-bytes-ignores-labels"/imm32
4502     68/push  0/imm32/true
4503     50/push-eax
4504     # . . call
4505     e8/call  check-ints-equal/disp32
4506     # . . discard args
4507     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
4508     # . epilogue
4509     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
4510     5d/pop-to-ebp
4511     c3/return
4512 
4513 test-num-bytes-ignores-segment-headers:
4514     # if the first word is '==', return 0
4515     # . prologue
4516     55/push-ebp
4517     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
4518     # setup
4519     # . clear-stream(_test-input-stream)
4520     # . . push args
4521     68/push  _test-input-stream/imm32
4522     # . . call
4523     e8/call  clear-stream/disp32
4524     # . . discard args
4525     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4526     # . clear-stream(_test-output-stream)
4527     # . . push args
4528     68/push  _test-output-stream/imm32
4529     # . . call
4530     e8/call  clear-stream/disp32
4531     # . . discard args
4532     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4533     # initialize input
4534     # . write(_test-input-stream, "== ab cd")
4535     # . . push args
4536     68/push  "== ab cd"/imm32
4537     68/push  _test-input-stream/imm32
4538     # . . call
4539     e8/call  write/disp32
4540     # . . discard args
4541     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
4542     # eax = num-bytes(_test-input-stream)
4543     # . . push args
4544     68/push  _test-input-stream/imm32
4545     # . . call
4546     e8/call  num-bytes/disp32
4547     # . . discard args
4548     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4549     # check-ints-equal(eax, 0, msg)
4550     # . . push args
4551     68/push  "F - test-num-bytes-ignores-segment-headers"/imm32
4552     68/push  0/imm32/true
4553     50/push-eax
4554     # . . call
4555     e8/call  check-ints-equal/disp32
4556     # . . discard args
4557     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
4558     # . epilogue
4559     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
4560     5d/pop-to-ebp
4561     c3/return
4562 
4563 test-num-bytes-counts-words-by-default:
4564     # without metadata, count words
4565     # . prologue
4566     55/push-ebp
4567     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
4568     # setup
4569     # . clear-stream(_test-input-stream)
4570     # . . push args
4571     68/push  _test-input-stream/imm32
4572     # . . call
4573     e8/call  clear-stream/disp32
4574     # . . discard args
4575     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4576     # . clear-stream(_test-output-stream)
4577     # . . push args
4578     68/push  _test-output-stream/imm32
4579     # . . call
4580     e8/call  clear-stream/disp32
4581     # . . discard args
4582     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4583     # initialize input
4584     # . write(_test-input-stream, "ab cd ef")
4585     # . . push args
4586     68/push  "ab cd ef"/imm32
4587     68/push  _test-input-stream/imm32
4588     # . . call
4589     e8/call  write/disp32
4590     # . . discard args
4591     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
4592     # eax = num-bytes(_test-input-stream)
4593     # . . push args
4594     68/push  _test-input-stream/imm32
4595     # . . call
4596     e8/call  num-bytes/disp32
4597     # . . discard args
4598     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4599     # check-ints-equal(eax, 3, msg)
4600     # . . push args
4601     68/push  "F - test-num-bytes-counts-words-by-default"/imm32
4602     68/push  3/imm32/true
4603     50/push-eax
4604     # . . call
4605     e8/call  check-ints-equal/disp32
4606     # . . discard args
4607     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
4608     # . epilogue
4609     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
4610     5d/pop-to-ebp
4611     c3/return
4612 
4613 test-num-bytes-ignores-trailing-comment:
4614     # trailing comments appropriately ignored
4615     # . prologue
4616     55/push-ebp
4617     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
4618     # setup
4619     # . clear-stream(_test-input-stream)
4620     # . . push args
4621     68/push  _test-input-stream/imm32
4622     # . . call
4623     e8/call  clear-stream/disp32
4624     # . . discard args
4625     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4626     # . clear-stream(_test-output-stream)
4627     # . . push args
4628     68/push  _test-output-stream/imm32
4629     # . . call
4630     e8/call  clear-stream/disp32
4631     # . . discard args
4632     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4633     # initialize input
4634     # . write(_test-input-stream, "ab cd # ef")
4635     # . . push args
4636     68/push  "ab cd # ef"/imm32
4637     68/push  _test-input-stream/imm32
4638     # . . call
4639     e8/call  write/disp32
4640     # . . discard args
4641     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
4642     # eax = num-bytes(_test-input-stream)
4643     # . . push args
4644     68/push  _test-input-stream/imm32
4645     # . . call
4646     e8/call  num-bytes/disp32
4647     # . . discard args
4648     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4649     # check-ints-equal(eax, 2, msg)
4650     # . . push args
4651     68/push  "F - test-num-bytes-ignores-trailing-comment"/imm32
4652     68/push  2/imm32/true
4653     50/push-eax
4654     # . . call
4655     e8/call  check-ints-equal/disp32
4656     # . . discard args
4657     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
4658     # . epilogue
4659     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
4660     5d/pop-to-ebp
4661     c3/return
4662 
4663 test-num-bytes-handles-imm32:
4664     # if a word has the /imm32 metadata, count it as 4 bytes
4665     # . prologue
4666     55/push-ebp
4667     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
4668     # setup
4669     # . clear-stream(_test-input-stream)
4670     # . . push args
4671     68/push  _test-input-stream/imm32
4672     # . . call
4673     e8/call  clear-stream/disp32
4674     # . . discard args
4675     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4676     # . clear-stream(_test-output-stream)
4677     # . . push args
4678     68/push  _test-output-stream/imm32
4679     # . . call
4680     e8/call  clear-stream/disp32
4681     # . . discard args
4682     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4683     # initialize input
4684     # . write(_test-input-stream, "ab cd/imm32 ef")
4685     # . . push args
4686     68/push  "ab cd/imm32 ef"/imm32
4687     68/push  _test-input-stream/imm32
4688     # . . call
4689     e8/call  write/disp32
4690     # . . discard args
4691     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
4692     # eax = num-bytes(_test-input-stream)
4693     # . . push args
4694     68/push  _test-input-stream/imm32
4695     # . . call
4696     e8/call  num-bytes/disp32
4697     # . . discard args
4698     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
4699     # check-ints-equal(eax, 6, msg)
4700     # . . push args
4701     68/push  "F - test-num-bytes-handles-imm32"/imm32
4702     68/push  6/imm32/true
4703     50/push-eax
4704     # . . call
4705     e8/call  check-ints-equal/disp32
4706     # . . discard args
4707     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
4708     # . epilogue
4709     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
4710     5d/pop-to-ebp
4711     c3/return
4712 
4713 == data
4714 
4715 # This block of bytes gets copied to the start of the output ELF file, with
4716 # some fields (the ones with labels capitalized) filled in.
4717 # http://www.sco.com/developers/gabi/latest/ch4.eheader.html
4718 Elf_header:
4719   # - size
4720   0x34/imm32
4721   # - data
4722 $e_ident:
4723   7f 45/E 4c/L 46/F
4724   01/32-bit  01/little-endian  01/file-version  00/no-os-extensions
4725   00 00 00 00 00 00 00 00  # 8 bytes of padding
4726 $e_type:
4727   02 00
4728 $e_machine:
4729   03 00
4730 $e_version:
4731   1/imm32
4732 $Elf_e_entry:
4733   0x09000000/imm32  # approximate default; must be updated
4734 $e_phoff:
4735   0x34/imm32  # offset for the 'program header table' containing segment headers
4736 $e_shoff:
4737   0/imm32  # no sections
4738 $e_flags:
4739   0/imm32  # unused
4740 $e_ehsize:
4741   0x34 00
4742 $e_phentsize:
4743   0x20 00
4744 $Elf_e_phnum:
4745   00 00  # number of segments; must be updated
4746 $e_shentsize:
4747   00 00  # no sections
4748 $e_shnum:
4749   00 00
4750 $e_shstrndx:
4751   00 00
4752 
4753 # This block of bytes gets copied after the Elf_header once for each segment.
4754 # Some fields need filling in each time.
4755 # https://docs.oracle.com/cd/E19683-01/816-1386/chapter6-83432/index.html
4756 Elf_program_header_entry:
4757   # - size
4758   0x20/imm32
4759   # - data
4760 $p_type:
4761   1/imm32/PT_LOAD
4762 $Elf_p_offset:
4763   0/imm32  # byte offset in the file at which a segment begins; must be updated
4764 $Elf_p_vaddr:
4765   0/imm32  # starting address to store the segment at before running the program
4766 $Elf_p_paddr:
4767   0/imm32  # should have same value as $Elf_p_vaddr
4768 $Elf_p_filesz:
4769   0/imm32
4770 $Elf_p_memsz:
4771   0/imm32  # should have same value as $Elf_p_filesz
4772 $Elf_p_flags:
4773   6/imm32/rw-  # read/write/execute permissions for the segment; must be updated for the code segment
4774 $p_align:
4775   # we hold this constant; changing it will require adjusting the way we
4776   # compute the starting address for each segment
4777   0x1000/imm32
4778 
4779 # . . vim:nowrap:textwidth=0