ShHolmes
ShHolmes

Reputation: 453

Computer theory, big and little endian

Currently, I am studying computer theory, assembly and I am stuck on the big-&little-endian.

I have examined EIP register (with command x/8xw in Linux), which holds value 0x00fc45c7 (one word, 4 bytes). When I request bytes instead of words (x/8xb), I get a reversed sequence (because it's little-endian machine): c7 45 fc 00. When I request half-word (x/8xh), I get this sequence: 45c7 and 00fc. These are all equal between each other and I would like to check it myself.

When converting the first hex value (0x00fc45c7) to decimal value, I get 16532935 (the algorithms is as follows: 16^7*0 + 16^6*0 + 16^5*15 + 16^4*12 + 16^3*4 + 16^2*5 + 16^1*12 + 16^0*7). That's a simple from-hex-to-decimal converting, and I understand it.

In the book I am reading, there is an example of calculation, that proves that 0x00fc45c7 is the same as c7 45 fc 00. The calculations are as follows: 0*(256^3) + 252*(256^2) + 69*(256^1) + 199*(256^0).

I understand, that 0, 252, 69 and 199 are values, that we get after converting each byte into decimal fc = 15*16^1 + 12*16^0 = 252, 45 = 69 and c7 = 199. What I can't understand is what does 256 stand for, why are we raising 256 to power? What is the algorithm of these calculations? What would they be if we would use not bytes (c7 45 fc 00) but half-word (45c7 and 00fc) to get the same value as in 0x00fc45c7? I have surfed through the internet, but still have not found any answer. All the articles just explain the differences between little and big endian systems, but the explanations do not dive into this issue. Would appreciate your help very much.

Upvotes: 1

Views: 125

Answers (1)

Heman Gandhi
Heman Gandhi

Reputation: 1371

You use 256 since you can think of two hex digits as a single digit in base 256. Hence, you can suppose that you're in base 256 as you work with every byte.

Upvotes: 3

Related Questions