Our server costs ~$56 per month to run. Please consider donating or becoming a Patron to help keep the site running. Help us gain new members by following us on Twitter and liking our page on Facebook!
Current time: April 28, 2024, 12:33 pm

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Arithmetic Expression Compiler
#32
RE: Arithmetic Expression Compiler
I've written another example program in AEC, this time it's printing the Pascal's Traingle:
Code:
;Pascal's triangle
AsmStart ;Inline assembly in AEC starts with "AsmStart" and ends with "AsmEnd".
    macro pushIntegerToTheSystemStack decimalNumber ;This is why I've chosen FlatAssembler for the back-end of my compiler: powerful and easy-to-use preprocessor.
    {
        sub esp,4 ;"esp" is the CPU register which points right below the data at the top of the system stack.
        fld dword [decimalNumber]
        fistp dword [esp] ;"fistp" is the x86 assembly language directive for converting decimal numbers to integers.
    }
    macro pushPointerToTheSystemStack pointer
    {
        sub esp,4
        lea ebx,[pointer]
        mov [esp],ebx
    }
    macro pushStringToTheSystemStack string
    {
        sub esp,4
        mov dword [esp],string
    }
    format PE console ;"PE" means 32-bit Windows executable.
    entry start

    include 'win32a.inc' ;FlatAssembler macros for importing functions from DLLs.
    section '.text' code executable
    
    start:
    jmp howManyRowsString$
    howManyRowsString:
        db "How many rows of Pascal's triangle do you want to be printed?",10,0 ;10 is '\n', and 0 is '\0'.
    howManyRowsString$:
    pushStringToTheSystemStack howManyRowsString
    call [printf] ;printf(howManyRowsString)
    jmp theFloatSymbol$
    theFloatSymbol:
        db "%f",0
    theFloatSymbol$:
    pushPointerToTheSystemStack numberOfRows
    pushStringToTheSystemStack theFloatSymbol
    call [scanf] ;scanf(theFloatSymbol,&numberOfRows)
AsmEnd
currentRow := 0
While currentRow < numberOfRows | currentRow = numberOfRows
    AsmStart
        jmp currentRowString$
        currentRowString:
            db "Row #%d:",9,0 ;9 is '\t' (the tabulator).
        currentRowString$:
        pushIntegerToTheSystemStack currentRow
        pushStringToTheSystemStack currentRowString
        call [printf] ;printf(currentRowString,currentRow)
    AsmEnd
    currentColumn:=0
    While currentColumn < currentRow | currentColumn = currentRow
        If currentColumn = 0
            array (currentRow * numberOfRows + currentColumn) := 1 ;When I haven't programmed the compiler to deal with 2-dimensional arrays...
        ElseIf currentColumn = currentRow
            array (currentRow * numberOfRows + currentColumn) := 1  
        Else
            numberImmediatelyAbove := array ( (currentRow - 1) * numberOfRows + currentColumn)
            numberBeforeTheImmediatelyAboveOne := array ( (currentRow - 1) * numberOfRows + currentColumn - 1)
            array (currentRow * numberOfRows + currentColumn) := numberBeforeTheImmediatelyAboveOne + numberImmediatelyAbove
        EndIf
        numberToBePrinted := array (currentRow * numberOfRows + currentColumn)
        AsmStart
            jmp integerSignWithTabulator$
            integerSignWithTabulator:
                db "%.0f",9,0 ;"%.0f\t", "%.0f" means for "printf" to round the decimal number to the nearest integer.
            integerSignWithTabulator$:
            fld dword [numberToBePrinted]
            fstp qword [esp] ;"qword" means "double", because "printf" from "MSVCRT.DLL" can't print "float" which hasn't been converted to "double". When writing in Assembly, you need to deal with that kind of annoying stuff.
            pushStringToTheSystemStack integerSignWithTabulator
            call [printf] ;printf(integerSignWithTabulator,numberToBePrinted)
        AsmEnd
        currentColumn := currentColumn + 1
    EndWhile
    AsmStart
        jmp newLineString$
        newLineString:
            db 10,0 ;"\n"
        newLineString$:
        pushStringToTheSystemStack newLineString
        call [printf] ;printf(newLineString)
    AsmEnd
    currentRow := currentRow + 1
