0

I am quit not sure about this problem for a long time and want some help.

Given the following 2 - bytes number y which is equal to: 0x0110, that's declared in assembly in data section, how is it stored in memory?

In little endian lower bytes are stored in lower memory addresses so we should see something like this:

At Adress 1001 : 01
At Adress 1000 : 10

Now in assembly x86-64 (att) when I so addb y, %al which option is correct:

  1. it always reads 10.
  2. it always reads 01.
  3. reads 01 in little endian and 10 in big endian.
  4. opposite of 3.

In other words, is y the lower of higher byte?

  • 1
    `Adress X+8` - huh? memory is byte addressable, so +8 is the next qword, not the next byte. Also, if you're talking about x86 with AT&T syntax, `addb` only reads a single byte. And you left one operand unspecified as `??` so I don't know which byte you're talking about. – Peter Cordes May 22 '21 at 23:32
  • But in general, x86 is always little-endian, and byte-loads on normal bi-endian CPUs always load from the byte address specified, they aren't affected by whether the CPU is in big or little-endian mode. What changes is the number you get from a wider load, which byte is at the bottom where a right shift by 8 would shift it out of the register for example. – Peter Cordes May 22 '21 at 23:34
  • @PeterCordes corrected all, so which claim is correct between the four? –  May 22 '21 at 23:34
  • So what's `y`? Is it the same as `Address X`? (I think you mean an instruction like `addb y, %al` - would be clearer to write that instead of ?? since you're already making up an example with add instead of just reading it with mov). – Peter Cordes May 22 '21 at 23:39
  • Also, when you say you have `y=0x0110`, are you saying the number might be stored with an endianness other than native, so e.g. if you loaded it with `mov y, %ax`, you'd get `0x1001` if it was in big-endian format? So you're asking about handling big-endian data on little-endian x86? Not about CPUs like MIPS that can operate in either mode. – Peter Cordes May 22 '21 at 23:40
  • I meant the usual way numbers are declared in assembly in data section. @PeterCordes updated question again. –  May 22 '21 at 23:42
  • 1
    Oh, if you have `y: .short 0x0110`, then it's *always* in little-endian format. x86 doesn't have a big-endian mode. So you'd of course always read `0x10`, the byte at the address the label `y:` is attached to. – Peter Cordes May 22 '21 at 23:43
  • so we read from lower addresses to higher ones? plus a small question I wrote x86-64 (in which we have 64 bits registers like %rax) is it the same as x64 or x86? –  May 22 '21 at 23:45
  • Little-endian word/dword/qword loads read from low to high, yes. Byte loads just read the byte you tell them to, there's no direction involved for the byte load. (The whole concept of endianness comes about because it's possible to use byte loads to access the individual bytes of a larger word) – Peter Cordes May 22 '21 at 23:56
  • Re: architecture naming: [The most correct way to refer to 32-bit and 64-bit versions of programs](https://stackoverflow.com/q/53364320), and [Why does x86 represent 32bit when x64 represents 64bit?](https://serverfault.com/q/188177) re: Microsoft's choice to define "x86" as 32-bit specifically. To other people, x86-64 is an official name for the ISA, "x64" is a synonym invented by MS, and "x86" is a catch-all that includes x86-64, but also older CPUs. e.g. for statements like "x86 has a strongly ordered memory-model", or "modern x86 has 256-bit SIMD instructions". – Peter Cordes May 22 '21 at 23:58

0 Answers0