Writing MIPS assembler instructions in C

Patrick Mahan mahan at mahan.org
Thu Feb 25 02:31:56 UTC 2010

See inline ...

> Hi Patrick,
> On Wed, Feb 24, 2010 at 17:38, Patrick Mahan <mahan at mahan.org> wrote:
> > part of some code built on the linux platform and
> > there they are using "%[rt]" which I cannot find
> > a description.
> %[foo] can be used to refer to named parameters (input or output,
> maybe even clobbered) of inline assembly instructions rather than
> using %0, %1, etc., which can be hard to read.  Consider:
> %%%
> static inline bool
> atomic_cmpset64(volatile uint64_t *p, uint64_t o, uint64_t v)
> {
> 	uint64_t temp;
> 	int res;
> 	asm volatile (
> 	"1:\n\t"
> 	"move	%[res], $0\n\t"
> 	"lld	%[temp], %[p]\n\t"
> 	"bne	%[temp], %[o], 2f\n\t"
> 	"move	%[temp], %[v]\n\t"
> 	"li	%[res], 1\n\t"
> 	"scd	%[temp], %[p]\n\t"
> 	"beqz	%[temp], 1b\n\t"
> 	"2:\n\t"
> 	: [res] "=&r"(res), [temp] "=&r"(temp), [p] "+m"(*p)
> 	: [o] "r"(o), [v] "r"(v)
> 	: "memory"
> 	);
> 	return (res != 0);
> }
> %%%
> The brackets with the input and output parameters specify what names
> to use to refer to them in the assembly listing (here they happen to
> be mostly the same as the variable names, but that's not necessary.)

Ah Sooo <*whack!*>......

I did not even twig on that... I had gotten it in my head that %[rt]
was something special to the compiler that caused a certain behavior
to occur.

Now my problem is I still need to force the value pointed to "addr" into
a specific register because there is a jalr to a function else where
that I only have binary access too and it expects it's a value in
that register.  Can I coerce this?

Thanks for the education.


More information about the freebsd-mips mailing list