@@ -1886,9 +1886,9 @@ void genInstrBody(ofstream& outFile, InstrNames instr, int instrNum, bool inputT
18861886 } else if (instr == Ijmp || instr == Ib) {
18871887 outFile <<
18881888 " mov rsi, " << instrNum << " \n "
1889- " cmp rcx, instruction_count\n "
1889+ " cmp rcx, OFFSET FLAT: instruction_count\n "
18901890 " ja jmp_error\n "
1891- " mov rbx, instruction_offsets\n "
1891+ " lea rbx, [rip + instruction_offsets] \n "
18921892 " jmp [rbx+8*rcx]\n " ;
18931893 } else if (instr == Il || instr == Is) { // handled in genCond
18941894 } else if (instr == Iswap) {
@@ -1900,7 +1900,7 @@ void genInstrBody(ofstream& outFile, InstrNames instr, int instrNum, bool inputT
19001900 " call print_unsigned\n " ;
19011901 } else if (instr == Ioutc) {
19021902 outFile <<
1903- " mov rdx, stdout_buff\n "
1903+ " lea rdx, [rip + stdout_buff] \n "
19041904 " mov [rdx], cl\n "
19051905 " mov r8, 1\n "
19061906 " call stdout_write\n " ;
@@ -1969,8 +1969,8 @@ void generate(ofstream& outFile, vector<Instr>& instrs) {
19691969 " push r8\n "
19701970 " push rdx\n "
19711971 " push rsi\n "
1972- " mov rdx, ERROR_template\n "
1973- " mov r8, ERROR_template_len\n "
1972+ " lea rdx, [rip + ERROR_template] \n "
1973+ " mov r8, OFFSET FLAT: ERROR_template_len\n "
19741974 " call stderr_write\n "
19751975 " pop rax # print instr number\n "
19761976 " call print_unsigned_err\n "
@@ -1979,7 +1979,7 @@ void generate(ofstream& outFile, vector<Instr>& instrs) {
19791979 " call stderr_write\n "
19801980 " pop rax # errorneous value\n "
19811981 " call print_unsigned_err\n "
1982- " mov rdx, stdout_buff\n "
1982+ " lea rdx, [rip + stdout_buff] \n "
19831983 " mov BYTE PTR [rdx], 10 # '\\ n'\n "
19841984 " mov r8, 1\n "
19851985 " call stderr_write\n "
@@ -1990,33 +1990,30 @@ void generate(ofstream& outFile, vector<Instr>& instrs) {
19901990 " sub rsp, 32 # reserve shadow space\n "
19911991 " mov rcx, -10 # stdin fd\n "
19921992 " call GetStdHandle\n "
1993- " mov rcx, stdin_fd\n "
1994- " mov [rcx], rax\n "
1993+ " mov QWORD PTR [rip + stdin_fd], rax\n "
19951994 " mov rcx, -11 # stdout fd\n "
19961995 " call GetStdHandle\n "
1997- " mov rcx, stdout_fd\n "
1998- " mov [rcx], rax\n "
1996+ " mov QWORD PTR [rip + stdout_fd], rax\n "
19991997 " mov rcx, -12 # stderr fd\n "
20001998 " call GetStdHandle\n "
2001- " mov rcx, stderr_fd\n "
2002- " mov [rcx], rax\n "
1999+ " mov QWORD PTR [rip + stderr_fd], rax\n "
20032000 " add rsp, 32 # remove shadow space\n "
20042001 " ret\n "
20052002 " \n "
20062003 " # rdx - buff, r8 - number of bytes -> rax - number written\n "
20072004 " stdout_write:\n "
2008- " mov rcx, QWORD PTR [QWORD PTR stdout_fd]\n "
2005+ " mov rcx, QWORD PTR [rip + stdout_fd]\n "
20092006 " jmp write_file\n "
20102007 " stderr_write:\n "
2011- " mov rcx, QWORD PTR [QWORD PTR stderr_fd]\n "
2008+ " mov rcx, QWORD PTR [rip + stderr_fd]\n "
20122009 " # fall through to write_file\n "
20132010 " \n "
20142011 " # rcx - fd, rdx - buff, r8 - chars / buffsize -> rax - number read/written\n "
20152012 " write_file:\n "
2016- " mov rax, WriteFile\n "
2013+ " lea rax, [rip + WriteFile] \n "
20172014 " jmp call_winapi_file_op\n "
20182015 " read_file:\n "
2019- " mov rax, ReadFile\n "
2016+ " lea rax, [rip + ReadFile] \n "
20202017 " call_winapi_file_op:\n "
20212018 " mov rbp, rsp # save rsp @ retval\n "
20222019 " and rsp, -16 # force 16-byte alignment\n "
@@ -2030,42 +2027,37 @@ void generate(ofstream& outFile, vector<Instr>& instrs) {
20302027 " mov rsp, rbp\n "
20312028 " ret\n "
20322029 " stdin_read:\n "
2033- " mov rcx, stdin_fd # get chars into stdin_buff\n "
2034- " mov rcx, [rcx]\n "
2035- " mov rdx, stdin_buff\n "
2036- " mov r8, STDIN_BUFF_SIZE\n "
2030+ " mov rcx, QWORD PTR [rip + stdin_fd] # get chars into stdin_buff\n "
2031+ " lea rdx, [rip + stdin_buff]\n "
2032+ " mov r8, OFFSET FLAT:STDIN_BUFF_SIZE\n "
20372033 " call read_file\n "
20382034 " \n "
2039- " mov rcx, stdin_buff_char_count # store how many were read\n "
2040- " mov [rcx], rax\n "
2041- " mov rcx, stdin_buff_chars_read # 0 chars were processed\n "
2042- " mov QWORD PTR [rcx], 0\n "
2035+ " mov QWORD PTR [rip + stdin_buff_char_count], rax # store how many were read\n "
2036+ " mov QWORD PTR [rip + stdin_buff_chars_read], 0 # 0 chars were processed\n "
20432037 " ret\n "
20442038 " \n "
20452039 " stdin_peek: # returns next raw stdin char, does not advance read ptr -> rdx char\n "
2046- " mov rax, stdin_buff_char_count # if (char_count == chars_read) fill the stdin_buff\n "
2047- " mov rcx, [rax]\n "
2048- " mov rax, stdin_buff_chars_read\n "
2049- " cmp rcx, [rax]\n "
2040+ " mov rcx, QWORD PTR [rip + stdin_buff_char_count] # if (char_count == chars_read) fill the stdin_buff\n "
2041+ " mov rdx, QWORD PTR [rip + stdin_buff_chars_read]\n "
2042+ " cmp rcx, rdx\n "
20502043 " jne stdin_peek_valid\n "
20512044 " call stdin_read\n "
20522045 " stdin_peek_valid:\n "
2053- " mov rax, stdin_buff_chars_read # char addr \n "
2054- " mov rcx, stdin_buff\n "
2055- " add rcx, [ rax] \n "
2046+ " mov rax, [rip + stdin_buff_chars_read] # chars read \n "
2047+ " lea rcx, [rip + stdin_buff] \n "
2048+ " add rcx, rax\n "
20562049 " \n "
20572050 " xor rdx, rdx # peek the char\n "
20582051 " mov dl, [rcx]\n "
20592052 " ret\n "
20602053 " get_next_char: # gets next char from stdin (buffered), advances read ptr -> rdx char\n "
20612054 " call stdin_peek # peek the first unread char\n "
2062- " mov rax, stdin_buff_chars_read # eat the char\n "
2063- " inc QWORD PTR [rax]\n "
2055+ " inc QWORD PTR [rip + stdin_buff_chars_read] # eat the char\n "
20642056 " ret\n "
20652057 " \n "
20662058 " utos: # n - rax -> r8 char count, r9 - char* str\n "
20672059 " xor r8, r8 # char count\n "
2068- " mov r9, stdout_buff+STDOUT_BUFF_SIZE-1 # curr buff pos\n "
2060+ " lea r9, [rip + stdout_buff+STDOUT_BUFF_SIZE-1] # curr buff pos\n "
20692061 " mov r10, 10 # base\n "
20702062 " utos_loop:\n "
20712063 " xor rdx, rdx\n "
@@ -2086,8 +2078,7 @@ void generate(ofstream& outFile, vector<Instr>& instrs) {
20862078 " ret\n "
20872079 " print_unsigned_err: # rax - n -> rax - num written\n "
20882080 " call utos\n "
2089- " mov rcx, stderr_fd\n "
2090- " mov rcx, [rcx]\n "
2081+ " mov rcx, QWORD PTR [rip + stderr_fd]\n "
20912082 " mov rdx, r9 # str\n "
20922083 " call write_file\n "
20932084 " ret\n "
@@ -2121,12 +2112,10 @@ void generate(ofstream& outFile, vector<Instr>& instrs) {
21212112 " _start:\n "
21222113 " # initialization\n "
21232114 " call get_std_fds\n "
2124- " mov rcx, stdin_buff_char_count\n "
2125- " mov QWORD PTR [rcx], 0\n "
2126- " mov rcx, stdin_buff_chars_read\n "
2127- " mov QWORD PTR [rcx], 0\n "
2115+ " mov QWORD PTR [rip + stdin_buff_char_count], 0\n "
2116+ " mov QWORD PTR [rip + stdin_buff_chars_read], 0\n "
21282117 " \n "
2129- " mov r13, cells\n "
2118+ " lea r13, QWORD PTR [rip + cells] \n "
21302119 " xor r14, r14\n "
21312120 " xor r15, r15\n "
21322121 " \n " ;
@@ -2144,8 +2133,8 @@ void generate(ofstream& outFile, vector<Instr>& instrs) {
21442133 " \n "
21452134 " # runtime errors, expect instr number in rsi, errorneous value in rcx\n "
21462135 " jmp_error:\n "
2147- " mov rdx, jmp_error_message\n "
2148- " mov r8, jmp_error_message_len\n "
2136+ " lea rdx, [rip + jmp_error_message] \n "
2137+ " mov r8, OFFSET FLAT: jmp_error_message_len\n "
21492138 " call error\n "
21502139 " \n "
21512140 " end:\n "
@@ -2170,7 +2159,7 @@ void generate(ofstream& outFile, vector<Instr>& instrs) {
21702159 " .equ STDIN_BUFF_SIZE, " << STDIN_BUFF_SIZE << " \n "
21712160 " \n "
21722161 " # error messages\n "
2173- " ERROR_template: .ascii \" ERROR : instr_\"\n "
2162+ " ERROR_template: .ascii \"\\ nERROR : instr_\"\n "
21742163 " .equ ERROR_template_len, . - ERROR_template\n "
21752164 " \n "
21762165 " jmp_error_message: .ascii \" : jmp destination out of bounds: \"\n "
0 commit comments