rubixibuc
rubixibuc

Reputation: 7407

Assembly Question Regarding defining words using NASM

This an excerpt from my assembly program

First: dw 0xaabbccdd

Now I realize that this is logically incorrect, and I was hoping the compiler would either spit out an error (nasm) or just create two side by side words.

Why didn't this produce an error, and why did this just truncate the first half of the double word? In other words in little endian, this printed in memory going towards higher addresses 0xdd 0xcc. This would make sense if I had wrote First: dw 0xccdd, but not for what I have written. Thanks in advance :-).

Upvotes: 0

Views: 1014

Answers (1)

adelphus
adelphus

Reputation: 10326

The assembler is correct. The literal value is 32 bits long, but the conversion to dw truncates the value to the 16 least significant bits: 0xaabbccdd will get truncated to 0xccdd.

Little-endian format means the value is always stored LSB first, regardless of whether the value is 16 bits or 32 bits. So...

  • 0xccdd will be stored in memory as 0xdd 0xcc
  • 0xaabbccdd will be stored in memory as 0xdd 0xcc 0xbb 0xaa

Hence it makes no difference if the value was truncated - the first two bytes in memory are the same.

As André Laszlo points out, NASM generates a warning for this scenario.

Upvotes: 1

Related Questions