Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

GNU Assembler (GAS) query

14 views
Skip to first unread message

thisismyidentity

unread,
Feb 27, 2008, 9:26:19 AM2/27/08
to
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

DJ Delorie

unread,
Feb 27, 2008, 3:03:45 PM2/27/08
to

For the macros, you can use the C preprocessor by renaming your source to .S
instead of .s and using gcc to compile it:

gcc -c foo.S

Then you can use #defines et al.

For self modifying code, just use .data for those functions.

Frank Kotler

unread,
Feb 27, 2008, 3:38:58 PM2/27/08
to

I've seen it done like:

ARG1 = 8
ARG2 = 12
...

And access 'em as "ARG1(%ebp)"... Not quite as clean as you'd like...
(could always use Nasm...)

> 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?

Using Nasm, I can do:

section .text write

and using objdump on the .o file, I can see that Nasm has obediently
flagged my .text section as writeable. But after linking with ld, .text
has become readonly - ld apparently "knows" that .text isn't supposed to
be writeable. Probably a command line switch or a "script" would fix it.
Naming the section "kode" or something works. If your "[rwx]" syntax is
correct for Gas, the same trick should work(?). You can also copy the
code to be modified to the stack, and run it from there, I guess...

Best,
Frank

Timothy Baldwin

unread,
Feb 28, 2008, 5:56:02 AM2/28/08
to
In message <xn8x16c...@delorie.com>, DJ Delorie <d...@delorie.com> wrote:

> For self modifying code, just use .data for those functions.

Just putting code in .data won't work in many modern systems as .data will
be marked as non-executable. You may need to use sys_mprotect if using
Linux.

0 new messages