Author Topic: Basic to Nasm compiler - Quick Basic Version  (Read 1013 times)

fatman2021

  • Hero Member
  • *****
  • Posts: 1052
  • Lord Jesus Christ, Son of God, have mercy on us.
Basic to Nasm compiler - Quick Basic Version
« on: May 21, 2012, 01:29:40 pm »
B2NASM.BAS

Code: [Select]
'B2Nasm - Basic to Nasm Compiler
'Copyright (C) 2003-2004  Nicholas Dark
'For more information, see license.txt

TYPE vartype
        name AS STRING * 10
        param AS STRING * 30
        vartype AS INTEGER      '0 for integer
                                '1 for string
                                '2 for sub
                                '-1 for error (shouldn't ever be used
                                '               see gettype)
        noalloc AS INTEGER      '1 do not add to .data section
                                '0 add to data section
END TYPE

DIM char AS STRING * 1

DIM SHARED include(20) AS STRING 'stores include file names for inclusion later
DIM SHARED look AS STRING  ' stores the current token from the file
DIM SHARED vars(20) AS vartype

DIM SHARED label AS INTEGER
DIM path AS STRING

DIM SHARED redir AS INTEGER 'defines whether emitln writes to test.s or s&f.s
label = 0

CLS

path = "c:\qbx\b2nasm\"

OPEN path + "test.bas" FOR BINARY AS #1
OPEN path + "test.s" FOR OUTPUT AS #2
OPEN path + "s&f.s" FOR OUTPUT AS #3

prolog  'main header
block   'main prog

epilog


CLOSE #1
CLOSE #3
OPEN path + "s&f.s" FOR INPUT AS #3
DO UNTIL EOF(3)
        LINE INPUT #3, sffile$
        PRINT #2, sffile$
LOOP
CLOSE #3
KILL path + "s&f.s"
'------------------include print
FOR i = 1 TO 20
        IF include(i) = "" THEN EXIT FOR
        OPEN "c:\qbx\b2nasm\inc\" + include(i) FOR INPUT AS #1
        DO UNTIL EOF(1)
                LINE INPUT #1, temp$
                emitln temp$
        LOOP
        CLOSE #1
NEXT

'-----------------var print
emitln "[Section .data]"
FOR i = 1 TO 20
        IF vars(i).noalloc = 0 THEN
                IF NOT LEFT$(vars(i).name, 1) = CHR$(0) AND LEFT$(vars(i).param, 1) = CHR$(0) THEN emitln vars(i).name + " dw 0"
                IF NOT LEFT$(vars(i).param, 1) = CHR$(0) THEN emitln vars(i).name + " db " + RTRIM$(vars(i).param)
        END IF
NEXT
CLOSE #2
SHELL ".\b2nasm\nasm -fbin .\b2nasm\test.s -o .\b2nasm\test.com"

SUB abort (msg AS STRING)
PRINT CHR$(9);
PRINT msg
END
END SUB

SUB allocvar (vname AS STRING, vtype, param AS STRING)
IF redir = 0 THEN       ' this stops us from reallocating variables when
                        ' inside subs. We will have to use else to handle
                        ' local vars though.
        FOR i = 1 TO 20
                IF LEFT$(vars(i).name, LEN(vname)) = vname THEN EXIT SUB
        NEXT


        IF vtype = 0 THEN 'integer
                FOR i = 1 TO 20
                        IF LEFT$(vars(i).name, 1) = CHR$(0) THEN
                                vars(i).name = vname
                                vars(i).vartype = vtype
                                EXIT FOR
                        END IF
                NEXT
        END IF
        IF vtype = 2 THEN   'sub
                FOR i = 1 TO 20
                        IF LEFT$(vars(i).name, 1) = CHR$(0) THEN
                                vars(i).name = vname
                                vars(i).vartype = vtype
                                vars(i).noalloc = 1
                                EXIT FOR
                        END IF
                NEXT
        END IF
END IF

IF vtype = 1 THEN         'literal strings however will be global
                          '(At least for now)
        FOR i = 1 TO 20
                IF LEFT$(vars(i).name, 1) = CHR$(0) THEN
                        vars(i).name = vname
                        vars(i).param = param
                        vars(i).vartype = vtype
                        EXIT FOR
                END IF
        NEXT
END IF

END SUB

SUB assignment
DIM variable AS STRING

