0

I've seen by using gdb that when bytes are copied from memory into registers they are reversed, if the system in use adopts the little endian approach.

Since, at the best of my knowledgs, the x86 assembly instruction set does not allow operations whose operands are both in memory, I was wondering: is there any operation which is performed directly on little endian values without being reversed first?

badnack
  • 737
  • 1
  • 11
  • 20
  • You can have 2 memory operands, but at least one of them must be implicit. See for example `push`, `pop` or `movs`. I don't understand the rest of your question, or why it would need 2 memory operands. For example `add [foo], eax` is legal and operates on memory. – Jester Jun 23 '16 at 22:33
  • What I meant was: consider the operation add -0x8(%ebp), %eax. What it does is to get the first value from memory, reverse it and then add it to the content of eax. Finally the result is stored in eax. My question is: is there any operation (besides those for moving memory around) which does not reverse the memory operand? – badnack Jun 23 '16 at 22:46
  • 1
    it is not "reversed", it is in memory in proper order and read from memory in proper order, ls byte lane first, ms byte lane last, the controller puts the bytes on the right byte lane, just like on a big endian machine the controller puts the bytes on the right byte lane. x86 allows unaligned so it is even worse than that it has to take bytes that are in any of the lanes and move them to the correct lane (either on the bus or after inside the processor core, depends on the design). – old_timer Jun 23 '16 at 22:57
  • anything that is byte based does not have an endianness, or at least from the way you are interpreting things. where that byte is in the 32 or 64 bit memory based on address is still very much an endianness thing (and has to move to the correct byte lane on the bus). But there is no "reversing" as you are calling it. – old_timer Jun 23 '16 at 22:58
  • What do you think of `add %eax, -0x8(%ebp)` ? – Jester Jun 23 '16 at 23:12

1 Answers1

2

This answer may appear a little bit strange to the experienced folks, but what the questioner is looking for is called MOVBE. It does copy the data as is(!)(relating to his arguments) to a register. It is not available on all architectures, but still the best solution to this specific problem. So the answer to

I was wondering: is there any operation which is performed directly on little endian values without being reversed first?

is yes: MOVBE does copy the bytes in the required order.

zx485
  • 28,498
  • 28
  • 50
  • 59
  • 1
    I believe that the experience folks where trying to say that, well, `movbe` is the one that reverse the bytes. Endianess is due to the internal wiring of the processor, of course Intel wasted no transistors for the sake of reversing words. You seems to spread the misunderstood that a register value is the numeral used to display it. We don't know how register like RAX is loaded, all we know is that the byte at low address goes into AL (the low byte) and vice versa. If AL is actually bits 56-63 or bits 0-7 we don't know. – Margaret Bloom Jun 24 '16 at 17:01