Thread: calling 32-bit printf from 64-bit binary?

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by abachler View Post
    the problem is more likely with your disassembler, since the binary for 32 bit instructions and 64 bit instructions are (mostly) identical. It just depends on what mode the processor is in when whether it executes a 32 bit or 64 bit version of the opcode.
    No, the assembler output by the compiler is as above, and it is correct.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Thanks for the explanations. I think I get it now, with just one more question -

    Since x86 is little-endian, doesn't edi represent the most significant dword of rdi? How does it work, then? I can understand if edi represent the least significant bits... Does printf do the shifting?

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    EDI represents the lower half of the 64-bit RDI register - the order those bytes are stored in memory is not important for the purpose of this discussion. When EDI is loaded with 32-bit value and the processor is in 64-bit mode [and that applies even when the current process is a 32-bit process in compatibility mode], the upper half of these registers are filled with bit 31 of the 32-bit value, so loading 0x00001234 into EDI will actually result in RDI containing 0000000000001234. Loading 0x80001234 will result in 0xFFFFFFFF80001234 - note that the behaviour of the upper bits in pure 32-bit mode is undefined, so when going to 64-bit mode, all registers need to be re-loaded to ensure that the upper 32 bits are set - this is usually not a problem tho', as most systems never switch back to 32-bit mode once 64-bit mode has been entered [because there are lots of other things that ALSO need to be changed when switching from one to the other mode].

    It is a common misconception that registers somehow have an "endian" behaviour. The only time that "endian" is involved is when data is accessed as bytes in memory - once it is in a register, the bits go from right to left in the same order as you would do on paper [1].

    [1] Of course, this is a logical left to right - there's nothing saying that the chip-layout people won't stack the bits in two rows, one with bits 0..15 going from left to right, the other with bits 31..16 from right to left - but that's not really what we care about - logically, the bits are number 0 at the rightmost and 31 or 63 at the left of the set.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Oh. I see what you mean now. I was under the misconception that registers have endian-ness, too (bytes in a register). Thanks for clearing it up.

    And sorry for the late reply, for some reason the email notification thing, which I have been relying on, didn't work for this thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  3. creating an array of rects
    By a1dutch in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2006, 06:15 PM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM