I wrote a simple program in x86 assembly and I tried to run it using TASM(Turbo) and TLINK.
When I input 4 and 3, subtraction outputs / and not 1, as it should.
But when I input 3 and 4, I get the correct result.
Sum and sort work fine, code is not correct just for subtraction.
Any help?
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
Well, here’s one bug. Your sorting code has a conditional jump (
jlin this case) that has no effect as it only jumps to the next instruction, that will be executed next anyway:Your variable names
Nombre1andNombre2aren’t very descriptive. If you want to have variable names in French, I’d changeNombre1eg. toNombre_plus_grande(orbigger_numberin English) andNombre2eg. toNombre_plus_grande(orsmaller_numberin English). Otherwise, it’s difficult to debug the code when variable names don’t mean anything.Fixed version with English variable names (to make it better understandable to most people):
Note I also changed signed
jl(jump if less) to unsignedjae(jump if above or equal;jae,jnbandjncare synonymous). As this code doesn’t handle negative numbers, I think it’s better to be consistent with the jumps too.jge/jnl(jump if greater or equal/jump if not less) would work too. You may want to check Intel x86 JUMP quick reference.After this fix you should replace the variable names accordingly in other parts of the code.
If the sum goes above 9, then it outputs trash. Subtraction, however, should work always correctly for valid inputs.