mu/subx/apps/crenshaw2-1.subx

89 lines
3.4 KiB
Plaintext

## 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
# syscall(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
# syscall(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(2/stderr, "Error: ")
# push args
68/push "Error: "/imm32
68/push 2/imm32/stderr
# call
e8/call write/disp32
# discard arg
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# write(2/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)
68/push 2/imm32/stderr
# call
e8/call write/disp32
# discard arg
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# write(2/stderr, " expected")
# push args
68/push " expected"/imm32
68/push 2/imm32/stderr
# call
e8/call write/disp32
# discard arg
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/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 arg
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# end
c3/return
# vim:nowrap:textwidth=0