Skip to contentSkip to conversation

__asm__ usage ????

DISCUSSION Raghava Raju 5 participants 6 messages Aug 10, 2001 – Aug 13, 2001

lore
Share & tools
lore
#0 lore raw

hi

I want some basic insights into assembly level code
emmbedded in C language. Following is the code of PowerPc ambedded in C languagge:

unsigned long old,mask, *p;

	__asm__ __volatile__(SMP_WMB "\
1:	lwarx 	%0,0,%3
	andc  	%0,%0,%2
	stwcx 	%0,0,%3
	bne 	1b"
	SMP_MB
	: "=&r" (old), "=m" (*p)
	: "r" (mask), "r" (p), "m" (*p)
	: "cc");

	1) what does these things denote: __volatile__,

SMP_WMB, SMP_MB, "r","=&r","=m",
"cc" and 1: .

2) Is it that %0,%2,%3 denote addresses of old,mask,p
respectively.

3) Say if it is PowerPc then how should I
access registers r1,r2 etc, i.e. what is the exact syntax.

4) I think in power PC we can't access
directly the contents of memory, but we should give addresses of memory in registers then use registers in instructions to access memory. But in above example he is using %3 in lwarx command accessing that memory directly. Is my interpretation of above instructions wrong.

5) Some people use "memory" in place of "cc" ,
like I want to know what are these things.

6) Finally I want to write a simple programme
to write the contents of a local variable "xyz" into register r33, then store the contents of r33 into local variable "abc". Kindly would u give me a sample code of doing it.

Please reply to [email protected], since I am not subscriber to list.

Thanks in advance.
Raghava.

__________________________________________________
Do You Yahoo!?
Send instant messages & get email alerts with Yahoo! Messenger.
http://im.yahoo.com/

#1 lore raw
Quoted 47 lines · Raghava Raju

hi

I want some basic insights into assembly level code
emmbedded in C language. Following is the code of PowerPc ambedded in C languagge:

unsigned long old,mask, *p;

        __asm__ __volatile__(SMP_WMB "\
1:      lwarx   %0,0,%3
        andc    %0,%0,%2
        stwcx   %0,0,%3
        bne     1b"
        SMP_MB
        : "=&r" (old), "=m" (*p)
        : "r" (mask), "r" (p), "m" (*p)
        : "cc");

        1) what does these things denote: __volatile__,

SMP_WMB, SMP_MB, "r","=&r","=m",
"cc" and 1: .

2) Is it that %0,%2,%3 denote addresses of old,mask,p
respectively.

3) Say if it is PowerPc then how should I
access registers r1,r2 etc, i.e. what is the exact syntax.

4) I think in power PC we can't access
directly the contents of memory, but we should give addresses of memory in registers then use registers in instructions to access memory. But in above example he is using %3 in lwarx command accessing that memory directly. Is my interpretation of above instructions wrong.

5) Some people use "memory" in place of "cc" ,
like I want to know what are these things.

6) Finally I want to write a simple programme
to write the contents of a local variable "xyz" into register r33, then store the contents of r33 into local variable "abc". Kindly would u give me a sample code of doing it.

Quoted 6 lines

Please reply to [email protected], since I am not subscriber to list.

Thanks in advance.
Raghava.

I think you need to spend a bit of time looking over the gcc info pages. From emacs use CTL-X h, else, try info (a program).

You might also want to go to the kernel "top dir" (where the master make file is) and do:

make TAGS (for emacs)
or
make ctags (for vi)

Then find the symbols SMP_WMB, etc.

This should get you past most of the questions and impart a great deal more info that you will need on the way.

Along the way I think you will come to understand that you are not just dropping asm code into the program, you are giving gcc enough info to optimize your asm along with its other productions.

George

#2 lore raw
In reply to Raghava Raju
Quoted 17 lines · Raghava Raju

I want some basic insights into assembly level code
emmbedded in C language. Following is the code of PowerPc ambedded in C languagge:

unsigned long old,mask, *p;

	__asm__ __volatile__(SMP_WMB "\
1:	lwarx 	%0,0,%3
	andc  	%0,%0,%2
	stwcx 	%0,0,%3
	bne 	1b"
	SMP_MB
	: "=&r" (old), "=m" (*p)
	: "r" (mask), "r" (p), "m" (*p)
	: "cc");

	1) what does these things denote: __volatile__,
Quoted 4 lines

SMP_WMB, SMP_MB, "r","=&r","=m",
"cc" and 1: .

From http://www.kernelnewbies.org/links/ :

http://www-106.ibm.com/developerworks/linux/library/l-ia.html
http://www.uwsg.indiana.edu/hypermail/linux/kernel/9804.2/0953.html

Erik

