Thread: Assembly

  1. #1
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382

    Assembly

    I have learned assembly before and, although I'm very rusty, I still understand the principles. Thing is, I never learned how to integrate an assembly file into my C/C++ project. I wouldn't know where to start with that sort of thing, so does anyone possess such knowledge? I'd love to start writing more optimal assembler routines for my projects (I'm getting into digital signal processing).

  2. #2
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    It all depends on your compiler.

    MSVC++ allows you to integrate full Intel assembly code with the __asm identifier.

    An example:

    Code:
    __asm{
    push eax
    mov eax, 5
    }

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Another option is to assemble the .asm source into a .obj file and then add it to the list of files to be linked, you'll need an assembler for that.

  4. #4
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    I'm aware of the inline assembler, but I read that before and after each block it backs up the registers (or something like that) so any gains made are lost there. It's making a piece of assembly code callable by a piece of C code that troubles me.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    To add to my earlier post:
    I think .obj files are specific to windows and that on unix/linux the files are .o. Also in your c/c++ source code define the function prototype as extern and at link time the linker will find those functions in the added object files.

  6. #6
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Thanks, this is useful. I've never had cause to use the extern keyword before and didn't really know what it was for (although I guessed it was something like that).

    How do you define a function in assembler?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    The only assembler I've ever used is MASM in that assembler a function would be like this, I think it is similar in others
    Code:
    MyFunction PROTO ;prototype of function
    
    MyFunction PROC
       xor eax, eax ;return 0
       ret
    MyFunction ENDP
    I hope I'm not forgetting anything as its been a while since I did anything in assembly.

  8. #8
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Quote Originally Posted by samGwilliam
    I'm aware of the inline assembler, but I read that before and after each block it backs up the registers (or something like that) so any gains made are lost there. It's making a piece of assembly code callable by a piece of C code that troubles me.
    http://msdn.microsoft.com/library/de..._attribute.asp

  9. #9
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Quote Originally Posted by Quantum1024
    The only assembler I've ever used is MASM in that assembler a function would be like this, I think it is similar in others
    Code:
    MyFunction PROTO ;prototype of function
    
    MyFunction PROC
       xor eax, eax ;return 0
       ret
    MyFunction ENDP
    I hope I'm not forgetting anything as its been a while since I did anything in assembly.
    Seems easy enough. What about writing asm functions that take arguments? Also does the return value automatically get retrieved from eax?
    Last edited by samGwilliam; 03-29-2005 at 11:58 PM.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    yes, to return a value you would place it into eax and then return from that function.
    using arguments in MASM is easy.
    Code:
    MyFunction PROC arg1:DWORD, arg2:DWORD
    
    MyFunction ENDP
    Thats using the high level syntax cababilities of MASM you can also set things up manually.

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Asm code
    Code:
    .386
    
    public MyFunction
    
    MyFunction PROC  arg1:DWORD, arg2:DWORD
    
    MyFunction ENDP
    C module
    Code:
    //Declare prototype for MyFunction
    //Specify C to prevent name mangling
    //Specify extern because the body of this function lies outside of
    //the current module
    extern "C"  MyFunction(DWORD arg1,DWORD arg2);
    
    int main(void)
    {
      MyFunction(10,20);
      return(0);
    }
    In C/C++ all integral return values are returned in EAX and all floating point return values are returned in ST(0).

    Also you should know which calling mode is being used as this determines in what order the parameters are pushed onto the stack.

    But for all 32-bit programs:

    EBP - last value of EBP
    EBP+4 - address of caller - return address
    EBP+8 - first parameter starts here

    When mixing pure assembly code and C/C++ you will always want to use the following if you are doing anything with parameters:

    Note this is TASM syntax, not MASM:

    Code:
    MyFunction PROC 
    ARG value1:DWORD,value2:DWORD
    
     ;always start with this
     push  ebp
     mov ebp,esp
    
    
     ;always end with this
     pop ebp
    MyFunction ENDP
    Failing to do this will crash the compiler and perhaps even Windows - yes, even XP. This is a requirement in TASM when using the ARG directive and I'm sure it probably is in MASM as well.
    NASM also must conform to this standard in order to function correctly when being mixed with C/C++ code.
    Last edited by VirtualAce; 03-30-2005 at 08:02 AM.

  12. #12
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Check your compiler, there may be a way of allowing you to enter the asm code into a procedure block without the compiler adding code to that block. THis will then allow you to do the same thing without using a seperate assembler

  13. #13
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Here's another question along the same lines: Why do some keywords have a normal version and a version with two underscores preceding (asm/__asm, for example)?

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    @samGwilliam
    I havn't come across an 'asm' keyword but I have seen '_asm' and '__asm' bot do the same thing, I don't know why there are too maybe for compatibility?
    @Bubba
    Nice complete post I had forgotten about using the "c" directive.
    In MASM you don't have to use
    Code:
    push  ebp
    mov ebp,esp
    ;...
    pop ebp
    The assembler adds the code to set up the stack itself when it encouters the PROC dirctive and ebp is popped by the ret macro.

  15. #15
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Which is why I don't like MASM. It's too political, I'd rather use NASM where nothing is assumed.

    Check your compiler, there may be a way of allowing you to enter the asm code into a procedure block without the compiler adding code to that block. THis will then allow you to do the same thing without using a seperate assembler
    Yes, there is a way to tell MSVC to keep it's grubby hands off of your assembly code - but you cannot guarantee that you are optimized to the max while doing it. For instance if MSVC was using a register prior to your code and then you use the same register....you have a problem.

    Here is how the compiler deals with it. Since you have no idea what registers the code is actually using because, for the most part, all you see is C/C++ code. So when the compiler sees that you want to use a register that it is already using, it pushes the value of that register onto the stack. When you are done with it, or when it deems you are done with it, it pops it off the stack and resumes what it was doing.

    In DJGPP and AT&T syntax there is a way to specify which registers get 'fragged' so the compiler can better use registers prior to and after your code.

    But with modern processor speeds, I doubt register problems will amount to much.

    And I might add that Fordy pointed something very important out to me not long ago about inline assembly. If you are going to need to access the stack, do it from your assembly code. Don't attempt to use temporaries and then load those in your code. It looks nicer, but it is significantly slower. I'm sure with some searching you could probably find this thread on the board in a search for 'Bilinear interpolation.'
    Last edited by VirtualAce; 03-30-2005 at 02:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learning Assembly
    By mrafcho001 in forum Tech Board
    Replies: 5
    Last Post: 03-12-2006, 05:00 PM
  2. C to assembly interface
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 02-04-2005, 03:51 PM
  3. assembly language...the best tool for game programming?
    By silk.odyssey in forum Game Programming
    Replies: 50
    Last Post: 06-22-2004, 01:11 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. C,C++,Perl,Java
    By brusli in forum C Programming
    Replies: 9
    Last Post: 12-31-2001, 03:35 AM