views:

716

answers:

1

I have a small inline assembly code written in my C code. The asm goes through an array and if needed, move values from a different array to a register. In the end, an interrupt is called. The code is similar to this:

cmp arrPointer[2],1h
jne EXIT
mov AX, shortArrPtr[2]
EXIT:
int 3h

This all work in x86 but according to microsoft: x64 doesn't support inline assembly. How can I translate it all to support x64? I couldn't find a compiler intrinsic procedure to execute what I want and I can't figure out how can I pass parameters to an external asm file.

+3  A: 

I think you just hit on the reason why inline assembly is a pain in the ass - it's completely unportable (and not just between architectures; compilers often have different and incompatible syntax). Write an external assembly file and do what you need to that way. Passing parameters to assembly routines is exactly the same as passing them to C functions; just forward declare your function signature somewhere and the calling code (in C) will do the right thing. Then implement the routine in the external assembly file (make sure to obey the calling convention) and export the appropriate symbol to have the linker tie everything up correctly. Presto - working assembly!

An example, as requested. I didn't try to compile or test this in any way, so it might not be 100%. Good luck.

myHeader.h:

void *someOperation(void *parameter1, int parameter2);

myAssemblyFile.s:

.text
.globl someOperation

someOperation:
    add %rdx, %rcx
    mov %rcx, %rax
    ret

.end

mySourceCode.c:

#include "myHeader.h" 

void someFunction(void)
{
  void *arg1 = (void *)0x80001000;
  int  arg2  = 12;
  void *returnValue;

  printf("Calling with %x %x\n", arg1, arg2);

  // call assembly function
  returnValue = someOperation(arg1, arg2);

  printf("Returned: %x\n", returnValue);
}
Carl Norum
I'm not so familiar with assembly yet. Can you give me some code?[If my C function would be: assembleThingy(short shortArr[], char byteArr)]
Eldad
I think that depends almost entirely on your assembler syntax, I'm afraid.
Carl Norum
It does depends on my assembler syntax, but you gave me a good start. Thanks!
Eldad
Good to hear; I hope it works out for you. =)
Carl Norum