flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Macro to define REG as pointer [x64]

Author
Thread Post new topic Reply to topic
alorent



Joined: 05 Dec 2005
Posts: 201
alorent
Hi guys,

We mostly are 32-bit ASM coders more than 64-bit ASM coders (at least for now Smile)

I have realized that I (probably others) can include errors when coding in x64 ASM. Example:

Code:
lea   rdi, [MyString]
...
...
...
inc   edi                    ; increment pointer to next char    


As you can see, we have included an error because it should be "inc RDI" instead of "EDI" (we are use to use EDI from years). When we run it, it works fine when "MyString" lays on a 32-bit address, but if "MyString" is located in higher space in another machine, it will crash there.

Would it be possible to create a macro where we can declare a register as "pointer"? Something like:

Code:
ASSUME_REG RDI: POINTER 

lea   rdi, [MyString]
...
...
...
inc   edi                    ; increment pointer to next char

ASSUME_REG RDI: NOTHING    


So, if we use EDI inside that context, it will produce an assembler error.

Any other ideas, solutions are welcome Wink

Thanks!
Post 03 Oct 2010, 06:42
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
Code:
edi equ I_am_an_error

lea   rdi, [MyString]
...
...
...
inc   edi

restore edi    
Post 03 Oct 2010, 06:47
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4237
Location: 2018
edfed
Code:
use64
edi equ rdi
...
restore edi
use32
rdi equ edi
...
restore rdi
    



but i just propose to avoid the errors by coding with care.

like 16 bit and 32 bit regiters can be mixed, 64 and 32 can be too.

the use of a 32 bit register as index in 64 bit can be used to limit the adress space. for example, if a 32 bit VM is running, and the 64 bit OS is filling it's adress space in 64 bit mode, then, it will need to do that with EDI instead of RDI, to wrap around the 4G adress space.
Post 03 Oct 2010, 13:08
View user's profile Send private message Visit poster's website Reply with quote
alorent



Joined: 05 Dec 2005
Posts: 201
alorent
Thanks guys!

revolution, I'd like to use your solution but defining a macro like "assume_reg rdi:pointer"

I have been trying to re-use the already defined "assume" macro, but I'm unable to make it work with my scarce knowledge about FASM macros.

Any help in defining the "assume_reg" macro?

Thanks in advance!
Post 03 Oct 2010, 17:32
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
Code:
macro assume_reg [stuff] {
 common
 match =RDI=:=POINTER,stuff \{
  edi equ dont_use_me
 \}
 match =RDI=:=NOTHING,stuff \{
  restore edi
 \}
}    
But I don't see any advantage to using a macro when the simple solution is just a single line "equ".
Post 03 Oct 2010, 21:21
View user's profile Send private message Visit poster's website Reply with quote
alorent



Joined: 05 Dec 2005
Posts: 201
alorent
Thanks a lot revolution! You always here to help! Wink
Post 04 Oct 2010, 10:24
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2915
Location: [RSP+8*5]
bitRAKE
I like edfed's type of solution because many algorithms only require slight changes to work in both 32-bit and 64-bit. I often rename registers with VIRTUAL, to keep the name local to a procedure (avoid the need for restore). Unfortunately, this only works for indirect access.
Code:
virtual at rbp
        .hWnd rq 1
end virtual

mov [.hWnd],rcx  ; kind of cool
mov rcx,.hWnd    ; ERROR    
IIRC, there is a 2GB limit in the PE file specification. So, loading your program as low as possible in memory can insure only 32-bit addresses are needed. Of course, HANDLEs and pointers from external processes must support the 64-bit size.
Code:
HINSTANCE = $10000
format PE64 GUI 5.0 at HINSTANCE    
...and any dynamically allocated stuff, or the stack pointer could require 64-bits. It's fun getting accustomed to the new environment.
Post 05 Oct 2010, 05:38
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.

Powered by rwasa.