Hello everybody. I ran into a problem programming x86 assembly and was hoping that you guy could help figure out what is wrong. The problem is with the code below. It should output W12 to the console when the enter key is pressed, very simple. The problem is that it outputs garbage. It outputs random characters and the program finishes.

Code ( (Unknown Language)):

data SEGMENT

key db 0

msg db "W12$"

data ENDS

code SEGMENT

ASSUME CS: code, DS: data

mov ax, SEG data

mov ds, ax

start:

mov ah, 8 ; read key from keyboard

int 21h

mov key, al

cmp key, 13

JNZ start ; if key is not carriage return go to start

mov ah, 9

mov dx, OFFSET msg

int 21h

mov ah, 04ch

int 21h

code ENDS

END

I tried using the code below. Which is very similar just a few changes and it worked perfectly fine.

Code ( (Unknown Language)):

.model small

.data

key db 0

msg db "W12$"

.code

mov ax, SEG @data

mov ds, ax

start:

mov ah, 8 ; read key from keyboard

int 21h

mov key, al

cmp key, 13

JNZ start ; if key is not carriage return go to start

mov ah, 9

mov dx, OFFSET msg

int 21h

mov ah, 04ch

int 21h

END

The first code is the way the professor taught us to structure the code(using SEGMENT, ENDS, etc.), so I am trying to make that work and the second is something I tried by looking up information on the internet.

I guess the root of your problem. Is that DSX is not pointing to the string msg in the first example. I suspect DS is not set up correctly. Problems like this call for using the debugger. Have your professor talked about this yet. In assembler programming it is VERY VERY important that you master using the debugger. Which assembler tool do you use?