diff options
author | pommicket <pommicket@gmail.com> | 2021-11-13 13:48:27 -0500 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2021-11-13 13:48:27 -0500 |
commit | 3fe11fa0cb21a5b78b401e86ca9ca382332e8a6f (patch) | |
tree | ef906a4089b9fb997c748c0a572199b4e96eb737 /03 | |
parent | 383d1fbe2740fd0bfd12521cf54ef680d4af7ba1 (diff) |
second pass (+hello world!), call A
Diffstat (limited to '03')
-rw-r--r-- | 03/ex03 | 39 | ||||
-rw-r--r-- | 03/in02 | 146 | ||||
-rw-r--r-- | 03/in03 | 63 |
3 files changed, 200 insertions, 48 deletions
@@ -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 @@ -20,6 +20,7 @@ DA im ##2. sy +::2p where the second pass starts from // write ELF header im ##4. @@ -109,7 +110,11 @@ je im ##78. ascii 'x' je -:-x# literal byte +:-#b literal byte +im +##27. ascii ' +je +:-#b literal byte im ##31. ascii '1' je @@ -194,6 +199,20 @@ im ##1. je :-cl +im +--SY "sycall" +IA +im +--LI +JA +im +--s= +cl +BA +im +##1. +je +:-sy jm :-ex // handle += instruction @@ -1069,7 +1088,7 @@ BA lq BI je -:-bl bad label if we've reached the end +:-!l bad label if we've reached the end JR im --s= @@ -1219,7 +1238,7 @@ DA jm :-wr // literal byte -::x# +::#b im --LI BA @@ -1735,9 +1754,23 @@ jm :-wr ::S) SA -// handle "call :label" +// handle call :label / call A ::cl 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 BA im @@ -1747,6 +1780,8 @@ BA im --l@ cl +// intentional fallthrough +::ca // emit 'call rax' im --Cl instruction @@ -1761,6 +1796,35 @@ jm :-rl jump back to read next line ::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 ::wr im @@ -1820,8 +1884,32 @@ im ##78. ascii 'x' je :-#x hexadecimal +im +##27. ascii ' +je +:-#' ascii character jm :-!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 ::#d zA @@ -1982,10 +2070,45 @@ jm :-ex // end of file ::ef -// TODO: second pass -zA exit code 0 +im +--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 -:-ex +:-2p ::2P second pass? 00 ::CL "call" text @@ -1994,6 +2117,15 @@ jm 'l 'l 20 +::SY "syscall" text +'s +'y +'s +'c +'a +'l +'l +20 ::IF input file name 'i 'n @@ -1,43 +1,24 @@ - -D]=d7 -e - -D]=C -D^=C -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 +J=d1 +I=:hello_world +D=:hello_world_end +D-=I +syscall d1 +J=d42 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 |