+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 20

Thread: Alcyone Calculator

  1. #1
    Wire Sofaking

    Fizyk will become famous soon enough Fizyk will become famous soon enough Fizyk's Avatar
    Join Date
    Jun 2008
    Location
    Łomianki, Poland
    Posts
    729
    Blog Entries
    1

    Default Alcyone Calculator

    Looks like I discovered the fun of making programs for my own OS
    I guess the Media Player should be here as well, but I didn't think much, when I was posting it. Mods: could you move Media Player thread here? Thanks in advance.

    Ok, so what can the calculator do? A screenshot:


    As you can see, what it does is evaluate expressions. You can use 5 operators: +, -, *, / and ^, also parentheses are supported. It displays the result to 3 digits after decimal point. The screenshot is from the version that could only read integers, current version can read real numbers as well.

    The code is in zASM - I thought of making it in ZC, but the version I have still has bugs with local variables and doing it without them would be... not very funny

    Thanks to BP for explaining to me how expression evaluation is done

    For some reason, I can't upload attachments, so here is the code for anyone who wants it:

    Code:
    db programsize
    
    jmp codestart
    
    //************** level 0 *******************//
    
    level0: //ecx - start, edx - end
      push ecx
      push edx
      push esi
      push ebx
      push ebp
      push edi
      mov edi,0
      mov ebx,ecx
      mov ebp,0
    
      level0_loop2:
        mov esi,expr
        add esi,ebx
       
        cmp #esi,43 //'+'
        jne l0_l2_next1
        
        push edx
        mov edx,ebx
        call level1
        pop edx
        call level0_perform  
        mov edi,0
        inc ebx
        mov ecx,ebx
        jmp level0_loop2
        
      l0_l2_next1:
        cmp #esi,45 //'-'
        jne l0_l2_next2
        
        push edx
        mov edx,ebx
        call level1
        pop edx
        call level0_perform  
        mov edi,1
        inc ebx
        mov ecx,ebx
        jmp level0_loop2
     
      l0_l2_next2:   
        cmp #esi,40
        jne l0_l2_next3
        
        call skip_par
        
      l0_l2_next3:
        inc ebx
        cmp ebx,edx
        jge l0_l2_end
      jmp level0_loop2
        
    l0_l2_end:
      call level1
      call level0_perform
    
    level0_end:
      mov eax,ebp
      pop edi
      pop ebp
      pop ebx
      pop esi
      pop edx
      pop ecx
      ret
    
    level0_perform:
      cmp edi,0
      jg subtract
      add ebp,eax
      jmp l0_perform_end
    subtract:
      sub ebp,eax
    l0_perform_end:
      ret
    
    //************** level 1 *******************//
    
    level1: //ecx - start, edx - end
      push ecx
      push edx
      push esi
      push ebx
      push ebp
      push edi
      mov edi,0
      mov ebx,ecx
      mov ebp,1
    
      level1_loop2:
        mov esi,expr
        add esi,ebx
       
        cmp #esi,42 //'*'
        jne l1_l2_next1
        
        push edx
        mov edx,ebx
        call level2
        pop edx
        call level1_perform  
        mov edi,0
        inc ebx
        mov ecx,ebx
        jmp level1_loop2
        
      l1_l2_next1:
        cmp #esi,47 //'/'
        jne l1_l2_next2
        
        push edx
        mov edx,ebx
        call level2
        pop edx
        call level1_perform  
        mov edi,1
        inc ebx
        mov ecx,ebx
        jmp level1_loop2
        
      l1_l2_next2:    
        cmp #esi,40
        jne l1_l2_next3
        
        call skip_par
        
      l1_l2_next3:
        inc ebx
        cmp ebx,edx
        jge l1_l2_end
      jmp level1_loop2
        
    l1_l2_end:
      call level2
      call level1_perform
    
    level1_end:
      mov eax,ebp
      pop edi
      pop ebp
      pop ebx
      pop esi
      pop edx
      pop ecx
      ret
    
    level1_perform:
      cmp edi,0
      jg divide
      mul ebp,eax
      jmp l1_perform_end
    divide:
      div ebp,eax
    l1_perform_end:
      ret
    
    //************** level 2 *******************//
    
    level2: //ecx - start, edx - end
      push ecx
      push edx
      push esi
      push ebx
      push ebp
      push edi
      mov edi,0
      mov ebx,ecx
      mov ebp,0
    
      level2_loop2:
        mov esi,expr
        add esi,ebx
       
        cmp #esi,94 //'^'
        jne l2_l2_next1
        
        push edx
        mov edx,ebx
        call level3
        pop edx
        call level2_perform
        mov edi,1 
        inc ebx
        mov ecx,ebx
        jmp level2_loop2
        
      l2_l2_next1:    
        cmp #esi,40
        jne l2_l2_next2
        
        call skip_par
        
      l2_l2_next2:
        inc ebx
        cmp ebx,edx
        jge l2_l2_end
      jmp level2_loop2
        
    l2_l2_end:
      call level3
      call level2_perform
    
    level2_end:
      mov eax,ebp
      pop edi
      pop ebp
      pop ebx
      pop esi
      pop edx
      pop ecx
      ret
    
    level2_perform:
      cmp edi,0
      jne l2_perf_next
      mov ebp,eax
      jmp l2_perf_end
    l2_perf_next:
      fpwr ebp,eax
    l2_perf_end:
      ret
    
    //************** level 3 *******************//
    
    level3: //ecx - start, edx - end
      push ecx
      push edx
      push esi
    
      mov eax,0
      cmp ecx,edx
      je level3_end
    
      mov esi,expr
      add esi,ecx
      cmp #esi,40 //'('
      jne not_parenthesis
    
      push ebx
      mov ebx,ecx
      call skip_par
      mov edx,ebx
      pop ebx
      inc ecx
      call level0
      jmp level3_end
    
    not_parenthesis:
      mov eax,0
      mov #decimal,0
      push ebx
      mov ebx,ecx
    
      eval_loop:  
        mov esi,expr
        add esi,ebx
        cmp #esi,46 //'.'
        jne eval_next1
        
        cmp #decimal,0
        jg parse_error
        inc ebx
        mov #decimal,1
        jmp eval_loop
        
      eval_next1:
        cmp #esi,48
        jl parse_error
        cmp #esi,57
        jg parse_error
        mul eax,10      //eax *= 10
        add eax,#esi
        sub eax,48      //eax += #esi-48
        cmp #decimal,0
        je eval_next2
        inc #decimal
      eval_next2:
        inc ebx
        cmp ebx,edx
        jge level3_next2
      jmp eval_loop
    
    level3_next2:
      pop ebx
      cmp #decimal,0
      je level3_end
    
      dec #decimal
      push ebx
      mov ebx,10
      fpwr ebx,#decimal
      div eax,ebx
      pop ebx
    
    level3_end:
      pop esi
      pop edx
      pop ecx
      ret
    
    alloc decimal
    
    //******************************************//
    
    alloc n_par
    
    skip_par:
      mov #n_par,1
      par_loop:
        inc ebx
        mov esi,expr
        add esi,ebx
        cmp #esi,40
        jne par_loop_next1
        inc #n_par
      par_loop_next1:
        cmp #esi,41
        jne par_loop_next2
        dec #n_par
      par_loop_next2:
        cmp #n_par,0
        je par_end
        cmp ebx,edx
        jge parse_error
      jmp par_loop
    par_end:
      ret
    
    //************** in case of error **********//
    
    error_txt:
      db 'Syntax error.',10,0
    
    parse_error:
      mov esp,#stack
      mov eax,9
      mov esi,error_txt
      int 32
      jmp main_loop
    
    //************** variables *****************//
    
    welcome:
      db 'Alcyone Calculator v1.0',10,0
    query:
      db '> ',0
    quit:
      db 'quit',0
    crlf:
      db 10,0
    expr:
      alloc 256
    buffer:
      alloc 20
    
    alloc stack
    
    //************** code **********************//
    
    codestart:
      mov eax,9
      mov esi,welcome
      int 32
      
      main_loop:
        mov eax,9
        mov esi,query
        int 32
        mov eax,8
        mov esi,expr
        mov ecx,256
        int 32
        mov eax,24
        mov esi,expr
        push es
        mov es,ds
        mov edi,quit
        int 32      //strcmp(expr,'quit')
        pop es
        cmp eax,0
        je program_end
        
        mov #stack,esp
        mov ecx,0
        mov edx,expr
        
        zero_search:
          cmp #edx,0
          je search_end
          inc edx
        jmp zero_search
      search_end:
        sub edx,expr
        call level0
        
        mov edx,eax
        mov eax,10
        mov esi,-3
        int 32
        
        mov eax,9
        mov esi,crlf
        int 32
      jmp main_loop
    
    program_end:
      retf
    
    Note: This program needs latest version of the BIOS, uploaded to GWCD SVN today. Previous version had bugs in displaying real numbers, so for example 1/2 gave 500 instead of 0.500, or 2^0.5 would give 1414 instead of 1.414.

    My programs: BIOS - Alcyone - Calculator - Notepad - Movie Player
    My tutorials: applyTorque - Quaternions - PID controllers
    Some other things I made: FT Chip - RK4 Solar System

  2. #2
    Wirezard

    Matte is just really nice Matte is just really nice Matte is just really nice Matte is just really nice Matte is just really nice Matte's Avatar
    Join Date
    Jan 2009
    Location
    Norway
    Posts
    3,151

    Default Re: Alcyone Calculator

    Awesome!

    Which algorithms are you using for this? Based on the supported operators, I'm guessing the shunting yard algo+RPN.


    BTW, Moved the media player thread.
    "If anybody says he can think about quantum physics without getting giddy, that only shows he has not understood the first thing about them."
    -- Niels Bohr


    Wire FPGA

  3. #3
    Wire Sofaking d3cr1pt0r is on a distinguished road d3cr1pt0r's Avatar
    Join Date
    Jul 2008
    Posts
    566

    Default Re: Alcyone Calculator

    Wow. Outstanding. I want more
    <3

  4. #4
    Wire Sofaking

    Fizyk will become famous soon enough Fizyk will become famous soon enough Fizyk's Avatar
    Join Date
    Jun 2008
    Location
    Łomianki, Poland
    Posts
    729
    Blog Entries
    1

    Default Re: Alcyone Calculator

    Quote Originally Posted by Matte
    Which algorithms are you using for this? Based on the supported operators, I'm guessing the shunting yard algo+RPN.
    I have no idea what is it called, but I checked the shunting yard algo and it's not the one. BP would be a better person to ask about this, because he was the one that explained it to me.

    Basically it works like this: I have 4 functions called level0 - level3. level0 takes care of addition/subtraction (level1 +|- level1), level1 is multiplication/division (level2 *|/ level2), level2 is powers (level3 ^ level3), and level3 is constant evaluation or in case of parenthesis, calling level0 for what is inside it.

    Thanks for moving the media player thread

    My programs: BIOS - Alcyone - Calculator - Notepad - Movie Player
    My tutorials: applyTorque - Quaternions - PID controllers
    Some other things I made: FT Chip - RK4 Solar System

  5. #5
    Lifetime Supporter
    Nikita will become famous soon enough Nikita's Avatar
    Join Date
    May 2009
    Posts
    676

    Default Re: Alcyone Calculator


    Keep up the good work!

  6. #6
    Wire Sofaking emspike will become famous soon enough emspike will become famous soon enough emspike's Avatar
    Join Date
    Feb 2008
    Location
    Western Springs, IL
    Posts
    565

    Default Re: Alcyone Calculator

    The irony is, I started work on a calculator that does something awesome with the digiscreen just 7 hours before you released, and I was hung up on the parsing.
    Download Link for Night-Hawk's Wired Joystick Module
    Quote Originally Posted by d3cr1pt0r View Post
    finnished

  7. #7
    Wirezard

    Matte is just really nice Matte is just really nice Matte is just really nice Matte is just really nice Matte is just really nice Matte's Avatar
    Join Date
    Jan 2009
    Location
    Norway
    Posts
    3,151

    Default Re: Alcyone Calculator

    Quote Originally Posted by emspike View Post
    The irony is, I started work on a calculator that does something awesome with the digiscreen just 7 hours before you released, and I was hung up on the parsing.
    I can reccommend the shunting-yard algorithm. It has proven itself useful to me in the past.

    Shunting-yard algorithm - Wikipedia, the free encyclopedia
    "If anybody says he can think about quantum physics without getting giddy, that only shows he has not understood the first thing about them."
    -- Niels Bohr


    Wire FPGA

  8. #8
    Wire Sofaking emspike will become famous soon enough emspike will become famous soon enough emspike's Avatar
    Join Date
    Feb 2008
    Location
    Western Springs, IL
    Posts
    565

    Default Re: Alcyone Calculator

    Thanks for the link.

    My problem was, every time I tried to think about recursive parsing like what Fizyk did, all my brain would say was "FUCK! NO NAMED CODE BLOCKS WITH PARAMETERS DETECTED!".

    Oh, by the way, does the digiscreen support resolutions that aren't powers of 2? I just foresaw a possible developmental speedbump.
    Download Link for Night-Hawk's Wired Joystick Module
    Quote Originally Posted by d3cr1pt0r View Post
    finnished

  9. #9
    Wirezard

    Matte is just really nice Matte is just really nice Matte is just really nice Matte is just really nice Matte is just really nice Matte's Avatar
    Join Date
    Jan 2009
    Location
    Norway
    Posts
    3,151

    Default Re: Alcyone Calculator

    Quote Originally Posted by emspike View Post
    Oh, by the way, does the digiscreen support resolutions that aren't powers of 2? I just foresaw a possible developmental speedbump.
    Yeah, as far as I know, the X and Y resolution of the digiscreen isn't clamped to keep the ratio.
    "If anybody says he can think about quantum physics without getting giddy, that only shows he has not understood the first thing about them."
    -- Niels Bohr


    Wire FPGA

  10. #10
    Wirererer ben1066 will become famous soon enough ben1066's Avatar
    Join Date
    Aug 2009
    Posts
    124

    Default Re: Alcyone Calculator

    WOW! I'm happy i begged you to release the programmes, also though whenever i try to upload any cpu it spams the console reliable stream overflow and now on sp i get compiler errors. btw fizuk you could do with a folder in full scale projects, finally could someone give me a one to one tutorial on Zasm, thanks.

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. Alcyone OS
    By Fizyk in forum Full-scale projects
    Replies: 22
    Last Post: 02-14-2010, 05:26 AM
  2. Alcyone Media Player
    By Fizyk in forum CPU, GPU, and Hi-speed
    Replies: 15
    Last Post: 09-06-2009, 08:06 AM
  3. [Request] Alcyone OS ZC Tutorial
    By ben1066 in forum CPU, GPU, and Hi-speed
    Replies: 3
    Last Post: 09-02-2009, 09:35 AM
  4. [E2 Help] Calculator
    By Mole2006 in forum Expression 1 & 2
    Replies: 4
    Last Post: 02-15-2009, 12:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
proceed-collector
proceed-collector
proceed-collector
proceed-collector
linguistic-parrots
linguistic-parrots
linguistic-parrots
linguistic-parrots