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: July 17, 2019, 3:26 am

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Arithmetic Expression Compiler
#11
RE: Arithmetic Expression Compiler
Quote:if you could compile custom assembly code for them, you could multiply their efficiency by maybe 10x
I seriously doubt it. I assume most of those chips can be targeted by the C compilers that are excellent at optimizing, such as GCC or CLANG, right? That is, those chips almost certainly can't run the newest versions of GCC or CLANG (even my 12-year-old laptop has trouble running them, the newest version of TDM-GCC runs out of the 512 MB RAM if you try to compile the Duktape framework with all optimizations turned on), but it's possible to cross-compile the C code to run on those chips using those compilers? Unless you are an extremely experienced assembly-language programmer with a lot of time, you can't write more efficient Assembly than the GCC's output or the CLANG's output when the optimization is turned on.
Programming in assembly can perhaps help if the only C compilers available for those chips are the likes of Tiny C Compiler, which is very fast itself, but that comes at the price of being unable to do optimizations (which is not important if the programs it produces will only run on computers or smartphones or even Raspberry Pi, but it's a huge drawback if you target low-powered embedded systems).
But, let's be honest, when you are forced to write some part of the code in Assembly (like when you are writing a compiler), do you really worry about how efficient the code you write is? Or do you just write whatever gets the things done in fewer lines of code, even if that involves allocating 4 MB of stack just to get the next permutation of a string? In JavaScript, an equivalent code would take quite some time to execute even on modern computers, but if, you write that in Assembly or C, that would execute in an imperceptible amount of time. At least on my 12-year-old laptop, but probably not on low-powered chips...
Reply
#12
RE: Arithmetic Expression Compiler
(January 10, 2019 at 8:16 am)FlatAssembler Wrote: But, let's be honest, when you are forced to write some part of the code in Assembly (like when you are writing a compiler), do you really worry about how efficient the code you write is? Or do you just write whatever gets the things done in fewer lines of code, even if that involves allocating 4 MB of stack just to get the next permutation of a string? In JavaScript, an equivalent code would take quite some time to execute even on modern computers, but if, you write that in Assembly or C, that would execute in an imperceptible amount of time. At least on my 12-year-old laptop, but probably not on low-powered chips...

The only time I wrote in assembly was: (1) when I wanted optimal speed for processing; (2) when I wanted to directly access things like drives and other peripherals. I was VERY concerned with efficiency, and at that time my comp's OS ran basic, which wasn't pre-compiled, so it saved tremendous CPU cycles.
Reply
#13
RE: Arithmetic Expression Compiler
Quote:when I wanted to directly access things like drives and other peripherals
And why it is that you didn't simply use C, or, if you couldn't do that using the available C libraries, simply inline some assembly in C?

I didn't quite understand what you are doing now. Are you developing some website that is supposed to help people learn English if they pay you some money? How does that work, how is it better than all the free services that help people learn English?

By the way, what is your experience with the DNS servers? They seem to cause a lot of trouble to me. When I was using the DNS server suggested by the Croatian Crisis Connection ISP, I used to get the "server not found" error every now and then. Some websites I used regularly, like philosophicalvegan.com, weren't accessible at all, I had to use kproxy.com to get to them. I tried to set up my router to use the CloudFare DNS, however, I wasn't even able to connect to 1.1.1.1 (if I tried to PING it, the connection timed out 7/10 times or so). I then set it up to use the Google DNS, and it kind of works. That is, I can connect to the websites such as philosophicalvegan.com without problems, however, the Internet overall seems to be even slower than when I used the DNS used by default on the Croatian Crisis Connection ISP. What would you suggest me to do? I surf foreign websites a lot, and such things cause a lot of problems to me.
Reply
#14
RE: Arithmetic Expression Compiler
C wasn't available to me at that time. Assembly language compilers were.

As for my site-- it's a front end for a real company with real people. The audio is used in making slideshows for speech presentation projects, which can be evaluated by real teachers or staff. Basically, it's like a very simple Powerpoint equivalent.

As for DNS servers-- I dunno. It may be a regional thing. I'm not in any way limited by technology (except for browsers or OS which don't support certain standards yet). But internet speed is true gigabit, I never have DNS problems or anything like that. I suspect you may just be living in a region which is very behind in terms of technology infrastructure.
Reply
#15
RE: Arithmetic Expression Compiler
By the way, the first text-editor I've managed to write a script to highlight my language for is Notepad++ (you can download it on the compiler page). It enables you to make such a script using a GUI.
As for VIM and EMACS, I understand they are theoretically highly customizable, but, in my experience, they just don't get the things done. EMACS forces you, if you want to use it to its full, to write in a (what's to me) quite alien language (a LISP dialect without the "basic" things like for-loops, enough said). My intuition tells me it would be slightly easier to make such a script for VIM (since it allows you to write scripts in a relatively C-like, but still quite unfamiliar, language), but I haven't really tried to. I can see how VIM's features can come useful, but honestly I get quite angry when the editor I am using surprises me, which VIM does quite often (How do you make it search for all occurrences of a string in file, rather than just the first one?).
Since I bought my new laptop and installed Linux on it, I've come to like the GEDIT editor. It correctly highlights both the source code of my compiler page and of the Pac-Man game, which no other editor does (at least without tweaking). Plus, unlike VIM or EMACS, it doesn't need to be learned to get the basics done.
Reply
#16
RE: Arithmetic Expression Compiler
I used Notepad++ about 10 years ago when writing my novel. I made a lot of use of the tabs feature.
Reply
#17
RE: Arithmetic Expression Compiler
(January 31, 2019 at 8:42 am)bennyboy Wrote: I used Notepad++ about 10 years ago when writing my novel. I made a lot of use of the tabs feature.
What was your novel about? How did you manage to edit it later for printing? When I was writing my book ("Jezici za Gimnazijalce"), the editor constantly complained about me using LibreOffice Writer instead of Microsoft Office Word.
Reply
#18
RE: Arithmetic Expression Compiler
Anyway, I've recently built a scientific calculator using mostly the same codebase as that compiler, you can download it here.
I am not sure what kind of calculator software would be most useful. To me it seems like the thing that makes it hard to do math is algebra, but not the algebra such as matrix multiplication or solving the systems of linear equations, which is relatively easy to do both by hand and by using a piece of software, but those repetitive and error-prone tasks of preparing the systems of linear equations to be solved suing the methods of linear algebra. This can especially be seen in, for instance, circuit analysis, and the methods such as mesh analysis or node voltages help very little, if at all (they are much more confusing than the Kirchoff's Laws are, and you are perhaps even more likely to get things wrong if you try to apply them instead of the Kirchoff's Laws). It seems like it would be a wonderful problem to be solved by a computer (it's so repetitive), but we see that calculators either don't even attempt to simplify arithmetic expressions, or they suck at it to the point of that feature being useless. On the other hand, I also have no idea how to make a program that would simplify systems of equations to the point when they can be solved using the easy algebraic methods.
So, what do you think, what useful feature do modern calculator softwares lack? Do you have a bright idea how to implement it?
Reply
#19
RE: Arithmetic Expression Compiler
What, nobody here thought about it?
Reply
#20
RE: Arithmetic Expression Compiler
(January 2, 2019 at 3:01 am)FlatAssembler Wrote: So, I'd like to share my first steps in designing programming languages. As some of you know, for the last 11 months or so, I've been developing a program called Arithmetic Expression Compiler. It's written mostly in JavaScript and it converts directives from my own programming language (at first just arithmetic expressions) to FlatAssembler-compatible and i486-compatible (let's pretend it's i486-compatible, I haven't bothered to actually test it on anything else than my 12-year-old PC with 1Ghz Celeron processor) assembly. Recently, using the Duktape framework, I extended it to be able to not just compile single directives, but entire simple programs written in files.
So, here is one of the first programs I've written in the first programming language I've made myself:
Code:
;Advanced example: implementing the permutation algorithm.
AsmStart
    debug=0
    macro pushIntToStack x
    {
        sub esp,4
        fld dword [x]
        fistp dword [esp]
    }
    macro pushPointerToStack x
    {
        sub esp,4
        lea ebx,[x]
        mov [esp],ebx
    }
    macro pushStringToStack x
    {
        sub esp,4
        mov dword [esp],x
    }
    format PE console
    entry start

    include 'win32a.inc'

    section '.text' code executable
    start:
    jmp enterNumber$
        enterNumber db "Enter a whole number (1 - 1'000'000).",10,0
    enterNumber$:
    pushStringToStack enterNumber
    call [printf]
    pushPointerToStack original
    jmp floatSign$
        floatSign db "%f",0
    floatSign$:
    pushStringToStack floatSign
    call [scanf]
    jmp permutationString$
        permutationString db "The permutations of its digits are:",10,0
    permutationString$:
    pushStringToStack permutationString
    call [printf]
