Page 1 of 1

ASM: backwards positioning

Posted: Fri Aug 05, 2005 12:28 am
by Solar
Now it's getting juicy. ;)

I'm writing a bootsector, let's say for "educational" purposes. The following works (GAS syntax warning):

Code: Select all

.set PARTITION_TABLE_OFFSET, 0x1be

    ...some code here...

. = PARTITION_TABLE_OFFSET

_P1_Boot_Indicator: .byte 0
_P1_Start_Head:     .byte 0
...
The idea is to merge the binary with an existing MBR, i.e. overwrite 0x1be - 0x1ff with the values of the MBR. Benefit is that I can conveniently address partition table entries in my code without having to juggle magic numbers and offsets.

So far, so good.

However, for aesthetic reasons (think hexdump), I would like my data not to be directly after the code, but rather directly in front of the partition table. So, after the last code line, I'd like to skip:

Code: Select all

.set PARTITION_TABLE_OFFSET, 0x1be

    ...some code here...

. = PARTITION_TABLE_OFFSET - ( PARTITION_TABLE_OFFSET - DATA_START )

DATA_START:

MESSAGE:      .asciz "Ready\n\r"
PANIC_MSG:    .asciz "PANIC\n\r"
LBA_OVERRIDE: .byte 0
BOOTDEVICE:   .byte 0xff

. = PARTITION_TABLE_OFFSET

_P1_Boot_Indicator: .byte 0
_P1_Start_Head:     .byte 0
...
However, that doesn't work since it's somewhat recursive logic. ;)

I know how I could make a workaround, but I'd like to know (since the purpose is educational anyhow): Is there a way to do such "backwards" positioning at all? Can I make my assembler to place some data in such a way that it ends at a known address?

Help appreciated.

Re:ASM: backwards positioning

Posted: Fri Aug 05, 2005 4:11 am
by fdarkangel
i suggest you to modify your linkscript rather playing around with the current position of assembler, either with an .org directive or ".=0x...".
you can simply place your data in a section such as ".data" and your boot code to something like ".init" and tell where to place them in linkscript.

Re:ASM: backwards positioning

Posted: Fri Aug 05, 2005 4:55 am
by AR
It's a bootsector... As such it would not be appropriate to have a .data section.

I personally don't believe it is possible (or at least will be unusually difficult and/or complicated), it would just be easier to know how many words are needed for the data section and manually subtract from the MBR start. I would be interested to know how if you find out though.

Re:ASM: backwards positioning

Posted: Fri Aug 05, 2005 5:37 am
by Solar
I feared so. Well, I'll just hardcode the locations then (i.e., recalculating DATA_START on every change to the "data section") - I need a map to the data area anyway, for the installation routine to install the user-defined parameters in the appropriate places.