variable = look

SELECT CASE gettype(variable)
CASE 2
       
        look = scan
        i = 0
        IF look = "(" THEN
                DO UNTIL look = ")"
                        i = i + 1
                        look = scan
                        term
                        emitln "push ax"
                        look = scan
                LOOP

                emitln "call " + variable
                look = scan
                emitln "add sp," + STR$(i * 2)
        END IF
CASE ELSE
        look = scan
        IF look = "=" THEN
                look = scan
        ELSE
                abort "Expected ="
        END IF

        expression

        allocvar variable, 0, ""
        emitln ("mov [" + variable + "],ax")
END SELECT

END SUB

SUB block
DIM trash AS STRING * 1
look = scan
DO UNTIL EOF(1)
'        look = scan$
                IF look = "" THEN look = scan
        SELECT CASE (LCASE$(look))
                CASE "print"
                        doprint

                CASE "input"
                        doinput
                CASE "asm"
                        doasm
                CASE "if"
                        doif
                CASE "else"
                        EXIT SUB
                CASE "rem"
                        docomm
                CASE "for"
                        dofor
                CASE "next"
                        EXIT SUB
                CASE "end"
                        EXIT SUB
                CASE "open"
                        doopen
                CASE "close"
                        doclose
                CASE "sub"
                        dossub
                CASE "declare"
                        dodeclare
                CASE CHR$(255)
                        PRINT "end"
jmp: GOTO jmp
                CASE ELSE
                        assignment

        END SELECT
        'GET #1, , trash
LOOP

END SUB

SUB condition (lab AS STRING)
look = scan
SELECT CASE look
        CASE ">"
                look = scan
                term
                emitln "pop bx"
                emitln "cmp bx,ax"
                emitln "jbe " + lab
        CASE "<"
                look = scan
                term
                emitln "pop bx"
                emitln "cmp bx,ax"
                emitln "jae " + lab
        CASE "="
                look = scan
                term
                emitln "pop bx"
                emitln "cmp ax,bx"
                emitln "jne " + lab
        CASE "#"
                look = scan
                term
                emitln "pop bx"
                emitln "cmp ax,bx"
                emitln "je " + lab
END SELECT
END SUB


SUB emitln (text AS STRING)
PRINT text
IF redir = 0 THEN
        PRINT #2, text
ELSE
        PRINT #3, text
END IF
END SUB

FUNCTION genlab$
label = label + 1
genlab$ = "L" + LTRIM$(STR$(label))
END FUNCTION

FUNCTION gettype (varname AS STRING)
FOR i = 1 TO 20
        IF LTRIM$(RTRIM$(vars(i).name)) = varname THEN
                gettype = vars(i).vartype
                EXIT FUNCTION
        END IF
NEXT
gettype = -1 'error var not exist (shouldn't ever get here)
END FUNCTION

FUNCTION ismathop (op AS STRING)
IF op = "+" OR op = "-" OR op = "/" OR op = "*" THEN
        ismathop = 1
ELSE
        ismathop = 0
END IF
END FUNCTION

SUB match (str AS STRING)
IF NOT LCASE$(look) = LCASE$(str) THEN abort "Expected: " + UCASE$(str)
END SUB

SUB postlab (label AS STRING)
DIM abel AS STRING
abel = label + ":"
emitln abel
END SUB


SUB term
emitln ";term"
'look = scan
IF isnum(look) = 1 THEN
        emitln "mov ax, " + look
ELSE
        emitln "mov ax, [" + look + "]"
        allocvar look, 0, ""
END IF

END SUB
Woe to those who call evil good, and good evil;
Who put darkness for light, and light for darkness;
Who put bitter for sweet, and sweet for bitter!

Isaiah 5:20

fatman2021

  • Hero Member
  • *****
  • Posts: 1052
  • Lord Jesus Christ, Son of God, have mercy on us.
Re: Basic to Nasm compiler - Quick Basic Version
« Reply #1 on: May 21, 2012, 01:31:18 pm »
INCLUDE.BAS

Code: [Select]
'B2Nasm - Basic to Nasm Compiler
'Copyright (C) 2003-2004  Nicholas Dark
'For more information, see license.txt

