flat assembler
Message board for the users of flat assembler.

Index > Compiler Internals > directive org

Author
Thread Post new topic Reply to topic
Endre



Joined: 29 Dec 2003
Posts: 215
Location: Budapest, Hungary
Endre 05 Feb 2005, 20:41
When writing bootloader for FAT filesystems you may want to have a signature word 0xaa55 from the byte 0x1fe of the given boot sector (0x7c00 + 0x1fe = 0x7dfe). The code below does however not want to compile correctly. Fasm ignores the org directive. Why? Why am I compelled to work-around this not working feature of the compiler? Why does it just put the thing at the current position without any warning? Maybe the theme was aleady discussed here, but I didn't find anything about it.

Thanks in advance for your answers,
Endre.

Code:
org    0x7c00
...
; any bootloader codes
...
org    0x7dfe
dw     0xaa55 ; boot signature    
Post 05 Feb 2005, 20:41
View user's profile Send private message Reply with quote
beppe85



Joined: 23 Oct 2004
Posts: 181
beppe85 05 Feb 2005, 21:13
Endre wrote:
The code below does however not want to compile correctly. Fasm ignores the org directive.

Code:
org    0x7c00
...
; any bootloader codes
...
org    0x7dfe
dw     0xaa55 ; boot signature    


I'll try to explain. Reaching a org directive, FASM will assembly addresses from that place on taking the new origin as base. It will not physically put padding.

See a template:
Code:
org    0x7c00
origin:
; any bootloader codes
rb $ + $200 - origin  - 2
dw     0xaa55 ; boot signature    

_________________
"I assemble, therefore I am"

If you got some spare time, visit my blog: http://www.beppe.theblog.com.br/ and sign my guestmap
Post 05 Feb 2005, 21:13
View user's profile Send private message Reply with quote
Endre



Joined: 29 Dec 2003
Posts: 215
Location: Budapest, Hungary
Endre 05 Feb 2005, 22:17
beppe85 wrote:

I'll try to explain. Reaching a org directive, FASM will assembly addresses from that place on taking the new origin as base. It will not physically put padding.


Yes, I know, and with this we can reach the following inconvenient behavior (sorry for the stupid example):
Code:
...
  cmp word [qqq], 0xaa55
...
  org 0x7dfe
qqq:
  dw 0xaa55    

That compiles to
Code:
...
  cmp word [0x7dfe], 0xaa55
; from here on you already lost
...
    
which seems to be correct but 0xaa55 won't be compiled at 0x7dfe. It will be somewhere after the code but not at the right point.

One sentence from fasm.txt:
Quote:
"org" directive sets address at which the following code is expected to appear in memory.
It's hard to misunderstand, and it's not what you have written and what it actually does.
Post 05 Feb 2005, 22:17
View user's profile Send private message Reply with quote
beppe85



Joined: 23 Oct 2004
Posts: 181
beppe85 05 Feb 2005, 22:29
From "expected to appear" you can presume that is not FASM responsibility put put your code/data there, but some other thing, be it BIOS, CPU or yourself. In a bootloader you usually put a rb just before dw $aa55.

Endre wrote:
The code below does however not want to compile correctly.

So what exactly you want?

_________________
"I assemble, therefore I am"

If you got some spare time, visit my blog: http://www.beppe.theblog.com.br/ and sign my guestmap
Post 05 Feb 2005, 22:29
View user's profile Send private message Reply with quote
Endre



Joined: 29 Dec 2003
Posts: 215
Location: Budapest, Hungary
Endre 06 Feb 2005, 10:52
beppe85 wrote:
From "expected to appear" you can presume that is not FASM responsibility put put your code/data there, but some other thing, be it BIOS, CPU or yourself. In a bootloader you usually put a rb just before dw.
Except the labels, which appropriately reference addresses given by org. That's what makes confusion here: From org the code and the labels - as my example showed above - start to live separate lives. That is, org is totally meaningless (but dangerous), since if you have to use work-arounds then why to use org - just for making your life hard? Actually I don't see any point to use org anyway.
Post 06 Feb 2005, 10:52
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 06 Feb 2005, 12:11
If the org worked like you want, the initial "org 0x7c00" would put 7C00h zero bytes at the beginning of your file (because binary file always starts at offset 0) and "org 100h" used when you want to make .com file would put the 100h zero bytes there - of course making the file unusable in both those case - because this is operating system who loads the codes at these addresses - with "org" directive you just tell the assembler where in memory the following piece of code will be loaded.
You use "org" more than once in your code, when you write a code that will have different parts loaded in different memory areas - but it's your responsibility to put the generated code in the place for which you have designed it. This is what the "org" directive tells: "the following code is designed to work in this place of memory".
Post 06 Feb 2005, 12:11
View user's profile Send private message Visit poster's website Reply with quote
Endre



