The Layout of Data in Memory

Now I have a question for you. After we execute the assembly language statement mov ax,[1000] to load the value of i into ax, what's in register ax? Of course; the answer is the value of i, but what is that value? The first byte of i, at location 1000, has the value 41 hexadecimal (abbreviated 41h), and the second byte, at location 1001, has the value 42h. But the value of i is 2 bytes long; is it 4142h or 4241h? These are clearly not the same!

That was a trick question; there's no way for you to deduce the answer with only the information I've given you so far. The answer happens to be 4241h, because that's the way Intel decided to do it; that is, the low part of the value is stored in the byte of RAM where the variable starts. Some other CPUs, e.g., Motorola's 680x0 series, do it the opposite way, where the high part of the value is stored in the byte of RAM where the variable starts; this is called big-endian, since the big end of the value is first, while the Intel way is correspondingly called little-endian. And some machines can use either of these methods according to how they are started up. This makes it easier for them to run software written for either memory orientation.

As you might have surmised, the same system applies to 4-byte values. Therefore, since we're on a little-endian machine, if we wrote the instruction mov eax,[1000], it would load the eax register with the value 44434241h; that is, the four bytes 41, 42, 43, and 44 (hex) would be loaded into the eax register, with the byte having the lowest address loaded into the low end of the register.

Here's another example. A little-endian system would represent the number 1234 (hex) stored at address 5000 as in Figure 3.3, whereas a big-endian system would represent the same value 1234 (hex) as illustrated in Figure 3.4.

Figure 3.3. One little endian value


Figure 3.4. A big endian example


This really isn't much of a problem as long as we don't try to move data from one type of machine to another; however, when such data transportation is necessary, dealing with mixed endianness can be a real nuisance.[17] Before going on, let's practice a bit with this notion of how data are stored in memory.

[17] Luckily, we won't run into this problem in this book. However, it is very common in dealing with networks of computers, and there are industry standards set up to allow diverse types of computers to coexist in a network.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset