1

任务是从用户那里获得一位输入并输出1-2位。我不知道为什么它不起作用 1digit + 1digit =1 digit 以及为什么有时它适用于 1digit + 1digit = 2 digit 但有时它不会。它还包括减法、乘法、除法和模运算。

标题

;-----------------------------------------------------

.MODEL SMALL
.STACK 64
.DATA

INPUT DB 13, 10, "Enter input : ","$"
SUM DB 13,10, "The sum is : ","$"
DIFF DB 13,10, "The difference is : ","$"
MULTI DB 13,10, "The product is : ","$"
DIVI DB 13,10, "The quotient is : ","$"
MODULO DB 13,10, "The modulo is : ","$"
NUM1 db ?
NUM2 db ?
OP db ?
RES db ?

;-----------------------------------------------------

.CODE
MAIN PROC NEAR

MOV AX,@DATA
MOV DS,AX

; outputs "input" message
LEA DX, INPUT
MOV AH, 09h
INT 21h

; get first input
MOV AH, 01h
INT 21h
SUB AL, '0'
MOV NUM1, AL

; get operand
MOV AH, 01h
INT 21h
MOV OP, AL

; get second input
MOV AH, 01h
INT 21h
SUB AL, '0'
MOV NUM2, AL

CMP OP, "+"
JE @ADD

CMP OP, "-"
JE @SUB

CMP OP, "*"
JE @MULTIPLY

CMP OP, "/"
JE @DIVIDE

CMP OP, "%"
JE @MOD

@ADD :
ADD AL, NUM1
MOV RES, AL

LEA DX, SUM
MOV AH, 09h
INT 21h
JMP @PRINT

@SUB :
MOV AL, NUM1
CMP AL, NUM2
JG @WITHOUTNEG
JMP @WITHNEG

@WITHNEG :
MOV AL, NUM2
SUB AL, NUM1
MOV RES, AL

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

MOV OP, "-"
MOV DL, OP
MOV AH,02h
INT 21h
JMP @PRINT

@WITHOUTNEG :
MOV AL, NUM1
SUB AL, NUM2
MOV RES, AL

LEA DX, DIFF
MOV AH, 09h
INT 21h
JMP @PRINT

@MULTIPLY :
MOV AL, NUM1
IMUL NUM2
MOV RES, AL

LEA DX, MULTI
MOV AH, 09h
INT 21h
JMP @PRINT

@DIVIDE :
XOR AX, AX
MOV AL, NUM1
IDIV NUM2
MOV RES, AL

LEA DX, DIVI
MOV AH, 09h
INT 21h
JMP @PRINT

@MOD :
XOR AX, AX
MOV AL, NUM1
IDIV NUM2
MOV RES, AH

LEA DX, MODULO
MOV AH, 09h
INT 21h
JMP @PRINT

@PRINT :
XOR AX, AX

MOV AL, RES
MOV BL, 10
IDIV BL

ADD AL, '0'
MOV DL, AL
MOV AH,02h
INT 21h

ADD AH, '0'
MOV DL, AH
MOV AH,02h
INT 21h
JMP @EXIT

@EXIT :
MOV AH,4Ch
INT 21h

MAIN ENDP

;----------------------------------------------------------------

END MAIN
4

3 回答 3

1

在您的“打印”过程中,您正在修改 AH,然后稍后尝试使用该值。

MOV AH,02h
INT 21h

ADD AH, '0'

在修改寄存器之前你必须保留你的值,PUSH 和 POP 指令会帮助你。

PUSH AX
MOV AH,02h
INT 21h

POP AX
ADD AH, '0'
于 2013-07-15T18:51:56.070 回答
0

这里。

.model small
.stack 100h
.data

first db 13,10, 'Enter 1st number: $'
second db 13, 10, 'Enter 2nd number: $'
result db 13,10, 'Result: $'
invalid db 13,10, 'Invalid number!!! $'

.code

start:
    mov ax,03
    int 10h

    mov ax,@data
    mov ds,ax

mn:
    mov dx, offset first
    mov ah, 9
    int 21h

    mov ah, 1
    int 21h

    mov cl, al
    cmp al, 30h
    jl nvalid
    cmp al, 39h
    jg nvalid
    jmp proceed

nvalid:
    mov dx, offset invalid
    mov ah, 9
    int 21h
    jmp mn

proceed:
    sub cl, 30h
    mov dx, offset second
    mov ah, 9
    int 21h
    mov ah, 1
    int 21h

    cmp al, 30h
    jl nvalid
    cmp al, 39h
    jg nvalid
    sub al, 30h

    xor ah,ah
    add al, cl
    aaa

    mov cx, ax
    add cx, 3030h
    mov dx, offset result
    mov ah, 9
    int 21h

    mov ah, 2
    mov dl, ch
    int 21h

    mov dl, cl
    int 21h

exit:
    mov ah, 4ch
    int 21h

end start
于 2013-12-13T06:29:24.430 回答
0

MOV AH,02h
INT 21h

ADD AH, '0'

您在此处使用 WRITE CHARACTER 函数编号覆盖其余部分。您需要保存和恢复AH,例如这样:

MOV DL, AL
PUSH AX   ; Save AX on the stack
MOV AH,02h
INT 21h
POP AX    ; Restore AX

ADD AH, '0'
于 2013-07-15T18:53:34.157 回答