Sign in to follow this  
TheUnbeliever

Assembly Code Problem

Recommended Posts

TheUnbeliever    963
I'm using Turbo Assembler 5.3 and Turbo Link 2.0. I've got a small assembly program (which I'm going through to get some base in the low level stuff before moving to C++ plus, it's fun!) which prints 'Hello World!' (without quotes) to the console. The code is:
.DOSSEG
.MODEL SMALL
.STACK
.CODE
	Msg db "Hello World!$"
	MOV DX,offset Msg;Move the offset of the message to the Data register
	ADD DX,80;Add 80 to the offset (otherwise we get unwanted garbage before it)
	MOV AX,0900h
	INT 21h
	MOV AX,4C00h
	INT 21h
END
I've got two questions, possibly related so I'll order them as I came across them: 1.) If I don't include the 'ADD DX,80' line, then I get a lot of garbage in the output, like so:
E:\Assembly>test2
═  ƒ Ü­■↔­Ì☺↑♦K☺↑♦V☺↑♦↑♦☺☺☺ ☻               ♀♣Þ♥┘♠¶ ↑ Ã♠        ♣
                                                 Hello World!
I've got round this by adding 80 to the offset of the message address (I'm guessing that this is adding 10 bytes). Can anyone explain this? 2.) Currently, when it prints, it prints a good way across the screen - why is this?
E:\Assembly>test2
                                                 Hello World!
I'm not necessarily looking for how to solve the problems, but an explanation of why they are occuring, so that I can work out how to sort the, would be much appreciated! Angus.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Both questions are connected. DOS adds a 256 byte header when the program is loaded into memory. When you program begins the ES segment points to this header and the DS segment register actually points to your program. When using DOS to print to the screen (through int 21h), DOS uses the ES segment pointer. So basically you are printing trash to the screen.

You should add a .DATA area and put your message there.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
A simple google search reveals:



title Hello World Program (hello.asm)
; This program displays "Hello, World!"

dosseg
.model small
.stack 100h

.data
hello_message db 'Hello, World!',0dh,0ah,'$'

.code
main proc
mov ax,@data
mov ds,ax

mov ah,9
mov dx,offset hello_message
int 21h

mov ax,4C00h
int 21h
main endp
end main


Share this post


Link to post
Share on other sites
TheUnbeliever    963
Thanks a lot. :) Changing the offset of the offset to 256 sorted the problem, then when I added the .DATA section, moved the message there and changed the offset of the offset to 272 (+16 bytes to account for the .CODE and .DATA sections) it continued to work.

EDIT: And your second post is pretty useful too. :)

Share this post


Link to post
Share on other sites
Zahlman    1682
Yeah, please make good use of symbols like '@data' or whatever the equivalent is for your assembler. Even the hardest of the hard-core don't waste their time calculating offsets that the assembler can find for them; it's not as though any optimization is possible there :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this