Skip to content

Commit 0f1b3aa

Browse files
STASH
1 parent 38efcad commit 0f1b3aa

File tree

2 files changed

+26
-41
lines changed

2 files changed

+26
-41
lines changed

Masfix.cpp

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,8 +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"
1904-
" mov [rdx], cl\n"
1903+
" mov BYTE PTR [rip + stdout_buff], cl\n"
19051904
" mov r8, 1\n"
19061905
" call stdout_write\n";
19071906
} else if (instr == Iinc) {
@@ -1969,7 +1968,7 @@ void generate(ofstream& outFile, vector<Instr>& instrs) {
19691968
" push r8\n"
19701969
" push rdx\n"
19711970
" push rsi\n"
1972-
" mov rdx, ERROR_template\n"
1971+
" lea rdx, [rip + ERROR_template]\n"
19731972
" mov r8, ERROR_template_len\n"
19741973
" call stderr_write\n"
19751974
" pop rax # print instr number\n"
@@ -1979,8 +1978,7 @@ void generate(ofstream& outFile, vector<Instr>& instrs) {
19791978
" call stderr_write\n"
19801979
" pop rax # errorneous value\n"
19811980
" call print_unsigned_err\n"
1982-
" mov rdx, stdout_buff\n"
1983-
" mov BYTE PTR [rdx], 10 # '\\n'\n"
1981+
" mov BYTE PTR [rip + stdout_buff], 10 # '\\n'\n"
19841982
" mov r8, 1\n"
19851983
" call stderr_write\n"
19861984
" mov rax, 1 # exit(1)\n"
@@ -1990,33 +1988,30 @@ void generate(ofstream& outFile, vector<Instr>& instrs) {
19901988
" sub rsp, 32 # reserve shadow space\n"
19911989
" mov rcx, -10 # stdin fd\n"
19921990
" call GetStdHandle\n"
1993-
" mov rcx, stdin_fd\n"
1994-
" mov [rcx], rax\n"
1991+
" mov QWORD PTR [rip + stdin_fd], rax\n"
19951992
" mov rcx, -11 # stdout fd\n"
19961993
" call GetStdHandle\n"
1997-
" mov rcx, stdout_fd\n"
1998-
" mov [rcx], rax\n"
1994+
" mov QWORD PTR [rip + stdout_fd], rax\n"
19991995
" mov rcx, -12 # stderr fd\n"
20001996
" call GetStdHandle\n"
2001-
" mov rcx, stderr_fd\n"
2002-
" mov [rcx], rax\n"
1997+
" mov QWORD PTR [rip + stderr_fd], rax\n"
20031998
" add rsp, 32 # remove shadow space\n"
20041999
" ret\n"
20052000
"\n"
20062001
"# rdx - buff, r8 - number of bytes -> rax - number written\n"
20072002
"stdout_write:\n"
2008-
" mov rcx, QWORD PTR [QWORD PTR stdout_fd]\n"
2003+
" mov rcx, QWORD PTR [rip + stdout_fd]\n"
20092004
" jmp write_file\n"
20102005
"stderr_write:\n"
2011-
" mov rcx, QWORD PTR [QWORD PTR stderr_fd]\n"
2006+
" mov rcx, QWORD PTR [rip + stderr_fd]\n"
20122007
" # fall through to write_file\n"
20132008
"\n"
20142009
"# rcx - fd, rdx - buff, r8 - chars / buffsize -> rax - number read/written\n"
20152010
"write_file:\n"
2016-
" mov rax, WriteFile\n"
2011+
" lea rax, [rip + WriteFile]\n"
20172012
" jmp call_winapi_file_op\n"
20182013
"read_file:\n"
2019-
" mov rax, ReadFile\n"
2014+
" lea rax, [rip + ReadFile]\n"
20202015
"call_winapi_file_op:\n"
20212016
" mov rbp, rsp # save rsp @ retval\n"
20222017
" and rsp, -16 # force 16-byte alignment\n"
@@ -2030,42 +2025,35 @@ void generate(ofstream& outFile, vector<Instr>& instrs) {
20302025
" mov rsp, rbp\n"
20312026
" ret\n"
20322027
"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"
2028+
" mov rcx, QWORD PTR [rip + stdin_fd] # get chars into stdin_buff\n"
2029+
" lea rdx, [rip + stdin_buff]\n"
20362030
" mov r8, STDIN_BUFF_SIZE\n"
20372031
" call read_file\n"
20382032
"\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"
2033+
" mov QWORD PTR [rip + stdin_buff_char_count], rax # store how many were read\n"
2034+
" mov QWORD PTR [rip + stdin_buff_chars_read], 0 # 0 chars were processed\n"
20432035
" ret\n"
20442036
"\n"
20452037
"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"
2038+
" mov rcx, QWORD PTR [rip + stdin_buff_char_count] # if (char_count == chars_read) fill the stdin_buff\n"
2039+
" cmp rcx, QWORD PTR [rip + stdin_buff_chars_read]\n"
20502040
" jne stdin_peek_valid\n"
20512041
" call stdin_read\n"
20522042
"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"
2043+
" mov rax, QWORD PTR [rip + stdin_buff_chars_read] # char addr\n"
2044+
" add rcx, QWORD PTR [rip + stdin_buff]\n"
20562045
"\n"
20572046
" xor rdx, rdx # peek the char\n"
20582047
" mov dl, [rcx]\n"
20592048
" ret\n"
20602049
"get_next_char: # gets next char from stdin (buffered), advances read ptr -> rdx char\n"
20612050
" 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"
2051+
" inc QWORD PTR [rip + stdin_buff_chars_read] # eat the char\n"
20642052
" ret\n"
20652053
"\n"
20662054
"utos: # n - rax -> r8 char count, r9 - char* str\n"
20672055
" xor r8, r8 # char count\n"
2068-
" mov r9, stdout_buff+STDOUT_BUFF_SIZE-1 # curr buff pos\n"
2056+
" mov r9, [rip + stdout_buff+STDOUT_BUFF_SIZE-1] # curr buff pos\n"
20692057
" mov r10, 10 # base\n"
20702058
"utos_loop:\n"
20712059
" xor rdx, rdx\n"
@@ -2086,8 +2074,7 @@ void generate(ofstream& outFile, vector<Instr>& instrs) {
20862074
" ret\n"
20872075
"print_unsigned_err: # rax - n -> rax - num written\n"
20882076
" call utos\n"
2089-
" mov rcx, stderr_fd\n"
2090-
" mov rcx, [rcx]\n"
2077+
" mov rcx, QWORD PTR [rip + stderr_fd]\n"
20912078
" mov rdx, r9 # str\n"
20922079
" call write_file\n"
20932080
" ret\n"
@@ -2121,12 +2108,10 @@ void generate(ofstream& outFile, vector<Instr>& instrs) {
21212108
"_start:\n"
21222109
" # initialization\n"
21232110
" 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"
2111+
" mov QWORD PTR [rip + stdin_buff_char_count], 0\n"
2112+
" mov QWORD PTR [rip + stdin_buff_chars_read], 0\n"
21282113
"\n"
2129-
" mov r13, cells\n"
2114+
" mov r13, QWORD PTR [rip + cells]\n"
21302115
" xor r14, r14\n"
21312116
" xor r15, r15\n"
21322117
"\n";
@@ -2144,7 +2129,7 @@ void generate(ofstream& outFile, vector<Instr>& instrs) {
21442129
"\n"
21452130
"# runtime errors, expect instr number in rsi, errorneous value in rcx\n"
21462131
"jmp_error:\n"
2147-
" mov rdx, jmp_error_message\n"
2132+
" lea rdx, [rip + jmp_error_message]\n"
21482133
" mov r8, jmp_error_message_len\n"
21492134
" call error\n"
21502135
"\n"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Masfix builds high‑level features from the ground up with a powerful macro sys
1919

2020
> Work in progress — This project may change at any time. Some features may be unimplemented or inconsistent with docs.
2121
22-
# Setup
22+
# Setup
2323
1) Install toolchain
2424
* `g++ (x86_64-posix-seh-rev0, Built by MinGW-Builds project) 13.2.0`
2525
* native compilation:

0 commit comments

Comments
 (0)