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
|
|
0
|
|
|
|
Reply
|
thisismyidentity
|
2/27/2008 2:26:19 PM |
|
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.
|
|
0
|
|
|
|
Reply
|
DJ
|
2/27/2008 8:03:45 PM
|
|
thisismyidentity wrote:
> 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?
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
|
|
0
|
|
|
|
Reply
|
Frank
|
2/27/2008 8:38:58 PM
|
|
In message <xn8x16cgke.fsf@delorie.com>, DJ Delorie <dj@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
|
|
|
|
Reply
|
Timothy
|
2/28/2008 10:56:02 AM
|
|
|
3 Replies
197 Views
(page loaded in 0.227 seconds)
Similiar Articles: AT&T to Intel Syntax Translation - comp.lang.asm.x86Bonus points if it can handle the range of GAS (GNU assembler) assembler directives as ... comp.lang.asm.x86 ..... cjb.net (already mentioned it in a reply to a question ... Re: GAS and GDB: Breakpoint not "breaking" - comp.lang.asm.x86 ...... Post Question ... breaking" - LinuxQuestions.org gas and gdb - breakpoint not "breaking" I have written a simple program for the GNU Assembler (GAS ... Using labels as immediate operands in GAS with intel syntax - comp ...... Post Question | Groups ... Opcode syntax - OSDev Wiki The AT&T syntax (as understood by GAS, the GNU assembler) is the ... Assembly Console Programming in Windows - comp.lang.asm.x86 ...... Post Question ... For anyone who wants to write assembler code for the Linux x86 platform, I would heartily recommend the GNU Assembler (GAS) as ... Problem with GNU AS - comp.lang.asm.x86... Post Question | ... ll have to use a different build of gcc/gas, or another 16 bit assembler ... comp.lang.asm.x86 - page 69 Problem with GNU ... Intel C++ Compiler (Linux x86_64) - "GNU-style Inline Assembly ...... to - understand, interpret, analyze and write "GNU ... Intel Compiler/Assembler ... - Intel® Software Network ... assembly instruction on Linux ... style syntax (GAS ... Indirect Intersegment Jump - comp.lang.asm.x86... the correct > instruction. > Well, I don't see the 0xe9 jmp in GNU binutils (GAS ... CPU instructions directly suitable for use in data compression (Assembler layman question ... Learning Ideal VS MASM syntax - comp.lang.asm.x86... the book Mastering Turbo Assembler ... GNU as vs nasm? - comp.lang.asm.x86 Learning Ideal VS MASM syntax - comp.lang.asm.x86 My reasoning was if GAS, NASM, MASM, etc. have an ... ASM86 code porting to MASM - comp.lang.asm.x86Currently I have large number of codes by Intel Macro Assembler - ASM86... ... Post Question | Groups | ... HLA for AMD64 (X86-64)? - comp.lang.asm.x86One one question, if you please: the general public was ... code as ... of either the GCC package or the GNU ... The most popular component of the High Level Assembler (HLA ... GNU Assembler (GAS) query - Application Forum at ObjectMix.comGNU Assembler (GAS) query - ASM x86 ASM 370 . This is a discussion on GNU Assembler (GAS) query - ASM x86 ASM 370; Hi all, I have a requirement where I need to ... assembly - Installing GNU Assembler in OSX - Stack Overflow... follow instruction on how to install the GNU Assembler ... excuse me if I am not answering your question ... What are CFI directives in Gnu Assembler (GAS) used for? 7/11/2012 4:45:26 AM
|