Thread: Assembly

  1. #16
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by samGwilliam
    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)?

    in my vc++ _asm is for one liners. __asm{} is for blocks

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    
    	int x = 65;
    	int *y = &x;
    	char format[] = "%i";
    	
    
    	printf("X equals: ");   
    	_asm mov eax, y;       //move the address of x into eax
    							//could have been placed in next block
    							//just using it as an example
    	__asm
    	{
    	
                      push format;        //push format string onto stack
                      push eax;			//now eax (the address of x)
    		call printf;        //call C's printf
                      pop eax;            //clean up the stack
    	}
    
    	_asm pop eax;           //clean up the stack 
    							//also could have been place inside __asm{}
    	return 0;
    }
    edit: this is probably not good or safe code. i've only dabbled in the realm of asm w/ c/c++
    Last edited by misplaced; 03-30-2005 at 02:07 PM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  2. #17
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    That underscore business is to avoid breaking existing code.

    For example, the type bool hasn't always been around, so it used to be that people conjured up their own way of storing boolean values. When C99 included a boolean type, it was named _Bool so as not to conflict with the various definitions of boolean types that existing programs used.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #18
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    ...........none the less, my above 'program' won't compile if i mix up _asm and __asm
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #19
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Then don't mix them up
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #20
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    The _s are added in front to show that they are compiler-specifc keywords. This way, if someone wants to define an integer called asm (which is perfectly fine by the C++ standard), there won't be any confusion over what they meant.

  6. #21
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Quote Originally Posted by Bubba
    Which is why I don't like MASM. It's too political, I'd rather use NASM where nothing is assumed.



    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.'
    But if you use something like MSVC's "naked" qualifier for a function definition (as I did here http://cboard.cprogramming.com/showthread.php?t=43552) then you forego that problem, as the C++ code sees it as another CALL instruction for which it doesnt need to "de-optimize". IA32 defines rules for dealing with registers over a CALL, and the compiler should'nt differentiate between a CALL to a function written in C or C++, or a call using "naked".

    With such a method, you end up achieveing exactly what you do with linking in an object file from an assembler, and with far less hassle. The only downside is that MSVC is a little more strict with the MASM syntax than the assembler, so if you like using lots of macros or some of the more esoteric features, then you are better off linking object files.

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