delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/03/06/20:20:31

Sender: nate AT cartsys DOT com
Message-ID: <36E1D37C.4C020F79@cartsys.com>
Date: Sat, 06 Mar 1999 17:16:45 -0800
From: Nate Eldredge <nate AT cartsys DOT com>
X-Mailer: Mozilla 4.08 [en] (X11; I; Linux 2.2.1 i586)
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: anyone see what's wrong with this ?
References: <FtJD+DAkka42EwCY AT xemu DOT demon DOT co DOT uk>
Reply-To: djgpp AT delorie DOT com

Dave Bird wrote:
> 
> "Parse error before string" at the INPUT line.
> 
> #include <stdio.h>
> /*                                                */
> /*  STRING-LENGTH of zero-terminated string...    */
> /*  just a demo of  using in-line assembler.      */
> /*                                                */
> int str_len(char *str){ int num=0;
>    asm(
>       "decl %ecx         /n/t"  /* --num          */
> "FIND: incl %ecx         /n/t"  /* ++num          */
>       "movb [%ebx], %al  /n/t"  /* al  = [ebx++]  */
>       "incl %ebx         /n/t"
>       "orb  %al,     %al /n/t"  /* if al !=0      */
>       "jnz  FIND         /n/t"  /* go round again */   |
>       :"c"(num)                /*[OUTPUT from ECX]*/   |
>       :"c"(num) "b"(str)       /*[INPUT to EBX,CX]*/ <=+
                ^^^
You need a comma.

>       :"%eax"                  /*[JUNKED eax]     */
>    ); return num;
> }

Some other problems:

All "%" signs need to be doubled (so decl %%ecx).  Otherwise GCC will
think they refer to asm operands and be confused.

Third line should read "movb (%%ebx), %%al", parens instead of square
brackets.

You shouldn't use a label like FIND; it pollutes the namespace and will
break if you ever compile with -O3 (the function will be inlined and the
label used multiple times).  Use a local label instead; see node "Symbol
names" in gas docs.

To mark ecx as an output, use "=c".  Actually, since it's read-write,
use "+c" and omit it as an input.  Also, you don't tell the compiler
that %ebx will have garbage after the asm finishes.  Traditionally you
add %ebx to the clobber list, but that's wrong, according to the GCC
docs, and I think EGCS enforces it.  (Can some EGCS guru explain how you
describe a read-clobber operand, other than outputting to a scratch
variable?)

/n/t should use backslashes.
-- 

Nate Eldredge
nate AT cartsys DOT com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019