简体   繁体   中英

subtract 2 numbers in TASM x86 assembly

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 ( jl in this case) that has no effect as it only jumps to the next instruction, that will be executed next anyway:

CMP Nombre1,AL ; 2éme chifre en AL
JL Permutation
Permutation:
MOV AL,Nombre1
MOV AH,Nombre2
MOV Nombre1,AH
MOV Nombre2,AL

Your variable names Nombre1 and Nombre2 aren't very descriptive. If you want to have variable names in French, I'd change Nombre1 eg. to Nombre_plus_grande (or bigger_number in English) and Nombre2 eg. to Nombre_plus_grande (or smaller_number in 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):

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:

Note I also changed signed jl (jump if less) to unsigned jae (jump if above or equal; jae , jnb and jnc are 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.

mov al,bigger_number
sub al,smaller_number
mov Difference,AL

mov al,bigger_number
add al,smaller_number
mov Somme,al

If the sum goes above 9, then it outputs trash. Subtraction, however, should work always correctly for valid inputs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM