GCC problems.

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
eL JeDi

GCC problems.

Post by eL JeDi »

Hi,

Here I have a problem I can't fix.:

In file included in stdio.c:20:
../include/string.h 26: warning: conflicting types for built-in function 'memset'

Here is my string line 26:
extern inline void *memset(void *s,char c,int count);
I also tried:
inline void *memset(void *s,char c,int count);

And the memset function in string.h:

inline void *memset(void *s,char c,int count)
{
int d0, d1;
__asm__ __volatile__(
   "cld\n\t"
   "rep\n\t"
   "stosb"
   : "=&c" (d0), "=&D" (d1)
   :"a" (c),"1" (s),"0" (count)
   :"memory");
return s;
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:GCC problems.

Post by distantvoices »

this problem I ve experienced too. I don't know if my solution is the right one, but I compiled the files with
-fno-builtin (or similar check man gcc for this) and it compiled without complaining anymore. This is because there exists a standard memset routine in c which the c compiler refers to usually - its already buldt in.

Pls correct me, if I am in err.

stay safe
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
eL JeDi

Re:GCC problems.

Post by eL JeDi »

:D
That's it! It works. :)

Thanks Beyond . :)
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:GCC problems.

Post by Pype.Clicker »

what i hardly understand is why you have ecx and edi declared as outputs ...

i personally use

Code: Select all


/* sets N bytes of DEST to character C */
__inline__ void memset(void* dest,int c,int n)
{
  asm volatile("cld;rep;stosb":
               :"D"(dest),"c"(n),"a"(c));
  asm("": : :"%edi","%ecx");
}
Where the last asm command do nothing, but tell the compiler
not to rely on the content of edi and ecx anymore. I guess you're doing something similar, but won't the compiler have to reserve
space for d0 and d1 and issue mov instructions ?
Maybe not if the optimizer is smart enough ... though i would be pleased if you could tell me if you checked the generated code :)
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:GCC problems.

Post by distantvoices »

I wonder why this function is made in asm instead of c?

in case of memset it is really easier to understand when there stands:

Code: Select all

void memset(void *target,unsigned char character,int length){
  while(length--) *target++=character;
}
The syntax of this inline assembly of gcc is also something very obscure to me :(.

what are the benefits of using inline assembly?
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:GCC problems.

Post by Pype.Clicker »

because the processor can make things faster when a "rep stosb" is used (afaik).

Note that in order to be *really* faster, it would be wise to use a dword-aligned rep stosd ...
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:GCC problems.

Post by distantvoices »

well... would this then look like the following?

extern void memset(void *target, unsigned char zeichen,int length);

in nasm:

Code: Select all

[global memset]
memset:
  push ebp
  mov ebp,esp
  mov [ebp+8],edi   ;*target   
  mov [ebp+12],al   ; zeichen 
  mov [ebp+13],ecx ; length
  cld
  rep stosb
  pop ebp
  ret
Would this code do what one expects from memset?
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
Tim

Re:GCC problems.

Post by Tim »

Not quite. There are a few mistakes there: you had the MOV operands the wrong way round, and everything on the stack is dword-aligned.

Code: Select all

[global memset]
memset:
  push ebp
  mov ebp,esp
  mov edi, [ebp+8]  ;*target  
  mov al, [ebp+12]  ; zeichen 
  mov ecx, [ebp+16] ; length
  cld
  rep stosb
  pop ebp
  ret
eL JeDi

Re:GCC problems.

Post by eL JeDi »

Well pype,

Maybe i forget to check generated code ::)

The true is i recently learned the asm at&t sintaxi. My natural sintaxis is intel. But i want to prove, but my frankeinstein don't works well.

Thanks for all.
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:GCC problems.

Post by distantvoices »

tim, thanks for correction ... this comes from trying to type code when one is too tired to think.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:GCC problems.

Post by Pype.Clicker »

Tim Robinson wrote:

Code: Select all

[global memset]
memset:
  push ebp
  mov ebp,esp
  mov edi, [ebp+8]  ;*target  
  mov al, [ebp+12]  ; zeichen 
  mov ecx, [ebp+16] ; length
  cld
  rep stosb
  pop ebp
  ret
BTW, GCC (and most other C compiler) will expect registers like EDI and ECX not to be destroyed by the function, so you should push them before and pop them after.

And that's why inlined code can be quicker than pure asm function.
Post Reply