Thread: Assembly in C code?

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Question Assembly in C code?

    Can you in some way implement assembly code in the middle of your C code?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    It depends on what compiler you are using. Read the compiler's documentation on how to embed ASM code in your C program.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    Isnt C just a higher level ASM?

    Sorry for the off topic question, just i dont feel like googling it :P

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Yes and no. If I recall, a compiler will transform C code into ASM code and then put the ASM code into binary form. Don't quote me on that though.

  5. #5
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    in gcc, but it's backwards worthless at&t syntax
    Code:
    asm("instruction");
    asm("instruction\n\t"
           "instruction\n\t" ...);
    With lots of extra stuff you can do, lookup inline-assembly with gcc.

    in cl it's
    Code:
    __asm { asm stuff }
    __asm instruction
    A compiler transforms code into assembly which is english binary, a 1:1 translation which is then assembled and linked into an executable format like elf or exe.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Assembly is the syntax of machine language.

    Let's say return is 0x7F. So anywhere in an executable file you have 0x7F, disregarding prior context and any previous opcodes occurring, this would execute a return from a subroutine back to the caller.

    But 0x7F could also represent an address, an offset into a segment, a segment, a portion of a descriptor, an immediate value, etc, etc.

    Data is simply just numbers. It's how data is interpreted that matters.

    A simple linear interpolation in x86 asm using the FPU.


    Code:
    proc   LinearInterp
    ARGS v1:QWORD,v2:QWORD,Interp:QWORD
    
    push  ebp
    mov   ebp,esp
    
    fld [v2]
    fsub [v1]
    fmul [Interp]
    fadd [v1]
    
    pop ebp
    ret
    endp
    Inline asm
    Code:
    float LinearInterp(float v1,float v2,float Interp)
    {
      __asm {
    
       fld [v2]
       fsub [v1]
       fmul [Interp]
       fadd [v1]
       
      }
      return st(0);
    }
    Last edited by VirtualAce; 06-26-2006 at 12:02 AM.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    You can read this document if you are using Ms. Visual C++.
    Link

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Isnt C just a higher level ASM?
    No.

    >If I recall, a compiler will transform C code into ASM code and
    >then put the ASM code into binary form.
    Not necessarily, though it's up to the compiler. It's generally easier to translate C into assembly and then assembly into machine code (gcc/as is a good example), but it's more efficient to translate C directly into machine code. Since compilation time is an issue among compiler writers, the most efficient way usually wins.
    My best code is written with the delete key.

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by valis
    in gcc, but it's backwards worthless at&t syntax
    As opposed to the 'highly valuable' Intel syntax?

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    In MS Visual C++, all assembly code is compiled 'as-is' without the compiler touching it. This is good and bad.

    For one you have no idea what the state of the registers are upon entry to the asm block. You do not know which registers Visual Studio is using and for what reason prior to the asm block. I'm sure that Visual Studio may do a pushf and as well save all the register states, but this means that every inline block of asm would incur a slight amount of overhead due to the register/flags preservation.

    Inline assembly can be good, but it is not always going to be faster than just doing the same code in C/C++. You must choose your battles wisely with inline assembly and examine the final assembly source in order to really see what is going on.

    Use it with caution.

    And yes, AT&T syntax sucks ass.

  11. #11
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    If I recall, a compiler will transform C code into ASM code and then put the ASM code into binary form.
    Often modern compilers will translate code into an intermediate, low level language that can resemble assembly in many respects (could resemble anything since it's just another language). This is done so that it may then easily be compiled to native assembly for many different architectures.

  12. #12
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    HA I love getting you guys talking and informing me on the good and bad :-)

    Oh and I have to ask, what is AT&T syntax?

    Is it worse the java?

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Modern compilers under Windows will assemble to MSIL which is a pain in the arse. MSIL is MS's attempt to get all the languages to 'get along' with one another. It has nothing to do AFAIK with platform independence - a concept which I believe is not even in the vocabulary in Redmond, WA.

  14. #14
    Disrupting the universe Mad_guy's Avatar
    Join Date
    Jun 2005
    Posts
    258
    In a lot of compiler's there's an intermediate code generation phase in which your input (after being lexed, parsed and semantically checked; aka after being processed by the front end) is taken and translated into a 'easily readable, easily compilable' intermediate stage. The basic standard for this language is pretty much that it's easily readible and easy to generate machine code from. The reason a lot of compilers take this option is so that they can then optimize that intermediatary input as needed to still produce the correct ouput but do it in a more 'efficient' manner (although *how* the compiler optimizes it is really up to what you want these days, i.e. size vs. speed.) Also, this allows you to easily swap out the back end of the compiler (the part that generates the machine code) with another so you can port the compiler inbetween architectures. That is, you only have to rewrite a very small portion that is 'modular' rather than grounded to one specific architecture; it would be much easier to take an already existing compiler and swap out the part that generates x86 assembly with SPARC assembly rather than just rewrite the whole compiler to get it to work on a SPARC machine. A lot of people have gone through the trouble of making/researching compilers for several languages, who's front ends all generate the same intermediatary language so that you could for example, write Java code and C++ code and stick them together. This field has only made so much progress however, as there are all sorts of little semantic 'gotchas' that a language can throw at you. .NET is a very good attempt at language interoperability so far though (probably the best), but I will say it isn't perfect and there are things that still need to be done.



    in gcc, but it's backwards worthless at&t syntax
    There's an intel option you can use that will let you use intel-based syntax in your inline assembly.

    Assembly is the syntax of machine language.
    Assembly is the delineation of the processor's opcodes that it executes directly. Normally it is very processor specific, but it is also very tool specific which is why there isn't really a standard defined for using assembly on a certain machine. It all depends on what assembler and processor you're writing for.
    operating systems: mac os 10.6, debian 5.0, windows 7
    editor: back to emacs because it's more awesomer!!
    version control: git

    website: http://0xff.ath.cx/~as/

  15. #15
    ---
    Join Date
    May 2004
    Posts
    1,379
    Mad_guy: Please please please use paragraphs
    As opposed to the 'highly valuable' Intel syntax?
    Well I prefer Intel syntax. It looks much cleaner and it makes more sense to me.
    Oh and I have to ask, what is AT&T syntax?

    Is it worse the java?
    lol I found that quite funny.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-02-2008, 10:00 PM
  2. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 01:30 PM
  4. The relationship between C++ and assembly and machine code
    By TotalBeginner in forum C++ Programming
    Replies: 5
    Last Post: 04-22-2002, 02:46 PM