Something which is very simple in BASIC is to print a number:
10 LET A = 42 20 PRINT A
Easy as that. This isn’t quite so simple in machine code however, as you need to work out, and print, each ‘column’ of the number – the units, the tens, the hundreds, and so on.
There are a number of different ways to do this – I can think of 3 which work well – with each having it’s own pros and cons, so will start with the most simple – printing a number from a register.
Printing a register – either an 8-bit register such as A, or a 16-bit register such as HL – is great because we can already do many different things with registers – add to them, subtract, read them from memory, and so on – which means that we only need to worry about how to print them. I’ll start with an 8-bit register…
8-bit registers can store a range of numbers between 0 and 255, so we only need to deal with three columns of digits – the hundreds, the tens, and the units – when you read from left to right. We do this by counting how many of each column there is, and printing that value, and it’s easiest to do this by subtracting from the number until it reaches zero. Here’s some code to count the number of hundreds in a register:
ld a, 246 ... ; Use B to hold the count ld b, 0 LOOP ; Subtract the value of our column from the number sub 100 ; If the carry flag is set then the number was less than this, so we're done. jp c, FINISHED ; Increase our count. inc b ; Loop around to subtract again jp LOOP FINISHED ; B holds the number of 100 in the number.
From the result you can either add the ASCII (or otherwise) for the number zero – ‘0’ – to B and print it, or just set B equal to that instead of zero at the beginning, and print it directly.
With the hundreds column handled, we’ll need to do the same for the tens, and can use the same system – the only thing to watch out for is that A is now 100 less than it should be, so we need to add 100 to it before we start subtracting 10s from it.
Lastly we need to handle the units. This is very simple as, after we add 10 to the number to correct it after the tens loop it’ll equal the number of units – so we just need to print it (once we’ve converted it to ASCII or similar if required)
If we want to print a 16-bit number the code is roughly the same. As a 16-bit number has the range of 0 to 65,535 we have to deal with ten-thousands & thousands, as well as the hundreds, tens, and units of the 8-bit number, but the method is the same but using 16-bit arithmetic – SUB HL, BC for example – as required.
The downside to this method is the size of number we can print, as it’s limited by the range of the register. For the display of lives or health for example, an 8-bit number is probably fine, but for a score you might want to go a bit higher than 65,535. You can be crafty though – if your score only ever goes up in multiples of 10 then you can instead go up in units, and just always print a zero at the end – making the range 0 – 655,350 in steps of 10, or 0 – 6,553,500 in steps of 100.
Both of the next two methods I’ll cover don’t have this limitation though – you can practically have as large a number as you like!