second pass (+hello world!), call A

This commit is contained in:
pommicket 2021-11-13 13:48:27 -05:00
parent 383d1fbe27
commit 3fe11fa0cb
3 changed files with 200 additions and 48 deletions

39
03/ex03 Normal file
View file

@ -0,0 +1,39 @@
D]=d7
D]=C
D^=C
D|=C
D&=C
~C
B|=A
8D=C
A=1B
B>=d33
call :funciton
x4b
!:label
?J<B:label
:label
1B=C
; :l ba b
;J=d0
A=d60
syscall x3c
;;;;;;;;;;;;;;;;;;;;;;;align
:label
;;;;;;;;;;;;;;;;;;;;;;;reserve d1000
B+=J
B<=d9
B-=J
?J=B:label
?A!B:label
?A>B:label
A=:label
x3c
;;;;;;;;;;;;;;;;;;;;;;;return
;;;;;;;;;;;;;;;;;;;;;;;*-
;;;;;;;;;;;;;;;;;;;;;;;/-
;;;;;;;;;;;;;;;;;;;;;;;*+
;;;;;;;;;;;;;;;;;;;;;;;/+
:funciton
call A

146
03/in02
View file

@ -20,6 +20,7 @@ DA
im im
##2. ##2.
sy sy
::2p where the second pass starts from
// write ELF header // write ELF header
im im
##4. ##4.
@ -109,7 +110,11 @@ je
im im
##78. ascii 'x' ##78. ascii 'x'
je je
:-x# literal byte :-#b literal byte
im
##27. ascii '
je
:-#b literal byte
im im
##31. ascii '1' ##31. ascii '1'
je je
@ -194,6 +199,20 @@ im
##1. ##1.
je je
:-cl :-cl
im
--SY "sycall"
IA
im
--LI
JA
im
--s=
cl
BA
im
##1.
je
:-sy
jm jm
:-ex :-ex
// handle += instruction // handle += instruction
@ -1069,7 +1088,7 @@ BA
lq lq
BI BI
je je
:-bl bad label if we've reached the end :-!l bad label if we've reached the end
JR JR
im im
--s= --s=
@ -1219,7 +1238,7 @@ DA
jm jm
:-wr :-wr
// literal byte // literal byte
::x# ::#b
im im
--LI --LI
BA BA
@ -1735,9 +1754,23 @@ jm
:-wr :-wr
::S) ::S)
SA SA
// handle "call :label" // handle call :label / call A
::cl ::cl
im im
##5. add 5 to line pointer to get pointer to label name
BA
im
--LI
+B
BA
zA
lb
BAs
im
##41.
je
:-ca call a
im
##5. add 5 to line pointer to get pointer to label name ##5. add 5 to line pointer to get pointer to label name
BA BA
im im
@ -1747,6 +1780,8 @@ BA
im im
--l@ --l@
cl cl
// intentional fallthrough
::ca
// emit 'call rax' // emit 'call rax'
im im
--Cl instruction --Cl instruction
@ -1761,6 +1796,35 @@ jm
:-rl jump back to read next line :-rl jump back to read next line
::Cl ::Cl
cl cl
// handle "syscall <number>"
::sy
im
##8.
BA
im
--LI
+B add 8 to line pointer to get pointer to number
BA
im
--nu
cl get syscall number
BA
im
--im
cl write 'mov rax, <syscall number>'
im
--Sy
IA
im
##2.
DA
im
--wr
cl write 'syscall'
jm
:-rl next line
::Sy
sy
// write to output file from rsi..rsi+rdx // write to output file from rsi..rsi+rdx
::wr ::wr
im im
@ -1820,8 +1884,32 @@ im
##78. ascii 'x' ##78. ascii 'x'
je je
:-#x hexadecimal :-#x hexadecimal
im
##27. ascii '
je
:-#' ascii character
jm jm
:-!n unrecognized number base :-!n unrecognized number base
// convert character pointed to by rsi to character code in rax
::#'
// make sure there's no trailing characters
im
##1.
BI
+B
BA
zA
lb
BA
im
##a.
jn
:-!n
// okay, no trailing characters, just set rax = *rsi
BI
zA
lb
re
// convert newline-terminated decimal representation in rsi to number in rax // convert newline-terminated decimal representation in rsi to number in rax
::#d ::#d
zA zA
@ -1982,10 +2070,45 @@ jm
:-ex :-ex
// end of file // end of file
::ef ::ef
// TODO: second pass im
zA exit code 0 --2P
BA
zA
lb
DA
// set 2P to 1 no matter what
im
##1.
sb
BD
zA
jn if 2nd pass is not zero,
:-ex exit
// okay we need to do the second pass.
// rewind file descriptors
// input
im
##3.
JA
zA
IA
DA
im
##8. lseek
sy
// output
im
##4.
JA
zA
IA
DA
im
##8. lseek
sy
// now go back to do the second pass
jm jm
:-ex :-2p
::2P second pass? ::2P second pass?
00 00
::CL "call" text ::CL "call" text
@ -1994,6 +2117,15 @@ jm
'l 'l
'l 'l
20 20
::SY "syscall" text
's
'y
's
'c
'a
'l
'l
20
::IF input file name ::IF input file name
'i 'i
'n 'n

63
03/in03
View file

@ -1,43 +1,24 @@
J=d1
D]=d7 I=:hello_world
e D=:hello_world_end
D-=I
D]=C syscall d1
D^=C J=d42
D|=C
D&=C
~C
B|=A
8D=C
e
A=1B
B>=d33
e
call :funciton
x4b
!:label
?J<B:label
:label
e
1B=C
; :l ba b
;J=d0
A=d60
syscall x3c syscall x3c
align
:label
reserve d1000
B+=J
B<=d9
B-=J
?J=B:label
?A!B:label
?A>B:label
A=:label
x3c
return
-*
-/
+*
+/
:hello_world
'H
'e
'l
'l
'o
',
x20
'w
'o
'r
'l
'd
'!
xa
:hello_world_end