Writing and extending TINY language compiler (1)

xiaoxiao2021-04-10  415

@echo off; goto make

;::::: :::::::::::::::::::::: ::::: :::::::::::::::::::::: ; DFA3_A_MNEM - Substation implementation, Tiny expansion revision; effect: ;; WenfengMtd@163.com); debug is fun job's in ollydbg v1.09 ;;; :::::::::::::::::;: ::::: :::::::::::::::::::::: ::::: ::::::::::::::::::::::

.386.Model flat, StdCallOption Casemap: NONE

;::::: :::::::::::::::::::::: ::::: :::::::::::::::::::::: Include files; :::::::::::::: :::::::::: ::::: :::::::::::::::::::::: :::

include /masm32/include/windows.incinclude /masm32/include/kernel32.incinclude /masm32/include/user32.incinclude /masm32/include/MASM32.INC include /masm32/include/shell32.incinclude /masm32/macros/mymacros.asm

Includelib /masm32/lib/kernel32.libincludelib /masm32/lib/user32.libincludedelib /masm32/lib/masm32.libincludedelib /masm32/lib/shell32.lib ;::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ::::: :::::::::::::::::::::: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ::::: :::::::::::::::::::::: :::::: :::::::::::::::::::::

; Define the c typectyped

Define The Buffer's Store Length of Read from File and The ID Maximus Lengthbufflen EQU 256MaxTokenlen EQU 40

; define the condition's compiler flagtracescan Equ True

Define The Tokentype Pre-Tenum TTYPE, TENDFILE, TEERROR, TIF, TTHEN, TELSE, TEND, TREPEAT, TUNTIL, TAAD, TWRITE, TID, TNUM, TASSIGN, TEQ, TLT, TPLUS, TMINUS, TTIMES, TOVER, TMOD, Tlparen, trparen, tsemi, twhile, tdo, / tnone ;: :::::::::::::::::::::::::::: ::::: :::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ::::: :::::::::::::::::::::: :::::::::::: See the MYMAM 9.0 Macro by the macros to see the MYMAM 9.0 MACRO

;::::: :::::::::::::::::::::: ::::: :::::::::::::::::::::: Data; ::: ::::::::::::::::::: ::::: :::::::::::::::::::::: ::. data

Define The DFA Table Colclassify Char 256 DUP (17); Use The Hash Funtion To Find The Token Id Or Keyword, The Hash Funtion IS S [1] S [2]% 19, Tnone Mean Idhashtab Byte Twhile, TEND , TNONE, TNONE, TREPEAT, TNONE, TNONE, TIF, TREAD, TNONE, TWRITE, T10, TNONE, TELSE, TTHEN, TDO, TUNTIL, TNONE; Some Message String Accert Char "Accert!", 0DH, 0AH, 0Error char "Error!", 0dh, 0ah, 0; the buffer to store the char from source filelpbuffer byte bUFLEN dup (0), 0; the source filenamefilename char "test.tny", 0; to save the token stingtokenString char MAXTOKENLEN DUP (0), 0; When Could Not Open the Source File, Show The MessagerrorSg Char "Could Not Open THE", 0DH, 0AH, 0; The Cr Lf Char To Convenience Show String CRLF Char 0DH, 0AH, 0; To Save 32bit B-Num to Ascii String, But You Will See Is Reversal, So Here I Open A Var To Change The Sequence Ascvalue1 Char 20 DUP (0), 0AsscValue2 Char 20 DUP (0), 0; SCOURE File's HandlehscoureFile DWORD?; The Var Record The Current Line Numberl ineno dword 0; the var record the char number of bufferbufferSize dword 0; the keywordreservedWord1 char 'while', 0reservedWord2 char 'end', 0reservedWord3 char 'repeat', 0reservedWord4 char 'if', 0reservedWord5 char 'read', 0reservedWord6 char 'write ', 0RESERVEDWORD7 CHAR', 0RESERVEDWORD9 CHAR 'DO', 0RESERVEDWORD0 Char 'Until', 0;

the keyword tab, use in the hash find is ID or keyword. if compare equ, the token is keyword.reservedWordTab dword offset reservedWord1, offset reservedWord2,0,0, offset reservedWord3,0,0, offset reservedWord4, / offset reservedWord5, 0 , Offset ReservedWord6, 0, 0, 0, Offset RESERVEDWORD7, OFFSET RESERVEDWORD8, OFFSET RESERVEDWORD 9, / OFFSET RESERVEDWORD 9, / OFFSET RESERVEDWORD0, 0; CHR $ generated strings in the stack, if other definitions are placed behind it, they will access errors; ::::: :::::::::::::::::::::: ::::::::::::::::::: ;::::: :::::::::::::::::::::: ::::: ::::::::::::::::::::::

