user2562953
user2562953

Reputation: 1

Perform an expression using Assembly code

TITLE
;--------------------------------------------------------------

.MODEL SMALL
.STACK 64
.DATA

    MSGA    DB 13, 10, "Input expression: ","$$$"               ;asks the user input expression ie. 5+2
    MSGADD  DB 13, 10, "The sum is: ","$"                       
    MSGSUB  DB 13, 10, "The difference is :","$"
    MSGMUL  DB 13, 10, "The product is: " ,"$"
    MSGDIV  DB 13, 10, "The quotient is: ","$" 


    NUM1 db ?
    NUM2 db ?
    OP db ?

    .CODE


    MAIN PROC NEAR


        MOV AX,@DATA 
        MOV DS,AX

        LEA DX,MSGA   
        MOV AH,09H      ;reads the user input
        INT 21H

        MOV AH,01H      ; scan NUM1
        INT 21H         ;interruption
        SUB AH,32
        MOV NUM1,AH


        MOV AH,01H      ; also reads from the keyboard
        INT 21H         ; scan NUM2
        SUB AH,32
        MOV NUM2,AH

        ;MOV NUM2,AL

        MOV AH,01H      ; also reads from the keyboard
        INT 21H         ; scan OP
        SUB AH,32
        MOV OP,AH




        CMP AX,'+'
        je @ADD

        CMP AX,'-'
        ;je @SUB

        CMP AX,'*'
        ;je @MUL

        CMP AX,'/'
        ;je @DIV

            @ADD:

                ADD AH,NUM1 ;add first number
                ADD AH,NUM2
                MOV NUM1,AH
                ADD NUM1,32

                LEA DX,MSGADD
                MOV AH,09h
                INT 21h

                MOV DL,NUM1
                MOV AH,02h
                INT 21h

            @DIV:
            XOR AX,AX ;EXAMPLE ON THE BOARD
            MOV AX,83H
            MOV BL,2H
            DIV BL

        MOV AH,4CH    ;for exiting purpose
        INT 21H

    @endif:

MAIN ENDP
;---------------------------------------------------------------
END MAIN

why does my code doesn't working? it does not give a number as a result, instead it usually give a pi character.

Upvotes: 0

Views: 246

Answers (1)

Gunner
Gunner

Reputation: 5884

You have absolutely no error checking, what happens if I enter an "a"? Second, why are you using ax? The character entered is returned in al, ax might contain garbage. Third, why are you subtracting 32? You should be subtracting 48 from the entered character to get the decimal version of the ASCII number entered.

This is wrong:

ADD AH,NUM1 ;add first number
ADD AH,NUM2

Should be:

mov AH,NUM1 ;add first number
ADD AH,NUM2

Otherwise, you are adding your NUM1 to whatever was returned in your last int 21 call.

After your cmps, you need an unconditional jmp otherwise it will always add.

Change your sub ax, 32 to sub al, 48 after your first 2 int 21 calls to get the number, after your last int 21 call to get the operator, you do not need to subtract anything since the operator entered is already correct. Fix those issues and it will work.

* Edit * This will only work for results less than 10 (1 digit result), any result 10 or more (2 digits) will not work with the current code, you will need to convert the result to ASCII a different way.

Upvotes: 1

Related Questions