Reputation: 453
Does .word
(and others) command change byte-order?
For example:
.byte 0xA
.byte 0xB
Should I use .word 0xAB
or .word 0xBA
?
Upvotes: 2
Views: 4503
Reputation: 8002
Endian-ness only describes the order that the bytes are stored. When you view the stored bytes as words, dwords, etc, they are always displayed with the most significant byte first. So in your example if you did
.word 0x0A0B
A little endian system would store these bytes as 0B 0A. A big endian system would store the bytes as 0A 0B. However, if you're viewing bytes as a word, it's always 0x0A0B, regardless of how they're stored.
In your example above, if you wanted to lay down two byes in little endian system, and you wanted ensure they're in the order 0A 0B (e.g., that's how you want them to be stored in memory) you should use
.byte 0xA
.byte 0xB
or
.word 0x0B0A
However, it's strange that you would be concerned with byte ordering like this. Typically, if you are defining words, it's because you want to retrieve that memory later (as words) to perform operations on, and thus the way they're stored (endianness) shouldn't matter.
Upvotes: 4
Reputation: 7257
From human's point of view - if you write (for example on paper) a hex number - say 0xABCD - it means that you are expressing some value using positional system with base 16. Right now your mind should not worry about endianess at all. When using hex you'll always write this number as 0xABCD and your digits (A, B..) must be always written on the same positions to keep the same value.
Writing the number on paper is not much different than writing it in either assembler or C/C++, java....
For example - hex number 0xABCD in some languages:
Java:
short s = 0xABCD;
C/C++:
short s = 0xABCD;
MASM/TASM:
s DW ABCDh
Regardless of language used - all above uses 0xABCD which if considered unsigned value must be understood as (A*4096 + B*256 + C*16 + D = 43981 ie. A is MSB and D is LSB).
You may start worrying about endianess if you work on byte level. As you've seen I did not say anything about how value 0xABCD was stored in Java, C, C++, MASM, TASM. You used your number as you would do on paper where endianess does not exist but on byte level it may either be little or big endian depending on your machine, network interface etc although this will still be 0xABCD in your program.
In your case if you work on 'word'/'short' abstraction level - do not worry about endianess.
Update:
If you want to store 0x0A0B value using 'byte'-like directives it will depend on your machine endianess (or network byte order). Let's assume you want to store that in memory.
On little-endian machines (x86 for example) you should do:
.byte 0xB
.byte 0xA
On big-endian machines you should do:
.byte 0xA
.byte 0xB
Upvotes: 2