Signature · 5 lines
J.A.K. (Erik) Mouw, Information and Communication Theory Group, Department
of Electrical Engineering, Faculty of Information Technology and Systems,
Delft University of Technology, PO BOX 5031,  2600 GA Delft, The Netherlands
Phone: +31-15-2783635  Fax: +31-15-2781843  Email: [email protected]
WWW: http://www-ict.its.tudelft.nl/~erik/
#3 lore raw
In reply to Raghava Raju

Am 10 Aug 2001 12:50:04 -0700 schrieb Raghava Raju:

Quoted 3 lines

I want some basic insights into assembly level code
emmbedded in C language. Following is the code of PowerPc ambedded in C languagge:

It's not really kernel related but nevertheless...

Quoted 12 lines

unsigned long old,mask, *p;

	__asm__ __volatile__(SMP_WMB "\
1:	lwarx 	%0,0,%3
	andc  	%0,%0,%2
	stwcx 	%0,0,%3
	bne 	1b"
	SMP_MB
	: "=&r" (old), "=m" (*p)
	: "r" (mask), "r" (p), "m" (*p)
	: "cc")"
	1) what does these things denote: __volatile__,

It means the compiler should by no means touch the code.

Quoted 1 line

SMP_WMB, SMP_MB,

I suspect those are defined to some memory barriing code and only in SMP kernels.

Quoted 1 line

"r","=&r","=m",

r means register, = means output and & is early clobber. =m means address that is written to and in ppc assembler this means a different notation in the final assembly in the form 4(%r1) for example.

Quoted 1 line

"cc" and 1: .

cc is the condition code register and 1: a local label which can be used for a look like "bne 1b" (branch if not equal to label 1 backwards).

Quoted 2 lines

2) Is it that %0,%2,%3 denote addresses of old,mask,p
respectively.

No, they are just labels which are replaced by the compiler with whatever is defined after the colons.

Quoted 7 lines

4) I think in power PC we can't access
directly the contents of memory, but we should give addresses of memory in registers then use registers in instructions to access memory. But in above example he is using %3 in lwarx command accessing that memory directly. Is my interpretation of above instructions wrong.

Yes, you're wrong. By issuing an "r" (p) the p (which is a pointer in this case) is assigned to a register which is then used in the load command as the absolute address.

Quoted 2 lines

5) Some people use "memory" in place of "cc" ,
like I want to know what are these things.

Those are other clobbers. memory means that memory has been modified by the command sequence and cc means that the condition register is modify and thus has to be saved by the compiler if in doubt.

Quoted 5 lines

6) Finally I want to write a simple programme
to write the contents of a local variable "xyz" into register r33, then store the contents of r33 into local variable "abc". Kindly would u give me a sample code of doing it.

Negative for two reasons: There is no register 33 (at least) on 32bit PPC CPUs, and second, you normally don't want to hardcode registers in inline assembly. If you really want to then use normal assembly.

But for your example:
long xyz, abc;

__asm__ __volatile__ ("mr %0,%1\n\t": "r" (abc) : "r" (xyz));

However this is a really dumb example.

Servus,
Daniel

#4 lore raw

Many thanks for ur reply but my actual meaning of question 4 and 6 was different. Please see the comments inline below and kindly clarify my doubts???

Thankyou in advance.
Raghava.

Quoted 27 lines · --- Daniel Egger

Am 10 Aug 2001 12:50:04 -0700 schrieb Raghava Raju:

Quoted 1 line

I want some basic insights into assembly level

code

Quoted 2 lines

emmbedded in C language. Following is the code of PowerPc ambedded in C languagge:

It's not really kernel related but nevertheless...

Quoted 12 lines

unsigned long old,mask, *p;

	__asm__ __volatile__(SMP_WMB "\
1:	lwarx 	%0,0,%3
	andc  	%0,%0,%2
	stwcx 	%0,0,%3
	bne 	1b"
	SMP_MB
	: "=&r" (old), "=m" (*p)
	: "r" (mask), "r" (p), "m" (*p)
	: "cc")"
        4) I think in power PC we can't access
Quoted 5 lines

directly the contents of memory, but we should give addresses of memory in registers then use registers in instructions to access memory. But in above example he is using %3 in lwarx command accessing that memory directly. Is my

Quoted 9 lines

interpretation

Quoted 1 line

of above instructions wrong.

Yes, you're wrong. By issuing an "r" (p) the p
(which is a
pointer in this case) is assigned to a register
which is then
used in the load command as the absolute address.

What I meant is that say in command
"andc %0,%0,%2" he is directly accessing the
contents of memory and using them in "andc". But it seems to be that he should use indirectly, like
storing the address of variable in register. Then use register in "andc" instead of directly using %0(i.e accessing memory directly). Correct me if I am wrong since powerPC mannual described that we should not use memory directly in instructions.

Quoted 17 lines
Quoted 1 line

6) Finally I want to write a simple

programme

Quoted 1 line

to write the contents of a local variable "xyz"

into

Quoted 2 lines

register r33, then store the contents of r33 into local variable "abc". Kindly would u give me a

sample

Quoted 1 line

code of doing it.