DECLARE SUB dodeclare ()
DECLARE FUNCTION gettype! (varname AS STRING)
DECLARE SUB dossub ()
DECLARE SUB dofprint ()
DECLARE SUB dofinput ()
DECLARE SUB doclose ()
DECLARE SUB match (str AS STRING)
DECLARE SUB doopen ()
DECLARE SUB dofor ()
DECLARE SUB docomm ()
DECLARE SUB condition (lab AS STRING)
DECLARE SUB doif ()
DECLARE SUB postlab (label AS STRING)
DECLARE SUB doasm ()
DECLARE FUNCTION genlab$ ()
DECLARE SUB allocvar (vname AS STRING, vtype!, param AS STRING)
DECLARE SUB assignment ()
DECLARE SUB doinput ()
DECLARE SUB abort (msg AS STRING)
DECLARE SUB block ()
DECLARE SUB doadd ()
DECLARE SUB dodiv ()
DECLARE SUB doinclude (filename AS STRING)
DECLARE SUB domul ()
DECLARE SUB doprint ()
DECLARE SUB dosub ()
DECLARE SUB emitln (text AS STRING)
DECLARE SUB expression ()
DECLARE FUNCTION ismathop! (op AS STRING)
DECLARE SUB prolog ()
DECLARE SUB term ()
DECLARE FUNCTION scan$ ()
DECLARE FUNCTION isalpha! (char AS STRING)
DECLARE FUNCTION isalphanum! (char AS STRING)
DECLARE FUNCTION isnum! (char AS STRING)
DECLARE FUNCTION isspecial! (char AS STRING)
DECLARE FUNCTION getchar$ ()
declare sub epilog ()
Woe to those who call evil good, and good evil;
Who put darkness for light, and light for darkness;
Who put bitter for sweet, and sweet for bitter!

Isaiah 5:20

fatman2021

  • Hero Member
  • *****
  • Posts: 1052
  • Lord Jesus Christ, Son of God, have mercy on us.
Re: Basic to Nasm compiler - Quick Basic Version
« Reply #2 on: May 21, 2012, 01:32:36 pm »
COMMAND.BAS

Code: [Select]
'B2Nasm - Basic to Nasm Compiler
'Copyright (C) 2003-2004  Nicholas Dark
'For more information, see license.txt

SUB doasm
DIM var AS STRING
var = getchar$
var = getchar$
emitln ";doasm"
DO
        var = ""
        DO
                var = var + getchar$
        LOOP UNTIL RIGHT$(var, 1) = CHR$(10)
        var = LEFT$(var, LEN(var) - 2)
        IF LEFT$(LCASE$(var), 7) = "end asm" THEN EXIT DO
        emitln var
LOOP
IF NOT LEFT$(LCASE$(var), 7) = "end asm" THEN abort "Expected: ASM"
look = scan
END SUB

SUB doclose
DIM fhandle AS STRING
emitln ";doclose"

look = scan
look = scan
fhandle = "b2nfh" + look
emitln "mov ah, 0x3e"
emitln "mov bx, [" + fhandle + "]"
emitln "int 0x21"

look = scan
END SUB

SUB docomm
DIM var AS STRING * 1

DO UNTIL var = CHR$(10)
        var = getchar
LOOP
look = scan
END SUB

SUB dodeclare
look = scan
SELECT CASE LCASE$(look)
        CASE "sub"
                look = scan 'look now contains sub name
                allocvar look, 2, ""
        CASE "function"
                look = scan 'look now contains function name
                'this will deal with declareing functions
        CASE ELSE
                abort "Expected : SUB or FUNCTION"
END SELECT
look = scan
END SUB

SUB dofinput
DIM fhandle AS STRING
DIM errh AS STRING
DIM noerr AS STRING
emitln ";dofinput"

errh = genlab
noerr = genlab

fhandle = "b2nfh" + scan
allocvar "b2nnumin", 0, ""
emitln "mov bx, [" + fhandle + "]"

look = scan
look = scan
emitln "call fgeti"
emitln "mov [" + look + "],ax"
allocvar look, 0, ""
        doinclude "putc.inc"
        doinclude "puti.inc"
        doinclude "fgetc.inc"
        doinclude "fgeti.inc"
       

END SUB

SUB dofor
DIM var AS STRING
DIM lab1 AS STRING
DIM lab2 AS STRING

