TASM:生成一个简单的计算器

时间:2014-11-04 08:42:53

标签: arrays assembly macros tasm

我正在尝试在TASM中制作一个简单的计算器,它还没有完成,我想弄清楚添加部分,但我因为操作数类型不匹配而卡住了,这就是我所拥有的;

;FILENAME: SimpleClc.asm
;FILE FORMAT: EXE
PAGE 55,132
.386

STACK_SEG SEGMENT STACK USE16

        DB 100 DUP(?)

STACK_SEG ENDS

DATA_SEG SEGMENT 'DATA' USE16

ADDITION    MACRO   Result,Char1,Char2


        MOV AX,Char1
        CWD
        ADD AX,Char2,
        MOV Result,AX

ENDM

SUBTRACTION MACRO   Char1, Char2

        SUB Char1,Char2
ENDM

DIVISION    MACRO   Char1, Char2

        DIV Char1,Char2
ENDM

MULTIPLICATION  MACRO   Char1, Char2

        MUL Char1,Char2
ENDM

Mainmsg   DB 'Please enter a algebraic command line: $'
ErMessage DB 'Error!!', 0DH, 0AH
          DB 'INPUT FORMAT:Operand1 Operator Operand',0DH, 0AH
          DB 'Operand: Decimal Numbers',0DH, 0AH
          DB 'Operator: + -'

INCHAR  DB 21
Res     DB 10 dup('$')
INCDAT  DB 21 dup('$')
VarX    DB 10 dup('$')
VarY    DB 10 dup('$')



DATA_SEG ENDS

CODE_SEG SEGMENT PARA 'CODE' PUBLIC USE16

ASSUME CS:CODE_SEG, DS:DATA_SEG, SS:STACK_SEG

MAIN PROC FAR

        PUSH DS ;INITIATE THE PROGRAM
        XOR AX,AX
        PUSH AX
        MOV AX,DATA_SEG
        MOV DS,AX

    AGAIN:

        LEA DX,Mainmsg ;PRINT MESSAGE
        MOV AH,9
        INT 21H

        MOV DX, OFFSET INCHAR
        MOV AH,0AH
        INT 21H

        MOV DX, OFFSET INCHAR
        MOV AH,0AH
        INT 21H

        LEA DX,INCDAT ;Writing the incoming input
        MOV AH,9
        INT 21H

        MOV DI,OFFSET INCDAT



        MOV AL, [DI]
        CMP AL, 9
        JLE OPERAND     ; jump if less or equal
        JG ERRORMESSAGE ; jump if not less or equal
        INC DI

        OPERAND:
        MOV AL, [DI]
        CMP AL, '+'
        JE  LASTOPERAND
        CMP AL, '-'
        JE  LASTOPERAND
        CMP AL, '*'
        JE  LASTOPERAND
        CMP AL, '/'
        JE LASTOPERAND
        JMP ERRORMESSAGE

        INC DI

        LASTOPERAND:
        MOV AL, [DI]
        CMP AL, 9
        JLE OPERATION   ; jump if less or equal
        JG ERRORMESSAGE ; jump if not less or equal

        OPERATION:

        CMP AL, '+'
        JE ADDITION1
        JMP AGAIN
ADDITION1:
            MOV DI,OFFSET INCDAT
            MOV AL,[DI]
            MOV VarX,AL
            MOV AL,[DI+2]
            MOV VarY,AL
            ADDITION Res,VarX,VarY
            JMP AGAIN
        CMP AL, '-'
        JE  SUBTRACTION1
        JMP AGAIN
SUBTRACTION1:

        CMP AL, '*'
        JE  MULTIPLICATION1
        JMP AGAIN
MULTIPLICATION1:

            JMP AGAIN

        CMP AL, '/'
        JE DIVISION1
        JMP AGAIN
DIVISION1:



        ERRORMESSAGE:
        LEA DX,ErMessage ;PRINT MESSAGE
        MOV AH,9
        INT 21H


MAIN    ENDP
CODE_SEG ENDS
END MAIN

我知道它现在看起来很乱,这只是因为我在同时尝试一堆东西,顺便说一句,我的计算器不会计算大于10的结果。感谢您的帮助。任何评论都将不胜感激。

1 个答案:

答案 0 :(得分:0)

Ups,很难对所有亚麻布进行数字化处理。

将代码放入数据段内并不是最好的选择。

以下说明是好的?:

    PUSH DS    ; pushing the old address of DS to the stack
    XOR AX,AX
    PUSH AX    ; pushing a zero word to the stack

最后如何终止程序?

我不确定,但是使用类似于汇编程序助记符名称的标签名可能会出错。

为什么一个接一个地使用输入函数来覆盖相同的缓冲区?

    MOV DX, OFFSET INCHAR
    MOV AH,0AH
    INT 21H

    MOV DX, OFFSET INCHAR
    MOV AH,0AH
    INT 21H