EndWhile
AsmStart
pushStringToTheSystemStack pauseString
call [system] ;system(pauseString), the "Press any key to continue..." message so that the console window doesn't immediately close.
invoke exit,0 ;exit(0)

pauseString db "PAUSE",0

section '.rdata' readable writable
    result dd ? ;A variable used internally by the AEC compiler.
    numberOfRows dd ?
    currentRow dd ?
    currentColumn dd ?
    numberBeforeTheImmediatelyAboveOne dd ?
    numberImmediatelyAbove dd ?
    numberToBePrinted dd ?
    array dd 30000 DUP(?)

section '.idata' data readable import
    library msvcrt,'msvcrt.dll' ;"Microsoft Visual C Runtime Library", available as "C:\Windows\System32\msvcrt.dll" on Windows 98 and newer.
        import msvcrt,printf,'printf',system,'system',exit,'exit',scanf,'scanf',clock,'clock'
AsmEnd
Now there are 7 example programs in the ZIP archive. It's kind of a symbolic number, I think I'll stop there for some time. I've also slightly refactored the "permutations" example to make it easier to read.



Messages In This Thread
Arithmetic Expression Compiler - by FlatAssembler - January 2, 2019 at 3:01 am
RE: Arithmetic Expression Compiler - by bennyboy - January 2, 2019 at 7:15 am
RE: Arithmetic Expression Compiler - by Angrboda - January 2, 2019 at 9:56 am
RE: Arithmetic Expression Compiler - by FlatAssembler - January 3, 2019 at 4:47 am
RE: Arithmetic Expression Compiler - by Angrboda - January 3, 2019 at 9:27 am
RE: Arithmetic Expression Compiler - by FlatAssembler - January 3, 2019 at 12:35 pm
RE: Arithmetic Expression Compiler - by bennyboy - January 3, 2019 at 9:09 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - January 9, 2019 at 5:08 am
RE: Arithmetic Expression Compiler - by bennyboy - January 9, 2019 at 5:09 pm
RE: Arithmetic Expression Compiler - by SteelCurtain - January 9, 2019 at 10:24 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - January 10, 2019 at 8:16 am
RE: Arithmetic Expression Compiler - by bennyboy - January 10, 2019 at 10:33 am
RE: Arithmetic Expression Compiler - by FlatAssembler - January 12, 2019 at 8:02 am
RE: Arithmetic Expression Compiler - by bennyboy - January 12, 2019 at 9:36 am
RE: Arithmetic Expression Compiler - by FlatAssembler - January 31, 2019 at 3:50 am
RE: Arithmetic Expression Compiler - by bennyboy - January 31, 2019 at 8:42 am
RE: Arithmetic Expression Compiler - by FlatAssembler - February 8, 2019 at 2:30 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - May 2, 2019 at 11:00 am
RE: Arithmetic Expression Compiler - by FlatAssembler - May 10, 2019 at 3:53 am
RE: Arithmetic Expression Compiler - by Smaug - May 10, 2019 at 7:45 am
RE: Arithmetic Expression Compiler - by FlatAssembler - May 25, 2019 at 2:28 pm
RE: Arithmetic Expression Compiler - by Smaug - May 31, 2019 at 5:56 am
RE: Arithmetic Expression Compiler - by FlatAssembler - June 1, 2019 at 1:50 am
RE: Arithmetic Expression Compiler - by Smaug - June 1, 2019 at 1:24 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - June 2, 2019 at 7:26 am
RE: Arithmetic Expression Compiler - by Smaug - June 6, 2019 at 4:12 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - June 7, 2019 at 8:01 am
RE: Arithmetic Expression Compiler - by Jehanne - May 31, 2019 at 7:17 am
RE: Arithmetic Expression Compiler - by FlatAssembler - August 17, 2019 at 1:50 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - October 31, 2019 at 12:50 am
RE: Arithmetic Expression Compiler - by FlatAssembler - January 4, 2020 at 9:26 am
RE: Arithmetic Expression Compiler - by FlatAssembler - May 1, 2020 at 12:08 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - May 22, 2020 at 2:40 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - May 27, 2020 at 8:52 am
RE: Arithmetic Expression Compiler - by FlatAssembler - June 14, 2020 at 9:32 am
RE: Arithmetic Expression Compiler - by FlatAssembler - June 18, 2020 at 1:12 pm
RE: Arithmetic Expression Compiler - by arewethereyet - June 18, 2020 at 1:27 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - June 18, 2020 at 3:35 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - June 24, 2020 at 3:59 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - August 22, 2020 at 1:16 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - August 30, 2020 at 4:24 am
RE: Arithmetic Expression Compiler - by BrianSoddingBoru4 - August 30, 2020 at 6:51 am
RE: Arithmetic Expression Compiler - by FlatAssembler - August 30, 2020 at 10:56 am
RE: Arithmetic Expression Compiler - by BrianSoddingBoru4 - August 30, 2020 at 11:37 am
RE: Arithmetic Expression Compiler - by FlatAssembler - August 30, 2020 at 12:02 pm
RE: Arithmetic Expression Compiler - by BrianSoddingBoru4 - August 30, 2020 at 1:09 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - August 30, 2020 at 1:37 pm
RE: Arithmetic Expression Compiler - by Angrboda - August 30, 2020 at 12:14 pm
RE: Arithmetic Expression Compiler - by BrianSoddingBoru4 - August 30, 2020 at 2:01 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - August 30, 2020 at 2:06 pm
RE: Arithmetic Expression Compiler - by BrianSoddingBoru4 - August 30, 2020 at 2:15 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - August 30, 2020 at 4:39 pm
RE: Arithmetic Expression Compiler - by Grandizer - September 3, 2020 at 3:43 pm
RE: Arithmetic Expression Compiler - by BrianSoddingBoru4 - August 30, 2020 at 6:55 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - September 3, 2020 at 12:08 pm
RE: Arithmetic Expression Compiler - by BrianSoddingBoru4 - September 3, 2020 at 3:18 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - September 3, 2020 at 4:51 pm
RE: Arithmetic Expression Compiler - by BrianSoddingBoru4 - September 3, 2020 at 5:06 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - September 4, 2020 at 4:05 am
RE: Arithmetic Expression Compiler - by Angrboda - September 3, 2020 at 3:10 pm
RE: Arithmetic Expression Compiler - by BrianSoddingBoru4 - September 4, 2020 at 4:17 am
RE: Arithmetic Expression Compiler - by FlatAssembler - September 21, 2020 at 3:12 pm
RE: Arithmetic Expression Compiler - by HappySkeptic - September 24, 2020 at 1:12 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - October 5, 2020 at 5:01 pm
RE: Arithmetic Expression Compiler - by HappySkeptic - October 5, 2020 at 9:12 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - October 6, 2020 at 1:55 pm
RE: Arithmetic Expression Compiler - by FlatAssembler - October 18, 2020 at 9:11 am
RE: Arithmetic Expression Compiler - by FlatAssembler - July 17, 2021 at 12:29 pm
RE: Arithmetic Expression Compiler - by arewethereyet - July 17, 2021 at 12:36 pm

Possibly Related Threads...
Thread Author Replies Views Last Post
  Compiler Theory FlatAssembler 5 1025 October 27, 2020 at 10:48 am
Last Post: Angrboda



Users browsing this thread: 2 Guest(s)