Author Topic: Convert x86 to x64  (Read 6706 times)

yq8

  • Regular Member
  • *
  • Posts: 18
  • Asm Beginner
Convert x86 to x64
« 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 :

Code: [Select]
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:

Code: [Select]
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

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
Re: Convert x86 to x64
« Reply #1 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.
« Last Edit: May 10, 2015, 03:27:48 AM by MichaelW »
Well Microsoft, here’s another nice mess you’ve gotten us into.

dedndave

  • Member
  • *****
  • Posts: 8827
  • Still using Abacus 2.0
    • DednDave
Re: Convert x86 to x64
« Reply #2 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

Mikl__

  • Member
  • *****
  • Posts: 1008
Re: Convert x86 to x64
« Reply #3 on: September 10, 2015, 04:12:07 PM »
Hi, yq8!
in x86
Code: [Select]
push ebp
mov ebp, esp
mov eax, [ebp+0x0C]
mov ecx, [ebp+0x8]
add eax, ecx
pop ebp
ret 0x8
in x64
Code: [Select]
lea rax,[rdx+rcx]
ret
« Last Edit: September 11, 2015, 12:02:05 PM by Mikl__ »

rrr314159

  • Member
  • *****
  • Posts: 1382
Re: Convert x86 to x64
« Reply #4 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:

Code: [Select]
mov eax, [ebp+0x0C]
mov ecx, [ebp+0x8]

to

Code: [Select]
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 ;)