lab1 = genlab
lab2 = genlab
emitln ";dofor"
look = scan
var = look
look = scan
IF NOT look = "=" THEN abort "Expected: ="
look = scan
term
emitln "mov [" + var + "],ax"
allocvar var, 0, ""
emitln "push ax"
look = scan
IF NOT LCASE$(look) = "to" THEN abort "Expected: TO"
look = scan
term
emitln "push ax"
postlab lab1
emitln "pop bx"
emitln "pop ax"
emitln "cmp ax,bx"
emitln "ja " + lab2
emitln "push ax"
emitln "push bx"
'look = scan

block
'IF LCASE$(look) = "next" THEN look = scan
look = scan
emitln "pop bx"
emitln "pop ax"
emitln "inc ax"
emitln "mov [" + var + "],ax"
emitln "push ax"
emitln "push bx"
emitln "jmp " + lab1
postlab lab2
'emitln "pop bx"
'emitln "pop ax"
'look = scan
END SUB

SUB dofprint
DIM var AS STRING
DIM handle AS STRING

handle = "b2nfh" + scan

look = scan
'var = scan
look = scan
allocvar "b2nnumin", 0, ""

doinclude "fputc.inc"
doinclude "putc.inc"
doinclude "fputi.inc"

term
'emitln "mov ax, [" + var + "]"
'emitln "add al,0x30"
emitln "mov bx,[" + handle + "]"
emitln "mov si,bx"
emitln "call fputi"

END SUB

SUB doif
DIM lab AS STRING
DIM lab2 AS STRING
emitln ";doif"
lab = genlab
lab2 = genlab

look = scan
term
emitln "push ax"
condition lab2
look = scan
IF NOT LCASE$(look) = "then" THEN abort "Expected: THEN"
look = scan
look = scan
block
emitln "jmp " + lab
postlab lab2
IF LCASE$(look) = "else" THEN
        block
END IF

postlab lab

look = scan
IF NOT LCASE$(look) = "if" THEN abort "Expected IF"
look = scan
END SUB

SUB doinclude (filename AS STRING)
FOR i = 1 TO 20
4 IF include(i) = filename THEN
                EXIT SUB
        END IF
NEXT

FOR i = 1 TO 20
        IF include(i) = "" THEN
                include(i) = filename
                EXIT FOR
        END IF
NEXT
END SUB

SUB doinput
DIM variable AS STRING
emitln ";doinput"
look = scan
IF look = "#" THEN
        dofinput
ELSE
        variable = look
        doinclude "putc.inc"
        doinclude "getc.inc"
        doinclude "geti.inc"
        emitln "call geti"
        emitln "mov [" + variable + "], ax"
        allocvar variable, 0, ""
END IF
look = scan
END SUB

SUB doopen
DIM finame AS STRING
DIM itype AS STRING
DIM fhandle AS STRING
DIM tempstr AS STRING
DIM ejmp AS STRING
DIM errstr AS STRING
DIM noerrjmp AS STRING

emitln ";doopen"

look = scan
look = scan
finame = look
look = scan
look = scan
match "for"
look = scan
itype = look
look = scan
look = scan
look = scan
fhandle = "b2nfh" + look
tempstr = genlab
allocvar tempstr, 1, "'" + finame + "',0x00"
errstr = genlab
ejmp = genlab
allocvar fhandle, 0, ""

allocvar errstr, 1, "'File Error: ',0x00"
emitln "mov dx, " + tempstr
emitln "mov ah, 0x3d" 'code for open file
IF LCASE$(itype) = "input" THEN
        emitln "mov al,0"
ELSEIF LCASE$(itype) = "output" THEN
        emitln "mov al,1"
END IF

emitln "int 0x21"
emitln "jc " + ejmp
emitln "mov [" + fhandle + "], ax"

noerrjmp = genlab
doinclude "putc.inc"
doinclude "puti.inc"
doinclude "putstr.inc"
emitln "jmp " + noerrjmp

'error do this             'DOS file error codes:
postlab ejmp               '2: File not found
emitln "push ax"           '4: Too many files open
emitln "mov si, " + errstr '5: Access denied
emitln "call putstr"       '12: Invalid access
emitln "pop ax"            '
emitln "call puti"
look = scan

postlab noerrjmp
END SUB

SUB doprint
DIM char AS STRING
DIM param AS STRING
DIM variable AS STRING
emitln ";doprint"
look = scan 'look now contains the 1st parameter

IF LEFT$(look, 1) = "#" THEN
        dofprint
        fileflag = 1
