GNU Assembler (GAS) query



Hi all,

I have a requirement where I need to interface some assembly routine
with C program (without using inline assembly). In the C file, I make
a call to assembly routine while passing _quite_ a few arguments to
the routine. The arguments being passed in the C function call need to
be used locally in the assembly routine. For this purpose, I need
local variables corresponding to each function argument and need to
move the stack value (being pushed due to function call) of an
argument to corresponding local variable. So at a high level whole
thing might look something like this:

//foo.c

x = asm_func(_a, _b, _c);


//bar.s

..section .data

a:
.int 0
b:
.int 0
c:
.int 0

..section .text

..global asm_func

asm_func:

movl 8(%ebp), %eax # a = _a
movl %eax, a

movl 12(%ebp), %eax # b = _b
movl %eax, b

movl 16(%ebp), %eax # c = _c
movl %eax, c


# remaining instructions follow

.

.

.

ret

Apparently, MASM has following syntax for achieving the above:

a equ [ebp + 8]

b equ [ebp + 12]

c equ [ebp + 16]

But GAS's .equ (or .equiv and .set) is not equivalent to MASM's equ
directive. Is there any other (cleaner) way in GAS for assigning the
function
arguments on stack to local variables in assembly code apart from
above?

Another doubt that I have is about writing SMC (self-modifying code)
in GAS. To do SMC in assembly, I need to allow the code area to be
writable. I tried doing ".section .text[rwx]" but somehow it gives me
segmentation fault. In MASM, I think you can alias code and data
segments by using public keyword. I am looking for how to do it in
GAS. I was unable to find any directive in GAS which would allow me to
do that. I read through this article http://asm.sourceforge.net/articles/smc.html
on SMC in Linux but unfortunately the author uses NASM. He seems to be
setting the page attribute bits to be writable etc. Is that the only
way of allowing SMC? Any ideas on this?

Thanks,
Viv

.