AsmEnd
numberOfDigits:=0
i:=0
While i<10
    countDigits[i]:=0
    i:=i+1
EndWhile
While original>0
    numberOfDigits:= numberOfDigits + 1
    lastDigit:= mod( original , 10 )
    countDigits[ lastDigit ]:=countDigits( lastDigit ) + 1
    original:= (original - lastDigit) / 10
EndWhile
AsmStart
    if debug=1
AsmEnd
        i:=0
        While i<10
            subscript:=4*i
            AsmStart
                fld dword [subscript]
                fistp dword [subscript]
                mov ebx,[subscript]
                pushIntToStack (countDigits+ebx)
                pushStringToStack integerSign
                call [printf]
            AsmEnd
            i:=i+1
        EndWhile
        AsmStart
            pushStringToStack newLineString
            call [printf]
        AsmEnd
AsmStart
end if
AsmEnd
topOfMyStack:=1
myStack[(numberOfDigits+1)]:=0
While topOfMyStack>0
    currentNumberOfDigits:=myStack ( topOfMyStack * ( numberOfDigits + 1 ) )
    i:=0
    While i<currentNumberOfDigits
        currentNumber(i):=myStack ( topOfMyStack * ( numberOfDigits + 1 ) + ( i + 1 ) )
        i:=i+1
    EndWhile
    AsmStart
        if debug=1
    AsmEnd
            i:=0
            While i<currentNumberOfDigits
                subscript:=i*4
                AsmStart
                    fld dword [subscript]
                    fistp dword [subscript]
                    mov ebx,[subscript]
                    pushIntToStack (currentNumber+ebx)
                    pushStringToStack integerSign
                    call [printf]
                AsmEnd
                i:=i+1
            EndWhile
            AsmStart
                pushStringToStack newLineString
                call [printf]
            AsmEnd
    AsmStart
        end if
    AsmEnd
    topOfMyStack:=topOfMyStack-1
    If currentNumberOfDigits=numberOfDigits
        i:=0
        While i<numberOfDigits
            subscript:=i*4
            AsmStart
                fld dword [subscript]
                fistp dword [subscript]
                mov ebx,[subscript]
                pushIntToStack (currentNumber+ebx)
                pushStringToStack integerSign
                call [printf]
            AsmEnd
            i:=i+1
        EndWhile
        AsmStart
            pushStringToStack newLineString
            call [printf]
        AsmEnd
    Else
        i:=0
        While i<10
            counter:=0
            j:=0
            While j<currentNumberOfDigits
                If currentNumber(j)=i
                    counter:=counter+1
                EndIf
                j:=j+1
            EndWhile
            If counter<countDigits(i)
                topOfMyStack:=topOfMyStack+1
                myStack(topOfMyStack*(numberOfDigits+1)):=currentNumberOfDigits+1
                j:=0
                While j<currentNumberOfDigits
                    myStack(topOfMyStack*(numberOfDigits+1)+(j+1)):=currentNumber(j)
                    j:=j+1
                EndWhile
                myStack (topOfMyStack * (numberOfDigits + 1) + (j + 1) ) := i
            EndIf
            i:=i+1
        EndWhile
    EndIf
