PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Porting x86 asm to ARM questions

hi, i want to port a function from x86 to arm for a PocketPC2003 app. It's a "simple" asm chunk. For this tasks I found myself reading the APCS, instruction set reference cards, arm macros, studying the stack frame layout, parameter passing, etc. too long for this task, so I ask for help.
I don't understand how to translate alloca() and the move data (arguments) from memory to memory in the block of code below. I've seen this related post http://www.pocketmatrix.com/forums/view ... hp?t=15487
How is the stack data aligned? (sp must point to 8-bytes boundaries?) and it is necessary to ensure the 8-byte alignment, so i must use AREA+ALIGN ? (i'm using ms evc++) First the x86 chunk
Code:

/* Max stack size */
#define FFI_MAX_ARGS 128
/* The stack used to assemble the arguments for a call */
static int dataArgs[FFI_MAX_ARGS];
/* The stack pointer while filling the stack */
static int argIndex = 0;

volatile static int intReturnValue;
volatile static int intReturnValue2;
volatile static double floatReturnValue;
....
int callAddress(int fn)
__asm {
push ebx
mov ebx, fn
push ecx
push edx
push edi
push esi
push ebp
/* mark the frame */
mov ebp, esp
/* alloca() stackIndex size bytes */
mov ecx, argIndex
shl ecx, 2
sub esp, ecx
/* copy stack */
mov edi, esp
lea esi, dataArgs
shr ecx, 2
cld
rep movsd
/* go calling */
call ebx
/* restore frame */
mov esp, ebp
/* store the return values */
mov intReturnValue, eax
mov intReturnValue2, edx
fstp floatReturnValue
/* restore register values */
pop ebp
pop esi
pop edi
pop edx
pop ecx
pop ebx
}

So, I make this try.
Code:

int callAddress(int fn) {
/* already know that evc doesn't allow inline asm,
just here for figure out fn purpose */
__asm {
mov ??
mov ??, fn
stmfd sp!, {r0-r3,lr} /* or Pre-incremental load stmed ? */
/* mark the frame */
mov r14,r13
/* alloca() stackIndex size bytes
.... see msdn comment below .... */
/* go calling */
bl ??
/* restore frame */
mov r13, r14
/* store the return values
check parameter passing and restore values doc */
mov ??, intReturnValue
mov r1, intReturnValue2
/* fstp floatReturnValue I must convert this float to fixed point ? */
/* restore register values */
ldmfd sp!, {r0-r3, lr}
}
...
alloca() asm version
mov argIndex, r0
/* it's necessary to do this sub and shifts ? */
sub r0, r0, r13, lsl#2
/* copy stack
study the stack frame layout :)
??
...
ldr r2, dataArgs
...
*/



* If a routine has alloca() locals, a frame pointer register (R11 for ARM or R7 for Thumb) is required to access the incoming arguments and locals. The routine must not modify the frame pointer register between the prolog and epilog

* In a routine that uses alloca(), everything in the frame below (at a lower address than) the alloca() area is referenced relative to R13 and never contains a defined value at the time of an alloca() call. Thus, the alloca() operation never has to copy this part of the stack frame, and no data relocation problems arise. Everything in the frame above (at an address higher than) the alloca() area is referenced relative to the frame pointer (R11 for ARM or R7 for Thumb).


Sorry for such big post. Hope someone can help me.
Regards
I don't read x86, but I can do ARM ;)

If you translate your x86 into C or pseudo-code then I'll show you how to do it in ARM (time permitting).
What is argIndex supposed to be when calling this function?
Well from what i can see this code will copy a given amount of memory from the stack (depending in the argIndex parameter) and copy it into the dataArgs buffer.
After that it will call the address specified in fn and
save the return values.
Besides that it will also get the first floating point from the FPU stack and save it into floatReturnValue.
I don't think this code will make much sense on ARM though.
(Accessing global static variables is also bad for caching reasons)
Thank you for your fast replies.
The original developer does this in x86 asm :( so I don't know easily how to write in C. Let me some days maybe there's a way to do it.

xPeterx wrote:Well from what i can see this code will copy a given amount of memory from the stack (depending in the argIndex parameter) and copy it into the dataArgs buffer.
After that it will call the address specified in fn and save the return values.
Besides that it will also get the first floating point from the FPU stack and save it into floatReturnValue.


That's exactly what it does. The function adapts the arguments (which are variable in size) dynamically in the stack. Next the call and finally the result which can be an integer or float, that's why the FSTP instruction. I'm looking for how the stack looks like after the function call (I don't want to use IDA yet :( ). Any ideas?
Best regards
rapunzel wrote:I'm looking for how the stack looks like after the function call (I don't want to use IDA yet :( ). Any ideas?

Maybe Refractor can help you with the stackframe layout .
Well on an ARM the stack address is traditionally held in R13 (certainly so far as APCS is concerned).

The essential part of APCS for this is that the arguments are returned first using R0-R3 and any excess should be on the stack. So, if I call a function that returns one 32-bit integer, it'll be in R0 after the function call. If I call a function that returns a 32-byte structure, then the first 16 bytes are in registers 0 to 3 and there are 4 more 32-bit words on the stack.

When you say:
I'm looking for how the stack looks like after the function call
.. what do you mean exactly?

If you want to know how much has been pushed on the stack by the function as the return (if any), just compare R13 before and after the call. If the value after the call is lower than the value before (APCS uses a full decrementing stack).. then you have return values on the stack (and in r0-r3).