Why do Computers use Hex Number System at assembly

2020-03-08 07:23发布

问题:

Why do computer use Hex Number System at assembly language? Why don't they use any other number system like binary, octal, decimal? What thing forced computer designer to use hex system at assembly? Why it looked so beneficial to them?

回答1:

Well it doesn't make a difference how you represent them but as we know that humans don't understand binary numbers, they are only to make the computer's life easier as it works on only two states true and false. So in order to make binary numbers(instructions) human readable we adapted the hexadecimal number system for representing assembly instructions. It has its roots in the history of computers.

For example we can represent this binary number

11010101110100110010001100111010 in

hex as     0xd5d3233a
octal as   32564621472
decimal as 3587384122

As you can see that it is easily readable and less prone to error for humans. The hex value is the most precise.



回答2:

Computers don't use hex. There are no components in a computer working on hex values. Instead, computers work on binary values for most part, on digital "yes" and "no" information, represented as "voltage applied" and "no voltage", also called "hi" and "lo", or "1" and "0"

But because representing a binary number to humans as a string of 0 and 1 makes them difficult to read, hex is used as a shortcut: groups of 4 bits (those 1 and 0 informations) are represented as one hex digit. This conversion from binary to hex, and vice versa, is very simple. Converting binary to decimal, or decimal to binary, involved more actions.

Therefore, the hex system isn't used by computers, but by humans, for terser representation of binary numbers, but easier convertibility than from/to decimal number system.

hex numbers have another characteristic which makes them preferable over decimal numbers: It is not uncommon that a change between two numbers involves only flipping one or few bits. The resulting decimal number would look very different - the more different the higher value a changed bit has. In a hex number, only the digit(s) covering the changed bit(s) change, and the remaining digits keep their values. this helps mentally grouping the values those hex numbers stand for.

Besides hex is/was octal system used frequently too. It's disadvantage is that one octal digits describes a group of 3 bits only, not 4. as binary numbers in computers (for example, addresses) have often a range of a power of 2, the numbers fit better into groups of four than into groups of three.

There is no such thing of humans "forced" to use hex for assembly. It is merely a convenience thing.



回答3:

In assembly language it is often important to generate numbers from bits and to see which bits are set in a certain number.

This is often also the case in C programs as you see in the following example:

a = b&0xE0; // high 3 bits
c = b&0x1F; // low 5 bits

Using binary numbers is not very readable as already said in other answers. Using decimal numbers however it would not be possible to see which bits are affected:

a = b&222; // which of these three instructions
a = b&224; // takes the high 3 bits?
a = b&226; // it is not easy to see!

When using a numbering system to the base of 2^N (octal or hexadecimal) one digit corresponds to some bits (while in decimal system for example a change of one digit in the decimal number may change all bits in the binary number).

Octal numbers are used less often than hexadecimal numbers. A possible reason for this could be that octal digits represent 3 bits while hexadecimal numbers represent 4 bits. All data types used in modern computers however use a multiple of 4 bits. Example: A "uint16" number can be represented by a 4-digit hexadecimal number while all 4-digit hexadecimal numbers can be represented by a "uint16" number.