🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.lang.asm
Article #455

messy calculator code.. please try this out

#455
From: Tom Harbert
Date: Thu, 15 May 2003 01:30
340 lines
9272 bytes
hi i am coding a calculator in x86 assembly for an assignment, i am pretty
stuck i think i have gone into too much detail and basically im lost.. could
anyone possibly give me some tips on how to go about this or change the code
i have.. thanks here it is..

thankyou



..model small
..stack 100h
..data

        input db 0,0,0,0,0,0,0,0

        realNumber1 db ?
        realOperator1 db ?
        realNumber2 db ?
        realOperator2 db ?

        numberOfInputs db ?
        lastInput db ?
        answer db ?
        valid db ?
        operatorKeyPressed db 01d
        equalsKeyPressed db ?


 calc db "********************"
      db "*                  *"
      db "********************"
             db "*    7   8   9     *"
             db "*    4   5   6     *"
             db "*    1   2   3     *"
             db "*    +   -   0     *"
             db "*    *   /   =     *"
      db "********************"



..code


    main proc
        mov ax, @data               ; move address of data segment to the ax
register
        mov ds, ax                  ; move ax into the data segment register

        mov ax, 03h
        int 10h

        mov ax, 0B800h
        mov es, ax
        xor di, di


        call print_calc
        mov cx, 10000d
        general:
        call get_char


        cmp lastInput, 00011011b ; was the escape key pressed
        jz quit                         ; if so jump to quit

        call check_valid
        call print_char
        loop general


        quit:
        mov ax, 4C00h
        int 21h
    main endp


    get_char proc
        mov ah, 00h
        int 16h
        mov lastInput, al
        mov si, 0
        mov input[si], al
        add si, 1

        ret
    get_char endp


    beep proc
        ; control byte
        mov al, 0B6h                ; control byte
        out 43h, al                 ; send control byte to control register
        ; load note
        mov ax, 3043d               ; mov appropriate array element into ax
register
        out 42h, al                 ; send the low byte
        mov al, ah                  ; move the high byte into the al
register
        out 42h, al                 ; send the high byte
        ; turn speaker on
        in al, 61h                  ; get current setting
        or al, 00000011b            ; set bits 1 and 0
        out 61h, al                 ; send new value
        ; delay
        push cx                     ; push original cx value onto stack
        mov cx, 0fffh               ; initialise cx register
     ac:
     push cx                     ; push original cx value onto stack
        mov cx, 0fffh               ; initialise cx register
     ab:
        nop                         ; no operation
        loop ab                     ; decrement cx and continue until cx = 0
        pop cx                      ; pop original cx value off stack
        loop ac                     ; decrement cx and continue until cx = 0
        pop cx                      ; pop original cx value off stack
        ; turn speaker off
        in al, 61h                  ; get value from port 61h
        and al, 11111100b           ; reset bits 1 and 0
        out 61h, al                 ; send new value
        ret                         ; return
    beep endp


    print_calc proc
        mov ah, 00001111b
        mov si, 0
        mov cx, 9
        mov di, 0

            zz:
            push cx
            mov cx, 20

            go:
            mov al, calc[si]
            mov es:[di], al
            add di, 1
            mov es:[di], ah
            add di, 1
            add si, 1
            loop go

            pop cx
            add di, 280

        loop zz
        ret
    print_calc endp


    check_valid proc
        cmp lastInput, "1"
        jz op2
        cmp lastInput, "2"
        jz op2
        cmp lastInput, "3"
        jz op2
        cmp lastInput, "4"
        jz op2
        cmp lastInput, "5"
        jz op2
        cmp lastInput, "6"
        jz op2
        cmp lastInput, "7"
        jz op2
        cmp lastInput, "8"
        jz op2
        cmp lastInput, "9"
        jz op2
        cmp lastInput, "0"
        jz op2
        cmp lastInput, "+"
        jz op1
        cmp lastInput, "-"
        jz op1
        cmp lastInput, "*"
        jz op1
        cmp lastInput, "/"
        jz op1
        cmp lastInput, "="
        jz op1

        jmp bad1

        op2:
        mov operatorKeyPressed, 00d
        jz ok1

        op1:
          cmp lastInput, "="
          jz eq1
          jmp eq2

          eq1:
          mov equalsKeyPressed, 01d

          eq2:
          cmp operatorKeyPressed, 01d
          jz bad1



        mov operatorKeyPressed, 01d

        call make_number ; try to make number into one int

        jmp ok1

        ok1:
     mov valid, 01h
        add numberOfInputs, 01d
        call beep
        jmp done1

        bad1:
    mov valid, 00h

        done1:
        ret
    check_valid endp


    make_number proc
        cmp numberOfInputs, 01d
        jz oneNum
        cmp numberOfInputs, 02d
        jz twoNum
        cmp numberOfInputs, 03d
        jz threeNum
        cmp numberOfInputs, 04d
        jz fourNum
        cmp numberOfInputs, 05d
        jz fiveNum
        cmp numberOfInputs, 06d
        jz sixNum


        oneNum:
        push si
        mov si, 1
        mov al, input[si]
        mov realNumber1, al
 pop si

        twoNum:
        push si
        mov si, 1
        mov al, input[si]
        mov ah, 10d
        mul ah
        mov realNumber1, al
 pop si

        mov di, 500
        mov ah, 00001111b

        mov al, realNumber1
        mov es:[di], al
        add di, 1
        mov es:[di], ah
        add di, 1


        threeNum:

        fourNum:

        fiveNum:

        sixNum:


     ret
    make_number endp


    print_char proc

        cmp valid, 01h
        jz ok2

        jmp bad2

        ok2:
        mov ah, 00001111b

        cmp numberOfInputs, 01d
        jz oneVal
        cmp numberOfInputs, 02d
        jz twoVal
        cmp numberOfInputs, 03d
        jz threeVal
        cmp numberOfInputs, 04d
        jz fourVal
        cmp numberOfInputs, 05d
        jz fiveVal
        cmp numberOfInputs, 06d
        jz sixVal
        cmp numberOfInputs, 07d
        jz sevenVal


        jmp bad2

        oneVal:
        mov di, 332
        jmp print1
        twoVal:
        mov di, 334
        jmp print1
        threeVal:
        mov di, 336
        jmp print1
        fourVal:
        mov di, 338
        jmp print1
        fiveVal:
        mov di, 340
        jmp print1
        sixVal:
        mov di, 342
        jmp print1
        sevenVal:
        mov di, 344
        jmp print1


        print1:
        mov al, lastInput
        mov es:[di], al
        add di, 1
        mov es:[di], ah
        add di, 1

        bad2:
        ret
    print_char endp


