Search code examples
unixintegertimestampcpu32bit-64bit

Use 64bit timestamp on a 32bit machine


We all know the Y2K problem , and this problem will arrive soon in 2038. All the Solution i was read it was say "use 64bit OS" , so i have a question; If my program was compiled with any 64-bit platform it`s possible to running on a machine that only work with 32-bit for example like old Pentium CPU? I was read some resource that say the int_64bit can be represent on 32bit machine by using two 32bit integer.


Solution

  • Simple Answer is

    • A program compiled for 64 bit platform can not run on 32 bit processor.

    • Timestamp is mainly application specific and Your programming language takes care of handling it and Operating System will take care of that.

    See you need to understand 2 things

    1. Compiling on a 64 bit platform doesn't mean you application is 64 bit. You can compile a 32 bit application on 64 bit platform.

    2. Now see under the hood everything a program or an application that you run on you computer is executed by the processor, so every program is converted or object code or machine code at the end moment before its being executed by the processor and these "bits" actually represents the size of "registers" (kind of memory that processor uses) that your processor has. So the point is whenever a high level language like program C++ is converted to machine code. The compilers follow these steps (in simple terms)

      Your Source Code ---> Convert to System specific Assembly ---> Binary Code

    Then this binary code is executed by the processor. Now see that I mentioned Assembly language is machine/processor specific. Its different for ARM processors, x86 Processors, PowerPC Processors etc..(these are processor architectures)

    Now in your case lets assume we are now thinking how an 32 bit Intel processor (x86) is gonna run 64 bit Program

    You wrote a program for adding to numbers 10 and 20 and compiled the binary for running directly on a processor (for time being forget about OS and Its Libs)

    Now lets see "Your Source Code" converted to both 32 bit Assembly Program as well as 64 Bit Assembly Program

    Assembly Output (32 Bit or x86 Code)

        mov eax, 10     
        add eax, 20 ;OUTPUT IS SAVED TO "EAX" REGISTER WHICH IS A 32 BITS REGISTER 
    

    Assembly Output (64 Bit or x86_64 Code)

        mov rax, 10
        add rax, 20 ;OUTPUT IS SAVED TO "RAX" REGISTER WHICH IS A 64 BIT REGISTER
    

    Now these Assembly program is line by line converted to binary code When you run the output binary of the 64 bit code on a 32 bit processor then 32 bit processor doesn't even know what "RAX" is, that will immediately cause an Interrupt (Error from Processor)

    Image of a Processor Register is shown in the figure (x86_64 specific)

    Image of a processor register

    There are many registers available inside a processor for simplicity I'm just using EAX and RAX register of x86_64 processor. For an x86_64 Processor (64 bit) knows what these "RAX" and "EAX" means. But a 32 bit Processor(x86) wont understand what an "RAX" means. Thats the only reason why 32 bit programs can run on a 64 bit machine and the opposite is not possible....

    Note : Its not just Registers there can be processor specific instructions. All the instructions that is supported by a newer processor may not be supported by an older processor.

    Example: " imul reg, reg " (Integer Multiplication) instruction is only available from Intel 80386 and above

    I know its a bit confusing. Its a bit complicated topic to explain. Still I hope this solves your issue.