Jump to content
Tuts 4 You

MASM32: hello world in DOS


alaphate

Recommended Posts

Posted (edited)

The code below can be compiled and linked to an .exe file.

However, when I executed it, it "encountered a problem, and needs to be closed".

MASM32 has no @data macro, how to get the msg's DS address?

Thank you for help in advance.

.386

.model flat, stdcall

.data

msg db "Hello World!$"

.code

Main PROC

;mov ax,@data

;mov ds,ax

lea dx,msg

mov ah,9

int 21

int 20

Main ENDP

end Main

Edited by alaphate
Posted
The code below can be compiled and linked to an .exe file.

However, when I executed it, it "encountered a problem, and needs to be closed".

MASM32 has no @data macro, how to get the msg's DS address?

You should really use an assembler that's designed to produce 16 bit DOS executables.

I just looked at the start of your source - you've told it to use a flat memory model DOS didn't have

a flat memory model - it was cut into 64kb segments - I'm about 90% sure this won't fly.

Never bothered to write exe file using assembler until I did it under windows - I always wrote .com files since they had

no header and one could easily produce a 10byte executable.

When writing dos .com files, DS is the same as CS. I.e code and data reside in the same segment.

All you have to do is

mov ax, cs
mov ds, ax

or

push cs
pop ds

Nasm combined with NasmIDE used to be my toolset of choice under DOS. You can find both windows and dos versions here:

http://sourceforge.net/project/showfiles.php?group_id=6208

Posted (edited)

Enhzflep,

Thank you for your help.

I downloaded TASM5.0, and the code below can run successfully.

Then, the problem comes to how to convert it to MASM version.

Especially, this line: mov ax, @data, because no @data syntax in MASM.

The program has data segment because it uses small model.

I don't know why I should fill DS when the program starts.

I think DS should be auto-filled when the program starts.

Does @data have value before the program starts?

.model small.data
msg db "Hello world!$".code
main:
mov ax, @data
mov ds, ax
mov dx, offset msg
mov ah,9h ;print string in dx
int 21h
mov ah,4ch ;terminate process
int 21h
end main
Edited by alaphate
Posted (edited)
Enhzflep,

Thank you for your help.

The program has data segment because it uses small model.

I don't know why I should fill DS when the program starts.

I think DS should be auto-filled when the program starts.

Does @data have value before the program starts?

That's okay, a pleasure.

Yes, from memory DS is set to the correct segment by the OS between loading and executing the code (though that may only be when you use the .tiny memory model)

As to the question about the @data notation in masm, it's as easy as writing "seg data"

Here's a masm source for a HelloWorld.

.model small
.stack
.data
message db "Hello world!", "$" .code main proc
mov ax,seg message
mov ds,ax mov ah,09
lea dx,message
int 21h mov ax,4c00h
int 21h
main endp
end main

Check out this thread for a 600kb zip of some dos asm stuff: http://www.masm32.com/board/index.php?topic=10213.0

The above code comes from this tutorial: http://www.xs4all.nl/~smit/asm01001.htm

Edited by enhzflep
Posted

Enhzflep,

Thank you very much!

Now I can compile it under MASM.

The tutorial is very helpful. Thx, bro!

  • 7 years later...

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
×
×
  • Create New...