2018-09-20 21:06:09 +00:00
|
|
|
## 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
|
|
|
|
#
|
2018-10-01 19:22:59 +00:00
|
|
|
# To run (from the subx directory):
|
|
|
|
# $ subx translate *.subx apps/crenshaw2.1.subx -o crenshaw 2.1
|
2018-09-20 21:06:09 +00:00
|
|
|
# $ echo '3' |subx run apps/crenshaw2.1 |xxd -
|
|
|
|
# Expected output:
|
|
|
|
# TODO
|
|
|
|
#
|
2018-09-20 23:32:37 +00:00
|
|
|
# The output is the code a function would need to include, returning the
|
|
|
|
# result in EAX.
|
|
|
|
#
|
2018-09-20 21:06:09 +00:00
|
|
|
# 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:
|
2018-09-24 04:04:32 +00:00
|
|
|
# abort("Integer")
|
2018-09-20 21:06:09 +00:00
|
|
|
# push args
|
2018-09-24 04:04:32 +00:00
|
|
|
68/push "Integer"/imm32
|
2018-09-20 21:06:09 +00:00
|
|
|
# call
|
2018-09-21 22:28:11 +00:00
|
|
|
e8/call abort/disp32
|
2018-09-20 22:51:24 +00:00
|
|
|
# discard arg
|
2018-10-01 22:35:56 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
2018-09-20 21:06:09 +00:00
|
|
|
# exit(0)
|
2018-10-01 22:35:56 +00:00
|
|
|
bb/copy . . . . . . . 0/imm32 # copy to EBX
|
|
|
|
b8/copy . . . . . . . 1/imm32/exit # copy to EAX
|
2018-09-21 23:55:46 +00:00
|
|
|
cd/syscall 0x80/imm8
|
2018-09-20 21:06:09 +00:00
|
|
|
|
2018-10-01 19:22:59 +00:00
|
|
|
## helpers
|
2018-09-24 04:12:39 +00:00
|
|
|
|
2018-09-20 23:23:51 +00:00
|
|
|
# print error message and exit
|
|
|
|
# really maps to the 'Expected' function in Crenshaw
|
2018-09-20 23:11:39 +00:00
|
|
|
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
|
2018-09-21 22:28:11 +00:00
|
|
|
e8/call error/disp32
|
2018-09-20 23:11:39 +00:00
|
|
|
# discard arg
|
2018-10-01 22:35:56 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
2018-09-20 23:11:39 +00:00
|
|
|
# exit(1)
|
2018-10-01 22:35:56 +00:00
|
|
|
bb/copy . . . . . . . 1/imm32 # copy to EBX
|
|
|
|
b8/copy . . . . . . . 1/imm32/exit # copy to EAX
|
2018-09-21 23:55:46 +00:00
|
|
|
cd/syscall 0x80/imm8
|
2018-09-20 23:11:39 +00:00
|
|
|
|
2018-09-20 23:23:51 +00:00
|
|
|
# print out "Error: #{s} expected\n" to stderr
|
2018-09-20 21:06:09 +00:00
|
|
|
error: # s : (address array byte) -> <void>
|
2018-09-20 23:23:51 +00:00
|
|
|
# write_stderr("Error: ")
|
2018-09-20 23:11:39 +00:00
|
|
|
# push args
|
2018-09-24 04:04:32 +00:00
|
|
|
68/push "Error: "/imm32
|
2018-09-20 23:11:39 +00:00
|
|
|
# call
|
2018-09-21 22:28:11 +00:00
|
|
|
e8/call write_stderr/disp32
|
2018-09-20 23:11:39 +00:00
|
|
|
# discard arg
|
2018-10-01 22:35:56 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
2018-09-20 23:11:39 +00:00
|
|
|
# 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
|
2018-09-21 22:28:11 +00:00
|
|
|
e8/call write_stderr/disp32
|
2018-09-20 23:11:39 +00:00
|
|
|
# discard arg
|
2018-10-01 22:35:56 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
2018-09-20 23:23:51 +00:00
|
|
|
# write_stderr(" expected")
|
|
|
|
# push args
|
2018-09-24 04:04:32 +00:00
|
|
|
68/push " expected"/imm32
|
2018-09-20 23:23:51 +00:00
|
|
|
# call
|
2018-09-21 22:28:11 +00:00
|
|
|
e8/call write_stderr/disp32
|
2018-09-20 23:23:51 +00:00
|
|
|
# discard arg
|
2018-10-01 22:35:56 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
2018-09-20 23:23:51 +00:00
|
|
|
# write_stderr("\n")
|
2018-09-20 23:11:39 +00:00
|
|
|
# push args
|
2018-09-21 23:55:46 +00:00
|
|
|
68/push Newline/imm32
|
2018-09-20 23:11:39 +00:00
|
|
|
# call
|
2018-09-21 22:28:11 +00:00
|
|
|
e8/call write_stderr/disp32
|
2018-09-20 23:11:39 +00:00
|
|
|
# discard arg
|
2018-10-01 22:35:56 +00:00
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
|
2018-09-20 23:11:39 +00:00
|
|
|
# end
|
|
|
|
c3/return
|
|
|
|
|
2018-09-20 23:30:40 +00:00
|
|
|
write_stdout: # s : (address array byte) -> <void>
|
2018-09-25 01:48:41 +00:00
|
|
|
# prolog
|
|
|
|
55/push-EBP
|
|
|
|
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
|
2018-09-24 04:12:39 +00:00
|
|
|
# save registers
|
|
|
|
50/push-EAX
|
|
|
|
51/push-ECX
|
|
|
|
52/push-EDX
|
|
|
|
53/push-EBX
|
2018-09-20 23:30:40 +00:00
|
|
|
# write(1/stdout, (data) s+4, (size) *s)
|
|
|
|
# fd = 1 (stdout)
|
2018-10-01 22:35:56 +00:00
|
|
|
bb/copy . . . . . . . 1/imm32 # copy to EBX
|
2018-09-20 23:30:40 +00:00
|
|
|
# x = s+4
|
2018-10-03 03:58:39 +00:00
|
|
|
8b/copy 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none . 1/r32/ECX 8/disp8 . # copy *(EBP+8) to ECX
|
2018-10-01 22:35:56 +00:00
|
|
|
81 0/subop/add 3/mod/direct 1/rm32/ECX . . . . . 4/imm32 # add to ECX
|
2018-09-20 23:30:40 +00:00
|
|
|
# size = *s
|
2018-10-03 03:58:39 +00:00
|
|
|
8b/copy 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none . 2/r32/EDX 8/disp8 . # copy *(EBP+8) to EDX
|
2018-09-20 21:06:09 +00:00
|
|
|
8b/copy 0/mod/indirect 2/rm32/EDX . . . 2/r32/EDX . . # copy *EDX to EDX
|
|
|
|
# call write()
|
2018-10-01 22:35:56 +00:00
|
|
|
b8/copy . . . . . . . 4/imm32/write # copy to EAX
|
2018-09-21 23:55:46 +00:00
|
|
|
cd/syscall 0x80/imm8
|
2018-09-24 04:12:39 +00:00
|
|
|
# restore registers
|
2018-09-25 06:01:38 +00:00
|
|
|
5b/pop-to-EBX
|
|
|
|
5a/pop-to-EDX
|
|
|
|
59/pop-to-ECX
|
|
|
|
58/pop-to-EAX
|
2018-09-20 23:11:39 +00:00
|
|
|
# end
|
2018-09-25 01:48:41 +00:00
|
|
|
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
|
|
|
|
5d/pop-to-EBP
|
2018-09-20 23:11:39 +00:00
|
|
|
c3/return
|
2018-09-20 21:06:09 +00:00
|
|
|
|
2018-09-24 04:12:39 +00:00
|
|
|
# vim:ft=subx:nowrap:so=0
|