FASM HelloWorld .exe program

purum picture purum · Nov 13, 2010 · Viewed 8.5k times · Source

I tried to write my first .exe program on FASM. It works ok when I use org 100h, but I want to compile .exe file. When I replaced first line with "format PE GUI 4.0" and tried to compile it the error occured: "value out of range" (line: mov dx,msg).

ORG 100h      ;format PE GUI 4.0

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

mov ah,10h
int 16h

int 21h

msg db "Hello World!$" 

How should I change the source code?
----------------------------------------------
The answer is:

format mz
org 100h

mov edx,msg
mov ah,9h
int 21h

mov ah,10h
int 16h

mov ax,$4c01
int 21h

msg db "Hello World!$" 

Answer

fasmnewbie picture fasmnewbie · Feb 23, 2011

Your first version is in COM format. It is a 16-bit real mode FLAT model. Your second version is in DOS MZ format. It is a 16-bit real mode SEGMENTED model.

Segmented model uses "segments" to describe your DS (segment) and DX (offset). So firstly you need to define segments for your data and code, and secondly you need to point correctly where is your data segment and what is your offset before you can use the int 21h, function 9.

int 21h, function 9 needs a DS:DX to be setup correctly in segmented model, to print a null terminated string

format MZ
entry .code:start
segment .code
start:
mov ax, .data ; put data segment into ax
mov ds, ax    ; there, I setup the DS for you
mov dx, msg   ; now I give you the offset in DX. DS:DX now completed.
mov ah, 9h
int 21h
mov ah, 4ch
int 21h
segment .data
msg db 'Hello World', '$'

Hope this helps some FASM newbies out there.