.code

;::::: :::::::::::::::::::::: ::::: :::::::::::::::::::::: ,:: ::::::::::::: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ::::: :::::::::::::::::::::: ::

;::::: :::::::::::::::::::::: ::::: :::::::::::::::::::::: ; getNextChar; ecx to save the current next char pos (linepos), if ecx> bufferSize mean must read the next line; esi save the lpbuffer current char point; edi save the tokenString current char point; and here 0 mean the EOF flag; ::::: :::::::::::::::::::::: :::::::::::::::::: GetNextchar Proc USES ECX MOV ECX, ESI SBB ECX, OFFSET LPBUFFER CMP ECX, Buffersize JB @Read; , 256) CMP EAX, 0 JE @eof; If you can not read, you will find that the end of the file is reached, return -1 representative EOV ESI, Offset LPBuffer JMP @ read @ EOF: MOV EAX, 0 Inc Edi Ret @ read: MOV Al, [ESI] MOV [EDI], Al CMP Al, 0DH JNE NOENDLINE INC LINENONOENDLINE: INC ESI Inc EDI AND ED, 0FFH RETGETNEXTCHAR ENDP; ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ::::: :::::::::::::::::::::: :::::::: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ::::: :::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::: "

;::::: :::::::::::::::::::::: ::::: :::::::::::::::::::::: ; fillTab; fill the DFA tab's col, may be you will ask how about DFA tab's row, you will see it define; in DFA produre, here i use a assembly tip The jump table.Jump table's always see in assembly;. code. This is hight language could not to give us! ;::::::::::::::::::: ::::: :::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::: " MOV [ESI EBX], 1 INC ESI CMP ESI, 90 JBE UALPHA_ MOV ESI, 97LALPHA_: MOV [ESI EBX], 1 Inc ESI CMP ESI, 122 JBE Lalpha_ Mov ESI, ':' MOV [ESI EBX], 2 MOV ESI, 'MOV [ESI EBX], 3 MOV ESI, 09H MOV [ESI EBX], 3 MOV ESI, 0DH MOV [ESI EBX], 3 MOV ESI, 0AH MOV [ESI EBX], 3 MOV ESI, '{' MOV [ESI EBX], 4 MOV ESI, '=' MOV [ESI EBX], 6 MOV ESI, 0 MOV [ESI EBX], 5 MOV E Si, '<' MOV [ESI EBX], 7 MOV ESI, ' ' MOV [ESI EBX], 8 MOV ESI, '-' MOV [ESI EBX], 9 MOV ESI, '*' MOV [ESI EBX], 10 MOV ESI, '/' MOV [ESI EBX], 11 MOV ESI, '%' MOV [ESI EBX], 12 MOV ESI, '(' MOV [ESI EBX], 13 MOV ESI, ')' MOV [ESI EBX], 14 MOV ESI, ';' MOV [ESI EBX], 15 MOV ESI, '}' MOV [ESI EBX], 16 Assume EBX: Nothing RetfillTab Endp; :::: ::::: :::::::::::::::::::::: :::::::::::::::::::::::::::::::: The Hash Valus (S [1] S [2])% 19;

THE Use the cmpsb to find the token is relays ts s i.......................... ::::: :::::::::::::::::::::: :::::::::::::::::::::::::::::: EDX MOV ECX, EDI MOV EDI, OFFSET TOKENSTRING SUB ECX, EDI MOV Al, [EDI 1] Add Al, [EDI 2] MOV EBX, 19 Div EBX MOV ESI, ReservedWordTab [EDX * 4] CMP ESI, 0 JE Hid Mov Cl, Hashtab [EDX] RET HID: MOV CL, TID RETHASHLOOKUP ENDP;: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ::::: :::::::::::::::::::::: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ::::: :::::::::::::::::::::: ::::::::::::::::::::::::ACS Proc Uses ESI ESI EDX EDI MOV ECX, 10 Lea ESI, Ascvalue1 Lea Edi : CMP EAX, ECX JB BTOACSL2 XOR EDX, EDX DIV ECX OR DL, 30H MOV [ESI], DL INC ESI JMP BTOACSL1BTOACSL2: OR Al, 30H MOV [EDI], Al Inc Edi Mov ECX, ESI SUB ECX, OFFSET Ascvalue1 CMP ECX, 0 JNE BTOACSL3 MOV BYTE PTR [EDI], ':' MOV BYTE PTR [EDI 1], '' MOV Byte Ptr [EDI 2], 0 Invoke Stdout, Addr AscValue2 Retout, Addr Ascvalue2 Rettoacsl3: Dec EsibtoAcSL4: MOV Al, [ESI] MOV [EDI], Al Dec ESI Inc EDI LOOP BTOACSL4 MOV BYTE PTR [EDI], ':' MOV BYTE PTR [EDI 1], '' MOV Byte Ptr [EDI 2], 0 Invoke Stdout, AddR AscValue2 Ret Btoacs Endp

