Number Systems - RetroKoH/S1Fixed GitHub Wiki
Whether modifying .asm text files or binary files (Either with a hex editor or a specialized tool), it's important to have at least a basic understanding of how number systems work. We all use at least one number system in our daily life: decimal! Computers do NOT use this though. That said, understanding how decimal works will help us understand the other number systems that we will likely need to use.
Every number system uses what's called a number base, the number of possible values that one digit can use to represent part of a number. Each of the number systems covered below use a different number base, each for a different, equally important reason. For each number system, we use this equation to understand the value of a digit in a given number, like so:
Value * Base^Place
Note that the first place (far-right digit) is place 0.
- Decimal (Base 10)
This is the standard number system that people use every day to denote integer (11) and non-integer (8.5) numbers. With a number base of 10, there are 10 possible values that every digit in a number could possibly be. These are: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. When we begin counting from 0, we add 1 to our number, only counting with a single digit until we reach 9. Once we reach 9, and try to add one more, the 9 loops back to 0, and the value carries over to the next digit to the left. This means that
09
becomes10
. This pattern is always carried out as higher numbers are attained. Using the above noted formula, let's figure out the value of the given number 4,631:
(4 * 10^3) + (6 * 10^2) + (3 * 10^1) + (1 * 10^0)
(4 * 1000) + (6 * 100) + (3 * 10) + (1 * 1)
4000 + 600 + 30 + 1 = 4631
So far, this should be pretty self-explanatory. If so, good, because it's about to get a little more complicated.
- Binary (Base 2) This is the number system used by all computers, old and new, simple and complex. In this system, the digits are referred to as "bits". With a number base of 2, there are only 2 possible values that a bit could be: 0 or 1; on or off. Many of the instructions used in M68K assembly, namely those that perform bitwise operations such as AND, OR, and bit-shifting, require an understanding of the binary system. Let's pull a number from the Sonic 1 disassembly and figure out its decimal equivalent using our formula. We'll handle a small one: %0111 1111. We can omit the far-left 0 for this exercise.
(1 * 2^6) + (1 * 2^5) + (1 * 2^4) + (1 * 2^3) + (1 * 2^2) + (1 * 2^1) + (1 * 2^0)
(1 * 64) + (1 * 32) + (1 * 16) + (1 * 8) + (1 * 4) + (1 * 2) + (1 * 1)
64 + 32 + 16 + 8 + 4 + 2 + 1 = 127
As you can see with just this example, even small binary numbers can become long and difficult to read. But if computers can't read decimal, and we cannot easily read binary, surely there must be a middle ground, right? Yes, there is!
- Hexadecimal (Base 16) Hexadecimal is a number system with a number base of 16, meaning that each digit has one of 16 possible values: 0-9, followed by the letters A, B, C, D, E, and F, each representing the numbers 10 through 15. The big advantage of using hexadecimal is that larger numbers can be represented with smaller values.
Image Credit: TechTarget