Search code examples
64-bit32-bitcpu-registersarithmetic-expressions

Computing 64-bit values in 32-bit mode


I'm writing a C99 compiler that works with 64-bit values.

For starters, this will compile 32 bit and 64 bit code. On 64 bit operating systems, I know I can use the r[]x registers. But for the 32 bit code, how do I do it.

I've tried loading the value into 2 registers each (4 total), but that doesn't work on big arithmetic. Googling how GCC/LLVM works brings up garbage, and I don't want to scrounge their source code.

I'm hoping you guys here can help me do this.


Solution

  • The r64 registers are 64-bit only. There is no legitimate way to access them (even on a 64-bit processor) from a 32-bit program.

    For load you do:

    mov r1, LO
    mov r2, HI
    

    For add you do

    ; allocate unused reg r1:r2
    mov r1, LO_1
    mov r2, HI_1
    add r1, LO_2
    adc r2, HI_2
    jc _int64_add_overflow
    ; result is r2:r1
    

    Subtract is symmetric:

    ; allocate unused reg r1:r2
    mov r1, LO_1
    mov r2, HI_1
    sub r1, LO_1
    sbb r2, HI_1
    jb _int64_sub_underflow
    ; result is r2:r1
    

    For multiply, mod, divide and shift you'll probably want to inject a call to a function in the runtime that'll perform the operation and return the result on EDX:EAX.