flat assembler
Message board for the users of flat assembler.

Index > Main > uneversal push for 32 and 64 bits

Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 1878
Roman 25 Aug 2020, 19:23
64 bits no push eax\edx\ecx

Fasm get error.

My solution macro push32:
Code:
macro push32 Reg {
if mod64 = 1
mov dword [rsp],Reg
add rsp,4
end if
if mod64 = 0
push Reg
end if

}
    
Post 25 Aug 2020, 19:23
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4154
Location: vpcmpistri
bitRAKE 25 Aug 2020, 19:41
Stacks move backward from high memory to low memory.

Also, since the top dword on the stack is undefined, it's best to PUSH RAX for case of EAX. The method you suggest is needed when wanting to preserve the high dword data, but by definition low stack values are undefined.

This is why I created register alias' - to ease macro creation. Prior to that I was passing strange parameters like: <RAX,EAX>, RAX:EAX, etc. and using MATCH to break out that condition.

Seems like you are going from 32-bit to 64-bit, though. fasm source has some examples of doing that in .\source\linux\x64\modes.inc There are some fasm specific caveats though.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 25 Aug 2020, 19:41
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: 20516
Location: In your JS exploiting you and your system
revolution 26 Aug 2020, 00:48
If you really need to emulate 32-bit push in 64-bit mode then do this:
Code:
lea rsp,[rsp-4]
mov dword [rsp],my_value    
Or this:
Code:
lea rsp,[rsp-4]
mov [rsp],eax ;any 32-bit register    
And now that you know how to do it, don't do it. Your stack will then be unaligned and cause you more problems later trying to fix it.
Post 26 Aug 2020, 00:48
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1878
Roman 26 Aug 2020, 04:11
Thanks.
But I have many 32 bit code with push/pop.
And I won't rewrite all this to 64 bits
Post 26 Aug 2020, 04:11
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20516
Location: In your JS exploiting you and your system
revolution 26 Aug 2020, 04:54
It you accept that 32-bit pushes must be promoted to 64-bit (as bitRAKE suggested) then you can do something like this:
Code:
macro push thing {
        if thing in <eax,edx,ecx,ebx,esp,ebp,esi,edi>
                use32
                push thing
                use64
        else
                push thing
        end if
}

;test it
use64
irp reg, ax,cx,dx,bx,sp,bp,si,di {
        push r#reg
        push e#reg
}
push 0
push 4*5    
It only accepts a single argument for each push, so it isn't as flexible as the native push. Although it could be fixed with some extra work in the macro.
Post 26 Aug 2020, 04:54
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1878
Roman 26 Aug 2020, 06:00
Thanks.
Work in 64 bits
Post 26 Aug 2020, 06:00
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20516
Location: In your JS exploiting you and your system
revolution 26 Aug 2020, 06:32
This might be more useful:
Code:
macro push thing {
        irp reg, ax,cx,dx,bx,sp,bp,si,di \{e\#reg equ r\#reg\}
        push thing
        restore eax,ecx,edx,ebx,esp,ebp,esi,edi
}

;test it
use64
irp reg, ax,cx,dx,bx,sp,bp,si,di {
        push r#reg e#reg
}
push 0 4*5 qword[rax]    
As long as you don't use 32-bit addresses "push qword[eax]".
Post 26 Aug 2020, 06:32
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1878
Roman 26 Aug 2020, 06:40
Nice ! Very nice !
Post 26 Aug 2020, 06:40
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.