Computer byte size judgment, computer byte size
Recently I have been studying the computer interface technology and network programming basics. In these two disciplines, I have mentioned the problem of computer byte size-end judgment.
What is the size end?
If two consecutive addresses are empty, a + 1 is saved to an integer that occupies the two spaces. Take the sixteen-digit number as an example. 0x0102. If the value in a is 01, it indicates large-end storage. If the value in a is 02, it indicates small-end storage.
C language code:
# Include <stdio. h>
Union {
Short num;
Char num1 [sizeof (short)];
} Un;
Int main (int argc, char ** argv)
{
Un. num = 0x0102;
If (sizeof (short) = 2)
{
If (un. num1 [0] = 1 & un. num1 [1] = 2)
Printf ("big-endian ");
Else if (un. num1 [0] = 2 & un. num1 [1] = 1)
Printf ("little-andian ");
Else
Printf ("erro! ");
}
Else
Printf ("erro! ");
Return 0;
}
Assembly code implementation:
Include io32.inc
. Data
Msg1 byte 'big-endian ', 13, 10, 0
Msg2 byte 'little-endian ', 13, 10, 0
Num word 1234 h
. Code
Start:
Xor ebx, ebx
Mov bl, byte ptr num
; Mov eax, ebx
; Call dispuid
Cmp bl, 12 h
Je loop1
Mov eax, offset msg2
Jmp loop2
Loop1:
Mov eax, offset msg1
Loop2:
Call dispmsg
Exit 0
End start