ELSE
        fileflag = 0
        IF NOT look = CHR$(34) THEN
                doinclude "putc.inc"
                doinclude "puti.inc"

                emitln "mov ax, [" + look + "]"
                allocvar look, 0, ""
                emitln "call puti"
        ELSE
                freeform = 1
                look = scan
                DO UNTIL look = CHR$(34)
                        param = param + look + " "
                        look = scan
                LOOP
                param = LEFT$(param, LEN(param) - 1)
                freeform = 0
                variable = genlab$
                allocvar variable, 1, "'" + param + "',0x00"
                doinclude "putc.inc"
                doinclude "putstr.inc"
                emitln "mov si, " + variable
                emitln "call putstr"
                'PRINT param
        END IF
END IF


        look = scan
IF NOT fileflag = 1 THEN
        IF NOT look = ";" THEN
                emitln "mov al,0x0d"
                emitln "call putc"
                emitln "mov al,0x0a"
                emitln "call putc"
        ELSE
                look = scan
        END IF
END IF


'        IF NOT LCASE$(look) = "end" AND NOT LCASE$(look) = "next" THEN look = scan

END SUB

SUB dossub
DIM forname(6) AS STRING ' max of 6 params

look = scan
redir = 1  ' emitln writes to s&f.s
emitln look + ":" 'name of sub

look = getchar
IF look = "(" THEN
        ctr = 0
        DO UNTIL look = ")"
                look = scan
                IF look <> ")" THEN
                        forname(ctr) = look
                        look = scan
                        IF look <> "," AND look <> ")" THEN abort "Expected : ,"
                        ctr = ctr + 1
                        IF ctr > 6 THEN abort ("Max 6 formal params for a sub")
                END IF
        LOOP
ELSE
        SEEK #1, SEEK(1) - 1
END IF
FOR j = 0 TO 6
        IF forname(j) = "" THEN EXIT FOR
NEXT
FOR i = 0 TO j
        IF forname(i) <> "" THEN emitln ("%idefine " + forname(i) + " bp+1*" + LTRIM$(STR$(((j - i) * 2) + 2)) + "")
NEXT
emitln "push bp"
emitln "mov bp, sp"

block
             
emitln "pop bp"
look = scan
IF NOT LCASE$(look) = "sub" THEN abort "Expected : END SUB"

look = scan
emitln "ret"
redir = 0 'emitln writes to test.s (ie. output file)
END SUB
Woe to those who call evil good, and good evil;
Who put darkness for light, and light for darkness;
Who put bitter for sweet, and sweet for bitter!

Isaiah 5:20

fatman2021

  • Hero Member
  • *****
  • Posts: 1052
  • Lord Jesus Christ, Son of God, have mercy on us.
Re: Basic to Nasm compiler - Quick Basic Version
« Reply #3 on: May 21, 2012, 01:34:21 pm »
DOS.BAS

Code: [Select]
SUB prolog
emitln "[bits 16]"
emitln "[org 0x100]"
emitln "[section .text]"
END SUB

sub epilog
emitln "mov ax,0x4c00"  '}
emitln "int 0x21"       '} DOS exit code
end sub
Woe to those who call evil good, and good evil;
Who put darkness for light, and light for darkness;
Who put bitter for sweet, and sweet for bitter!

Isaiah 5:20

fatman2021

  • Hero Member
  • *****
  • Posts: 1052
  • Lord Jesus Christ, Son of God, have mercy on us.
Re: Basic to Nasm compiler - Quick Basic Version
« Reply #4 on: May 21, 2012, 01:35:41 pm »
MATH.BAS

Code: [Select]
SUB doadd
emitln ";doadd"
look = scan
emitln "push ax"
IF look = "(" THEN
        emitln "push ax"
        expression
ELSE
        term
END IF
emitln "pop bx"
emitln "add ax,bx"

END SUB

SUB dodiv
emitln ";dodiv"
emitln "push ax"
look = scan
IF look = "(" THEN
        emitln "push ax"
        expression
ELSE
        term
END IF
emitln "pop bx"
emitln "xchg ax,bx"
emitln "mov dx,0" 'zero extend ax into dx
emitln "div bx"

END SUB

SUB domul
emitln ";domul"
emitln "push ax"
look = scan
IF look = "(" THEN
        emitln "push ax"
        expression
ELSE
        term
