Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare and initialize local variables in gcc inline assembly without extended inline asm?

I know this is a very basic question but I am really stuck on it. In fact I am absolutely newbie in GCC syntax.

I want to have local variables (Stack addresses with labels) without using extended inline assembly. Something like the following code in Intel syntax:

DATA1  DB  100 
MOV AL, DATA1

This is the code I guess may substitute in GCC:

int someFunction(int x)
{
    __asm__ volatile(
                     "function1:"
                     ".data;"
                     ".2byte $4 data1   ;"

                     ".text;"
                     "pushq %rbp;"
                     "movq %rsp , %rbp ;"

                     "movl var , %eax;"  // this is source of error

                     "popq %rbp;"
                     "leaveq;"
                     "retq ; "
                    ); 
}

But this code results in this error:

symbol(s) not found for architecture x86_64

I can use global variables in x86 but the same result comes in x64 or x86_x64.

Setting: LLVM 4.1; Cocoa used in Xcode 4

What is the correct syntax?

like image 688
Aug Avatar asked Jan 04 '13 10:01

Aug


1 Answers

GCC inline assembler doesn't support local variables, use GCC's extended syntax.

If you are uncomfortable with AT&T syntax there are ways to use Intel syntax on GCC.

This is an excellent how-to on GCC asm.

like image 157
istepaniuk Avatar answered Sep 18 '22 06:09

istepaniuk