flat assembler
Message board for the users of flat assembler.

Index > Main > 32 bits pointers in 64 bits mode

Author
Thread Post new topic Reply to topic
pabloreda



Joined: 24 Jan 2007
Posts: 116
Location: Argentina
pabloreda 08 May 2012, 00:23
Hi

I need a 32 bits pointers to code and to data in 64 bits mode for a compiler.

I read
http://www.codeproject.com/Articles/28818/32-bit-pointers-in-a-64-bit-world

but perhaps someone here have a better idea.

I need 32 bits adress because with this I can compile the same code without modification.

thanks
Pablo
Post 08 May 2012, 00:23
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: 20360
Location: In your JS exploiting you and your system
revolution 08 May 2012, 00:38
This works fine in assembly:
Code:
use64
mov rax,[esi]    
But this is no guarantee that it will work as expected. Your 64-bit OS may allocate memory above 4GB and 32-bit pointers could never access it.
Post 08 May 2012, 00:38
View user's profile Send private message Visit poster's website Reply with quote
pabloreda



Joined: 24 Jan 2007
Posts: 116
Location: Argentina
pabloreda 10 May 2012, 14:29
thanks revolution,

my english is bad bad, i try to explain more.

In my compiler I have pointer to code and pointer to data.
then

Code:
#var 23    


I traslate to

Code:
var dd 23    


define a variable with the value 23

Code:
#var2 'var    


I traslate to

Code:
var2 dd var    


and with the address to code are the same history

Code:
#codes 'tocode    


Code:
codes dd tocode    


well, I need keep the size of pointers in 32bits because I like to compile the same code in 32 bits and 64 bits. because, for example, I add 4 to address for get the next value, and if I change this size need change the pointers aritmethics in the program.

I read about RIP relative but a address to code in a variable can be called from many places.

I hope this explanation are better.

thanks again
Pablo
Post 10 May 2012, 14:29
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: 20360
Location: In your JS exploiting you and your system
revolution 10 May 2012, 14:43
Do you mean this?
Code:
align 4
var dd 12345
var2 dd var/4
mov esi,[var2] ;var2 contains a 1/4th pointer to var. rsi is zero extended from esi
mov eax,[rsi*4] ;get the value of var    
But that code cannot compile in 32-bit mode due to the address requirements of 34 addressing bits.
Post 10 May 2012, 14:43
View user's profile Send private message Visit poster's website Reply with quote
pabloreda



Joined: 24 Jan 2007
Posts: 116
Location: Argentina
pabloreda 10 May 2012, 20:22
I have this

Code:
use32

vectorlist dd rut1,rut2

rut1:
mov eax,1
ret
rut2:
mov eax,2
ret

main:
mov ebx,vectorlist
mov eax,[ebx+4]
call eax
ret
    


I need this

Code:
use64
 
vectorlist dd rut1,rut2  ; qd ???

rut1:
mov rax,1
ret
rut2:
mov rax,2
ret

main:
mov rbx,vectorlist
mov rax,[rbx+4] ; +8 ??
call rax
ret

    


this work ?

Code:
use64
 
vectorlist dd rut1-inicode,rut2-inicode 

inicode:

rut1:
mov rax,1
ret
rut2:
mov rax,2
ret

main:
mov rbx,vectorlist
movsx rax,dword [rbx+4] 
add rax,inicode
call rax
ret

    
Post 10 May 2012, 20:22
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 10 May 2012, 20:50
Don't use sign extension, the upper 2GB of the 32-bit address space would map to kernel memory that way.
Code:
use64
 
vectorlist dd rut1-inicode,rut2-inicode 

inicode:

rut1:
mov eax,1 ; Instead of rax since the CPU will clear the upper 32-bits automatically
ret
rut2:
mov eax,2 ; Instead of rax since the CPU will clear the upper 32-bits automatically
ret

main:
mov eax, [vectorlist+4]
add rax, inicode
call rax
ret    


BTW, have you considered the possibility of forcing memory allocation to be always below 4G? That would require very little effort to implement 64-bit addressing arithmetic with 32-bit pointers (but this might not be a viable option for you)
Post 10 May 2012, 20:50
View user's profile Send private message Reply with quote
pabloreda



Joined: 24 Jan 2007
Posts: 116
Location: Argentina
pabloreda 10 May 2012, 21:02
thank for the tips Loco

more easy for my to talk (sorry english readers)

Genero el codigo para compilar en FASM
puedo decirle que el .exe se cargar en memoria abajo de 4G?
esa decision no la toma el SO?

No utilizo asignacion de Memoria ni garbage collector. asi que creo que como decis, no es viable esa opcion.

hay alguna posibilidad de utilizar los segmentos en 64 bits ? ya que con esto lo solucionaria..
Post 10 May 2012, 21:02
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 10 May 2012, 21:48
I'll have to reply in English sorry Razz (PM me if you don't understand the answer)

So the first two questions are whether you can tell the OS to load the executable below 4G or not. If you avoid relocations (something that is the default in fasm for executable formats), the OS will be forced to load everything at the same location as indicated in compile-time (but not the DLLs/shared libs you use, those can still be loaded anywhere). I believe that with "ELF64 executable" this is also the case. On the other hand, if you need your executables relocatable for ASLR compatibility (or you even need to create DLLs/shared libs), then you have to stick with the more laborious pointer arithmetic.

The third question is if there is a chance of using segmentation in 64-bit. I'll let wikipedia answer that for me.
Post 10 May 2012, 21:48
View user's profile Send private message Reply with quote
pabloreda



Joined: 24 Jan 2007
Posts: 116
Location: Argentina
pabloreda 10 May 2012, 22:12
thank a lot!!
Post 10 May 2012, 22:12
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.