flat assembler
Message board for the users of flat assembler.

Index > DOS > Art Of Assembly

Author
Thread Post new topic Reply to topic
emu1



Joined: 29 Jul 2010
Posts: 3
emu1
Hello, I'm learning assembly from the Art of Assembly online, I've chosen to use the FASM compiler..however from time to time there are some things from Art of Assembly I have trouble translating to fasm syntax. For example I'm trying to code a dos TSR key counter

http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_18/CH18-1.html#top

The obvious problem is all resident code and variables are supposed to be in one segment and you need to know how large that segment is for the INT 31h paragraph count. I can't find any FASM directives for resident segment definition..would
Code:
push  ds
push  cs
pop  ds
    


be sufficient to place the program variables in code segment?(I seem to recall reading in manual that no matter where you declare variables they are placed at end of code by compiler?) The other problem is figuring out how large the resident segment code is..would code subtracting $-$$ and placing value into ax be accurate?
Post 29 Jul 2010, 19:14
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
To make multi-segment program in FASM, you need to produce MZ file format (.EXE), not just "org 100h" (which defaults to .COM format). .COM is single-segment, .EXE is multi-segment.

However, for TSR it is enough to use .COM, and place all the stuff which has to resident at the beginning of segment, and then only leave that resident.

http://flatassembler.net/docs.php?article=manual#2.4.1
Post 29 Jul 2010, 20:00
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
emu1



Joined: 29 Jul 2010
Posts: 3
emu1
If I compile a program with
ORG 100h
USE16
as 'fasm program.asm program.exe'

will the .exe be in .com format because of my org 100h or is it necessary to 'fasm program.asm program.com' to get the single segment .com format?

Thanks.
Post 29 Jul 2010, 20:55
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4240
Location: 2018
edfed
the executable format is not dependant of the compiler, it depends of the OS where it will run.

for .COM, it is used to start with ORG 100h
for .EXE, it is used to strat with FORMAT MZ, PE GUI, PE CONSOLE, etc
for linux, it is an other way
for menuet, it starts with a menuet header...
and so on.

just think about WHERE the program is IN RAM, and where point CS:EIP.
.COM starts with CS=??? and (E)IP = 100h

some OS brings the possibility to make abstraction of this.
.EXE is a mecanism like that.

let's enjoy the asm coding now.
Post 29 Jul 2010, 22:19
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17450
Location: In your JS exploiting you and your system
revolution
emu1 wrote:
If I compile a program with
ORG 100h
USE16
as 'fasm program.asm program.exe'

will the .exe be in .com format because of my org 100h or is it necessary to 'fasm program.asm program.com' to get the single segment .com format?
The format of the output file depends entirely upon the source. The filename extension is up to you, fasm does not use the extension to determine the file format. If you do not include the format directive then by default fasm uses binary format.
Code:
format binary ;<--- this is the default
org 100h
use16
;...    
Post 30 Jul 2010, 01:58
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
> I can't find any FASM directives for resident segment definition

There is none.

> To make multi-segment program in FASM, you need to produce MZ file format (.EXE)

Right.

> not just "org 100h" (which defaults to .COM format).

NO. FASM defaults "format binary" , "org 100h" doesn't default to anything.

> COM is single-segment, .EXE is multi-segment.

Not exactly. COM is always single-segment, EXE can be multi-segment but also single-segment.

> However, for TSR it is enough to use .COM, and place all the stuff
> which has to resident at the beginning of segment, and then
> only leave that resident.

Right. You can throw away the init code before going resident, or you can keep it (YES, this wastes memory).

See here for a simple FASM TSR: http://board.flatassembler.net/topic.php?t=9256

> ORG 100h
> USE16
> as 'fasm program.asm program.exe'

Don't do this - this brews a missnamed COM Shocked

> the executable format is not dependant of the compiler,
> it depends of the OS where it will run.

NO. Brewn format depends from the source code only in FASM.

> for .COM, it is used to start with ORG 100h

You can write "format binary as "COM" ... but maybe FASM will forgive if you don't ...

> for .EXE, it is used to strat with FORMAT MZ, PE GUI, PE CONSOLE, etc

Make sure to pick a format suitable for your OS:

MZ : DOS, can be 8086-compatible
PE : Win32, or DOS, but needs DOS extender and 80386

> .COM starts with CS=??? and (E)IP = 100h

COM runs always in real mode, there is no EIP, and you have unbreakable 64 KiB segment limit.

> let's enjoy the asm coding now.

Right Smile

FYI, programming with FASM is easier than with MASM (as you usually can simply throw away all the segment/assume/code-vs-data/... stuff). You can't link 16-bit FASM with HLL code (as FASM doesn't support OMF), and you need segments (or some other hack) only for > 64 KiB of bloat.

EDIT : fixed BUG pointed below


Last edited by DOS386 on 30 Jul 2010, 04:51; edited 1 time in total
Post 30 Jul 2010, 04:28
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler
> You can't link with HLL code
Question Wha' da ya mean? My trampoline is assembled by fasm(format elf) and linked by ld into my "kernel"(Not really a kernel, just a bunch of code that happens to be bootable.). Everything else is in C.
Post 30 Jul 2010, 04:45
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
> You can't link with HLL code
> Wha' da ya mean?

16-bit code.
Post 30 Jul 2010, 04:51
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Quote:
If I compile a program with
ORG 100h
USE16
as 'fasm program.asm program.exe'

will the .exe be in .com format

Yes, file will have .exe extension, but it will be in a ".COM format" and when you execute, it will be executed as such when you run it.

Quote:
because of my org 100h

COM format is in fact pure binary format, which is loaded at offset 100h (hence the "org 100h").

Quote:
or is it necessary to 'fasm program.asm program.com' to get the single segment .com format?

No no, as revolution said, contents of resulting file depend entirely on contents of source, not on command line.

Also when DOS executes file, it doesn't care about extension. It looks into the file, if it startz with ascii "MZ" characters, it is executed as EXE format, otherwise it is treated as COM format.
Post 30 Jul 2010, 11:22
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
emu1



Joined: 29 Jul 2010
Posts: 3
emu1
Thanks for all the great infos!
Post 30 Jul 2010, 20:55
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.