The MASM Forum

64 bit assembler => 64 bit assembler. Conceptual Issues => Topic started by: yq8 on May 10, 2015, 12:11:31 AM

Title: Convert x86 to x64
Post by: yq8 on May 10, 2015, 12:11:31 AM
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
Title: Re: Convert x86 to x64
Post by: MichaelW on May 10, 2015, 01:46:13 AM
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.
Title: Re: Convert x86 to x64
Post by: dedndave on May 10, 2015, 01:52:17 AM
no need to zero RAX and RCX if you are going to fill them with something else   :P
Title: Re: Convert x86 to x64
Post by: Mikl__ on September 10, 2015, 04:12:07 PM
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
Title: Re: Convert x86 to x64
Post by: rrr314159 on September 11, 2015, 02:09:06 AM
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