简体   繁体   中英

Sum of two numbers (each 2 characters) in assembly 8086

I'm actually a beginner in using emu8086 for assembly codes. I want to do the sum of 2 numbers with 2 characters each and later generate it for n characters. I was able to do it with one character, but not with two.

data segment                                     
    mess1 db "saisir le premier nombre : $"               
    mess2 db 0Ah,0Dh, "saisir le second nombre : $"    ;0ah, 0dh pour faire le saut de ligne
    mess3 db 0Ah,0Dh, "le resultat est : $"
    a db ?         ; contient le nombre apres conversion 
    b db ?         
    res db ?            
    buffer db 2 dup ('$')   
    dix db 10        
data ends

code segment

    assume cs:code, ds: data

debut:        
    mov ax,data
    mov ds,ax
     ;affichage du 1er message  
     mov DX, offset mess1 ; lea DX,mess1
     call AffichageMess 

     ;saisie du premier nombre   
     call SaisiNb

     ;convertion du premier nombre   
     call ConvertNb 


     mov a,al


      ;affichage du 2eme message 
     mov DX, offset mess2 ; lea DX,mess2 
     call AffichageMess 

     ;saisie du deuxieme nombre   
     call SaisiNb

     ;convertion du deuxieme nombre   
     call ConvertNb   

     mov b,al



     call Somme 


     mov DX, offset mess3 ;lea DX,mess3
     call AffichageMesRes     



fin: mov ah,4ch
     int 21h


     ;DECLARATION DES PROC

     affichageMess Proc 
         mov ah,09h
         int 21h   
         Ret
     affichageMess endp





     SaisiNb Proc 
         xor si,si
         xor cx,cx
         mov cx,2   ;saisie de chaine numerique de 2caracteres  

         Repeat: 
         mov ah,01h                      
         int 21h  
         mov buffer[si],al        ;Mode d'adressage INDEXE RELATIF
         inc si
         loop Repeat   

         Ret 
     SaisiNb endp




     ConvertNb Proc   
         xor ax,ax
         mov al,buffer[0] 
         sub al,'0'
         mul dix
         mov bl,buffer[1]  
         sub bl,'0'
         add al,bl


         Ret
     ConvertNb endp   



     Somme Proc
         mov al,a
         add al,b
         mov res,al
         Ret

     Somme endp

     AffichageMesRes Proc
          mov ah,09h
         int 21h  

         call Somme
         mov ah,02h
         int 21h            
         Ret 
     AffichageMesRes endp  
code ends
end debut

Your AffichageMesRes procedure is wrong. First, it calls Somme again, in spite both numbers have already been added in the main thread debut: . Second, using the DOS function AH=2 WRITE CHARACTER TO STANDARD OUTPUT expects the character be loaded in DL, which is not.

The calculated result in res is an 8bit unsigned binary number. For instance if you entered 12 as le premier nombre and 34 as le second nombre , the result is 12+34=46 which is stored in res as 0x2E. You need to convert the binary 0x2E into two decadic characters '4' and '6' prior to writing them on console. Hint: divide 0x2E by dix and use the divident (4) as the first digit and the remainder (6) as the second digit.

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