EndWhile
AsmStart
invoke system,_pause
invoke exit,0

_pause db "PAUSE",0
integerSign db "%d",0
newLineString db 10,0

section '.rdata' readable writable
original dd ?
result dd ?
lastDigit dd ?
numberOfDigits dd ?
countDigits dd 11 dup(?)
subscript dd ?
myStack dd 1000 dup(?)
topOfMyStack dd ?
counter dd ?
i dd ?
currentNumber dd 11 dup(?)
currentNumberOfDigits dd ?
j dd ?


section '.idata' data readable import
library msvcrt,'msvcrt.dll'
import msvcrt,printf,'printf',system,'system',exit,'exit',scanf,'scanf'
AsmEnd
You can download the source code of my compiler there, there is a link there to a ZIP-archive containing the source code and instructions on how to compile it using CLANG or GCC (000webhost doesn't allow me to host executable files):
http://flatassembler.000webhostapp.com/compiler.html
I've designed this programming language to be as easy to integrate with Assembly as possible. The Assembly code C compilers have to produce (trying to declare variables themselves, trying to declare functions themselves...) has to be almost completely rewritten if you are targeting a different assembly language compiler or a different operating system.
However, the solution I've made may be even worse than the problem. The programs written in that programming language are completely non-portable. Also, since my compiler tries to make as few assumptions as possible, it produces way inferior Assembly code than any decent C-compiler would. However, it seems to make no perceptible difference on a what's now a 12-year-old computer (the Assembly code a JavaScript JIT-compiler produces is probably even less optimal). Also, because of the design of my programming language, it often happens that a compiler doesn't catch an error such as using an undeclared variable and outputs syntactically invalid Assembly.
I am also dreaming about making a compiled LISP-like language in which you could write arithmetic expressions in both S-expressions and infix-expressions, but be able to use only S-expressions elsewhere (since they come very handy in string and array-manipulation). However, I probably won't have time for that in foreseeable future.

Basically, you're trying to write a computer algebera system, am I right? That is a very interesting task. Do you plan to develop it to some usable version or just write it to learn?

I myself made a simple parser for analytic expressions a year ago or so. I wrote it in C# since I had no intent to make it optimal or something (I also don't know low-level languages nearly good enaugh to get into optimization). The goal was to learn from practice some of the the principles of how systems like Maple and Matlab operate. The parcer could parse algebraic expressions into trees, differentiate in respect to a variable or to time and prepare Lagrange's DE's.
Reply





Users browsing this thread: 1 Guest(s)