Negative for two reasons: There is no register 33 (at least)
on 32bit PPC CPUs, and second, you normally don't want to
hardcode registers in inline assembly. If you really want to
then use normal assembly.

Quoted 7 lines

But for your example:
long xyz, abc;

__asm__ __volatile__ ("mr %0,%1\n\t": "r" (abc) :
"r" (xyz));

However this is a really dumb example.

Here I dont bother about logic of the example, but I want know how to load the value of some (valid register, if not r33 something else in powerPC) register into a local variable xyz and vice versa.

Quoted 4 lines

Servus,
Daniel

__________________________________________________
Do You Yahoo!?
Send instant messages & get email alerts with Yahoo! Messenger.
http://im.yahoo.com/

#5 lore raw
Quoted 18 lines · Raghava Raju
Quoted 18 lines
Quoted 10 lines
	__asm__ __volatile__(SMP_WMB "\
1:	lwarx 	%0,0,%3
	andc  	%0,%0,%2
	stwcx 	%0,0,%3
	bne 	1b"
	SMP_MB
	: "=&r" (old), "=m" (*p)
	: "r" (mask), "r" (p), "m" (*p)
	: "cc")"
        4) I think in power PC we can't access
Quoted 5 lines

directly the contents of memory, but we should give addresses of memory in registers then use registers in instructions to access memory. But in above example he is using %3 in lwarx command accessing that memory directly. Is my

interpretation

Quoted 1 line

of above instructions wrong.

Quoted 8 lines
Quoted 4 lines

Yes, you're wrong. By issuing an "r" (p) the p (which is a pointer in this case) is assigned to a register which is then used in the load command as the absolute address.

What I meant is that say in command
"andc %0,%0,%2" he is directly accessing the
contents of memory and using them in "andc". But

NO! He is not doing that. The compiler changes the above into this:

1. address generation for the variable "p"
2. whatever SMP_WMB is
3. something like "lwarx r5,0,r8" (gcc picks the registers)
4. something like "andc r5,r5,r7"
5. something like "stwcx r5,0,r8"
6. a branch back to the lwarx
7. whatever SMP_MB is

Note: the lwarx is a special type of load instruction.

Quoted 4 lines

it seems to be that he should use indirectly, like
storing the address of variable in register. Then use register in "andc" instead of directly using %0(i.e accessing memory directly). Correct me if

The funny notation at the end instructs gcc to load the variable into a register and save it back as needed.

Quoted 2 lines

I am wrong since powerPC mannual described that we should not use memory directly in instructions.

You may not use memory directly in the andc instruction.

Quoted 18 lines
Quoted 18 lines
Quoted 5 lines

6) Finally I want to write a simple programme to write the contents of a local variable "xyz" into register r33, then store the contents of r33 into local variable "abc". Kindly would u give me a sample code of doing it.

Negative for two reasons: There is no register 33 (at least) on 32bit PPC CPUs, and second, you normally don't want to hardcode registers in inline assembly. If you really want to then use normal assembly.

But for your example:
long xyz, abc;

__asm__ __volatile__ ("mr %0,%1\n\t": "r" (abc) :
"r" (xyz));

Quoted 6 lines
Quoted 1 line

However this is a really dumb example.

Here I dont bother about logic of the example, but I want know how to load the value of some (valid register, if not r33 something else in powerPC) register into a local variable xyz and vice versa.

Stop thinking like this. You can not write good code if you insist on choosing registers. The strange gcc assembly notation is designed to let you cooperate with the compiler. Rather than saying you want "r33" you say you want "any normal integer register containing the content of variable xyz". Then gcc will pick a free register, call it %0 or %1 or whatever, and load it with variable "xyz".

If variable "xyz" is already in register r6, then why wouldn't you want to use r6 for your assembly? If you pick a specific register, then the compiler might have to copy from r6 into the one you specified. That would be bad. When you let gcc choose, it picks a register that is good to use.

Looking at this again:

__asm__ __volatile__ (

"mr %0,%1"               /* %0 and %1 may represent r6 */
: "=r" (abc)             /* the output (can have many!) */
: "r" (xyz)              /* the input (can have many!) */
:                        /* no special registers will be modified */

);

You could even code this as a NOP, using the funny constraint notation to make gcc do the copy for you. Simply specify that the input is to be the exact same register as the input. I think it is something like this:

__asm__ __volatile__ (

""                       /* just empty -- don't even need a NOP */
: "=r" (abc)             /* let gcc select an output register */
: "0" (xyz)              /* ask gcc to use the same register for input */
:                        /* no special registers will be modified */

);

Well there you go. This makes gcc load xyz into a register if it isn't already in one, then makes gcc assume that the register contains the new value for abc.

Be careful about the constraint letters you choose. For most PowerPC instructions, any integer register will do. For load/store instructions, the CPU treats r0 as a special case. You might need to change the "r" to something else, like "b" or "a" maybe. Look it up in the gcc manual.