I doubt that.
> __asm__ ( "xorl %eax, %eax\n\t"
> "xorl %ecx, %ecx\n\t"
> "loo:\n"
> "cmpl _lenght, %ecx\n\t"
_lenght --> _length
See, this code CAN'T possibly work due to the misspelling ... ;-)
> "jge don\n\t"
%ecx is zero. That's due to the XOR, i.e., 'xorl'.
So, I think this will exit immediately for
any positive values. Is that what you wanted?
> "addl $1, _array(,%ecx,8)\n\t"
You're using 32-bit (4-byte) registers but 8 byte (64-bit)
array values? That seems odd. Do you want 4 instead?
Or, are your register sizes incorrect? e.g., %rcx %rax
> "incl %ecx\n\t"
> "jmp loo\n\t"
Don't add a 'p' to 'loo'. 'loop' is an x86 instruction.
> "don:\n"
> );
>
>
...
> My question is how to define array and length as local variables ?
Without regards as to why you want to do this or whether it's a good
idea or not, you'll have to allocate space on the stack for them and
reference them by their position on the stack. You'll need to know
specific information on how your C compiler generates a stackframe
in the prolog(ue) and epilog(ue) of a called function. Many x86 C
compilers use a stack based calling convention that uses %ebp and %esp
to create a linked-list of stackframes. IIRC, this is known as the
CDECL calling convention. Other compilers use %esp only, while some
use a purely register based calling convention, or have the option to
do so.
It appears you're using AT&T syntax, so I'm assuming you're using GCC
and GAS. In which case, you can code a C function similar to what
you want in assembly, use GCC -S (capital S) to emit assembly to .s
file. The beginning code for the procedure which likely adjusts %esp
and %ebp for the parameters is called the prolog(ue). No -ue on prolog
in America, but prologue is used for other English speaking countries.
The epilog(ue) is the tail code which adjusts %esp and %ebp. You can
adjust %esp via 'pushl' 'popl' or direct addition of values, e.g., 4 or 8.
For 64-bit GAS syntax, that's probably 'pushq' 'popq' %rbp %rsp .
http://en.wikipedia.org/wiki/X86_calling_conventions#cdecl
Now, we'll get to whether it's a good idea or not. Of course, there is
no problem with making 'length' local. However, I would STRONGLY argue
against allocating an array on a C compiler's parameter stack. An array
should be allocated in memory due to the fact that they're generally
larger in size than typical variables in C, which typically fit in
registers. If the array is allocated in memory, that means the array
WILL NOT be a local variable, as requested. It also means you'll need
to call C's malloc(), or an OS specific memory allocation routine from
assembly. You'll also need to call free() or the appropriate OS function.
> I want to use this code inside an function in C that return an array.
AFAIK, the C language is not able to return an array from a function.
But, I'm not going to review the C specifications or K&R or H&S to confirm.
If you read the CDECL specification above, you'll realize that
C return values on x86 are almost alway returned in %eax, and that
register is insufficient to store an entire array. You could return
the starting address of the array via assembly ... In C, this would
likely need a cast on the array address to a large unsigned integer
type, or would be better if handled that way.
Rod Pemberton