DSPRelated.com
Forums

a way to put C variables inside inline assembler (in C files) for Android NDK which is basically arm-eabi-gcc

Started by GoodMan November 12, 2012
I never found a way to put C variables inside inline assembler (in C files) for Android NDK which is basically arm-eabi-gcc.  I would greatly appreciate if someone could help on that!

Thanks,
AM
GoodMan wrote:
> I never found a way to put C variables inside inline assembler (in C > files) for Android NDK which is basically arm-eabi-gcc. I would > greatly appreciate if someone could help on that! > > Thanks, AM >
This works for Mingw on a Win7 PC ( not an arm ) int k = 1; int main(void) { //k++; asm("lea _k,%eax"); asm("incl (%eax)"); printf("%d\n",k); return 0; } Note the underscore "_" before the name. It also will not work for "auto" variables local to the routine, which are referred to by stack position. It *may not work* on your compiler. It worked on mine, but this is seriously specific stuff. I feel pretty confident it will work since it's a gcc in both cases. -- Les Cargill
On Mon, 12 Nov 2012 17:42:11 -0800, GoodMan wrote:

> I never found a way to put C variables inside inline assembler (in C > files) for Android NDK which is basically arm-eabi-gcc. I would greatly > appreciate if someone could help on that! > > Thanks, > AM
RTFM. The gnu compiler has a great assembly interface, and it is documented in detail. This is code for the Cortex M3, compiled on arm-eabi-gcc. So yes, it works for ARM. CFractional CFractional::operator - (CFractional y) const { int32_t a = _x; int32_t b = y._x; asm ( "subs %[a], %[b]\n" // subtract // many lines snipped // : [a] "=r" (a), [b] "=r" (b) : "[a]" "r" (a), "[b]" "r" (b)); return CFractional(a); } -- Tim Wescott Control system and signal processing consulting www.wescottdesign.com
Tim Wescott <tim@seemywebsite.please> writes:

> On Mon, 12 Nov 2012 17:42:11 -0800, GoodMan wrote: > >> I never found a way to put C variables inside inline assembler (in C >> files) for Android NDK which is basically arm-eabi-gcc. I would greatly >> appreciate if someone could help on that! >> >> Thanks, >> AM > > RTFM. The gnu compiler has a great assembly interface, and it is > documented in detail. > > This is code for the Cortex M3, compiled on arm-eabi-gcc. So yes, it > works for ARM. > > CFractional CFractional::operator - (CFractional y) const > { > int32_t a = _x; > int32_t b = y._x; > asm ( "subs %[a], %[b]\n" // subtract > // many lines snipped // > : [a] "=r" (a), [b] "=r" (b) > : "[a]" "r" (a), "[b]" "r" (b)); > > return CFractional(a); > }
Real men write pure assembly and don't have to deal with this tripe. -- Randy Yates Digital Signal Labs http://www.digitalsignallabs.com
On Tue, 13 Nov 2012 08:45:01 -0500, Randy Yates wrote:

> Tim Wescott <tim@seemywebsite.please> writes: > >> On Mon, 12 Nov 2012 17:42:11 -0800, GoodMan wrote: >> >>> I never found a way to put C variables inside inline assembler (in C >>> files) for Android NDK which is basically arm-eabi-gcc. I would >>> greatly appreciate if someone could help on that! >>> >>> Thanks, >>> AM >> >> RTFM. The gnu compiler has a great assembly interface, and it is >> documented in detail. >> >> This is code for the Cortex M3, compiled on arm-eabi-gcc. So yes, it >> works for ARM. >> >> CFractional CFractional::operator - (CFractional y) const { >> int32_t a = _x; >> int32_t b = y._x; >> asm ( "subs %[a], %[b]\n" // subtract >> // many lines snipped // >> : [a] "=r" (a), [b] "=r" (b) >> : "[a]" "r" (a), "[b]" "r" (b)); >> >> return CFractional(a); >> } > > Real men write pure assembly and don't have to deal with this tripe.
Real men get large applications working in a form that can be maintained by regular humans. Without feeling that their manliness is lessened by the statements of self-obsessed machismos. Neener. -- Tim Wescott Control system and signal processing consulting www.wescottdesign.com
Tim Wescott <tim@seemywebsite.please> writes:

> On Tue, 13 Nov 2012 08:45:01 -0500, Randy Yates wrote: > >> Tim Wescott <tim@seemywebsite.please> writes: >> >>> On Mon, 12 Nov 2012 17:42:11 -0800, GoodMan wrote: >>> >>>> I never found a way to put C variables inside inline assembler (in C >>>> files) for Android NDK which is basically arm-eabi-gcc. I would >>>> greatly appreciate if someone could help on that! >>>> >>>> Thanks, >>>> AM >>> >>> RTFM. The gnu compiler has a great assembly interface, and it is >>> documented in detail. >>> >>> This is code for the Cortex M3, compiled on arm-eabi-gcc. So yes, it >>> works for ARM. >>> >>> CFractional CFractional::operator - (CFractional y) const { >>> int32_t a = _x; >>> int32_t b = y._x; >>> asm ( "subs %[a], %[b]\n" // subtract >>> // many lines snipped // >>> : [a] "=r" (a), [b] "=r" (b) >>> : "[a]" "r" (a), "[b]" "r" (b)); >>> >>> return CFractional(a); >>> } >> >> Real men write pure assembly and don't have to deal with this tripe. > > Real men get large applications working in a form that can be maintained > by regular humans. Without feeling that their manliness is lessened by > the statements of self-obsessed machismos. > > Neener.
Tim, Hah! I'm just amazed at the hoops people jump through to avoid a little assembly. IMO, this stuff is more confusing than a native assembly routine. -- Randy Yates Digital Signal Labs http://www.digitalsignallabs.com
On Tue, 13 Nov 2012 12:34:24 -0500, Randy Yates wrote:

> Tim Wescott <tim@seemywebsite.please> writes: > >> On Tue, 13 Nov 2012 08:45:01 -0500, Randy Yates wrote: >> >>> Tim Wescott <tim@seemywebsite.please> writes: >>> >>>> On Mon, 12 Nov 2012 17:42:11 -0800, GoodMan wrote: >>>> >>>>> I never found a way to put C variables inside inline assembler (in C >>>>> files) for Android NDK which is basically arm-eabi-gcc. I would >>>>> greatly appreciate if someone could help on that! >>>>> >>>>> Thanks, >>>>> AM >>>> >>>> RTFM. The gnu compiler has a great assembly interface, and it is >>>> documented in detail. >>>> >>>> This is code for the Cortex M3, compiled on arm-eabi-gcc. So yes, it >>>> works for ARM. >>>> >>>> CFractional CFractional::operator - (CFractional y) const { >>>> int32_t a = _x; >>>> int32_t b = y._x; >>>> asm ( "subs %[a], %[b]\n" // subtract >>>> // many lines snipped // >>>> : [a] "=r" (a), [b] "=r" (b) >>>> : "[a]" "r" (a), "[b]" "r" (b)); >>>> >>>> return CFractional(a); >>>> } >>> >>> Real men write pure assembly and don't have to deal with this tripe. >> >> Real men get large applications working in a form that can be >> maintained by regular humans. Without feeling that their manliness is >> lessened by the statements of self-obsessed machismos. >> >> Neener. > > Tim, > > Hah! > > I'm just amazed at the hoops people jump through to avoid a little > assembly. IMO, this stuff is more confusing than a native assembly > routine.
One reason is because when you use even a little bit of assembly you severely restrict the population of folks that can maintain your code later. There's a lot of people out there who are quite facile with C and C++, but who lock up in the presence of "mov" or "lsl". In this case, the driving reason that this was done this way was to let the compiler do the name-mangling for the function call, then have assembly inside. I run 50-50 on doing it this way in C, but unless the manual details the name-mangling algorithm, in C++ I do wrap & stuff. Frankly, I don't trust that the version 4 name mangling is going to survive the transition to version 5. The gnu compiler has a very elegant in-line assembly interface, which certainly encourages me to use it. If the compiler is one that just emits assembly inline with no way to tie it into the C code, then I'll rite a pure assembly file. -- Tim Wescott Control system and signal processing consulting www.wescottdesign.com
On Tue, 13 Nov 2012 12:34:24 -0500
Randy Yates <yates@digitalsignallabs.com> wrote:

> Tim Wescott <tim@seemywebsite.please> writes: > > > On Tue, 13 Nov 2012 08:45:01 -0500, Randy Yates wrote: > >>> On Mon, 12 Nov 2012 17:42:11 -0800, GoodMan wrote: > >>> > >>> > >>> CFractional CFractional::operator - (CFractional y) const { > >>> int32_t a = _x; > >>> int32_t b = y._x; > >>> asm ( "subs %[a], %[b]\n" // subtract > >>> // many lines snipped // > >>> : [a] "=r" (a), [b] "=r" (b) > >>> : "[a]" "r" (a), "[b]" "r" (b)); > >>> > >>> return CFractional(a); > >>> } > >> > > I'm just amazed at the hoops people jump through to avoid a little > assembly. IMO, this stuff is more confusing than a native assembly > routine. > -- > Randy Yates > Digital Signal Labs > http://www.digitalsignallabs.com
Just not sure how you'd do specifically that with a native assembly routine; the defining of an overloaded operator, or even any kind of C++ class method. Can you define an extern method of a class? And how would you build up the returned class reliably in asm without making some massive assumptions about how the compiler arranges the classs data? Alternatively, you could let the C++ method definition give you a class skeleton, and then call a native assembly function from a separate file, but then you're not inline anymore. Add a branch and a return, with all the associated pipeline dumping, and you've quite likely lost any advantage you were hoping to gain from writing assembly in the first place. -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix.
Randy Yates wrote:
> Tim Wescott <tim@seemywebsite.please> writes: > >> On Mon, 12 Nov 2012 17:42:11 -0800, GoodMan wrote: >> >>> I never found a way to put C variables inside inline assembler (in C >>> files) for Android NDK which is basically arm-eabi-gcc. I would greatly >>> appreciate if someone could help on that! >>> >>> Thanks, >>> AM >> >> RTFM. The gnu compiler has a great assembly interface, and it is >> documented in detail. >> >> This is code for the Cortex M3, compiled on arm-eabi-gcc. So yes, it >> works for ARM. >> >> CFractional CFractional::operator - (CFractional y) const >> { >> int32_t a = _x; >> int32_t b = y._x; >> asm ( "subs %[a], %[b]\n" // subtract >> // many lines snipped // >> : [a] "=r" (a), [b] "=r" (b) >> : "[a]" "r" (a), "[b]" "r" (b)); >> >> return CFractional(a); >> } > > Real men write pure assembly and don't have to deal with this tripe. >
Real men ship :) --- Les Cargill
On Tue, 13 Nov 2012 10:16:35 -0800, Rob Gaddi wrote:

> On Tue, 13 Nov 2012 12:34:24 -0500 Randy Yates > <yates@digitalsignallabs.com> wrote: > >> Tim Wescott <tim@seemywebsite.please> writes: >> >> > On Tue, 13 Nov 2012 08:45:01 -0500, Randy Yates wrote: >> >>> On Mon, 12 Nov 2012 17:42:11 -0800, GoodMan wrote: >> >>> >> >>> >> >>> CFractional CFractional::operator - (CFractional y) const { >> >>> int32_t a = _x; >> >>> int32_t b = y._x; >> >>> asm ( "subs %[a], %[b]\n" // subtract >> >>> // many lines snipped // >> >>> : [a] "=r" (a), [b] "=r" (b) >> >>> : "[a]" "r" (a), "[b]" "r" (b)); >> >>> >> >>> return CFractional(a); >> >>> } >> >> >> >> >> I'm just amazed at the hoops people jump through to avoid a little >> assembly. IMO, this stuff is more confusing than a native assembly >> routine. >> -- >> Randy Yates Digital Signal Labs http://www.digitalsignallabs.com > > Just not sure how you'd do specifically that with a native assembly > routine; the defining of an overloaded operator, or even any kind of C++ > class method. Can you define an extern method of a class? And how > would you build up the returned class reliably in asm without making > some massive assumptions about how the compiler arranges the classs > data? > > Alternatively, you could let the C++ method definition give you a class > skeleton, and then call a native assembly function from a separate file, > but then you're not inline anymore. Add a branch and a return, with all > the associated pipeline dumping, and you've quite likely lost any > advantage you were hoping to gain from writing assembly in the first > place.
Yup! -- Tim Wescott Control system and signal processing consulting www.wescottdesign.com