# Assign addresses (co-ordinates) to instructions (landmarks) in a program # (landscape). # Use the addresses assigned to: # a) replace labels # b) add an ELF header and segment headers with addresses and offsets correctly filled in # # To build: # $ bootstrap/bootstrap translate [01]*.subx subx-params.subx survey_elf.subx -o survey_elf # # The expected input is a stream of bytes with '==' segment headers, comments # and some interspersed labels. # $ cat x # == code 0x1 # l1: # aa bb l1/imm8 # cc dd l2/disp32 # l2: # ee foo/imm32 # == data 0x10 # foo: # 00 # # The output is the stream of bytes without segment headers or label definitions, # and with label references replaced with numeric values/displacements. # # $ cat x |bootstrap/bootstrap run survey_elf # ...ELF header bytes... # # ELF header above will specify that code segment begins at this offset # aa bb nn # some computed address # cc dd nn nn nn nn # some computed displacement # ee nn nn nn nn # some computed address # # ELF header above will specify that data segment begins at this offset # 00 # # The ELF format has some persnickety constraints on the starting addresses of # segments, so input headers are treated as guidelines and adjusted in the # output. == 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/in
## print out a (global variable) string to stdout
var size : int = 14
var x : (array character) = "hello, world!"
fn main [
call write 1/stdout, x, size
call exit, 0
]
fn write fd : int, x : (address array byte), size : int [
EBX <- copy fd
ECX <- copy x
EDX <- copy size
EAX <- copy 4/write
syscall
]
fn exit x : int [
code/EBX <- copy x
code/EAX <- copy 1/exit
syscall
]