News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Convert x86 to x64

Started by yq8, May 10, 2015, 12:11:31 AM

Previous topic - Next topic

yq8

Hey Folks,

I've already asked a few people on the fasm forum about this issue, but they couldnt figure out my problem either, so I think
I will ask here, maybe you guys have some ideas :)
What I am trying to do is to convert some x86 asm code which performs an addition into x64 code.
So this is the x86 code which works perfectly fine :

push ebp
mov ebp, esp
mov eax, [ebp+0x0C]
mov ecx, [ebp+0x8]
add eax, ecx
pop ebp
ret 0x8


My attempt to convert it to x64-bit-ready-code was this:

push rbp
mov rbp, rsp
xor rax, rax
xor rcx, rcx
mov rax, qword[rbp+0x10]
mov rcx, qword[rbp+0x18]
add rax, rcx
pop rbp
ret 0x10


But ow the addition returns completly wrong results, aka 100+5=485219888  :icon_eek:
Can somebody give me some advice what I am doing wrong? ;o

MichaelW

#1
Assuming that this code is in a procedure, you need to adjust for the change in calling convention.

https://msdn.microsoft.com/en-us/library/ms235286.aspx

The attachment contains the source files and exe for a demo done in Pelles C and POASM.

Edit: Corrected some minor problems with the attachment.
Well Microsoft, here's another nice mess you've gotten us into.

dedndave

no need to zero RAX and RCX if you are going to fill them with something else   :P

Mikl__

#3
Hi, yq8!
in x86push ebp
mov ebp, esp
mov eax, [ebp+0x0C]
mov ecx, [ebp+0x8]
add eax, ecx
pop ebp
ret 0x8
in x64lea rax,[rdx+rcx]
ret

rrr314159

Hi yq8,

MichaelW is probably right that your caller will pass arguments differently in 64-bit (namely, they'll be QWORDs); but just considering your problem as stated, you need to get the arguments off the stack as DWORDs not QWORDs. Thus translate these two:

mov eax, [ebp+0x0C]
mov ecx, [ebp+0x8]


to

movsx rax, DWORD PTR [ebp+0x0C]
movsx rcx, DWORD PTR [ebp+0x8]


If you happen to know there are no negative numbers involved, you can simply leave those two statements as they are
I am NaN ;)