blob: d5bd2a010e1dcdfeeebdc240ece238e4f0227650 (
plain) (
tree)
|
|
## port of https://github.com/akkartik/crenshaw/blob/master/tutor2.1.pas
# corresponds to the section "single digits" in https://compilers.iecc.com/crenshaw/tutor2.txt
#
# To run (from the subx directory):
# $ subx translate *.subx apps/crenshaw2.1.subx -o crenshaw 2.1
# $ echo '3' |subx run apps/crenshaw2.1 |xxd -
# Expected output:
# TODO
#
# The output is the code a function would need to include, returning the
# result in EAX.
#
# Major note: byte strings are not null-terminated. Instead they're prefixed
# with a 32-bit length.
== code
# instruction effective address operand 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
# main:
# abort("Integer")
# push args
68/push "Integer"/imm32
# call
e8/call abort/disp32
# discard arg
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
# exit(0)
bb/copy-to-EBX 0/imm32
b8/copy-to-EAX 1/imm32/exit
cd/syscall 0x80/imm8
## helpers
# print error message and exit
# really maps to the 'Expected' function in Crenshaw
abort: # s : (address array byte) -> <void>
# error(s)
# push args
ff 6/subop/push 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none . . 4/disp8 . # push *(ESP+4)
# call
e8/call error/disp32
# discard arg
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
# exit(1)
bb/copy-to-EBX 1/imm32
b8/copy-to-EAX 1/imm32/exit
cd/syscall 0x80/imm8
# print out "Error: #{s} expected\n" to stderr
error: # s : (address array byte) -> <void>
# write-stderr("Error: ")
# push args
68/push "Error: "/imm32
# call
e8/call write-stderr/disp32
# discard arg
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
# write-stderr(s)
# push args
ff 6/subop/push 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none . . 4/disp8 . # push *(ESP+4)
# call
e8/call write-stderr/disp32
# discard arg
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
# write-stderr(" expected")
# push args
68/push " expected"/imm32
# call
e8/call write-stderr/disp32
# discard arg
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
# write-stderr("\n")
# push args
68/push Newline/imm32
# call
e8/call write-stderr/disp32
# discard arg
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
# end
c3/return
write-stdout: # s : (address array byte) -> <void>
# prolog
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
# write(1/stdout, (data) s+4, (size) *s)
# fd = 1 (stdout)
bb/copy-to-EBX 1/imm32
# x = s+4
8b/copy 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none . 1/r32/ECX 8/disp8 . # copy *(EBP+8) to ECX
81 0/subop/add 3/mod/direct 1/rm32/ECX . . . . . 4/imm32 # add to ECX
# size = *s
8b/copy 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none . 2/r32/EDX 8/disp8 . # copy *(EBP+8) to EDX
8b/copy 0/mod/indirect 2/rm32/EDX . . . 2/r32/EDX . . # copy *EDX to EDX
# call write()
b8/copy-to-EAX 4/imm32/write
cd/syscall 0x80/imm8
# restore registers
5b/pop-to-EBX
5a/pop-to-EDX
59/pop-to-ECX
58/pop-to-EAX
# end
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
|