Thread: how to use variables within __asm__()?

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    how to use variables within __asm__()?

    hi!

    how do i use C variables within a __asm__() statement?

    assume i have the following function:
    Code:
    void doit(void)
    {
      __asm__("movl $0x1, %eax\n"
                    "movl $0x1, %ebx\n"
                    "int $0x80\n");
    }
    and i want to use hardcoded variables instead of $0x1 for example.

    how would i do that?
    i tried
    Code:
    __asm__(".include \"defines.asm\"\n");
    but that didnt seem to work.

    can i hardcode the values of the variables in a C header file and use them afterwards from __asm__()?

    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    That would depend entirely on which C compiler you have.
    Everything about asm() is implementation-specific.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    im using gcc 3.3.1 on a linux x86 machine...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Try here
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    No matter which implementation you use, you have to understand that an assembler may only deal with variables via registers or register references to RAM.

    Registers are limited in number. The more 'C' variables you use in an inline assembler function, the more the compiler has to juggle real registers to handle your needs.
    It is not the spoon that bends, it is you who bends around the spoon.

  6. #6
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    thanks for the replies. i think i got it working now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. Replies: 6
    Last Post: 01-02-2004, 01:01 PM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 02:39 PM