PocketMatrix forums

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

Workaround for missing __asm keyword...

Hello ARM asm gurus :-)

I would like to port C/Invoke project to cl/arm architecture(then use it with Lua as ctypes in Python), so I'm looking for some kind of workaround for missing __asm keyword in cl/arm compiler. The workaround seems to be using __emit hackery - I don't have any other idea how to do it with this incomplete compiler anyway...

So, I just wanted to know if it's possible with __emit so as a proof I'm trying to port 'ARCH_SAVE_REGPARMS' macro(sample code taken from cl/x86 arch):

Code:

#define ARCH_SAVE_REGPARMS(regparms) \
__asm mov [(regparms).first], ecx \
__asm mov [(regparms).second], edx


but for cl/arm I will have regparams struct which will save all four r0-r3 registers, something like:

Code:

typedef struct _ArchRegParms {
int first; /* r0 */
int second; /* r1 */
int third; /* r2 */
int fourth; /* r3 */
} regparams;


arm_macros.asm
Code:

AREA arm_macros, CODE, READONLY
EXPORT arm_save_registers

arm_save_registers proc
mov r12, lr

str r4, [r0]
str r5, [r1]
str r6, [r2]
str r7, [r3]

mov pc, r12
endp

END


test.c
Code:

#include <windows.h>
#include <stdio.h>
#include <armintr.h>

void arm_save_registers(int *r0, int *r1, int *r2, int *r3);

int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
wchar_t *buf[MAX_PATH+1];
int r0=0x72307230; /* r0r0 */
int r1=0x72317231;
int r2=0x72327232;
int r3=0x72337233;

__emit(0xE28DD064); //add sp, sp, #0x64
__emit(0xE88D00F0); //stmia sp, {r4-r7}
__emit(0xE24DD064); //sub sp, sp, #0x64
__emit(0xE1A04000); //mov r4, r0
__emit(0xE1A05001); //mov r5, r1
__emit(0xE1A06002); //mov r6, r2
__emit(0xE1A07003); //mov r7, r3
arm_save_registers(&r0, &r1, &r2, &r3);
__emit(0xE28DD074); //add sp, sp, #0x74 (0x74 = 0x64 + (4 * 4 = 0x10))
__emit(0xE91D00F0); //ldmdb sp, {r4-r7}
__emit(0xE24DD074); //sub sp, sp, #0x74

_snwprintf(buf, MAX_PATH, L"r0: 0x%x r1: 0x%x\nr2: 0x%x r3: 0x%x\n", r0, r1, r2, r3);
MessageBox(GetForegroundWindow(), buf, L"Registers", MB_OK);

return 0;
}}


The concept of 'ARCH_SAVE_REGPARMS' macro in test.c seems to work as is - I'm getting right values for r0-r3, but it's my first asm code for ARM so I would like to get some feedback/optimizations/suggestions first before I move forward. Thanks :-)

-- ynezz
I'm not sure exactly what you're trying to achieve here, but there are some problems that you need to fix if this really is the desired "solution".

The problems:


  1. You're adding to the stack pointer, when the stack is full decrementing...

  2. You can use stmia in your macro rather than the 4 strs

  3. You don't need to put the r14 into r12 either: stmia r0,{r4-r7}
    mov pc,r14

  4. "Guessing" that the compiler is using a register for a variable is just that... guessing.


Perhaps you could explain again what you're trying to achieve (and why)?[/list]
refractor wrote:

  1. You're adding to the stack pointer, when the stack is full decrementing...

  2. You can use stmia in your macro rather than the 4 strs

  3. You don't need to put the r14 into r12 either: stmia r0,{r4-r7}
    mov pc,r14

  4. "Guessing" that the compiler is using a register for a variable is just that... guessing.





For example, I want to port macros bellow. Since there is no __asm keyword in cl compiler for ARM CPU I'm looking for some kind of workaround for it.

Code:

typedef struct _ArchRegParms {
int first; /* r0 */
int second; /* r1 */
int third; /* r2 */
int fourth; /* r3 */
} regparams;

#define ARCH_SAVE_REGPARMS(regparms) \
__asm mov [(regparms).first], r0 \
__asm mov [(regparms).second], r1 \
__asm mov [(regparms).third], r2 \
__asm mov [(regparms).fourth], r3

#define ARCH_LOAD_REGPARMS(regparms) \
__asm mov r0, [(regparms).first], \
__asm mov r1, [(regparms).second] \
__asm mov r2, [(regparms).third] \
__asm mov r3, [(regparms).fourth]

#define ARCH_PUT_STACK_BYTES(bcount) \
__asm sub sp, sp, bcount;

#define ARCH_REMOVE_STACK_BYTES(bcount) \
__asm add sp, sp, bcount;

#define ARCH_GET_STACK(stackp) \
__asm mov stackp, sp;

#define ARCH_GET_FRAME_PTR(framep) \
__asm mov framep, r11;
Yes, arguments are passed in r0-r3, and then on the stack if there are more. That's normal. How long they're there for, and the registers assigned for variabled is another matter. Imagine that, as in your case, the arguments are not actually used: the compiler may well just decide to use the register for something totally different.

The other way to do it would be to avoid emit and just write functions in assembler to do what you want instead. It should be less "voodoo" than "emit", if less optimal. I just think that you're going to end up having pain sooner or later with emit....
refractor wrote:Imagine that, as in your case, the arguments are not actually used: the compiler may well just decide to use the register for something totally different.


In my case
Code:
void arm_save_registers(int *r0, int *r1, int *r2, int *r3);
I think, that I should always get args in r0-r3 passed and they're used as pointer to integer, right? Since it's external to C( in ASM), C compiler shouldn't optimize it out - just add prolog and epilog code and include already compiled code from armasm I think.

refractor wrote:
The other way to do it would be to avoid emit and just write functions in assembler to do what you want instead.


Yea, it might end like this :-)

refractor wrote:
It should be less "voodoo" than "emit", if less optimal. I just think that you're going to end up having pain sooner or later with emit....


I call it already emit pain :-) Thank you for your help!
ynezz wrote:
In my case
Code:
void arm_save_registers(int *r0, int *r1, int *r2, int *r3);
I think, that I should always get args in r0-r3 passed and they're used as pointer to integer, right? Since it's external to C( in ASM), C compiler shouldn't optimize it out - just add prolog and epilog code and include already compiled code from armasm I think.

Yes, but I meant the main function as the example. Argument passing itself won't be optimised out, but you'd need to ensure that your code was called before anything else was touched.

ynezz wrote:
I call it already emit pain :-) Thank you for your help!

No problem. :)