* gcc question for inline assembly.
@ 2006-09-12 4:25 Stuart Cracraft
2006-09-12 6:14 ` Brian Budge
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Stuart Cracraft @ 2006-09-12 4:25 UTC (permalink / raw)
To: help-gcc
Hi - someone supplied the following code
in a C++ program to me that g++ compiled.
However, I want to convert it to use it in
a regular C program that gcc can compile.
Needless to say, the construct does not work
for GNU C.
I've tried placing each line of the assembly
in asm("...."); and that also fails to get
gcc to compile it.
What would the equivalent GNU C function for the
below be?
static unsigned int bitScanAndReset(unsigned long long & bb) {
__asm
{
xor edx, edx
mov ebx, [bb]
xor eax, eax
inc edx
bsf ecx, [ebx]
jnz found
bsf ecx, [ebx+4]
lea ebx, [ebx+4]
xor eax, 32
found:
shl edx, cl
xor eax, ecx
xor [ebx], edx
}
}
Thanks,
Stuart
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: gcc question for inline assembly.
2006-09-12 4:25 gcc question for inline assembly Stuart Cracraft
@ 2006-09-12 6:14 ` Brian Budge
2006-09-12 12:16 ` John Love-Jensen
2006-09-12 12:24 ` Marcelo Slomp
2 siblings, 0 replies; 4+ messages in thread
From: Brian Budge @ 2006-09-12 6:14 UTC (permalink / raw)
To: Stuart Cracraft; +Cc: help-gcc
Hi Stuart -
This is really a C/C++ question, and not really a gcc/c++ question.
However, in C, you can't pass things by reference (the &), you should
pass by address instead, and then dereference to set the values.
Brian
On 9/11/06, Stuart Cracraft <cracraft@cox.net> wrote:
> Hi - someone supplied the following code
> in a C++ program to me that g++ compiled.
>
> However, I want to convert it to use it in
> a regular C program that gcc can compile.
>
> Needless to say, the construct does not work
> for GNU C.
>
> I've tried placing each line of the assembly
> in asm("...."); and that also fails to get
> gcc to compile it.
>
> What would the equivalent GNU C function for the
> below be?
>
> static unsigned int bitScanAndReset(unsigned long long & bb) {
>
> __asm
> {
> xor edx, edx
> mov ebx, [bb]
> xor eax, eax
> inc edx
> bsf ecx, [ebx]
> jnz found
> bsf ecx, [ebx+4]
> lea ebx, [ebx+4]
> xor eax, 32
> found:
> shl edx, cl
> xor eax, ecx
> xor [ebx], edx
> }
> }
>
> Thanks,
>
> Stuart
>
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: gcc question for inline assembly.
2006-09-12 4:25 gcc question for inline assembly Stuart Cracraft
2006-09-12 6:14 ` Brian Budge
@ 2006-09-12 12:16 ` John Love-Jensen
2006-09-12 12:24 ` Marcelo Slomp
2 siblings, 0 replies; 4+ messages in thread
From: John Love-Jensen @ 2006-09-12 12:16 UTC (permalink / raw)
To: Stuart Cracraft, help-gcc
Hi Stuart,
Your question is off-topic for this forum.
The routine looks for the lowest bit set in the bb variable, a 64-bit
number. It then sets the bb variable to that number. It also returns the
bit position of that lowest bit (1-based: starting at 1 for the "least
significant bit", to 64 for the "most significant bit").
If the number passed in is 0 (i.e., bb == 0ULL), it poor behavior. Don't
call this routine with 0ULL.
I recommend rewriting the routine in pure C, and avoid assembly language.
If you need help on how to do that, get this book: Hacker's Delight
http://www.amazon.com/Hackers-Delight-Henry-Warren-Jr/dp/0201914654
HTH,
--Eljay
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: gcc question for inline assembly.
2006-09-12 4:25 gcc question for inline assembly Stuart Cracraft
2006-09-12 6:14 ` Brian Budge
2006-09-12 12:16 ` John Love-Jensen
@ 2006-09-12 12:24 ` Marcelo Slomp
2 siblings, 0 replies; 4+ messages in thread
From: Marcelo Slomp @ 2006-09-12 12:24 UTC (permalink / raw)
To: gcc-help
Stuart Cracraft wrote:
>
> Hi - someone supplied the following code
> in a C++ program to me that g++ compiled.
>
> However, I want to convert it to use it in
> a regular C program that gcc can compile.
>
> Needless to say, the construct does not work
> for GNU C.
>
> I've tried placing each line of the assembly
> in asm("...."); and that also fails to get
> gcc to compile it.
>
> What would the equivalent GNU C function for the
> below be?
>
> static unsigned int bitScanAndReset(unsigned long long & bb) {
>
> __asm
> {
> xor edx, edx
> mov ebx, [bb]
> xor eax, eax
> inc edx
> bsf ecx, [ebx]
> jnz found
> bsf ecx, [ebx+4]
> lea ebx, [ebx+4]
> xor eax, 32
> found:
> shl edx, cl
> xor eax, ecx
> xor [ebx], edx
> }
> }
>
> Thanks,
>
> Stuart
>
>
Notice that the assembler used by gcc (as) uses by default the at&t assembly
syntax, and you have intel code.
You can either, translate the code to at&t syntax or use the -masm=intel
flag to compile (mount) this code.
read more on at&t syntax at:
http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html
Regards,
Marcelo Slomp
--
View this message in context: http://www.nabble.com/gcc-question-for-inline-assembly.-tf2256651.html#a6264969
Sent from the gcc - Help forum at Nabble.com.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2006-09-12 12:24 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-09-12 4:25 gcc question for inline assembly Stuart Cracraft
2006-09-12 6:14 ` Brian Budge
2006-09-12 12:16 ` John Love-Jensen
2006-09-12 12:24 ` Marcelo Slomp
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 read-only IMAP folder(s) and NNTP newsgroup(s).