flat assembler
Message board for the users of flat assembler.

Index > Main > 16 bit data vs. 32 bit address

Author
Thread Post new topic Reply to topic
bluecode



Joined: 29 Sep 2005
Posts: 4
bluecode
hi,

I've got a problem with fasm Crying or Very sad
I'm using 32bit code and I got a label some where (lets name it _label). Now I want to save the address of that label like this (in a 16bit variable):
dw _label
Why isn't that working: Fasm spits out an error ("error: invalid use of symbol.")? How can I fix that? Can I use something like:
sw _label >> 16
when I want to get the upper 16 bits of the address?
I'm really sorry if that's already explained in the manual, but I just couldn't find it.
Post 29 Sep 2005, 14:33
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2141
Location: Estonia
Madis731
hi,

I've got a problem with fasm Crying or Very sad
I'm using 32bit code and I got a label some where (lets name it _label). Now I want to save the address of that label like this (in a 16bit variable):
dw _label
Why isn't that working: Fasm spits out an error ("error: invalid use of symbol.")? How can I fix that? Can I use something like:
sw _label >> 16
when I want to get the upper 16 bits of the address?
I'm really sorry if that's already explained in the manual, but I just couldn't find it.

_label >> 16 looks like this in FASM:
Code:
_label shr 16 ;pronounced SHift Right
    


The addresses in 32-bit are always 32-bit too so taking 16 bits from it doesn't make sense. But if you'd really like to do that then:
Code:
  ;some code
_label:
  ;do some stuff
mov eax,_label ;or you could do: lea eax,[_label]
shr eax,16 ;16 upper bits of eax are now in ax and you can access it like this:
mov word[data_area],ax
;-OR-
cmp ax,word[binary_data]
;etc.
    


Hope it helped...
Post 29 Sep 2005, 14:44
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
bluecode



Joined: 29 Sep 2005
Posts: 4
bluecode
Quote:
The addresses in 32-bit are always 32-bit too so taking 16 bits from it doesn't make sense

That does make sense, eg. for a descriptor in the gdt you got two different 16bit fields that take one half of a 32bit address. That's a deisgn flaw in the i386 I know.
Post 29 Sep 2005, 19:13
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
Location: In your JS exploiting you and your system
revolution
Actually it depends on your output format. If you use binary format then what you have above will work fine if _label is < 64K, if you use PE format (or any relocatable format) then you can't use a symbol like that because FASM can't make a 16 bit relocatable value for a 32 bit address.
Post 30 Sep 2005, 00:54
View user's profile Send private message Visit poster's website Reply with quote
bluecode



Joined: 29 Sep 2005
Posts: 4
bluecode
I'm using elf. But why can (imho) nasm then do that?
As I already said, I need the address split up in two 16Bit parts (for gdt/idt entries), so it should work for _label > 2^16!
Is it just a missing feature in fasm?
Post 30 Sep 2005, 11:22
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7724
Location: Kraków, Poland
Tomasz Grysztar
The "invalid use of symbol" means you are using some relocatable format, perhaps object file. Neither ELF nor COFF has any relocation type that would allow splitting 32-bit address into two separate values. I see two possible reasons for your problem: either you are writing some code with absolute addresses in mind, but are using relocatable format - in such case either use some format that doesn't have relocations (like binary), or use "org" to change addressing to absolute (but make sure you know what you're doing). Second possible case is that you're using relocatable (object) format because you need your code to be relocatable/linkable - in such case you have to split the 32-bit address into parts at the run-time, because only at run-time the whole 32-bit address (after being relocated) will be known.
Post 30 Sep 2005, 11:38
View user's profile Send private message Visit poster's website Reply with quote
bluecode



Joined: 29 Sep 2005
Posts: 4
bluecode
hi,

if I use org, is it also possible to switch back to relocateable address, that means only turning on absolute addressing for some labels?

Thanks for your replies!
Post 30 Sep 2005, 12:43
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7724
Location: Kraków, Poland
Tomasz Grysztar
You can do something like:
Code:
macro startorg addr
 { org_delta = $ - addr
   org addr }

macro endorg
 { org $ + org_delta }

startorg 1000h
 ; code/data that will be placed at absolute address 1000h
endorg    
Post 30 Sep 2005, 12:59
View user's profile Send private message Visit poster's website 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.