# Assign addresses (co-ordinates) to instructions (landmarks) in a program
# (landscape).
#
# To build:
# $ bootstrap/bootstrap translate [01]*.subx subx-params.subx survey_baremetal.subx -o survey_baremetal
#
# The expected input is a stream of bytes and some interspersed labels.
# Comments and '==' segment headers are allowed, but names are ignored. The
# emitted code will all lie in a single contiguous address range starting at
# address 0x7c00. Addresses in segment headers are optional. If provided, this
# program will insert padding in the output until the desired address is
# reached.
#
# $ cat x
# == code
# l1:
# aa bb l1/imm8
# cc dd l2/disp32
# l2:
# ee foo/imm32
# == data 0x7c10
# foo:
# 34
#
# The output is a list of labels and their computed addresses.
#
# $ cat x |bootstrap/bootstrap run survey_baremetal
# 0x7c00 l1
# 0x7c09 l2
# 0x7c10 foo
== code
# instruction effective address register displacement immediate
# . op subop mod rm32 base index scale r32
# . 1-3 bytes 3 bits 2 bits 3 bits 3 bits 3 bits 2 bits 2 bits 0/1/2/4 bytes 0/1/2/4 bytes
Entry: # run tests if necessary, convert stdin if not
# . prologue
89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
# Heap = new-segment(Heap-size)
# . . push args
68/push Heap/imm32
ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Heap-size/disp32 # push *Heap-size
# . . call
e8/call new-segment/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# initialize-trace-stream(Trace-size)
# . . push args
ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Trace-size/disp32 # push *Heap-size
# . . call
e8/call initialize-trace-stream/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# - if argc > 1 and argv[1] == "test", then return run_tests()
# if (argc <= 1) goto interactive
81 7/subop/compare 1/mod/*+disp8 5/rm32/ebp . . . . 0/disp8 1/imm32 # compare *ebp
7e/jump-if-<= $subx-survey-main:interactive/disp8
# if (!kernel-string-equal?(argv[1], "test")) goto interactive
# . eax = kernel-string-equal?(argv[1], "test")
# . . push args
68/push "test"/imm32
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
# . . call
e8/call kernel-string-equal?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . if (eax == false) goto interactive
3d/compare-eax-and 0/imm32/false
74/jump-if-= $subx-survey-main:interactive/disp8
# run-tests()
e8/call run-tests/disp32
# syscall_exit(*Num-test-failures)
8b/copy 0/mod/indirect 5/rm32/.disp32 . . 3/r32/ebx Num-test-failures/disp32 # copy *Num-test-failures to ebx
eb/jump $subx-survey-main:end/disp8
$subx-survey-main:interactive:
# - otherwise convert stdin
# subx-survey(Stdin, Stdout)
# . . push args
68/push Stdout/imm32
68/push Stdin/imm32
# . . call
e8/call subx-survey/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # . write-stream(2/stderr, Trace-stream)
#? # . . push args
#? ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Trace-stream/disp32 # push *Trace-stream
#? 68/push 2/imm32/stderr
#? # . . call
#? e8/call write-stream/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# syscall_exit(0)
bb/copy-to-ebx 0/imm32
$subx-survey-main:end:
e8/call syscall_exit/disp32
subx-survey: # infile: (addr buffered-file), out: (addr buffered-file)
# pseudocode
# var in: (stream byte Input-size)
# slurp(infile, in)
# var labels: (stream {label-name, address} Max-labels)
# compute-addresses(in, labels)
# emit-labels(out, labels)
#
# . prologue
55/push-ebp
89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
# . save registers
51/push-ecx
52/push-edx
56/push-esi
# var labels/edx: (stream {label-name, address} Max-labels)
# (we get more rows than Max-labels advertises because row size is smaller than in survey_elf)
# . data
2b/subtract 0/mod/indirect 5/rm32/.disp32 . . 4/r32/esp Max-labels/disp32 # subtract *Max-labels from esp
# . size
ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Max-labels/disp32 # push *Max-labels
# . read
68/push 0/imm32/read
# . write
68/push 0/imm32/write
89/copy 3/mod/direct 2/rm32/edx . . . 4/r32/esp . . # copy esp to edx
# var in/esi: (stream byte Input-size)
# . data
2b/subtract 0/mod/indirect 5/rm32/.disp32 . . 4/r32/esp Input-size/disp32 # subtract *Input-size from esp
# . size
ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Input-size/disp32 # push *Input-size
# . read
68/push 0/imm32/read
# . write
68/push 0/imm32/write
89/copy 3/mod/direct 6/rm32/esi . . . 4/r32/esp . . # copy esp to esi
# slurp(infile, in)
# . . push args
56/push-esi
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
# . . call
e8/call slurp/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# compute-addresses(in, labels)
# . . push args
52/push-edx
56/push-esi
# . . call
e8/call compute-addresses/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# emit-labels(out, labels)
# . . push args
52/push-edx
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12)
# . . call
e8/call emit-labels/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# flush(out)
# . . push args
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12)
# . . call
e8/call flush/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
$subx-survey:end:
# . reclaim locals
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x18/imm32 # add to esp
03/add 0/mod/indirect 5/rm32/.disp32 . . 4/r32/esp Max-labels/disp32 # add *Max-labels to esp
03/add 0/mod/indirect 5/rm32/.disp32 . . 4/r32/esp Input-size/disp32 # add *Input-size to esp
# . restore registers
5e/pop-to-esi
5a/pop-to-edx
59/pop-to-ecx
# . epilogue
89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
5d/pop-to-ebp
c3/return
test-subx-survey-computes-addresses:
# input:
# == code
# ab x/imm32
# == data
# x:
# 01
#
# trace contains (in any order):
# label x is at address 0x7c05
#
# . prologue
55/push-ebp
89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
# setup
# . clear-stream(_test-input-stream)
# . . push args
68/push _test-input-stream/imm32
# . . call
e8/call clear-stream/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# . clear-stream($_test-input-buffered-file->buffer)
# . . push args
68/push $_test-input-buffered-file->buffer/imm32
# . . call
e8/call clear-stream/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# . clear-stream(_test-output-stream)
# . . push args
68/push _test-output-stream/imm32
# . . call
e8/call clear-stream/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# . clear-stream($_test-output-buffered-file->buffer)
# . . push args
68/push $_test-output-buffered-file->buffer/imm32
# . . call
e8/call clear-stream/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# initialize input
# . write(_test-input-stream, "== code\n")
# . . push args
68/push "== code\n"/imm32
68/push _test-input-stream/imm32
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . write(_test-input-stream, "ab x/imm32\n")
# . . push args
68/push "ab x/imm32\n"/imm32
68/push _test-input-stream/imm32
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . write(_test-input-stream, "== data\n")
# . . push args
68/push "== data\n"/imm32
68/push _test-input-stream/imm32
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . write(_test-input-stream, "x:\n")
# . . push args
68/push "x:\n"/imm32
68/push _test-input-stream/imm32
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . write(_test-input-stream, "01\n")
# . . push args
68/push "01\n"/imm32
68/push _test-input-stream/imm32
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# subx-survey(_test-input-buffered-file, _test-output-buffered-file)
# . . push args
68/push _test-output-buffered-file/imm32
68/push _test-input-buffered-file/imm32
# . . call
e8/call subx-survey/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# check trace
#? # dump *Trace-stream {{{
#? # . write(2/stderr, "^")
#? # . . push args
#? 68/push "^"/imm32
#? 68/push 2/imm32/stderr
#? # . . call
#? e8/call write/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # . write-stream(2/stderr, *Trace-stream)
#? # . . push args
#? ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Trace-stream/disp32 # push *Trace-stream
#? 68/push 2/imm32/stderr
#? # . . call
#? e8/call write-stream/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # . write(2/stderr, "$\n")
#? # . . push args
#? 68/push "$\n"/imm32
#? 68/push 2/imm32/stderr
#? # . . call
#? e8/call write/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # }}}
# . check-trace-contains("label 'x' is at address 0x00007c05.", msg)
# . . push args
68/push "F - test-subx-survey-computes-addresses/0"/imm32
68/push "label 'x' is at address 0x00007c05."/imm32
# . . call
e8/call check-trace-contains/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . epilogue
89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
5d/pop-to-ebp
c3/return
test-subx-survey-computes-addresses-with-padding:
# input:
# == code
# ab x/imm32
# == data 0x7c10
# x:
# 01
#
# trace contains (in any order):
# label x is at address 0x7c10
#
# . prologue
55/push-ebp
89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
# setup
# . clear-stream(_test-input-stream)
# . . push args
68/push _test-input-stream/imm32
# . . call
e8/call clear-stream/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# . clear-stream($_test-input-buffered-file->buffer)
# . . push args
68/push $_test-input-buffered-file->buffer/imm32
# . . call
e8/call clear-stream/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# . clear-stream(_test-output-stream)
# . . push args
68/push _test-output-stream/imm32
# . . call
e8/call clear-stream/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# . clear-stream($_test-output-buffered-file->buffer)
# . . push args
68/push $_test-output-buffered-file->buffer/imm32
# . . call
e8/call clear-stream/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# initialize input
# . write(_test-input-stream, "== code\n")
# . . push args
68/push "== code\n"/imm32
68/push _test-input-stream/imm32
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . write(_test-input-stream, "ab x/imm32\n")
# . . push args
68/push "ab x/imm32\n"/imm32
68/push _test-input-stream/imm32
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . write(_test-input-stream, "== data\n")
# . . push args
68/push "== data 0x7c10\n"/imm32
68/push _test-input-stream/imm32
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . write(_test-input-stream, "x:\n")
# . . push args
68/push "x:\n"/imm32
68/push _test-input-stream/imm32
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . write(_test-input-stream, "01\n")
# . . push args
68/push "01\n"/imm32
68/push _test-input-stream/imm32
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# subx-survey(_test-input-buffered-file, _test-output-buffered-file)
# . . push args
68/push _test-output-buffered-file/imm32
68/push _test-input-buffered-file/imm32
# . . call
e8/call subx-survey/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# check trace
#? # dump *Trace-stream {{{
#? # . write(2/stderr, "^")
#? # . . push args
#? 68/push "^"/imm32
#? 68/push 2/imm32/stderr
#? # . . call
#? e8/call write/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # . write-stream(2/stderr, *Trace-stream)
#? # . . push args
#? ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Trace-stream/disp32 # push *Trace-stream
#? 68/push 2/imm32/stderr
#? # . . call
#? e8/call write-stream/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # . write(2/stderr, "$\n")
#? # . . push args
#? 68/push "$\n"/imm32
#? 68/push 2/imm32/stderr
#? # . . call
#? e8/call write/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # }}}
# . check-trace-contains("label 'x' is at address 0x00007c10.", msg)
# . . push args
68/push "F - test-subx-survey-computes-addresses-with-padding/0"/imm32
68/push "label 'x' is at address 0x00007c10."/imm32
# . . call
e8/call check-trace-contains/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . epilogue
89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
5d/pop-to-ebp
c3/return
compute-addresses: # in: (addr stream byte), labels: (addr stream {(handle array byte), address})
# pseudocode:
# var current-address = 0x7c00
# var line: (stream byte 512)
# while true # line loop
# clear-stream(line)
# read-line(in, line)
# if (line->write == 0) break # end of file
# while true # word loop
# word-slice = next-word(line)
# if slice-empty?(word-slice) # end of line
# break
# else if slice-starts-with?(word-slice, "#") # comment
# break # end of line
# else if slice-equal?(word-slice, "==") # segment header
# word-slice = next-word(line)
# if slice-empty?(word-slice)
# abort
# word-slice = next-word(line) # segment address
# if slice-empty?(word-slice)
# goto line-loop # segment address is optional
# new-address = parse-hex-int-from-slice(word-slice)
# if new-address < current-address
# abort
# current-address = new-address
# else if label?(word-slice)
# strip trailing ':' from word-slice
# trace("label '" word-slice "' is at address " current-address ".")
# # labels occupy no space, so no need to increment offsets
# else
# width = compute-width-of-slice(word-slice)
# current-address += width
#
# . prologue
55/push-ebp
89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
# . save registers
50/push-eax
51/push-ecx
52/push-edx
53/push-ebx
56/push-esi
57/push-edi
# var current-address/esi: int = 0x7c00
be/copy-to-esi 0x7c00/imm32
# var line/ecx: (stream byte 512)
81 5/subop/subtract 3/mod/direct 4/rm32/esp . . . . . 0x200/imm32 # subtract from esp
68/push 0x200/imm32/size
68/push 0/imm32/read
68/push 0/imm32/write
89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx
# var word-slice/edx: (addr slice)
68/push 0/imm32
68/push 0/imm32
89/copy 3/mod/direct 2/rm32/edx . . . 4/r32/esp . . # copy esp to edx
$compute-addresses:line-loop:
# clear-stream(line)
# . . push args
51/push-ecx
# . . call
e8/call clear-stream/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# read-line(in, line)
# . . push args
51/push-ecx
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
# . . call
e8/call read-line/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# if (line->write == 0) break
8b/copy 0/mod/indirect 1/rm32/ecx . . . 0/r32/eax . . # copy *ecx to eax
3d/compare-eax-and 0/imm32
0f 84/jump-if-= $compute-addresses:end/disp32
#? # dump line {{{
#? # . write(2/stderr, "LL: ")
#? # . . push args
#? 68/push "LL: "/imm32
#? 68/push 2/imm32/stderr
#? # . . call
#? e8/call write/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # write-stream(2/stderr, line)
#? # . . push args
#? 51/push-ecx
#? 68/push 2/imm32/stderr
#? # . . call
#? e8/call write-stream/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # . write(2/stderr, "$\n")
#? # . . push args
#? 68/push "$\n"/imm32
#? 68/push 2/imm32/stderr
#? # . . call
#? e8/call write/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # . rewind-stream(line)
#? # . . push args
#? 51/push-ecx
#? # . . call
#? e8/call rewind-stream/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
#? # }}}
$compute-addresses:word-loop:
# next-word(line, word-slice)
# . . push args
52/push-edx
51/push-ecx
# . . call
e8/call next-word/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
$compute-addresses:case-empty:
# if slice-empty?(word-slice) break
# . eax = slice-empty?(word-slice)
# . . push args
52/push-edx
# . . call
e8/call slice-empty?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# . if (eax != false) break
3d/compare-eax-and 0/imm32/false
0f 85/jump-if-!= $compute-addresses:line-loop/disp32
$compute-addresses:case-comment:
# if slice-starts-with?(word-slice, "#") break
# . . push args
68/push "#"/imm32
52/push-edx
# . . call
e8/call slice-starts-with?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . if (eax != false) break
3d/compare-eax-and 0/imm32/false
0f 85/jump-if-!= $compute-addresses:line-loop/disp32
$compute-addresses:case-segment-header:
# if !slice-equal?(word-slice, "==") goto next case
# . eax = slice-equal?(word-slice, "==")
# . . push args
68/push "=="/imm32
52/push-edx
# . . call
e8/call slice-equal?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . if (eax != false) break
3d/compare-eax-and 0/imm32/false
0f 84/jump-if-= $compute-addresses:case-label/disp32
# next-word(line, word-slice)
# . . push args
52/push-edx
51/push-ecx
# . . call
e8/call next-word/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# if slice-empty?(word-slice) abort
# . eax = slice-empty?(word-slice)
# . . push args
52/push-edx
# . . call
e8/call slice-empty?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# . if (eax != false) abort
3d/compare-eax-and 0/imm32/false
0f 85/jump-if-!= $compute-addresses:abort/disp32
# next-word(line, word-slice)
# . . push args
52/push-edx
51/push-ecx
# . . call
e8/call next-word/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# if slice-empty?(word-slice) break
# . eax = slice-empty?(word-slice)
# . . push args
52/push-edx
# . . call
e8/call slice-empty?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# . if (eax != false) break
3d/compare-eax-and 0/imm32/false
0f 85/jump-if-!= $compute-addresses:line-loop/disp32
# var new-address/eax: int = parse-hex-int-from-slice(word-slice)
# . . push args
52/push-edx
# . . call
e8/call parse-hex-int-from-slice/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# if (new-address < current-address) abort
39/compare 3/mod/direct 0/rm32/eax . . . 6/r32/esi . . # compare eax with esi
0f 82/jump-if-addr< $compute-addresses:error-bad-segment-address/disp32
# current-address = new-address
89/copy 3/mod/direct 6/rm32/esi . . . 0/r32/eax . . # copy eax to esi
# break
e9/jump $compute-addresses:line-loop/disp32
$compute-addresses:case-label:
# if (!label?(word-slice)) goto next case
# . eax = label?(word-slice)
# . . push args
52/push-edx
# . . call
e8/call label?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# . if (eax == false) goto next case
3d/compare-eax-and 0/imm32/false
0f 84/jump-if-= $compute-addresses:case-default/disp32
# strip trailing ':' from word-slice
ff 1/subop/decrement 1/mod/*+disp8 2/rm32/edx . . . . 4/disp8 . # decrement *(edx+4)
# var tmp/eax: (addr int) = get-or-insert-slice(labels, word-slice, row-size=12)
# . . push args
68/push Heap/imm32
68/push 0xc/imm32/row-size
52/push-edx
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12)
# . . call
e8/call get-or-insert-slice/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp
# *tmp = current-address
89/copy 0/mod/indirect 0/rm32/eax . . . 6/r32/esi . . # copy esi to *eax
# trace-slsns("label '" word-slice "' is at address " current-address ".")
# . . push args
68/push "."/imm32
56/push-esi
68/push "' is at address "/imm32
52/push-edx
68/push "label '"/imm32
# . . call
e8/call trace-slsns/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x14/imm32 # add to esp
# continue
e9/jump $compute-addresses:word-loop/disp32
$compute-addresses:case-default:
# width/eax = compute-width-of-slice(word-slice)
# . . push args
52/push-edx
# . . call
e8/call compute-width-of-slice/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# current-address += width
01/add 3/mod/direct 6/rm32/esi . . . 0/r32/eax . . # add eax to *esi
#? # dump segment-offset {{{
#? # . write(2/stderr, "segment-offset: ")
#? # . . push args
#? 68/push "segment-offset: "/imm32
#? 68/push 2/imm32/stderr
#? # . . call
#? e8/call write/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # . clear-stream($Stderr->buffer)
#? # . . push args
#? 68/push $Stderr->buffer/imm32
#? # . . call
#? e8/call clear-stream/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
#? # . write-int32-hex-buffered(Stderr, segment-offset)
#? # . . push args
#? 52/push-edx
#? ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . compute-addresses:segment-offset/disp32 # push *segment-offset
#? 68/push Stderr/imm32
#? # . . call
#? e8/call write-int32-hex-buffered/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # . flush(Stderr)
#? # . . push args
#? 68/push Stderr/imm32
#? # . . call
#? e8/call flush/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
#? # . write(2/stderr, "\n")
#? # . . push args
#? 68/push Newline/imm32
#? 68/push 2/imm32/stderr
#? # . . call
#? e8/call write/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # }}}
e9/jump $compute-addresses:word-loop/disp32
$compute-addresses:end:
# . reclaim locals
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x214/imm32 # add to esp
# . restore registers
5f/pop-to-edi
5e/pop-to-esi
5b/pop-to-ebx
5a/pop-to-edx
59/pop-to-ecx
58/pop-to-eax
# . epilogue
89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
5d/pop-to-ebp
c3/return
$compute-addresses:abort:
# . _write(2/stderr, error)
# . . push args
68/push "'==' must be followed by segment name and optionally an address\n"/imm32
68/push 2/imm32/stderr
# . . call
e8/call _write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . syscall_exit(1)
bb/copy-to-ebx 1/imm32
e8/call syscall_exit/disp32
# never gets here
$compute-addresses:error-bad-segment-address:
# . _write(2/stderr, error)
# . . push args
68/push "'==' specifies an address that implies negative padding\n"/imm32
68/push 2/imm32/stderr
# . . call
e8/call _write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . syscall_exit(1)
bb/copy-to-ebx 1/imm32
e8/call syscall_exit/disp32
# never gets here
test-compute-addresses:
# input:
# == code
# ab x/imm32 # skip comment
# == data
# 00
# x:
# 34
#
# trace contains (in any order):
# label 'x' is at address 0x7c06.
#
# . prologue
55/push-ebp
89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
# setup
# . clear-stream(_test-input-stream)
# . . push args
68/push _test-input-stream/imm32
# . . call
e8/call clear-stream/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# var labels/edx: (stream byte 2*12)
81 5/subop/subtract 3/mod/direct 4/rm32/esp . . . . . 0x18/imm32 # subtract from esp
68/push 0x18/imm32/size
68/push 0/imm32/read
68/push 0/imm32/write
89/copy 3/mod/direct 2/rm32/edx . . . 4/r32/esp . . # copy esp to edx
# initialize input
# . write(_test-input-stream, "== code\n")
# . . push args
68/push "== code\n"/imm32
68/push _test-input-stream/imm32
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . write(_test-input-stream, "ab x/imm32 # skip comment\n")
# . . push args
68/push "ab x/imm32 # skip comment\n"/imm32
68/push _test-input-stream/imm32
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . write(_test-input-stream, "== data\n")
# . . push args
68/push "== data\n"/imm32
68/push _test-input-stream/imm32
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . write(_test-input-stream, "00\n")
# . . push args
68/push "00\n"/imm32
68/push _test-input-stream/imm32
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . write(_test-input-stream, "x:\n")
# . . push args
68/push "x:\n"/imm32
68/push _test-input-stream/imm32
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . write(_test-input-stream, "34\n")
# . . push args
68/push "34\n"/imm32
68/push _test-input-stream/imm32
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# compute-addresses(_test-input-stream, labels)
# . . push args
52/push-edx
68/push _test-input-stream/imm32
# . . call
e8/call compute-addresses/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # dump *Trace-stream {{{
#? # . write(2/stderr, "^")
#? # . . push args
#? 68/push "^"/imm32
#? 68/push 2/imm32/stderr
#? # . . call
#? e8/call write/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # . write-stream(2/stderr, *Trace-stream)
#? # . . push args
#? ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Trace-stream/disp32 # push *Trace-stream
#? 68/push 2/imm32/stderr
#? # . . call
#? e8/call write-stream/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # . write(2/stderr, "$\n")
#? # . . push args
#? 68/push "$\n"/imm32
#? 68/push 2/imm32/stderr
#? # . . call
#? e8/call write/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # }}}
# . check-trace-contains("label 'x' is at address 0x00007c06.", msg)
# . . push args
68/push "F - test-compute-addresses"/imm32
68/push "label 'x' is at address 0x00007c06."/imm32
# . . call
e8/call check-trace-contains/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . check-ints-equal(labels->write, 0xc, msg)
# . . push args
68/push "F - test-compute-addresses-maintains-labels-write-index"/imm32
68/push 0xc/imm32/1-entry
ff 6/subop/push 0/mod/indirect 2/rm32/edx . . . . . . # push *edx
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
# . reclaim locals
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x24/imm32 # add to esp
# . epilogue
89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
5d/pop-to-ebp
c3/return
emit-labels: # out: (addr buffered-file), labels: (addr stream {(handle array byte), address})
# pseudocode:
# curr = table->data
# max = &table->data[table->write]
# while curr < max
# var label: (addr array byte) = lookup(*curr)
# curr += 8
# write-buffered(out, label)
# write-buffered(out, ' ')
# write-buffered(out, *curr)
# curr += 4
# write(out, '\n')
# return 0
#
# . prologue
55/push-ebp
89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
# . save registers
50/push-eax
51/push-ecx
52/push-edx
56/push-esi
# esi = table
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 0xc/disp8 . # copy *(ebp+12) to esi
# var curr/ecx: (addr handle array byte) = table->data
8d/copy-address 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 0xc/disp8 . # copy esi+12 to ecx
# var max/edx: (addr byte) = &table->data[table->write]
8b/copy 0/mod/indirect 6/rm32/esi . . . 2/r32/edx . . # copy *esi to edx
8d/copy-address 0/mod/indirect 4/rm32/sib 1/base/ecx 2/index/edx . 2/r32/edx . . # copy ecx+edx to edx
$emit-labels:loop:
# if (curr >= max) return null
39/compare 3/mod/direct 1/rm32/ecx . . . 2/r32/edx . . # compare ecx with edx
0f 83/jump-if-addr>= $emit-labels:end/disp32
# var label/eax: (addr array byte) = lookup(*curr)
# . . push args
ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 4/disp8 . # push *(ecx+4)
ff 6/subop/push 0/mod/indirect 1/rm32/ecx . . . . . . # push *ecx
# . . call
e8/call lookup/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# curr += 8
81 0/subop/add 3/mod/direct 1/rm32/ecx . . . . . 8/imm32 # add to ecx
# write-buffered(out, label)
# . . push args
50/push-eax
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
# . . call
e8/call write-buffered/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# write-buffered(out, ' ')
# . . push args
68/push Space/imm32
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
# . . call
e8/call write-buffered/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . flush(Stderr)
# . . push args
68/push Stderr/imm32
# . . call
e8/call flush/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# write-int32-hex-buffered(out, *curr)
# . . push args
ff 6/subop/push 0/mod/indirect 1/rm32/ecx . . . . . . # push *ecx
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
# . . call
e8/call write-int32-hex-buffered/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# curr += 4
81 0/subop/add 3/mod/direct 1/rm32/ecx . . . . . 4/imm32 # add to ecx
# write-buffered(out, '\n')
# . . push args
68/push Newline/imm32
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
# . . call
e8/call write-buffered/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# loop
e9/jump $emit-labels:loop/disp32
$emit-labels:end:
# . restore registers
5e/pop-to-esi
5a/pop-to-edx
59/pop-to-ecx
58/pop-to-eax
# . epilogue
89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
5d/pop-to-ebp
c3/return
# - some helpers for tests
# some variants of 'trace' that take multiple arguments in different combinations of types:
# n: int
# c: character [4-bytes, will eventually be UTF-8]
# s: (addr array byte)
# l: (addr slice)
# one gotcha: 's5' must not be empty
trace-slsns: # s1: (addr array byte), l2: (addr slice), s3: (addr array byte), n4: int, s5: (addr array byte)
# . prologue
55/push-ebp
89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
# write(*Trace-stream, s1)
# . . push args
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Trace-stream/disp32 # push *Trace-stream
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# write-slice(*Trace-stream, l2)
# . . push args
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12)
ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Trace-stream/disp32 # push *Trace-stream
# . . call
e8/call write-slice/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# write(*Trace-stream, s3)
# . . push args
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0x10/disp8 . # push *(ebp+16)
ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Trace-stream/disp32 # push *Trace-stream
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# write-int32-hex(*Trace-stream, n4)
# . . push args
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0x14/disp8 . # push *(ebp+20)
ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Trace-stream/disp32 # push *Trace-stream
# . . call
e8/call write-int32-hex/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# trace(s5) # implicitly adds a newline and finalizes the trace line
# . . push args
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0x18/disp8 . # push *(ebp+24)
# . . call
e8/call trace/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
$trace-slsns:end:
# . epilogue
89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
5d/pop-to-ebp
c3/return
test-trace-slsns:
# . prologue
55/push-ebp
89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
# setup
# . *Trace-stream->write = 0
8b/copy 0/mod/indirect 5/rm32/.disp32 . . 0/r32/eax Trace-stream/disp32 # copy *Trace-stream to eax
c7 0/subop/copy 0/mod/direct 0/rm32/eax . . . . . 0/imm32 # clear *eax
# (eax..ecx) = "b"
b8/copy-to-eax "b"/imm32
8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx
8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx
05/add-to-eax 4/imm32
# var b/ebx: slice = {eax, ecx}
51/push-ecx
50/push-eax
89/copy 3/mod/direct 3/rm32/ebx . . . 4/r32/esp . . # copy esp to ebx
# trace-slsls("A" b "c " 3 " e")
# . . push args
68/push " e"/imm32
68/push 3/imm32
68/push "c "/imm32
53/push-ebx
68/push "A"/imm32
# . . call
e8/call trace-slsns/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x14/imm32 # add to esp
#? # dump *Trace-stream {{{
#? # . write(2/stderr, "^")
#? # . . push args
#? 68/push "^"/imm32
#? 68/push 2/imm32/stderr
#? # . . call
#? e8/call write/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # . write-stream(2/stderr, *Trace-stream)
#? # . . push args
#? ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Trace-stream/disp32 # push *Trace-stream
#? 68/push 2/imm32/stderr
#? # . . call
#? e8/call write-stream/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # . write(2/stderr, "$\n")
#? # . . push args
#? 68/push "$\n"/imm32
#? 68/push 2/imm32/stderr
#? # . . call
#? e8/call write/disp32
#? # . . discard args
#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
#? # }}}
# check-trace-contains("Abc 0x00000003 e")
# . . push args
68/push "F - test-trace-slsls"/imm32
68/push "Abc 0x00000003 e"/imm32
# . . call
e8/call check-trace-contains/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
# . epilogue
89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
5d/pop-to-ebp
c3/return
# . . vim:nowrap:textwidth=0