END IF
emitln "pop bx"
emitln "mul bx"
END SUB

SUB dosub
emitln ";dosub"
PRINT "push ax"
look = scan
IF look = "(" THEN
        emitln "push ax"
        expression
ELSE
        term
END IF
emitln "pop bx"
emitln "sub ax,bx"
emitln "neg ax"

END SUB

SUB expression
IF look = "(" THEN look = scan: expression
IF look = ")" THEN emitln "pop bx": GOTO missterm
term
look = scan
missterm:
DO WHILE ismathop(look) = 1
       
        SELECT CASE look
                CASE "+"
                        doadd
                CASE "-"
                        dosub
                CASE "/"
                        dodiv
                CASE "*"
                        domul
        END SELECT
        look = scan
LOOP

END SUB
Woe to those who call evil good, and good evil;
Who put darkness for light, and light for darkness;
Who put bitter for sweet, and sweet for bitter!

Isaiah 5:20

fatman2021

  • Hero Member
  • *****
  • Posts: 1052
  • Lord Jesus Christ, Son of God, have mercy on us.
Re: Basic to Nasm compiler - Quick Basic Version
« Reply #5 on: May 21, 2012, 01:57:42 pm »
SCAN.BAS


Code: [Select]
dim SHARED freeform AS INTEGER

FUNCTION getchar$
DIM char AS STRING * 1

GET #1, , char
getchar$ = char

END FUNCTION

FUNCTION isalpha (char AS STRING)
IF ASC(UCASE$(char)) > 64 AND ASC(UCASE$(char)) < 91 OR char = "$" OR char = ":" OR char$ = "\" OR char$ = "/" OR char$ = "." THEN
        isalpha = 1
ELSE
        IF freeform = 1 AND char = CHR$(32) THEN
                isalpha = 1
        ELSE
                isalpha = 0
        END IF
END IF
END FUNCTION

FUNCTION isalphanum (char AS STRING)
IF isalpha(char) = 1 OR isnum(char) = 1 THEN
        isalphanum = 1
ELSE
        isalphanum = 0
END IF
END FUNCTION

FUNCTION isnum (char AS STRING)
IF ASC(char) > 47 AND ASC(char) < 58 THEN
        isnum = 1
ELSE
        isnum = 0
END IF
END FUNCTION

FUNCTION isspecial (char AS STRING)
SELECT CASE char
        CASE CHR$(34)
                isspecial = 1
        CASE "="
                isspecial = 1
        CASE "+"
                isspecial = 1
        CASE "-"
                isspecial = 1
        CASE "/"
                isspecial = 1
        CASE "*"
                isspecial = 1
        CASE "("
                isspecial = 1
        CASE ")"
                isspecial = 1
        CASE ">"
                isspecial = 1
        CASE "<"
                isspecial = 1
        CASE "#"
                isspecial = 1
        CASE ";"
                isspecial = 1
        CASE ","
                isspecial = 1
        CASE ELSE
                isspecial = 0
END SELECT
END FUNCTION

FUNCTION scan$
DIM char AS STRING * 1
DIM holding AS STRING

char = getchar
IF ASC(char) = 13 THEN char = getchar
IF ASC(char) = 10 THEN char = getchar  'SEEK #1, SEEK(1) - 1: GOTO xit
IF ASC(char) = 32 AND freeform = 0 THEN char = getchar'this will need fixing
IF ASC(char) = 9 THEN char = getchar
IF ASC(char) = 0 THEN GOTO xit
IF EOF(1) THEN GOTO xit

IF isspecial(char) = 1 THEN
        holding = char
        GOTO xit
END IF
IF isalpha(char) THEN
        DO WHILE isalphanum(char) = 1
                holding = holding + char
                char = getchar
        LOOP
        SEEK #1, SEEK(1) - 1
        GOTO xit
END IF

IF isnum(char) = 1 THEN
        DO WHILE isnum(char) = 1
                holding = holding + char
                char = getchar
        LOOP
        SEEK #1, SEEK(1) - 1
        GOTO xit
END IF

xit:
'IF ASC(char) = 0 THEN abort "Unexpected End-Of-File!"
scan$ = holding
END FUNCTION
Woe to those who call evil good, and good evil;
Who put darkness for light, and light for darkness;
Who put bitter for sweet, and sweet for bitter!

Isaiah 5:20

