我有一个大学项目,我需要添加或减去两个 2 位数字(用户输入数字)。我得到了工作的补充。减法使十位数正确,但在个位数位置给出奇数字符。这就是我的意思(结果图片):
我用:
- TASM 和 TLINK
- x86
- Windows XP(虚拟盒子)
代码:
.MODEL SMALL
.STACK 100h
.DATA
startMsg DB 13,10,'1.) Add ',10,'2.) Subtract',10,'3.) Exit',10,10,'Select a function: $'
integer1Msg DB 13,10,10,'Enter the first integer: $'
integer2Msg DB 13,10,'Enter the second integer: $'
errorOccuredMsg DB 13,10,13,10,'An error occured, please try again! $'
sumMsg DB 13,10,10,'The sum is: $'
subMsg DB 13,10,10,'The differance is: $'
gotNum DB 0
func DB 0
.CODE
start:
mov ax,@data
mov ds,ax
mov gotNum, 0 ; initialize var
;display & get the selection
mov ah,09
mov dx,OFFSET startMsg
int 21h
mov ah,01
int 21h
mov func,al
;check what the selection was
cmp func,'1'
je additionIntermediate
cmp func,'2'
je subtractionIntermediate
cmp func,'3'
je exit
exit:
mov ah,4ch
int 21h
getNum1:
;get the first integer (1st digit)
mov ah,09
mov dx,OFFSET integer1Msg
int 21h
mov ah,01
int 21h
;check that input is a number
cmp al, 30h ;0 (ASCII 48)
jl errorIntermediate
cmp al, 39h ;9 (ASCII 57)
jg errorIntermediate
mov bh,al
sub bh,30h
;get the first integer (2nd digit)
mov ah,01
int 21h
;check that input is a number
cmp al, 30h ;0 (ASCII 48)
jl errorIntermediate
cmp al, 39h ;9 (ASCII 57)
jg errorIntermediate
mov bl,al
sub bl,30h
jmp getNum2
additionIntermediate:
jmp addition
subtractionIntermediate:
jmp subtraction
errorIntermediate:
jmp errorOccured
getNum2:
;get the second integer
mov ah,09
mov dx,OFFSET integer2Msg
int 21h
mov ah,01
int 21h
;check that input is a number
cmp al, 30h ;0 (ASCII 48)
jl errorOccured
cmp al, 39h ;9 (ASCII 57)
jg errorOccured
mov ch,al
sub ch,30h
;get the second integer
mov ah,01
int 21h
;check that input is a number
cmp al, 30h ;0 (ASCII 48)
jl errorOccured
cmp al, 39h ;9 (ASCII 57)
jg errorOccured
mov cl,al
sub cl,30h
mov gotNum,1
cmp func,'1'
je addition
cmp func,'2'
je subtraction
cmp func,'3'
je errorOccured
getNumIntermediate:
jmp getNum1
addition:
cmp gotNum,0
je getNumIntermediate
;add the two numbers and adjust for addition
mov ah,0
add bx,cx
aaa
or bx,3030h
;display result
mov ah,09
mov dx,OFFSET sumMsg
int 21h
mov dl,bh
mov ah,02
int 21h
mov dl,bl
mov ah,02
int 21h
;return to beginning
jmp start
errorOccured:
lea dx, errorOccuredMsg
mov ah,09
int 21h
jmp start
subtraction:
cmp gotNum,0
je getNumIntermediate
;determine which subtraction to use
cmp bx,cx
jg subtractionPos
cmp bx,cx
jl subtractionNeg
subtractionPos: ;integer1 is larger than integer2
;subtract
sub bx,cx
aas
or bx,3030h
;display result
mov ah,09
mov dx,OFFSET subMsg
int 21h
mov dl,bh
mov ah,02
int 21h
mov dl,bl
mov ah,02
int 21h
;return to beginning
jmp start
subtractionNeg: ;integer2 is larger than integer1
;subtract
sub cx,bx
aas
or cx,3030h
;display result
mov ah,09
mov dx, OFFSET subMsg
int 21h
mov ah,06
mov dl,2dh ;displays the negative sign
int 21h
mov dl,ch
mov ah,02
int 21h
mov dl,cl
mov ah,02
int 21h
;return to beginning
jmp start
end start
我对组装很陌生,所以任何建议都会很棒。
编辑
;subtract
sub bx,cx
mov ch,bh ;store the value of bh
xchg bx, ax
mov bl,0Ah
div bl
xchg ah, al
xchg ax, bx
mov bh,ch ;restore the value of bh
or bx,3030h