end main
Technical Details
Message-ID: <3ec2edca@news.comindico.com.au>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-04!sn-xit-01!sn-post-01!supernews.com!corp.supernews.com!news.crayne.org!clax86!robomod!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111ae6,e011cb8cb61ece24,start
X-Google-Attributes: gid111ae6,public
X-Google-Thread: 1093da,e011cb8cb61ece24,start
X-Google-Attributes: gid1093da,public
X-Google-Thread: f7d8d,e011cb8cb61ece24,start
X-Google-Attributes: gidf7d8d,public
X-Google-Thread: 114917,e011cb8cb61ece24,start
X-Google-Attributes: gid114917,public
X-Google-ArrivalTime: 2003-05-14 19:58:49 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-04!sn-xit-01!sn-post-01!supernews.com!corp.supernews.com!news.crayne.org!clax86!robomod!not-for-mail
From: "Tom Harbert" <harbert@dodo.com.au>
Newsgroups: alt.lang.asm,alt.os.assembly,comp.lang.asm.x86,it.comp.lang.assembly
Subject: messy calculator code.. please try this out
Approved: CLAX86 Moderators <ccrayne@crayne.org>
Date: Thu, 15 May 2003 01:30:51 GMT
Organization: Comindico Australia - reports relating to abuse should be sent to abuse@comindico.com.au
Message-ID: <3ec2edca@news.comindico.com.au>
X-CLAX86-Policy: http://www.pacificsites.com/~ccrayne/clax86.html
X-CLAX86-Faq: http://www2.dgsys.com/~raymoon/x86faqs.html
X-CLAX86-Info-1: Send submissions to             comp-lang-asm-x86@moderators.isc.org
X-CLAX86-Info-2: Send technical complaints to    ccrayne@crayne.org
X-CLAX86-Info-3: Send complaints about policy to ccrayne@crayne.org
X-Comment: moderators do not necessarily agree or disagree with this article.
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
X-Original-NNTP-Posting-Host: 203.221.71.202
X-Original-Trace: 15 May 2003 11:30:50 +1000, 203.221.71.202
X-MailScanner: Found to be clean
X-MailScanner-SpamCheck: not spam (whitelisted), SpamAssassin (score=1.6,
 required 5, NOSPAM_INC, PRIORITY_NO_NAME, SPAM_PHRASE_00_01)
X-UIDL: =^,#!bZa"!A#Y"!Yag"!
X-Complaints-To: abuse@supernews.com
Lines: 355