fatman2021

  • Hero Member
  • *****
  • Posts: 1052
  • Lord Jesus Christ, Son of God, have mercy on us.
Re: Basic to Nasm compiler - Quick Basic Version
« Reply #6 on: May 21, 2012, 02:29:04 pm »
fgetc.inc

Code: [Select]
;B2Nasm - Basic to Nasm Compiler
;Copyright (C) 2003-2004  Nicholas Dark
;For more information, see license.txt

fgetc:
push dx
push cx
mov ah, 0x3f
mov cx,1
mov dx, b2nnumin
int 0x21
jc .err

mov ax, [b2nnumin]

jmp .noerr

.err:
;we should probably set some sort
;of error flag here.
add al,0x30
call puti ;print error code
ret

.noerr
;if we are here, then ax contains
;the ascii code.
pop cx
pop dx
ret
Woe to those who call evil good, and good evil;
Who put darkness for light, and light for darkness;
Who put bitter for sweet, and sweet for bitter!

Isaiah 5:20

fatman2021

  • Hero Member
  • *****
  • Posts: 1052
  • Lord Jesus Christ, Son of God, have mercy on us.
Re: Basic to Nasm compiler - Quick Basic Version
« Reply #7 on: May 21, 2012, 02:30:10 pm »
fgeti.inc

Code: [Select]
;B2Nasm - Basic to Nasm Compiler
;Copyright (C) 2003-2004  Nicholas Dark
;For more information, see license.txt

fgeti:
push cx
push dx
mov dx,0
.get
;imul dx,10
call fgetc
sub ax,0x30
cmp al,9
ja .end
mov ah,0x00
add dx,ax
imul dx,10
jmp .get
.end
mov ax,dx
mov cl,10
idiv cl
pop dx
pop cx
ret
Woe to those who call evil good, and good evil;
Who put darkness for light, and light for darkness;
Who put bitter for sweet, and sweet for bitter!

Isaiah 5:20

fatman2021

  • Hero Member
  • *****
  • Posts: 1052
  • Lord Jesus Christ, Son of God, have mercy on us.
Re: Basic to Nasm compiler - Quick Basic Version
« Reply #8 on: May 21, 2012, 02:32:14 pm »
fputc.inc

Code: [Select]
;B2Nasm - Basic to Nasm Compiler
;Copyright (C) 2003-2004  Nicholas Dark
;For more information, see license.txt

fputc:
push ax
push bx
push cx
push dx
mov bx,si
mov [b2nnumin],ax
mov ah,0x40
mov cx,0x0001
mov dx,b2nnumin
int 0x21
jc .err
pop dx
pop cx
pop bx
pop ax
ret
.err
;if we are here, we should probably
;set some sort of error flag

add ax,0x30
call putc
pop dx
pop cx
pop bx
pop ax

ret
Woe to those who call evil good, and good evil;
Who put darkness for light, and light for darkness;
Who put bitter for sweet, and sweet for bitter!

Isaiah 5:20

fatman2021

  • Hero Member
  • *****
  • Posts: 1052
  • Lord Jesus Christ, Son of God, have mercy on us.
Re: Basic to Nasm compiler - Quick Basic Version
« Reply #9 on: May 21, 2012, 02:34:24 pm »
fputi.inc

Code: [Select]
;B2Nasm - Basic to Nasm Compiler
;Copyright (C) 2003-2004  Nicholas Dark
;For more information, see license.txt

fputi:
push ax
cmp ax,0
jge fdoit
push ax
mov al,'-'
call fputc
pop ax
neg ax
fdoit:
call fputi2
pop ax
ret

fputi2:
push bx
push cx
push dx
mov bx,10
xor cx,cx
fputi2lp:
xor dx,dx
div bx
or dl,'0' ;(instead of add ax,0x30)
push dx
inc cx
or ax,ax
jnz fputi2lp
fpopi2lp:
pop ax
call fputc
loop fpopi2lp
pop dx
pop cx
pop bx
mov ax,0x0d
call fputc
mov ax,0x0a
call fputc
ret
Woe to those who call evil good, and good evil;
Who put darkness for light, and light for darkness;
Who put bitter for sweet, and sweet for bitter!

Isaiah 5:20

fatman2021

  • Hero Member
  • *****
  • Posts: 1052
  • Lord Jesus Christ, Son of God, have mercy on us.
