x86 assembly output string

Thread Starter

adrenalina

Joined Jan 4, 2011
78
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.

Rich (BB code):
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.

Rich (BB code):
.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.
 

t06afre

Joined May 11, 2009
5,934
I guess the root of your problem. Is that DS:DX 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?
 
Interesting to see people still using assembler.
I last used it 15-20 years ago on the PC partly because there was little choice.
I wrote a 300,000 line program for designing printed circuit boards.

I think since about Vista you can no longer run DOS programs under Windows.
It works ok with XP though.

I guess its like electronic engineers still learning about valves despite it being highly unlikely they will use them now.
 

MrChips

Joined Oct 2, 2009
30,720
I guess that is a wrong guess. Both ASM and vacuum tubes are still in use today. If you want to be a well rounded engineer, you should have those tools under your belt.
 

Thread Starter

adrenalina

Joined Jan 4, 2011
78
no, my professor hasn't talked about a debugger. We are using MASM 6.11. What debugger could I use?
I tried that mrchips, but the assembler gave me an error.
error A2006: undefined symbol.
 

Thread Starter

adrenalina

Joined Jan 4, 2011
78
Problem fixed! At the end it should say end start(or the name of the label where the program starts)

Rich (BB code):
data 	SEGMENT	

key	db 0
msg	db "W12$"
data	ENDS


code	SEGMENT
	ASSUME CS: code, DS: data
start:
mov	ax, SEG data
mov	ds, ax

mov 	ah, 8		; read key from keyboard
int	21h
mov 	key, al
cmp	key, 13
JNZ	start1		; if key is not carriage return go to loop

mov 	ah, 9
mov	dx, OFFSET msg
int	21h

mov	ah, 04ch
int	21h

code	ENDS

	END start
 
Top