[英]subtract 2 numbers in TASM x86 assembly
我在x86匯編中編寫了一個簡單程序,並嘗試使用TASM(Turbo)和TLINK運行它。
當我輸入4和3時,減法將輸出/
而不是1
。
但是當我輸入3和4時,我得到正確的結果。
求和和排序工作正常,代碼不適合減法。
有什么幫助嗎?
code Tasm
.model small
.stack
.code
DONNEES SEGMENT
Nombre1Msg db 'Entrez le 1er chiffre:$'
Nombre2Msg db 'Entrez le 2er chiffre:$'
DifferenceMsg db 'Leur difference:$'
SommeMsg db 'Leur Somme:$'
AscMsg db 'En Ordre croissant:$'
DescMsg db 'En Order decroissant:$'
Sperateur db ',$'
return db 0DH,0AH,('$')
Nombre1 db
Nombre2 db
Somme db
Difference db
DONNEES ENDS
Main PROC FAR
ASSUME DS:DONNEES
MOV AX,DONNEES
MOV DS,AX
MOV DX,offset Nombre1Msg
MOV AH,9
INT 21H
MOV AH,1 ; saisie
INT 21H ; le caractere lu arrive dans AL
Sub AL,'0';le chiffre= code ASCII-ASCII(0)
MOV Nombre1,AL
call LineFeed
MOV DX,offset Nombre2Msg
MOV AH,9
INT 21H
MOV AH,1 ; saisie
INT 21H ; le caractere lu arrive dans AL
Sub AL,'0';le chiffre= code ASCII-ASCII(0)
MOV Nombre2,AL
CMP Nombre1,AL ; 2éme chifre en AL
JL Permutation
Permutation:
MOV AL,Nombre1
MOV AH,Nombre2
MOV Nombre1,AH
MOV Nombre2,AL
MOV AL,Nombre1
SUB AL,Nombre2
MOV Difference,AL
MOV Al,Nombre1
add AL,Nombre2
MOV Somme,AL
call LineFeed
MOV DX,offset DifferenceMsg
MOV AH,9
INT 21H
MOV DL,Difference
ADD DL,48
MOV AH,2
INT 21H
call LineFeed
MOV DX,offset SommeMsg
MOV AH,9
INT 21H
MOV DL,Somme
ADD DL,48
MOV AH,2
INT 21H
call LineFeed
MOV DX,offset AscMsg
MOV AH,9
INT 21H
MOV DL,Nombre2
ADD DL,48
MOV AH,2
INT 21H
MOV DX,offset Sperateur
MOV AH,9
INT 21H
MOV DL,Nombre1
ADD DL,48
MOV AH,2
INT 21H
call LineFeed
MOV DX,offset DescMsg
MOV AH,9
INT 21H
MOV DL,Nombre1
ADD DL,48
MOV AH,2
INT 21H
MOV DX,offset Sperateur
MOV AH,9
INT 21H
MOV DL,Nombre2
ADD DL,48
MOV AH,2
INT 21H
MOV AH,4CH
INT 21H
Main ENDP
LineFeed proc near
MOV dx,offset return
MOV ah,9
int 21h
ret
LineFeed endp
end
好吧,這是一個錯誤。 您的排序代碼具有條件跳轉(在本例中為jl
,該條件跳轉無效,因為它僅跳轉到下一條指令,該指令無論如何都會執行:
CMP Nombre1,AL ; 2éme chifre en AL
JL Permutation
Permutation:
MOV AL,Nombre1
MOV AH,Nombre2
MOV Nombre1,AH
MOV Nombre2,AL
您的變量名稱Nombre1
和Nombre2
的描述性不是很好。 如果您想使用法語命名變量,請更改Nombre1
例如。 到Nombre_plus_grande
(或bigger_number
以英文)及Nombre2
如。 到Nombre_plus_grande
(或smaller_number
英文)。 否則,當變量名沒有任何意義時,很難調試代碼。
具有英文變量名的固定版本(以使大多數人更容易理解):
bigger_number db
smaller_number db
mov smaller_number,al ; store 2nd input into smaller_number
cmp bigger_number,al ; 1st input in bigger_number, 2nd input in al
jae order_ready ; if numbers are equal or if bigger_number is bigger,
; there's then nothing to do.
xchg al,bigger_number ; exchange al (2nd input) and bigger_number
mov smaller_number,al ; store al (now the 1st input) into smaller_number
order_ready:
注意,我還將帶符號的jl
(如果小於則跳轉)更改為無符號的jae
(如果大於或等於則跳轉; jae
, jnb
和jnc
是同義詞)。 由於此代碼不能處理負數,因此我認為最好也與跳轉保持一致。 jge
/ jnl
(如果大於或等於則跳轉,如果不小於則跳轉)也可以。 您可能需要查看Intel x86 JUMP快速參考 。
修復之后,您應該在代碼的其他部分中相應地替換變量名稱。
mov al,bigger_number
sub al,smaller_number
mov Difference,AL
mov al,bigger_number
add al,smaller_number
mov Somme,al
如果總和超過9,則輸出垃圾。 但是,對於有效輸入,減法應始終正確工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.