Search code examples
x86-16emu8086

Getting error after copying 8086 code from book


I am having troubles running this 8086 programs that take one letter input from keyboard and outputs "the letter you typed is _"

I just started reading my college book on this and trying to run some code from the book on my computer but got stuck here.

The code below is from my college book. I am running it in emu8086 emulator.

CODE SEGMENT
            ; set the DS register
MOV AX, DATA
MOV DS, AX
            ; Read Keyboard
MOV AH, 08H
INT 21H
            ; Save input
MOV BL, AL
            ; Display first part of Message
MOV AH, 09H
MOV DX, OFFSET MESSAGE
INT 21H
            ; Display character of BL register
MOV AH, 02H
MOV DL, BL
INT 21H
            ; Exit to DOS
MOV AX, 4C00H
INT 21H

CODE ENDS
DATA SEGMENT
     MESSAGE DB “The letter you typed is $”
DATA ENDS
END

The error I am getting is

INT 21h, AH=09h - 
address: 00020
byte 24h not found after 2000 bytes.
; correct example of INT 21h/9h:
mov dx, offset msg
mov ah, 9
int 21h
ret
msg db "Hello$"

After further debugging by myself I have concluded there is some problem with this

MOV AX, DATA
MOV DS, AX

If I remove it from my code it executes but with some garbage in the start.

Output after removing those statements

Any suggestions will be appreciated.

Update: Image of emulator around the string "the letter you typed $" Image of emulator around the string "the letter you typed $


Solution

  • umm so after giving up for 2 days I referenced another book, and in there they weren't using segments. So I removed segments and the program ran flawlessly here is the new code:

    ORG 100H
    
    MOV AH, 08H ; Read Keyboard
    INT 21H
                
    MOV BL, AL  ; Save input
                
    MOV AH, 09H ; Display first part of Message
    MOV DX, OFFSET MESSAGE
    INT 21H
                
    MOV AH, 02H  ; Display character of BL register
    MOV DL, BL
    INT 21H
                
    MOV AX, 4C00H ; Exit to DOS
    INT 21H
    
    RET
    
    
    MESSAGE DB "The letter you typed is $"
    

    Thank you to everyone who tried to help me, really appreciated.