装配乘法和加法

时间:2013-07-07 10:33:41

标签: assembly calculator masm32

我似乎无法输出大于128的汇编中的整数。它可以正常工作,直到我将数字相加/相乘得到一个整数>它返回一个垃圾字符。救命!如何输出这些整数。这是完整的代码。

TITLE

.MODEL SMALL

.STACK 64

.DATA
MSGA DB 13,10,"Input Equation: ","$"
MSGB DB 13,10,"The sum is ","$"
MSGC DB 13,10,"The difference is ","$"
MSGD DB 13,10,"The product is ","$"
MSGE DB 13,10,"The quotient is ","$"
MSGF DB 13,10,"The difference is -","$"
MSGG DB 13,10,"The remainder is ","$"

OP1A db ?
OP1B db ?
OPRND db ?
OP2A db ?
OP2B db ?

RESULT db ?
NUM1 db ?
NUM2 db ?

;FOR THREE DIGIT OUTPUTS
FIRST db ?
SECOND db ?
THIRD db ?

.CODE
MAIN PROC NEAR
MOV AX, @DATA
MOV DS, AX

@STRT:
LEA DX, MSGA
MOV AH, 09H
INT 21H

;Get tens digit of first number
MOV AH, 01H
INT 21H
SUB AL, '0'
MOV OP1A, AL

;Get ones digit of first number
MOV AH, 01H
INT 21H
SUB AL, '0'
MOV OP1B, AL

;get OPERAND
MOV AH, 01H
INT 21H
MOV OPRND, AL

;Get tens digit of second number
MOV AH, 01H
INT 21H
SUB AL, '0'
MOV OP2A, AL

;Get ones digit of second number
MOV AH, 01H
INT 21H
SUB AL, '0'
MOV OP2B, AL

XOR AX, AX
XOR BX, BX

;Form the NUM2
MOV AL, OP2A
MOV BL, 10
MUL BL
ADD AL, OP2B
MOV NUM2, AL

;Form the NUM1
MOV AL, OP1A
MUL BL
ADD AL, OP1B
MOV NUM1, AL

XOR AX, AX
XOR BX, BX

;CHECK WHAT OPERAND IT IS
CMP OPRND, '+'
JE @ADD
CMP OPRND, '*'
JE @MULTI
CMP OPRND, '-'
JE @SUB
CMP OPRND, '/'
JE @DIV
CMP OPRND, '%'
JE @DIV

JMP @EXIT

;DIVIDE 
@DIV:
    CMP NUM2, 0
    JE @EXIT

    XOR AX, AX
    MOV AL, NUM1
    MOV BL, NUM2
    DIV BL

    CMP OPRND, '%'
    JE @MOD

    MOV RESULT, AL

    LEA DX, MSGE
    MOV AH, 09H
    INT 21H
JMP @PRINT

;MODULO
@MOD:
    MOV RESULT, AH

    LEA DX, MSGG
    MOV AH, 09H
    INT 21H
JMP @PRINT

;SUBTRACT
@SUB:
    MOV AL, NUM2
    CMP NUM1, AL
    JGE @PSTV

    ;Add the negative sign
    LEA DX, MSGF
    MOV AH, 09H
    INT 21H

    SUB AL, NUM1
    MOV RESULT, AL
JMP @PRINT

    @PSTV:
        MOV AL, NUM1
        SUB AL, NUM2
        MOV RESULT, AL  

        LEA DX, MSGC
        MOV AH, 09H
        INT 21H
JMP @PRINT

;MULTIPLY
@MULTI:     
    MOV AL, NUM2
    MOV BL, NUM1
    MUL BL
    MOV RESULT, AL

    LEA DX, MSGD
    MOV AH, 09H
    INT 21H
JMP @PRINT

;ADD
@ADD:
    MOV AL, NUM2
    ADD AL, NUM1
    MOV RESULT, AL

    LEA DX, MSGB
    MOV AH, 09H
    INT 21H
JMP @PRINT

;Print RESULT
@PRINT:

    CMP RESULT, 9
    JLE @ONEDIGIT

    CMP RESULT, 99
    JLE @TWODIGITS

        ;Separate the FIRST digit
        XOR AX, AX
        XOR BX, BX

        MOV AL, RESULT
        MOV BL, 100
        DIV BL
        MOV RESULT, AL
        MOV RESULT, AH

        ;Output the HUNDREDTHS frigging digit
        ADD FIRST, '0'
        MOV DL, FIRST
        MOV AH, 02H
        INT 21H

    @TWODIGITS:

        XOR AX, AX
        XOR BX, BX
        ;Separate the two remaining frigging digits
        MOV AL, RESULT
        MOV BL, 10
        DIV BL
        MOV SECOND, AL
        MOV THIRD, AH

        ;Output the TENS frigging digit
        ADD SECOND, '0'
        MOV DL, SECOND
        MOV AH, 02H
        INT 21H

        ;Output the ONES frigging digit
        ADD THIRD, '0'
        MOV DL, THIRD
        MOV AH, 02H
        INT 21H
    JMP @EXIT

    @ONEDIGIT:
        ADD RESULT, '0'
        ;Print one digit
        MOV DL, RESULT
        MOV AH, 02H
        INT 21H

@EXIT:
    MOV AH, 4CH
    INT 21H

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

1 个答案:

答案 0 :(得分:1)

您使用的是8位寄存器BL,AL等.8位值的范围是+ -128。要使用更大的值,您需要使用16或32位寄存器。 16位寄存器的名称是BX,AX等。您还需要将结果定义为dw(对于16位)而不是db。

在此处查看更多信息:

http://en.wikipedia.org/wiki/X86#x86_registers