Search code examples
assemblyinterruptmasmcpu-registers

Unexpected result of assembly program


I have just started learning assembly language and I am following this tutorial.

I have written the following program:

.MODEL SMALL
.STACK 100H
.DATA
    msg db "123123.$"
.CODE
MAIN PROC

    MOV AH,02   ; Function to output a char
    MOV DL,"!"      ; Character to output
    INT 21h     ; Call the interrupt to output "!"

    MOV AH, 09
    MOV DX, OFFSET msg
    INT 21h

    MOV AH,04Ch ; Select exit function
    MOV AL,00   ; Return 0
    INT 21h     ; Call

MAIN ENDP           ; Terminate program
END MAIN

I am expecting the following output:

! 123123

But I am getting the following:

enter image description here

  • Register AH is used to store the sub-function.
  • Register DL is used for storing character to output using INT 21h
  • Register DX is used for storing the address of string to print using INT 21h

Questions:

1.) Are the above points right? If not, what am I not getting right?

2.) If the above points are right then why am I getting unexpected output?

3.) How to avoid this kind of behavior in the future?

Thanks.


Solution

  • Your tutorial deals with .MODEL TINY to produce a .com-executable. You use .MODEL SMALL and produce an .exe-executable. For .MODEL SMALL you must explicitely set the DS-register which points by default to the PSP (not to .DATA):

    .MODEL SMALL
    .STACK 1000H    ; Mor stack, less trouble
    .DATA
        msg db "123123.$"
    .CODE
    MAIN PROC
        MOV AX, @DATA
        MOV DS, AX
    
    ... (no change) ...