label lookup working!

This commit is contained in:
pommicket 2021-11-12 16:51:28 -05:00
parent b5735552e2
commit 9d90c7045a
2 changed files with 168 additions and 23 deletions

179
03/in02
View file

@ -1,17 +1,3 @@
im
--TE
BA
im
--nu
cl
jm
:-ex
::TE
'e
'1
'f
'c
\n
// open input file // open input file
im im
--IF --IF
@ -34,6 +20,19 @@ DA
im im
##2. ##2.
sy sy
// write ELF header
im
##4.
JA output fd
im
##400000. address of ELF header in this executable
IA
im
##78. length
DA
im
##1. write
sy
// read next line // read next line
::rl ::rl
im im
@ -172,9 +171,126 @@ im
BA BA
AJ AJ
sq sq
cc // read the next line
// jm
// :-rl
// label lookup--set rax to address of label in rbx
::ll
RB put ptr to label in rbp
// if it's the first pass, just return 0
im
--2P
BA
zA
lb
BA
zA
je
:-r0
// okay it's not the second pass
im
##a.
CA terminator '\n'
// use rsi to keep track of position in label list
im
--LB
IA
::lL
// first, check if we've reached the end of the label list (rsi == *L$)
im
--L$
BA
lq
BI
je
:-bl bad label if we've reached the end
JR
im
--s=
cl
BA
im
##1.
je
:-l=
// this isn't the label; advance
::l\
zA
BI
lb
DA
// increment rsi
BI
im
##1.
+B
IA
// check if that byte we looked at was a newline
BD
im
##a.
jn
:-l\ if not, keep looping
// now we need to increment rsi by another 4 bytes, to skip over the address
BI
im
##4.
+B
IA
jm
:-lL
re
::l=
// label found!
// first, increment rsi past newline:
BI
im
##1.
+B
IA
// then, read dword at rsi into rax
BI
zA
ld
// we're done!!
re
// set rax to 1/0 depending on whether rsi and rdi have the same string, up to the terminator in rcx.
::s=
BI
zA
lb
DA
BJ
zA
lb
BD
jn
:-r0 1st characters are not equal
BC
je
:-r1 we reached the end of the string
// increment rsi, rdi
BI
im
##1.
+B
IA
BJ
im
##1.
+B
JA
jm
:-s= keep looping
// return 0
::r0
zA
re
// return 1
::r1
im
##1.
re
// exit with code in rax // exit with code in rax
::ex ::ex
JA JA
@ -319,6 +435,24 @@ im
##1. ##1.
jm jm
:-ex :-ex
// bad label
::bl
im
##2. stderr
JA
im
--BL error message
IA
im
##a. length of error message
DA
im
##1. write
sy
im
##1.
jm
:-ex
// end of file // end of file
::ef ::ef
// TODO: second pass // TODO: second pass
@ -352,6 +486,17 @@ jm
'e 'e
'r 'r
\n \n
::BL bad label error message
'B
'a
'd
20
'l
'a
'b
'e
'l
\n
::LI line buffer ::LI line buffer
~~ ~~
::L$ end of current label list ::L$ end of current label list

12
03/in03
View file

@ -1,12 +1,12 @@
:lbab :lbab
;J=d0 ;J=d0
A=d60 A=d60
syscall x3c sys x3c
res d1000 align
:label :label
res d1000
B+=J B+=J
B<<=d9 B<=d9
B-=J B-=J
J?<B:label ?J<B:label
A=:label