-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathASMCODE.ABC
1 lines (1 loc) · 12.2 KB
/
ASMCODE.ABC
1
Nick Dark QB to NASM [email protected] 12-11-02 ( : ) QB,PDS 340 12683 B2NASM.BAS DECLARE SUB badend (fromparse!)ãDECLARE SUB conv (outfile$)ãDECLARE FUNCTION TRIM$ (tin$)ãDECLARE SUB parser (infile$)ã'The idea of this program is to convert QB source code into nasm source code.ã'I have spent a long time searching the net for a program that does this, butã'to no avail. So i have decided to write this.ã'I am trying to keep as close to the Qbasic / QB syntax as possible, whileã'adding what i consider to be usefull additions.ã'I eventually hope to create a linux version as well.ã'It is in the very early stages of development (currently only supportingã'5 functions fully.) The input function Now works for strings, and theã'internal alloc function has been deleted. This program is completely freeã'for you to use as you wish, (but please acknowlege me). I however doubt itã'would be of much use to anyone in this state.ã'The only other thing I ask is that if you modify it please send me a copy ofã'the modified source code.ã'If you have any questions comments etc. please email me: [email protected]ã'Please send your modifications to that address as well.ã'If you would like an updated version, please email me at the abouve addressã'and i will send you the latest version.ãã'More information can be found in the help sub.ããTYPE varãname AS STRING * 20ãEND TYPEããDIM SHARED cvnum, vmax, linectr, outfile$ããcvnum = 0ãvmax = 20 'Max number of variablesãprintctr& = 0ãinputctr& = 0ãlinectr = 0ãpcomm = 0ããDIM SHARED vars(cvnum) AS varãCLSãã' INPUT "Input file: ", infile$ã' INPUT "Output file: ", outfile$ãinfile$ = "c:\test.bas"ãoutfile$ = "c:\test.asm"ãparser infile$ãconv outfile$ããSUB badend (fromparse)ã PRINT "Error on line:"; linectrã CLOSE #1ã CLOSE #2ã CLOSE #3ã IF fromparse = 0 THENã KILL "~data.dat"ã KILL outfile$ã ELSEã KILL "~parser.dat"ã END IFã ENDããEND SUBããSUB conv (outfile$)ãOPEN "~parser.dat " FOR INPUT AS #1ãOPEN "~data.dat" FOR OUTPUT AS #2ãOPEN outfile$ FOR OUTPUT AS #3ãOPEN "~bss.dat" FOR OUTPUT AS #4ããPRINT #3, "[bits 16]"ãPRINT #3, "org 0x100"ãPRINT #3, "[section .text]"ããDO UNTIL EOF(1)ãLINE INPUT #1, source$ãlinectr = linectr + 1ãsource$ = source$ + " "ãlinectr% = 0ãDO UNTIL linectr% = LEN(source$)ãlinectr% = linectr% + 1ããmidsrc$ = LEFT$(source$, linectr%)ãIF LCASE$(source$) = "/asm " THEN noconv = 0ãIF noconv = 1 THEN PRINT #3, source$: EXIT DOãIF ASC(MID$(source$, linectr%, 1)) = 32 THENããREM ****************************************ãREM ************ Between Here **************ãREM ****************************************ããSELECT CASE LCASE$(RTRIM$(midsrc$))ã CASE "print"ã PRINT #3, ";s"ã PRINT #3, ";s " + source$ã PRINT #3, ";s"ãã printctr& = printctr& + 1 'printctr gives the number on the end of the nasm pointerã tmp$ = RIGHT$(TRIM$(source$), LEN(source$) - linectr% - 1)ã IF NOT tmp$ = CHR$(34) THENã IF RIGHT$(TRIM$(source$), 1) = ";" THENã REM IF TRIM$(RIGHT$((tmp$), 1)) = ";" THENã flag = 1ã IF LEN(tmp$) >= 2 THENã tmp$ = LEFT$(tmp$, LEN(tmp$) - 2)ã ELSEã tmp$ = LEFT$(tmp$, LEN(tmp$) - 1)ã END IFã ELSEã flag = 0ã tmp$ = LEFT$(tmp$, LEN(tmp$) - 1)ã END IFã ELSEã tmp$ = ""ã END IFã PRINT #3, "mov ah,0x09"ã printctr$ = STR$(printctr&)ã PRINT #3, "mov dx,print" + RIGHT$(printctr$, LEN(printctr$) - 1)ã PRINT #3, "int 0x21"ã IF LEFT$(tmp$, 1) = CHR$(34) THENã tmp$ = RIGHT$(tmp$, LEN(tmp$) - 1)ã END IFãã IF flag = 1 THENã PRINT #2, "print" + TRIM$(printctr$) + " db " + "'" + tmp$ + "'" + ",'$'"ã ELSEãã PRINT #2, "print" + TRIM$(printctr$) + " db " + "'" + tmp$ + "'" + ",13,10,'$'"ã END IFã CASE "cls"ã PRINT #3, ";s"ã PRINT #3, ";s " + source$ã PRINT #3, ";s"ã PRINT #3, "push es"ã PRINT #3, "mov ax,0b800h"ã PRINT #3, "mov es, ax"ã PRINT #3, "xor di,di"ã PRINT #3, "mov ax,720h"ã PRINT #3, "cld"ã PRINT #3, "mov cx, 80 * 25 * 2"ã PRINT #3, "shr cx, 1"ã PRINT #3, "rep stosw"ã PRINT #3, "mov ah, 2"ã PRINT #3, "xor bx,bx"ã PRINT #3, "xor dx,dx"ã PRINT #3, "int 10h"ã PRINT #3, "pop es"ã CASE "asm"ã noconv = 1ã CASE "input"ã PRINT #3, ";s"ã PRINT #3, ";s " + source$ã PRINT #3, ";s"ã inputctr& = inputctr& + 1ã var$ = TRIM$(RIGHT$(source$, LEN(source$) - 5))ã PRINT #4, var$ + " " + "resb" + " 256"ã PRINT #3, "mov si," + var$ã PRINT #3, "inmain" + TRIM$(STR$(inputctr&)) + ":"ã PRINT #3, "mov ah,0"ã PRINT #3, "int 0x16"ã PRINT #3, "cmp al,0"ã PRINT #3, "je inmain" + TRIM$(STR$(inputctr&))ã PRINT #3, "stosb"ã PRINT #3, "mov ah,0x0e"ã PRINT #3, "int 0x010"ã PRINT #3, "cmp al,0x0d"ã PRINT #3, "jne inmain" + TRIM$(STR$(inputctr&))ãã CASE "pcomm" ' if pcomm is in the source file then anyã ' comments appear in the asm source as well.ã ' pcomm can be toggled on and off by callingã ' it multiple times.ã IF pcomm = 0 THEN pcomm = 1 ELSE pcomm = 0ã CASE "rem"ã IF pcomm = 1 THENã PRINT #3, ";"ã PRINT #3, "; " + RIGHT$(source$, LEN(source$) - 3)ã PRINT #3, ";"ã END IFã CASE "ap"ã PRINT #3, ";i"ã PRINT #3, ";i " + RIGHT$(source$, LEN(source$) - 2)ã PRINT #3, ";i"ã ãã CASE "'"ã IF pcomm = 1 THENã PRINT #3, ";"ã PRINT #3, "; " + RIGHT$(source$, LEN(source$) - 1)ã PRINT #3, ";"ã END IFãã CASE ELSEã SELECT CASE LCASE$(source$)ã CASE "/asm "ã CASE " "ã CASE ELSEã badend 0ã END SELECTãEND SELECTããREM ****************************************ãREM ************ And Here ******************ãREM ****************************************ãREM ****** Is platform specific!! **********ãREM ****************************************ãEXIT DOããEND IFããLOOPãLOOPãCLOSE #2ãCLOSE #4ããPRINT #3, ";s"ãPRINT #3, ";s DOS Quit Prog Code"ãPRINT #3, ";s"ãPRINT #3, "mov ah,0x4c"ãPRINT #3, "mov al,0x00"ãPRINT #3, "int 0x21"ããOPEN "~data.dat" FOR INPUT AS #2ãPRINT #3, ""ãPRINT #3, "[section .data]"ãDO UNTIL EOF(2)ãLINE INPUT #2, in$ãPRINT #3, in$ãLOOPããCLOSE #1ãCLOSE #2ããOPEN "~bss.dat" FOR INPUT AS #2ãPRINT #3, ""ãPRINT #3, "[section .bss]"ãDO UNTIL EOF(2)ãLINE INPUT #2, in$ãPRINT #3, in$ãLOOPãCLOSE #2ãCLOSE #3ããKILL "~data.dat"ãKILL "~bss.dat"ããEND SUBããSUB helpã'This sub is not designed to be run. If you wish to convert it into aã'callable / interactive resource of some kind feal free to do so.ã'I will only elaborate on any differences from the standard QB commands,ã'as standard statements are well documented in QB's online help.ã'Note: Variables are usupported at the moment.ãã'The currently supported commands are: (they are NOT case sensitive)ã'PRINT useing print on its own, currently dosn't workã' to get a blank line, you must use print ""ã'CLSã'ASM /ASMã' Any code inbetween these two markers, will assumed to be nasm assemblyã' source code and passed straight through to the output file completelyã' unchanged and unchecked.ã'INPUTã' Now works for string variables. (string variables=255 bytesã' currently)ã'PCOMM (pass comments)ã' Any comments between two PCOMM statements will appear in the assemblyã' source.ã'REMã' using a ' instead of REM will also workã' Currently, all remarks must be on a separate line. :-(ããã'To assemble the output file eg. test.asm, use:ã' nasm -fbin test.asm -o test.comã' if all goes well, then nasm will create test.com which you can run.ã'ãEND SUBããSUB parser (infile$)ãOPEN infile$ FOR INPUT AS #1ãOPEN "~parser.dat" FOR OUTPUT AS #2ãDO UNTIL EOF(1)ãLINE INPUT #1, source$ãlinectr% = 0ãmidsrc$ = ""ãDO UNTIL linectr% = LEN(source$)ãlinectr% = linectr% + 1ãmidsrc$ = LEFT$(source$, linectr%)ãlastspeech = 0ãSELECT CASE LCASE$(midsrc$)ã CASE "input"ã prin$ = ""ã param$ = RIGHT$(TRIM$(source$), LEN(source$) - 5)ã FOR i = 1 TO LEN(param$)ã IF MID$(param$, i, 1) = CHR$(34) THENã lastspeech = iã IF prin = 0 THENã prin = 1ã ELSEã prin = 0ã END IFã END IFã IF prin = 1 AND NOT MID$(param$, i, 1) = CHR$(34) THEN prin$ = prin$ + MID$(param$, i, 1)ã NEXTã var$ = TRIM$(RIGHT$(param$, lastspeech - 1))ã IF LEN(prin$) > 0 THENã PRINT #2, "print " + CHR$(34) + CHR$(34)ã PRINT #2, "print " + CHR$(34) + prin$ + CHR$(34)ã PRINT #2, "ap this and the command after this form the input command"ã PRINT #2, "input " + var$ã ELSEã PRINT #2, "input " + var$ã END IFã CASE "print"ã PRINT #2, source$ã CASE "cls"ã PRINT #2, source$ã CASE "rem"ã PRINT #2, source$ã CASE "'"ã PRINT #2, source$ã CASE "asm"ã PRINT #2, source$ã CASE "/asm"ã PRINT #2, source$ã CASE "pcomm"ã PRINT #2, source$ã CASE ELSEã 'badend 1ãEND SELECTãLOOPãLOOPãCLOSE #1ãCLOSE #2ãEND SUBããFUNCTION TRIM$ (tin$)ãTRIM$ = LTRIM$(RTRIM$(tin$))ãEND FUNCTIONãã