Search code examples
assemblydosx86-16emu8086

Why is my data segment clashing with my code segment?


I have a program here that divides two user input numbers and shows the quotient. The program already runs fine and produces the correct result but when I added the data segment, instead of showing the quotient, it displays the capital "T". I have tried single stepping it and noticed that the value of the AX register changes before printing MSG3. I want to be able to add the string Input First Number and such, but something in the process seems to be messing with the code. How can I solve this?

Here is the code:

.MODEL SMALL
 
READ MACRO MSG
    MOV AH,0AH
    LEA DX,MSG
    INT 21H
ENDM

PRINT MACRO MSG
    MOV AH,09H
    LEA DX,MSG
    INT 21H
ENDM

.DATA 
 
 CR EQU 0DH
 LF EQU 0AH
    MSG1 DB "Enter First Digit: $"
    MSG2 DB CR,LF,"Enter Second Digit: $"
    MSG3 DB CR,LF,"The Quotient is: $"

BUFF DB 2  
DB 3 DUP ('$')
DATA ENDS

.CODE
ASSUME CS:CODE,DS:DATA 

    MAIN PROC
        MOV AX,DATA
        MOV DS,AX
    
        PRINT MSG1
        
        MOV AH,1
        INT 21H      ;Dividend
        MOV BH,AL
        SUB BH,48    ;Convert to decimal no        
        
        PRINT MSG2
        
        MOV AH,1
        INT 21H      ;Divisor
        MOV BL,AL
        SUB BL,48
        
        MOV CL,BH    ;Move dividend to cl reg
        MOV CH,00    ;Empty register?
        MOV AX,CX    ;Move 16bit to 16bit reg
        
        DIV BL       ;AX/BL
        AAD          ;Stop dividing. ASCII adjust AX before division. Clears AH reg       
        
        PRINT MSG3
        
        ADD AL,48
        MOV AH,2
        MOV DL,AL    ;Print
        INT 21H
        
        EXIT:
        MOV AH,4CH
        INT 21H
        
    MAIN ENDP
    
END MAIN 

Solution

  • The reason T is being printed is because the Int 21h/AH=9h DOS function call is clobbering the value in AL with the $ character.

    DOS 1+ - WRITE STRING TO STANDARD OUTPUT
    AH = 09h
    DS:DX -> '$'-terminated string
    
    Return:
    AL = 24h (the '$' terminating the string, despite official docs which
    state that nothing is returned) (at least DOS 2.1-7.0 and
    NWDOS)
    

    The value 24h is being returned in AL, and you add 30h(48) to it to get 54h. The letter T is 54h and that is the character printed. You will need to put the value you want to preserve in a register not used by the Int 21h/AH=9h DOS function call. Any of the unused 8-bit registers will suffice like BL.