PocketMatrix forums

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

I can not get the expected result.

c code:
Code:

#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
//#include "test1.h"
#define DECLARE_ALIGNED_MATRIX(name,sizex,type,alignment) \
__declspec(align(alignment)) type name[(sizex)]

extern "C" {
extern int foo(int a[] ,int aa,int bb,int cc,int dd,int ee,int ff, int gg);
}


int _tmain(int argc, _TCHAR* argv[])
{
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
int f = 6;
int g = 7;


DECLARE_ALIGNED_MATRIX(kk,16,int ,64);


int i;

for(i=0;i<16;i++)
{
kk[i] = 0;
}

foo(kk,a,b,c,d,e,f,g);



printf("%d\n" , kk[0]);
printf("%d\n" , kk[1]);
printf("%d\n" , kk[2]);
printf("%d\n" , kk[3]);



printf("%d\n" , kk[4]);
printf("%d\n" , kk[5]);
printf("%d\n" , kk[6]);
printf("%d\n" , kk[7]);


printf("%d\n" , kk[8]);
printf("%d\n" , kk[9]);
printf("%d\n" , kk[10]);
printf("%d\n" , kk[11]);


return 0;
}


assemble code

mov R12,SP

STMDB SP!,{R4-R7}

STR R1, [R0] ,#4
STR R2, [R0] ,#4
STR R3, [R0] ,#4

mov pc,lr

END

expected : 1 2 3 0 0 0 0 0 0 0 0 0 0 0 0
got : 69632 536870943 0 0 1 2 3 0 0 0 0 0 0 0 0 0
change "STMDB SP!,{R4-R7}" into "STMDB SP,{R4-R7}" I can got expected result.


what is the problem? Does "STMDB SP!,{R4-R7}" affect r1 ?
You're corrupting the stack.

STMDB SP!,{R4-R7}

Stores r4,r5,r6,r7 onto the stack (full, descending), and moves r13(sp) by -16 accordingly. You never move it back, hence it is pointing at the wrong location when it is passed back into the C code. I'm quite surprised it didn't crash.

Without the ! (writeback) you're stacking r4,r5,r6,r7 but not moving the stack pointer and hence not corrupting it.

With what you have so far, don't touch the stack at all.

Code:
STR R1, [R0] ,#4
STR R2, [R0] ,#4
STR R3, [R0] ,#4

mov pc,lr


Will do fine and you'll get the results you expect. You only need to load from the stack when you want to access the d,e,f,g variables.

(If you want to be "fancy" you can also combine the above stores to:

stmia r0,{r1-r3}

)
Yes, It is a test code.

I want to pass more than 4 parameters to assemble code, so I need to pass them by stack. And I want to use R4-R8 , so I should protect save them before I change its value , and I push them into stack with code below:

mov R12,SP
STMDB SP!,{R4-R7}
STR R1, [R0] ,#4
STR R2, [R0] ,#4
STR R3, [R0] ,#4
mov pc,lr
END

what can I do? Thanks in advance!

Bye the way, it will corrupt when I stmdb more register and write back to sp. Such as
STMDB SP!,{R4-R11}
Try this:

Code:
// first we store the stack in r12 and stack
// r4-r7 as we're going to corrupt them
mov r12,sp
stmfd sp!,{r4-r7, r14}

// store a,b,c into kk (and move r0)
stmia r0!,{r1-r3}

// load d,e,f,g from the (original) stack pointer
ldmia r12,{r4-r7}

stmia r0!,{r4-r7}

ldmfd sp!,{r4-r7, pc}


The key point is that you must leave the stack pointer exactly as you found it... generally that means that if you store onto the stack at the start, you load from the stack at the end.
thanks a lot. I have got it!