Re: Writing Assembly Code in x64



On Jun 2, 6:50 pm, Manesh S <spamt...@xxxxxxxxxx> wrote:
Hi All,

This is Manesh from India. Later on i have been assigned to a porting
task of a 32-bit application(x86) to 64-bit version of Vista(AMD64).
I'm very new to
this & now facing problems with 32-bit Inline assembly code.

1) I have a situation where my 32-bit inline assembly code looks like
this

long int var;
__asm mov var, EAX
table->g[0] = var;
__asm mov var,EBX
table->g[1] = var;
__asm mov var, ECX
table->g[2] = var;

where this table->g[] is something specific to my application.

As far as i know, the only way to get around this is to completely
rewrite this to a .asm file & link it seperately using ml64 and call
it thru a function defined in my source file.

1) Now while moving this to a seperate .asm file, is it possible to
pass these registers values(EAX,EBX) as the parameter to the
function(defined in d source file) and handle it seperately in
the .asm file( since i need to assign the return value of VAR to
table-

g[]).

Ex: in source file:
============

testFunc(var, EAX);

in .asm file:
========
************************************
.code

testFunc PROC x:QWORD, y:SDWORD

cmp y, 'eax'
je FOR_EAX

cmp y, 'ebx'
je FOR_EBX

FOR_EAX:
mov x, rax
RET

FOR_EBX:
mov x, RBX
RET
testFunc ENDP

END ; // This is what i was able to write as a sample. I know this
wont work. But want to confirm whether my approach is right..
*****************************

2) Now if need to move this to a seperate .asm file, whether i should
use the 32-bit register like EAX,ABX or the 64-bit register like RAX,
RBX..

I'm very new to assembly coding & couldn't find any manual on the
Assembly coding in AMD64.

It would have been great if could help me by writing a sample .asm
file with my above scenario. That would help me a lot.

Any reply will be hugely appreciated!!

-Manesh

Hello Manesh,

first of all, keep in mind that ML64 does support PROC directive in
very limited way. See this:

http://www.masm32.com/board/index.php?topic=7932.0

In other words, you can use PROC, but without function arguments. You
must address the arguments directly. First four arguments go in RCX,
RDX, R8, and R9. See this for detailed description:

http://msdn.microsoft.com/en-us/magazine/cc300794.aspx

And your function:

testFunc PROC

cmp rcx, 12345678h ; first parametr goes in RCX (QWORD)
je x_is_equal

cmp edx, -12345678h ; second in EDX (SDWORD)
jl y_is_less

mov rax, -1 ; return value
ret

y_is_less:
mov rax, -12345678h
ret

x_is_equal:
mov rax, 12345678h
ret

testFunc ENDP

As for the manual: Learn 32-bit assembly first, there are lots of
books. All you need to learn 64-bit assembly are different calling
conventions.

.



Relevant Pages