Thread: Making programs work on 64bit and 32bit machines?

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    69

    Question Making programs work on 64bit and 32bit machines?

    Well, does anyone have a link to some documentation that explain how to make my code work on both? What do I have to do to my code to make sure it works on both 64bit and 32bit machines?

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    If you don't make invalid assumptions about size of data types, you don't need to do anything. Your code should already work on both.

    Most common mistakes by far is assuming pointers are 4 bytes (assigning pointers to/from integral types) and ints are 4 bytes (though this is true on some 64-bit architectures for compatibility).

    Never assign pointers to integral types or integral types to pointers. They are never guaranteed to be same size.

    If you want a variable of a specific size, use fixed width types in stdint.h. Eg. int32_t, uint32_t, int64_t, uint64_t.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Oh, okay. It's that simple. Thanks for your reply.
    You wouldn't have an answer to my other stupid question, would you?
    How come C can be cross-processor/architecture, but ASM can't?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Different computer architectures will require different assembly code, for a program. Allowing code to be compiled in C for any architecture, by it's own C compiler, was one of the big breakthroughs for the C language.

    You might picture ASM as a beautiful (verbal) language, but it's only known to one country's people. Nobody else speaks it, so nobody can translate it. C, on the other hand, always has a (human) interpreter (in the form of the compiler) - so it's language can be made understandable, for any country.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I program microprocessors and I can tell you a good reason to program stuff in C rather than assembly - Not all assembly languages are the same. If I want to use subroutine/functions that I've developed on a AVR on a PIC I can, even though they have COMPLETELY different Assembly languages.

    For all the things that are device specific (writing to registers to set up interrupts or reading analogue ports), you use the pre-processor commands.
    Code:
    #if defined(__18F8722)
        #pragma config ...
    
        	/* Clear the peripheral interrupt flags */	PIR1 = 0;
    	
    
    
    	// Enable interrupt priority
    	RCONbits.IPEN = 1;
    	
    	// Make receive interrupt high priority
    	// USART enabled in init_usart()
    	IPR1bits.RCIP = 1;
    	
    	/* Enable the timer interrupt */
    	PIE1bits.TMR2IE = 1;
    	IPR1bits.TMR2IP = 0;
    	
    	// Enable all high priority interrupts
    	INTCONbits.GIEH = 1;
    	
    	// Enable all low priority interrupts
    	INTCONbits.GIEL = 1;
    
    
    #elif defined(PIC18F46J50_PIM)
        #pragma config 
    
    #elif defined(...)
        #pragma config ..
    
    #endif
    Another thing that may be an issue are writing to output ports that exist on one device, but not the other.

    Basically, the way you get around this is by defining outputs in a config file the same way as above.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    So... RunPE is able to compile an EXE and have that same compiled EXE run on different Windows machines?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-12-2008, 12:10 AM
  2. 32bit - 64bit portability
    By l2u in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2007, 05:13 PM
  3. prog runs on 64bit - seg faults on 32bit
    By hollie in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:59 AM
  4. Replies: 2
    Last Post: 01-13-2003, 01:28 PM
  5. Making super-packed programs?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 09-26-2002, 05:01 AM