assemblymasmx86-16dosdosbox

Load byte value from a DB variable into 16-bit register (sign or zero extend a byte)


I m using MASM compilor and DOSBOX. I want to want to save value from a variable into register. I want to save num1 value into cx register. How can I do that?

      .MODEL SMALL
.STACK  50H
.DATA
num1 db '5'
    NL  DB  0DH, 0AH, '$'        
    msg     db ?,0AH,0DH,"Enter an odd number between 0 to 10:$"
     nxtline db 0Ah,0DH,"$"
.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX
    
    
            
              LEA DX,msg     
              mov ah,9
              int 21H
              LEA DX,nxtline    
              mov ah,9
              int 21H
              
              MOV AH,1                    
              INT 21H 
              LEA DX,nxtline    
              mov ah,9
              int 21H
              
              
              
       mov bl,al   ;save the value from input
              mov num1,bl
               LEA DX,num1     
              mov ah,9
              int 21H
              mov cl,al 
              
   main endp
   end main

Solution

  • You are losing the value entered by the user in AL. You input one char with this:

              MOV AH,1                    
              INT 21H 
    

    The char is stored in AL, but before you save the value in BL you display a line break:

              LEA DX,nxtline    
              mov ah,9
              int 21H
    

    And the value in AL is gone because this interrupt uses AL to display a string. The solution is to save the value in BL before you display the line break:

              MOV AH,1                    
              INT 21H 
       mov bl,al   ;save the value from input
              LEA DX,nxtline    
              mov ah,9
              int 21H
    

    Edit : move value into CX :

    xor cx,cx       ;CLEAR CX.
    mov cl,bl       ;MOVE CHAR INTO CL.
    sub cl, 48      ;CONVERT CHAR TO DIGIT, EXAMPLE: '5' -> 5.