;::::: :::::::::::::::::::::: ::::: :::::::::::::::::::::: : PrintFtoken; Here Same The C Tiny Printftoken, But I Use Jump Table Instead of The Switch Statement ;: :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ::::: :::::::::::::::::::::: :::::::::::::: Print FTOKEN Proc Uses EBX ECX

movzx ebx, cl jmp TokenCodeTab [ebx * 4] TokenCodeTab dword PENDFILECode, PErrorCode, PReservedWordCode, PReservedWordCode, PReservedWordCode, / PReservedWordCode, PReservedWordCode, PReservedWordCode, PReservedWordCode, / PReservedWordCode, PIDCode, PNUMCode, PASSIGNCode, PEQCode, PLTCode, PPLUSCode, PMINUSCode, / PTIMESCode, POVERCode, PMODCode, PLPARENCode, PRPARENCode, PSEMICode, / PReservedWordCode, PReservedWordCodePErrorCode: invoke StdOut, chr $ ( 'ERROR:') invoke StdOut, addr tokenString invoke StdOut, addr CRLF retPReservedWordCode: invoke StdOut, chr $ ( 'reserved word: ') invoke StdOut, addr tokenString invoke StdOut, addr CRLF retPASSIGNCode: invoke StdOut, chr $ (': = ') invoke StdOut, addr CRLF retPLTCode: invoke StdOut, chr $ (' < ') invoke StdOut, addr CRLF retPEQCode: invoke Stdout, Chr $ ('=') Invoke Stdout, Addr CRLF RETPLPARENCODE: Invoke Stdout, Chr $ (40) Invoke Stdout, Addr CRLF RETPARENCODE: Invoke Stdout, Chr $ (41) Invoke Stdout, Addr CRL F retPSEMICode: invoke StdOut, chr $ ( ';') invoke StdOut, addr CRLF retPPLUSCode: invoke StdOut, chr $ ( ' ') invoke StdOut, addr CRLF retPMINUSCode: invoke StdOut, chr $ ( '-') invoke StdOut, addr CRLF retPTIMESCode: invoke StdOut, chr $ ( '*') invoke StdOut, addr CRLF retPOVERCode: invoke StdOut, chr $ ( '/') invoke StdOut, addr CRLF retPMODCode: invoke StdOut, chr $ ( '%') invoke StdOut , addr CRLF retPENDFILECode: invoke StdOut, chr $ ( 'EOF') invoke StdOut, addr CRLF retPNUMCode: invoke StdOut, chr $ ( 'NUM, val =') invoke StdOut, addr tokenString invoke StdOut, addr CRLF retPIDCode: invoke StdOut, CHR $ ('

ID, Name = ') Invoke stdout, addr tokenstring invoke stdout, addr crlf Retprintftoken endp ;: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ::::: :::::::::::::::::::::: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ::::: :::::::::::::::::::::: ::::::::::::::::::::::

getToken proc uses ebx eax xor eax, eax xor ecx, ecx lea ebx, ClassifyState0: invoke getNextChar xlat Classify mov cl, TokenTab [eax] cmp al, 3 jb S0next cmp al, 5 ja S0next dec edi S0next: jmp State0Tab [eax * 4]

Tnone, Tminus, Tlt, TPlus, Tlparen, Telus, Tlparen, Telus, Tlparen, Trparen, Temi, Tlparen, Trparen, TSEMI, TLPARROR, TLUS, TLPARROR, TLUS, TLPARROR, TLUS, TLPARROR, TEERROR

State0Tab DWord State3, State4, State2, State0, State1, State5, State5, State5, / State5, State5, State5, State5, State5, State5, State5, State5, / State5, State5

State1: Invoke GetNextchar Dec Edi XLAT CMP Al, 16 JE State0 JMP State1

State2: Invoke GetNextchar XLAT CMP Al, 6 JNE S2_NEQ MOV CL, TASSIGN JMP State5S2_Neq: Invoke Ungetnextchar Dec Edi Mov Cl, Terror JMP State5

State3: Invoke GetNextchar XLAT CMP Al, 0 Je State3 Invoke Ungetnextchar Dec Edi Mov Cl, Tnum Jmp State5

State4: Invoke GetNextchar XLAT CMP Al, 1 Je State4 Invoke UngetnextChar Dec EDI MOV CL, TID; JMP State5 - This instruction can save

State5: assume edi: ptr byte mov [edi], 0 .if cl == TID invoke hashLookup .endif if TraceScan push ecx invoke StdOut, chr $ (09h) mov eax, lineno invoke BTOACS pop ecx invoke printfToken endif assume edi: nothing RETGETTOKEN ENDP; ::::::::::::::::::::::::::: ::::: :::::::::::::::::::::: ::: :::::::::::::::::::::: ::::: :::::::::::::::::::::: :::: start proc invoke FillTab mov esi, offset lpbuffer mov hScoureFile, fopen_r (filename) push ecxtest_: mov edi, offset tokenString invoke getToken cmp cl, TENDFILE jne test_ pop ecx invoke ExitProcess, 0start endp

;::::: :::::::::::::::::::::: ::::: :::::::::::::::::::::: ; :::::: :::::::::::::::::::::: ::::: :::::::::::::::::::::: :

End Start

: Make

SET DRV = DFA

/ MASM32 / BIN / ML / C / COFF% DRV% .BAT / MASM32 / BIN / LINK / SUBSYSTEM: CONSOLE% DRV% .Obj

DEL% DRV% .Obj

echo.pause

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;

Macro definition

Comment * -------------------------------------------------- ----------------- Preprocessor Code for High Level Language Simulation In Masm32

Updated 22th 6 2006 ------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------ *

Reparg Macro Arg Local Nustr Quot Substr , 1, 1 iFidn Quot, <"> ;; if 1st char =" .data Nustr DB Arg, 0 ;; Write Arg to .Data section .code exitm ;; else return arg endif endm; ------------------------------- ------------------------------------------; Open an existing file with read / Write access and return the file handle; ------------------------------------------ ------------------------------ Fopen_r Macro FileName Local ErrorMsg .data ErrorMsg DB "Could Not Open THE FILE", 0DH, 0ah, 0 .code invoke CreateFile, addr filename, GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL .if eax == INVALID_HANDLE_VALUE invoke MessageBox, NULL, addr ErrorMsg, NULL, MB_OK OR MB_ICONEXCLAMATION invoke ExitProcess, 0 .endif EXITM ;; Return File Han DLE ENDM

; ------------------------------------; Read Data from An Open File Into A Memory Buffer; --------------------------------------- --------- FREAD MACRO HFILE, BUFFER, BCNT LOCAL VAR .DATA? VAR DD? Code Invoke Readfile, HFile, Buffer, BCNT, ADDR VAR, NULL MOV Eax, Var Exitm ; Return BYTES read ENDM

; ------------------------------------; DEFINE A ENUM TYPE ENUMNAME A THEPE NAME; ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------- Enum macro enumname, PARMList: VARARG LOCAL Count Count = 0 Enumname Typef Byte For Parm, PARM = Count Count = Count 1 Endm Endm; ---------- --------------------------------------; Define C Type; ------- ----------------------------------------- Ctyped Macro Integer TypedEf Word Unsigned Typef Word Ushort Typedef Word Long TypedEf DWORD ULONG TYPEDEF DWORD CHAR TYPEDEF BYTE ENDM

CHR $ macro any_text: vararg local txtname .data txtname db any_text, 0 .code exitm endm

Len Macro LPSTRING Invoke Szlen, REPARG (LPSTRING) EXITM Endm

ustr $ MACRO DDvalue ;; unsigned integer from string LOCAL rvstring .data rvstring db 20 dup (0) align 4 .code ;; invoke dwtoa, DDvalue, ADDR rvstring invoke crt__ultoa, DDvalue, ADDR rvstring, 10 EXITM ENDM

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Test file file name Test.yny

{Sample Program in Tiny Language - Computes Factorial} = {INPUT An INTEGER} IF 0

转载请注明原文地址:https://www.9cbs.com/read-133238.html

New Post(0)