MZ-40K Intro part 2

A Brief Overview of Counting in ‘Nibbles’

Written by Richard Cornelius / UK

Earlier, during the overview of the MZ-40K organ abilities, the starting address where each note is located was provided. If you are used to using 8 bit ‘words’ or just decimal numbers, then understanding these can be a bit of a problem. Thankfully there is a list supplied with the machine giving the location of each note, but if you want to work it out for yourself, then please read on.

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 0’s and 1’s. 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 )

bit #
nibble #
Address Note #
0 – 3
0
000
1 ( 1st part )
4 – 7
1
001
1 ( 2nd part )
8 – 11
2
002
1 ( 3rd part )
12 – 15
3
003
1 ( 4th part )
16 – 19
4
004
2
20 – 23
5
24 – 27
6
28 – 31
7
32 – 35
8
008
3
36 – 39
9
40 – 43
10
44 – 47
11
48 – 51
12
00C
4
.
.
1712 – 1715
428
1AC
108

DECIMAL, BINARY & HEX CONVERSIONS

During the overview of nibbles, I arbitrarily converted decimal into Hex without explaining it, so again if you are unsure about this, I’ll try to do a brief explanation of how this is done.

Decimal is base 10, as it has 10 numbers, 0 – 9, with the highest being 9. In the same way Binary, as I mentioned before is base 2, and so has 2 numbers 0 & 1, with the highest being 1.

Converting to Decimal

If I write a decimal number down for example 428, I could write it that way or as what each column means;

100’s
10’s
Units
102
101
100
4
2
8

In the same way a Binary number can also be written, for example 110101100 can be seen as;

256’s
128’s
64’s
32’s
16’s
8’s
4’s
2’s
Units
28
27
26
25
24
23
22
21
20
1
1
0
1
0
1
1
0
0

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.

256’s
16’s
Units
162
161
160
1
A
C

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

To convert from decimal to a different base is also very simple.

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.

172 can be divided by 128, and gives a remainder of 44.
44 cannot be divided by 64, but can be divided by 32, this time with a remainder of 12
12 cannot be divided by 16, but can be divided by 8, again with a remainder of 4
Finally 4 divided by 4 exactly with no remainder.

The result should look a bit like the table below

512’s
256’s
128’s
64’s
32’s
16’s
8’s
4’s
2’s
Units
29
28
27
26
25
24
23
22
21
20
1
r – 172
1
r – 44
1
r – 12
1
r – 4
1
r – 0

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;

428 is not divisible by 4096, but is divisible by 256.
256 goes into 428 only once, and has a remainder of 172.
1 is entered into the 256’s column.
172 is divisible by 16.
16 goes into 172, 10 times with a remainder of 12
‘A’ ( 10 in Hex ) is entered into the 16’s column
The remaining 12 goes into the last column as ‘C’ ( C being 12 in Hex )
4096’s
256’s
16’s
Units
163
162
161
160
1
r – 172
A
r – 12
C

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

8’s
4’s
2’s
Units
8’s
4’s
2’s
Units
8’s
4’s
2’s
Units
0
0
0
1
1
0
1
0
1
1
0
0
1
8 + 2 = 10 ( = A )
8 + 4 = 12 ( = C )

Again, the final result, is 1 A C