linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* __asm__ usage ????
@ 2001-08-10 19:50 Raghava Raju
  2001-08-10 22:25 ` george anzinger
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Raghava Raju @ 2001-08-10 19:50 UTC (permalink / raw)
  To: linux-kernel



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 vraghava_raju@yahoo.com, 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/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: __asm__ usage ????
  2001-08-10 19:50 __asm__ usage ???? Raghava Raju
@ 2001-08-10 22:25 ` george anzinger
  2001-08-10 23:24 ` Erik Mouw
  2001-08-11 17:32 ` Daniel Egger
  2 siblings, 0 replies; 6+ messages in thread
From: george anzinger @ 2001-08-10 22:25 UTC (permalink / raw)
  To: Raghava Raju; +Cc: linux-kernel

Raghava Raju wrote:
> 
> 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 vraghava_raju@yahoo.com, 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: __asm__ usage ????
  2001-08-10 19:50 __asm__ usage ???? Raghava Raju
  2001-08-10 22:25 ` george anzinger
@ 2001-08-10 23:24 ` Erik Mouw
  2001-08-11 17:32 ` Daniel Egger
  2 siblings, 0 replies; 6+ messages in thread
From: Erik Mouw @ 2001-08-10 23:24 UTC (permalink / raw)
  To: Raghava Raju; +Cc: linux-kernel

On Fri, Aug 10, 2001 at 12:50:04PM -0700, Raghava Raju wrote:

>    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: .

>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

-- 
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: J.A.K.Mouw@its.tudelft.nl
WWW: http://www-ict.its.tudelft.nl/~erik/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: __asm__ usage ????
  2001-08-10 19:50 __asm__ usage ???? Raghava Raju
  2001-08-10 22:25 ` george anzinger
  2001-08-10 23:24 ` Erik Mouw
@ 2001-08-11 17:32 ` Daniel Egger
  2001-08-11 21:03   ` Raghava Raju
  2 siblings, 1 reply; 6+ messages in thread
From: Daniel Egger @ 2001-08-11 17:32 UTC (permalink / raw)
  To: Raghava Raju; +Cc: linux-kernel

Am 10 Aug 2001 12:50:04 -0700 schrieb 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:

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

 
> 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.

> SMP_WMB, SMP_MB,

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

> "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.

> "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).

> 	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.
 
>         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.

>         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.
 
>         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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: __asm__ usage ????
  2001-08-11 17:32 ` Daniel Egger
@ 2001-08-11 21:03   ` Raghava Raju
  2001-08-13  5:02     ` Albert D. Cahalan
  0 siblings, 1 reply; 6+ messages in thread
From: Raghava Raju @ 2001-08-11 21:03 UTC (permalink / raw)
  To: Daniel Egger; +Cc: linux-kernel


 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. 

--- Daniel Egger <egger@suse.de> wrote:
> Am 10 Aug 2001 12:50:04 -0700 schrieb 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:
> 
>  It's not really kernel related but nevertheless...
> 
>  
> > 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
> > 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.
> 
   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.

> >         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.
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.


> 
> Servus,
>        Daniel
> 


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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: __asm__ usage ????
  2001-08-11 21:03   ` Raghava Raju
@ 2001-08-13  5:02     ` Albert D. Cahalan
  0 siblings, 0 replies; 6+ messages in thread
From: Albert D. Cahalan @ 2001-08-13  5:02 UTC (permalink / raw)
  To: Raghava Raju; +Cc: Daniel Egger, linux-kernel

Raghava Raju writes:

>>> 	__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
>>> 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.
>
>    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.

>     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.

>    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.

>>> 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.
>
> 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.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2001-08-13  5:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-10 19:50 __asm__ usage ???? Raghava Raju
2001-08-10 22:25 ` george anzinger
2001-08-10 23:24 ` Erik Mouw
2001-08-11 17:32 ` Daniel Egger
2001-08-11 21:03   ` Raghava Raju
2001-08-13  5:02     ` Albert D. Cahalan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).