| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | strlen |
Hi Jasen! :-)
JB> int sleng( char *c)
JB> {
JB> asm(
JB> " movl %0 ,%%edi; "
JB> " xorl %%eax ,%%eax; "
JB> " movl %%eax ,%%ecx; "
JB> " notl %%ecx; "
JB> " cld; "
JB> " repnz ; scasb; "
JB> " movl $-2,%%eax; "
JB> " subl %%ecx,%%eax; "
JB> : /* no outputs - eax becomes the function's return value */
JB> :"rm"(c)
JB> :"eax","ecx","edi","cc");
JB> }
JB> This is my first attempt at gcc style inline asm, I'd welcome any
JB> comments...
Looks good. Most people prefer to put a "\n" as the last thing on
each asm line. I think that makes error messages from the assembler more
readable since otherwise all of your asm block is one big line.
JB> hmm, if I had the glibc source I could see how the experts did it.
The generic i386 version is mixed asm and C and can be found in the file
glibc-2.3.2/sysdeps/i386/strlen.c:
size_t
strlen (const char *str)
{
int cnt;
asm("cld\n" /* Search forward. */
/* Some old versions of gas need `repne' instead of `repnz'. */
"repnz\n" /* Look for a zero byte. */
"scasb" /* %0, %1, %3 */ :
"=c" (cnt) : "D" (str), "0" (-1),
"a" (0));
return -2 - cnt;
}
Short and to the point, mostly replacing some of what you did in asm with
proper input and output operand specs. There are also long (more than 50
lines) variants for 486 and 586 processors.
JB> is the syntax for inline asm described somewhere in the GCC info
JB> tree? (where?)
Well, there are some notes about input and output operands under C
Extensions, Extended Asm. I would expect gcc to pass asm blocks into gas
with only minimal pre-processing for the input and memory operands. The as
info manual mainly lists the differences between Intel and AT&T syntax
with some special information under Machine Dependencies, i386-Dependent.
If you already know Intel syntax, that should be enough to get you started.
The main difference is that target and source operand are the other way
around and that mnemonics carry an operand size suffix. This style is
actually quite similar to the asm used natively on other processors (MIPS,
for example).
Ciao
Pascal
--- Msged/LNX 6.1.1
* Origin: sed s/$(echo $HOME | sed 's/[[:punct:]]/\\&/g')/~/ (1:153/401.2)SEEN-BY: 633/267 270 @PATH: 153/401 307 140/1 106/2000 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.