inline asm for local variables

Discussion and support of the compiler from ImageCraft for the M8C.

Moderator: richard@imagecraft.com

inline asm for local variables

Postby rosaldorosa » Mon Mar 14, 2011 11:43 pm

I have problems when using local variables in inline assm for C
void Func(void){
BYTE tr[3];
asm(" ad [_tr + 2],1 \n");
}

The above example doesn't work woth linker which reports unkonwn tr
robert
rosaldorosa
The Big Cheese
The Big Cheese
 
Posts: 232
Joined: Sun Feb 21, 2010 10:51 pm

Re: inline asm for local variables

Postby slandrum » Tue Mar 15, 2011 4:52 am

This is not surprising, as local variables will not resolve to an address at compile/link time, unless they are static. I don't know if the compiler will resolve it if it's a static variable or not.

The local variable will either be in registers or on the stack (in the case of an array, almost certainly on the stack), and code to access it will be non-trivial.
slandrum
The Big Cheese
The Big Cheese
 
Posts: 453
Joined: Fri Feb 29, 2008 11:38 am
Location: California

Re: inline asm for local variables

Postby Somerset » Thu Jan 12, 2012 1:42 am

Instead of writing in-line assembly, perhaps you could write your assembly in a seperate, pure assembly file which could be linked during the build. Also, if you're not comfortable with AT&T style assembly, have a look at NASM, the Netwide Assembler. I've used it many times.
Somerset
Newbie
Newbie
 
Posts: 1
Joined: Thu Jan 12, 2012 1:38 am

Re: inline asm for local variables

Postby bobmarlowe » Thu Jan 12, 2012 8:32 am

To access local vars from assembly is quite easy: The vars are allocated on the stack and a stack-frame is built, which means that the originally contents of the stack can be restored.
When you write a function with some local vars, you can have a look into the corresponding .lst-file (more to the end, the first part is the boot-part). As a bonus, the location of a var in the stack is documented and when you access a var in your function (add one and store it) you can see how the compiled accesses them and you can see the usage of the c-internal registers _r0 to _rz which you may use as well.

Bob
User avatar
bobmarlowe
The Big Cheese
The Big Cheese
 
Posts: 1490
Joined: Thu Oct 06, 2011 2:03 am
Location: Germany

Re: inline asm for local variables

Postby Smooth » Thu Jan 12, 2012 10:09 pm

I've tried both, but how much stack is allocated for each local variable/parameter, what's their order, how many padding bytes in between, these are all compiler option dependent, will mess up unless I code my own compiler, so forget about it.
As I've already shown that the AT&T GCC extended syntax is inferior to Intel syntax, there are certain particular sequences of particular instructions which AT&T just cannot handle.
Smooth
Newbie
Newbie
 
Posts: 1
Joined: Thu Jan 12, 2012 10:06 pm

Re: inline asm for local variables

Postby bobmarlowe » Fri Jan 13, 2012 7:33 am

For your PSoC1 you find under Help->Documentation->compiler and Programming Docs->C Language Compiler... in chapter 6 a detailled documentation how to interface with assembly and C.
There is shown how parameters are transfered, register and stack usage. Of course for a byte-var only a byte is allocated on the stack and 2 bytes for an int. The only problem for you is how to calculate the address of a var from beginning of stackframe and how to (indirectly) load that var. All this will not result in a single asm-inline statement, but in a few lines of asm.
And a tip: DO check the .lst-file!

I schouldn't have said that it is easy
Bob
User avatar
bobmarlowe
The Big Cheese
The Big Cheese
 
Posts: 1490
Joined: Thu Oct 06, 2011 2:03 am
Location: Germany

Re: inline asm for local variables

Postby slandrum » Fri Jan 13, 2012 8:42 am

It's worse than not being easy, there's no guarantee that any given local variable will end up at any particular location in the local stack frame (or at all) when the code is compiled again. Especially if you turn on any compiler optimization (which I would expect, without optimizations enabled compilers generally will create terrible code for the M8C processor core).

Code maintenance becomes difficult because the writing the code is now an iterative process to locate the relative addresses of the variables, and every time you compile the code you have to check and make sure nothing moved.

It's much easier to just write some assembly language functions and link them in.

Also, as near as I can tell NASM would be completely inappropriate. I don't see any support for the M8C processor core for it.
slandrum
The Big Cheese
The Big Cheese
 
Posts: 453
Joined: Fri Feb 29, 2008 11:38 am
Location: California


Return to “%s” ImageCraft ICCv7 for M8C Compiler

Who is online

Users browsing this forum: No registered users and 1 guest