![]() ![]() A Brief Overview of Counting in 'Nibbles' written by Richard Cornelius / UK
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Please forgive me if some of this information is too basic for some
of you, but as I do not know the readership, I thought I'd better start
at the bottom. Binary is the lowest numbering system and uses base 2 ( decimal uses base 10, as when you get to 10, you need a second digit, the 1 ), which consists of 0s and 1s. Each individual number is called a bit. 8 'bit's are called a 'byte', which is the most common form seen, as it works well with 8-bit processors ( e.g. the Z80 ), and is easily converted into Hex ( base 16 ), as 11111111 in Binary is FF in Hex. Since the MZ-40K uses only a 4-bit processor, it cannot handle whole 'bytes' so it uses half a byte ( 4-bits ), called a 'nibble'. On a side point, do you think the person who created these words had
a fixation with food - bit, byte and nibble? Now, onto the mathematics of how an address is worked out. In the earlier example, above, the 108th note started at address 1AC. Now as each note definition consists of 4 x 4bits it would seem to make sense that the address would be; 108 x 4 x 4 But that equals 1728 which when converted to Hex is 6C0, which is nothing like 1AC. If this machine used a Z80, like many later Sharp machines, I would immediately say that you only need to count the bytes, not the bits, and since the note definition consists of 2 of these bytes the maths would be; 108 x 2 This now equals 216, or D8 in Hex, still a long way from 1AC. The reason both the two upper calculations didn't work is that this machine uses a 4-bit processor, and counts in nibbles, so we need to do the same. In nibbles, the note definition consists of 4 of these, so now the maths is; 108 x 4 Hurray!!... Not quite The answer now is 432, or 1B0 in Hex, and still not the 1AC we require. The answer to this next part lies in the fact that processors start counting from 0, not 1, so the 1st note is actually in location 0, and the 108th note is actually in location 107. So if we now use the location position, rather than the note number we come up with; 107 x 4 = 428, or 1AC in Hex This is now the same as our display. As this can be a bit confusing here's a short table, which may help you to understand it. ( I've missed some lines out, just to try and make the starting address of the notes show a bit clearer )
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
DECIMAL, BINARY & HEX CONVERSIONS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Converting to Decimal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
In the same way a Binary number can also be written, for example 110101100 can be seen as;
Finally Hex, being base 16, 1AC is shown as below. As we normally use decimal we do not have characters for numbers larger than 9 and since the highest number in Hex ( base 16 ) is 15, the characters A, B, C, D, E & F are used for the numbers 10, 11, 12, 13, 14 & 15.
Converting any of these bases into decimal is very easy, just multiple the BASE number by it's column amount, and then add them all together. For example in the decimal number, 428, above, would be; ( 4 x 100 ) + ( 2 x 10 ) + ( 8 ) = 428 The Binary number, 110101100, would be; ( 1 x 256 ) + ( 1 x 128 ) + ( 1 x 32 ) + ( 1 x 8 ) + ( 1 x 4 ) = 428 and the Hex number 1AC, would be; ( 1 x 256 ) + ( 10 ( A=10 ) x 16 ) + ( 12 ( C=12 ) ) = 428. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Converting to Decimal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
I'll just show it on Binary first, as this is the easiest, and then explain the differences for Hex afterwards. I'll use the same number as above, 428, as our example. First find the largest column amount that will fit into you number. 512 is larger than 428, so it won't fit there, but the next highest column, 256, will fit into 428 ( 428 can be divided by 256 ). As it divides into 428 only once, a '1', will go in that column. The remainder is then used for the next step. In this case it's 172 ( 428 - 256 = 172 ). Then, a bit like shampoo, you repeat as necessary.
The result should look a bit like the table below
Finally, add 0's to the empty columns, to get 1 1 0 1 0 1 1 0 0 Hex conversion is similar, but as well as noting the remainder, you also have to note the number of times the number is divisible. Again, starting with 428;
As shown the final result is 1 A C |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Converting Decimal directly into Hex | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
As Binary and Hex are closely related converting between the two is very simple. So simple in fact that I find that it is easier to convert a decimal number into binary first and then into Hex, rather than having to try and remember my 16 times table. If you want to convert from Hex to Binary, just use the same process in reverse. For the example, I'll use 428 again, which converts to 110101100 using the process above. If we now split this Binary number into 4-bit nibbles, we get the following; 0001 1010 1100 As the largest number for any nibble is now 15 ( 1 + 2 + 4 + 8 ), which is the same as the largest number for Hex ( F ), all that now needs to be done is convert each nibble on it's own back to decimal, but use the letters A - F instead of the number 10 - 15. For example
Again, the final result, is 1 A C |