What's the difference between a 32-bit and 64-bit program in Windows

9,159

Solution 1

Disclaimer: I've never written an OS in my life. Better wait for someone more competent to answer, and read some OSDev articles in the mean time.

Does the program have a piece of code telling Windows that it's a 64-bit program or does Windows simply fail to execute it?

It's both.

  • All Windows executable files (.exe, .dll, … – PE executables in general) have a header telling the OS what architecture they were compiled for. It could be Intel x86 (16-bit or 32-bit) or x86_64 or Itanium or Alpha AXP…

    If the OS sees an incompatible architecture in the header, it will completely refuse to run the program. In other cases – if you try to run an x86 program on x86_64, for example – it will know that the program needs to be run in a different mode than the rest.

  • For another, the actual machine-code instructions inside the program are different. (That's kind of the whole point of having different architectures, by the way.) The 64-bit x86_64 architecture has more instructions than 32-bit x86 had, and 32-bit x86 had more instructions than 16-bit. (And things like Alpha or ARM or Itanium are so different that there's very little that could even be compared.)

    (Even different x86 CPUs have different sets of instructions – over time, they accumulated several dozens of extensions on top of the basic ones, e.g. "SSE" or "MMX" or "AES-NI" instruction sets, so it's possible that a program won't run on old CPUs like Pentium if the compiler was allowed to optimize it for the newest ones.)

    And even for basic instructions that are the same across both architectures, the data sizes can be larger, and most importantly, memory addresses are larger – 64-bit architectures are usually called "64-bit" because they use 64 bits to express locations in memory, and so on.

    So even if you hand-edited the header to tell the OS a different architecture, you'd only end up with a program that crashed every time.

  • On the other hand, x86_64 still has the same instructions that x86 did (it simply adds more of them), so it's relatively easy for the OS to run 32-bit programs while in 64-bit mode; it mostly just takes care to only use x86 features when dealing with that program. However, the same doesn't apply in the other direction – even if the 32-bit OS had been written to switch the CPU to the 64-bit mode for your program, then back, it could still easily happen that the program did something that the OS couldn't cope with.

If you have the source code, you can easily compile it for different architectures (assuming it was correctly written – some programs still have assumptions like memory pointers always being 32 bits wide…) But if you just have a compiled binary, you're out of luck.

Solution 2

The number of bits in this case refers to the size of memory addresses used by the computer.

A 32-bit memory address system can point to up to 2^32 (4294967296) different memory locations (which is exactly 4GB of storage) while 64-bit can address can point upto 2^64 locations (~16 trillion GB).

Why can't a 64-bit program run on a 32-bit copy of Windows?
A 32-bit program stores the various locations where it has stored its data in 32-bits. And since it is possible to store 32-bits in 64-bits of storage, Windows will happily run the program while filling the other 32-bits of the 64-bit appropriately.

A 64-bit program on the otherhand uses 64-bits to indicate the locations. Since it is not possible to store 64-bits in a 32-bit storage without throwing away half of it, Windows will refuse to run a 64-bit program on a 32-bit computer.

Does the program have a piece of code telling Windows that it's a 64-bit program or does Windows simply fail to execute it?
Every program has a header, that tells Windows, every thing it needs to know about how to run the program including if it is a 32-bit application or a 64-bit application.

I saw a significant difference in file size of the two versions of the same program, so I think it's the code that's different. Is it possible to modify the code and make the program run?
64-bits occupy twice the amount of memory as 32-bits. That is why 64-bit programs are larger than their 32-bit counterparts.

These memory locations only make up a fraction of a program; the majority of it being instructions (which remain the same size) and stuff like text and icons. Since only the memory locations double in size, the 64-bit version of a program is only slightly larger than being double the size.

Hope this helps.

Solution 3

Practically, there are two main difference between 32bits and 64bits executable. Size and speed.

64 bits, access to more memory

32 bits means that the address space of a program ranges 2^32 integer values. Which mean a program can only know how to count up to 2^32; thus it can only ever reference memory within that range. That's about ~2GB.

  • 32 bits program can see/use at most ~4GB of memory. In practice, it's less than that since some of that range is reserved for OS/code.

Because 2^64 is a much larger number, it's obvious that 64bits programs can access a much wider range of values; they can see/use a lot more memory. Large enough that you likely don't know the SI prefix: 16 exbibytes. What's an exbibytes? Large enough that nobody will really care for the foreseeable future.

  • 64 bits program can (practically) refer to all the memory it could ever want (in this decade).

32 bits, consumes slightly less memory, runs slightly faster

So it seems obvious that all software should be compiled for 64bits. That's pretty much true, except for one thing: 32 bits takes less space than 64 bits. This has two major consequences:

  1. 64bits program take up more space. Each memory reference (pointer) takes double the size. That difference is usually negligible.
  2. 64bits program run a bit slower. Because pointers are larger, the data in memory is less tightly packed together; the various levels of cache in your machine get filled with less data, leading to more cache misses. Good cache utilization (memory locality) is essential nowadays for performance software, because latency. Also, some arithmetic operations take time proportional to the bit width of the integer; some CPU instructions are slower on 64bits vs 32bits.
Share:
9,159

Related videos on Youtube

Avasyu
Author by

Avasyu

Updated on September 18, 2022

Comments

  • Avasyu
    Avasyu over 1 year

    Why can't a 64-bit program run on a 32-bit copy of Windows? How does my PC know that I'm running a 64-bit program? Does the program have a piece of code telling Windows that it's a 64-bit program or does Windows simply fail to execute it?

    I saw a significant difference in file size of the two versions of the same program, so I think it's the code that's different. Is it possible to modify the code and make the program run?

    • tvdo
      tvdo over 10 years
      Windows won't run it because the executable header says it's a different architecture. But even if you modify the header, you won't be able to successfully execute it. You'd need to recompile from source - or run it under an emulator.
  • Avasyu
    Avasyu over 10 years
    I did not understand your explanation of why the 64-bit program won't be twice as large. If they contain memory locations as well as instructions and data, shouldn't it be more than twice as large?
  • user1686
    user1686 over 10 years
    @Avasyu: No, because the program doesn't consist entirely of memory addresses – that's only a small part of it. The majority is instructions [which remain mostly the same size themselves] and various resources [like dialog layouts, icons, etc. – i.e. not code].
  • Peter Hahndorf
    Peter Hahndorf over 10 years
    Good explanation. There is one aspect that needs clarification: 'relatively easy for the OS to run 32-bit programs while in 64-bit mode'. 32-bit program don't run in 64-bit mode and it is not easy to do that. They run in a different Subsystem of the OS, in this case in WOW64 (Windows on Windows 64). In certain versions of Windows you can remove WOW64 and no 32-Bit Windows program will run in such an environment.