Joined: 29 Dec 2003
Posts: 215
Location: Budapest, Hungary
Endre 06 Feb 2005, 12:41
Yes, that's why I use the next macro, which does I expect from built in org to do. Rather unelegant...
Code:
macro org addr {
  if $ = 0 
    org addr 
  else if addr >= $
     rb addr - $
  else
    display "origin error", 0xd, 0xa
  end if
}    
Post 06 Feb 2005, 12:41
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 06 Feb 2005, 16:37
Why unelegant? It's pretty nice example of adapting fasm's syntax for your needs. Even though those needs are inconsequent.
Post 06 Feb 2005, 16:37
View user's profile Send private message Visit poster's website Reply with quote
Endre



Joined: 29 Dec 2003
Posts: 215
Location: Budapest, Hungary
Endre 06 Feb 2005, 17:20
Privalov wrote:
Even though those needs are inconsequent.

My needs were never consequent Smile It's enough to me keeping thoughts consequent. Sometimes even unsuccessfuly...
Post 06 Feb 2005, 17:20
View user's profile Send private message Reply with quote
beppe85



Joined: 23 Oct 2004
Posts: 181
beppe85 06 Feb 2005, 20:14
Endre wrote:
Yes, that's why I use the next macro, which does I expect from built in org to do.

How would your bootsector fit in a 512B sector, if it contains at beginning 0x7c00 zeros? A BIOS would contain almost 64KB of unneeded stuff...

And your macro shows clearly that this org as you think, is totally redundant.

_________________
"I assemble, therefore I am"

If you got some spare time, visit my blog: http://www.beppe.theblog.com.br/ and sign my guestmap
Post 06 Feb 2005, 20:14
View user's profile Send private message Reply with quote
Endre



Joined: 29 Dec 2003
Posts: 215
Location: Budapest, Hungary
Endre 07 Feb 2005, 09:58
beppe85 wrote:
How would your bootsector fit in a 512B sector, if it contains at beginning 0x7c00 zeros? A BIOS would contain almost 64KB of unneeded stuff...

And your macro shows clearly that this org as you think, is totally redundant.

Binary format starts from zero that is the condition $ = 0 is true so the next line, the built in org directive is achieved. When you use org again (i.e. $ >= 0) then the second condition is fulfilled and the 'rb addr - $' is carried out. Ok?
Post 07 Feb 2005, 09:58
View user's profile Send private message Reply with quote
beppe85



Joined: 23 Oct 2004
Posts: 181
beppe85 07 Feb 2005, 10:20
Endre wrote:
Binary format starts from zero that is the condition $ = 0 is true so the next line, the built in org directive is achieved. When you use org again (i.e. $ >= 0) then the second condition is fulfilled and the 'rb addr - $' is carried out. Ok?

Ok. But redundant if were to be the original meaning of org, and subtracting a feature of FASM. Ok?

I'm not questioning your macro, but that as the default way. How could you get "org $ - some_unsigned_immediate_value"? At least, the first condition should be "$ = 0 or addr <= $", but if you try to go back and forth, you may cause a chaos. And you think going back is an error...

_________________
"I assemble, therefore I am"

If you got some spare time, visit my blog: http://www.beppe.theblog.com.br/ and sign my guestmap
Post 07 Feb 2005, 10:20
View user's profile Send private message Reply with quote
Endre



Joined: 29 Dec 2003
Posts: 215
Location: Budapest, Hungary
Endre 07 Feb 2005, 10:45
You ain't an assembly practitioner, are you?
Post 07 Feb 2005, 10:45
View user's profile Send private message Reply with quote
beppe85



Joined: 23 Oct 2004
Posts: 181
beppe85 07 Feb 2005, 11:12
No. Not as you.
Post 07 Feb 2005, 11:12
View user's profile Send private message Reply with quote
Octavio



Joined: 21 Jun 2003
Posts: 366
Location: Spain
Octavio 08 Feb 2005, 18:19
Code:
org 7c00h
;boot code
align 7c00h+510 
dw     0xaa55 
    


I think that newer versions of fasm only acept powers of two as align parameters ,but it shoud work with the align macro used on the firsts versions.
Post 08 Feb 2005, 18:19
View user's profile Send private message Visit poster's website Reply with quote
Endre



Joined: 29 Dec 2003
Posts: 215
Location: Budapest, Hungary
Endre 09 Feb 2005, 06:58
My path was the org directive and yours is the align one. All the same. I guess other people would use fully other solutions. The only argument I chose org is, that I had formerly used AS (gnu assembler) which handles org directive as I expected fasm to do it the same way, the way I tried to emulate with my macro.
Post 09 Feb 2005, 06:58
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.