马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
.....但是败在我不懂编程语言的词法分析、语法分析、语义分析的原理,也写不出啊。
我是设计了汇编程序的模板,模拟我的全新编译器所生成的源文件。这个全新编译器本来是想读取现有C#源文件(仅接受ConsoleApp项目),然后转换成汇编(FASM)源文件,以便进一步生成Windows可执行文件,但是我败在不懂词法分析、语法分析、语义分析,不知道如何诠释C#语言啊(或任何其他编程语言)。
虽然如此,还是发布我的汇编程序模板在这里。
- format PE console
- include 'WIN32A.INC'
- entry start
- MAX_LEN equ 80
- section '.code' code readable executable
- start:
- ;HANDLE WINAPI GetStdHandle(
- ; _In_ DWORD nStdHandle
- ;);
- invoke GetStdHandle, -10
- mov dword [_stdin], eax
- invoke GetStdHandle, -11
- mov dword [_stdout], eax
- ;BOOL WINAPI ReadConsole(
- ; _In_ HANDLE hConsoleInput,
- ; _Out_ LPVOID lpBuffer,
- ; _In_ DWORD nNumberOfCharsToRead,
- ; _Out_ LPDWORD lpNumberOfCharsRead,
- ; _In_opt_ LPVOID pInputControl
- ;);
- invoke ReadConsole, dword [_stdin], _msg, MAX_LEN, _dummy, 0
- ;BOOL WINAPI WriteConsole(
- ; _In_ HANDLE hConsoleOutput,
- ; _In_ const VOID *lpBuffer,
- ; _In_ DWORD nNumberOfCharsToWrite,
- ; _Out_opt_ LPDWORD lpNumberOfCharsWritten,
- ; _Reserved_ LPVOID lpReserved
- ;);
- invoke WriteConsole, dword [_stdout], _msg, MAX_LEN, _dummy, 0
- invoke WriteConsole, dword [_stdout], _crlf, 2, _dummy, 0
- ;add
- mov eax, dword [_var_1]
- mov dword [_var_0], eax
- mov eax, dword [_var_2]
- add dword [_var_0], eax
- call wsprint
- ;subtract
- mov eax, dword [_var_1]
- mov dword [_var_0], eax
- mov eax, dword [_var_2]
- sub dword [_var_0], eax
- call wsprint
- ;multiply
- cdq
- mov eax, dword [_var_1]
- mov ebx, dword [_var_2]
- imul ebx
- mov dword [_var_0], eax
- call wsprint
- ;divide
- cdq
- mov eax, dword [_var_1]
- mov ebx, dword [_var_2]
- idiv ebx
- mov dword [_var_0], eax
- call wsprint
- ;void ExitProcess(
- ; [in] UINT uExitCode
- ;);
- invoke ExitProcess, 0
- wsprint:
- ;int WINAPIV wsprintfA(
- ; [out] LPSTR unnamedParam1,
- ; [in] LPCSTR unnamedParam2,
- ; ...
- ;);
- cinvoke wsprintfA, _buf, _fmt, dword [_var_0]
- mov dword [_len], eax
- invoke WriteConsole, dword [_stdout], _buf, dword [_len], _dummy, 0
- invoke WriteConsole, dword [_stdout], _crlf, 2, _dummy, 0
- ret
- section '.data' data readable writeable
- _buf rb 10 ;MAX =4294967295
- _fmt db '%d'
- _len dd ?
- _dummy dd ?
- _msg rb MAX_LEN
- _crlf db 13,10,0
- _stdin dd ?
- _stdout dd ?
- _var_1 dd 1234
- _var_2 dd 2
- _var_0 dd ?
- section '.idata' import data readable writeable
- library kernel32, 'kernel32.dll',\
- user32, 'user32.dll'
- include 'API/KERNEL32.INC'
- include 'API/USER32.INC'
复制代码 |
|