Re: Basic to Nasm compiler - Quick Basic Version
« Reply #10 on: May 21, 2012, 02:35:50 pm »
getc.inc

Code: [Select]
;B2Nasm - Basic to Nasm Compiler
;Copyright (C) 2003-2004  Nicholas Dark
;For more information, see license.txt

getc:
    xor ax,ax
    int 0x16
    ret
Woe to those who call evil good, and good evil;
Who put darkness for light, and light for darkness;
Who put bitter for sweet, and sweet for bitter!

Isaiah 5:20

fatman2021

  • Hero Member
  • *****
  • Posts: 1052
  • Lord Jesus Christ, Son of God, have mercy on us.
Re: Basic to Nasm compiler - Quick Basic Version
« Reply #11 on: May 21, 2012, 02:37:00 pm »
geti.inc

Code: [Select]
;B2Nasm - Basic to Nasm Compiler
;Copyright (C) 2003-2004  Nicholas Dark
;For more information, see license.txt

geti:
xor ax,ax
push ax
call getc
cmp al,'-'
jne ooops
mov cx,1
call putc
call getc
ooops:
call putc
cmp al,13
je .exit
sub al,0x30
xor dx,dx
mov dl,al
pop ax
add ax,dx
push ax

.getii:
call getc
call putc
cmp al,13
je .exit
sub al,0x30
xor dx,dx
mov dl,al
pop ax
imul ax,10
add ax,dx
push ax
jmp .getii

.exit:
pop ax
cmp cx,1
jne noneg
neg ax

noneg:
push ax
mov al,0x0d
call putc
mov al,0x0a
call putc
pop ax
ret
Woe to those who call evil good, and good evil;
Who put darkness for light, and light for darkness;
Who put bitter for sweet, and sweet for bitter!

Isaiah 5:20

fatman2021

  • Hero Member
  • *****
  • Posts: 1052
  • Lord Jesus Christ, Son of God, have mercy on us.
Re: Basic to Nasm compiler - Quick Basic Version
« Reply #12 on: May 21, 2012, 02:38:40 pm »
putc.inc

Code: [Select]
;B2Nasm - Basic to Nasm Compiler
;Copyright (C) 2003-2004  Nicholas Dark
;For more information, see license.txt

putc:
    mov ah,0x0e
    int 0x10
ret

Woe to those who call evil good, and good evil;
Who put darkness for light, and light for darkness;
Who put bitter for sweet, and sweet for bitter!

Isaiah 5:20

fatman2021

  • Hero Member
  • *****
  • Posts: 1052
  • Lord Jesus Christ, Son of God, have mercy on us.
Re: Basic to Nasm compiler - Quick Basic Version
« Reply #13 on: May 21, 2012, 02:39:38 pm »
puti.inc

Code: [Select]
;B2Nasm - Basic to Nasm Compiler
;Copyright (C) 2003-2004  Nicholas Dark
;For more information, see license.txt

puti:
push ax
cmp ax,0
jge doit
push ax
mov al,'-'
call putc
pop ax
neg ax
doit:
call puti2
pop ax
ret

puti2:
push bx
push cx
push dx
mov bx,10
xor cx,cx
puti2lp:
xor dx,dx
div bx
or dl,'0'
push dx
inc cx
or ax,ax
jnz puti2lp
popi2lp:
pop ax
call putc
loop popi2lp
pop dx
pop cx
pop bx
;        mov ah,0x0d
;        call putc
;        mov ah,0x0a
;        call putc
ret

Woe to those who call evil good, and good evil;
Who put darkness for light, and light for darkness;
Who put bitter for sweet, and sweet for bitter!

Isaiah 5:20

fatman2021

  • Hero Member
  • *****
  • Posts: 1052
  • Lord Jesus Christ, Son of God, have mercy on us.
Re: Basic to Nasm compiler - Quick Basic Version
« Reply #14 on: May 21, 2012, 02:40:35 pm »
putstr.inc

Code: [Select]
;B2Nasm - Basic to Nasm Compiler
;Copyright (C) 2003-2004  Nicholas Dark
;For more information, see license.txt

putstr:
    lodsb
    cmp al,0x00
    je .end
    call putc
    jmp putstr
.end
    ret
Woe to those who call evil good, and good evil;
Who put darkness for light, and light for darkness;
Who put bitter for sweet, and sweet for bitter!

Isaiah 5:20