Skip to content

Turn a score into readable digits

  • Sinclair ZX Spectrum
  • Assembly

The score byte and the characters printed on screen are different representations. We will trace how one number becomes a row of digit codes.

This lesson’s program adds a display to unit 16’s game and changes no rule: the same stars award the same units to the same score byte. What is new is the line at the top of the screen that prints it. A stored score of 7 means seventy points, but the character code for '7' is not the number 7. To display it, the program converts the value into digit characters and leaves a fixed trailing '0' in the buffer.

Source · view and edit
ZX Spectrum
Companion file: assets.inc

The include line reads this editable data when you assemble. Keep it beside your source for a local build.

Edit the source, then assemble.

Edits are saved in this browser. Keep a downloaded copy of work you want to keep.

Focus the Spectrum screen to use the keyboard, or hold these buttons. Assemble again for a fresh machine.

Run the program to inspect it. Editing source does not change the running machine.

score
—

Subtract places, not individual points

decimal3 receives A=0..255 and HL=the first of three writable output bytes. It subtracts 100 until the remaining value is below 100, counting hundreds in B. Then it subtracts 10 to count tens. Whatever remains is the units digit.

B begins at the character '0', not numeric zero. Incrementing it changes the character to '1', '2' and so on. For the units digit, add a,'0' makes the same conversion directly.

meteor-storm.asm, excerpt
decimal3:
 ld b,'0'
decimal_hundreds:
 cp 100
 jr c,decimal_hundred_done
 sub 100
 inc b
 jr decimal_hundreds
decimal_hundred_done:
 ld (hl),b
 inc hl
 ld b,'0'
decimal_tens:
 cp 10
 jr c,decimal_ten_done
 sub 10
 inc b
 jr decimal_tens
decimal_ten_done:
 ld (hl),b
 inc hl
 add a,'0'
 ld (hl),a
 ret

For A=137, the routine subtracts 100 once, leaving 37, then subtracts 10 three times, leaving 7. It writes the characters 1, 3, 7. The separate trailing zero in score_digits_text makes the printed score 1370.

Keep the buffer’s agreement visible

score_digits_text reserves "0000",0. The first three bytes are writable digit positions; the fourth is the fixed zero for points; the final zero byte terminates the string. The character '0' and the terminator byte 0 are not the same value.

score_line loads A from score and HL from the digit buffer, calls decimal3, then sends the complete score_text string to text. text expects B=row, C=column and DE=the string address, so those arguments are set after decimal3 has finished changing registers.

Why not increment the last printed character whenever a star is collected?

Show the explanation

That would mix the display encoding with the score rule and make carries between decimal digits another problem. Keeping a numeric score lets collision code add units and record code compare values. Printing can recreate the characters whenever needed.

Draw the line only when it changes

hud keeps the score line on screen: our heads-up display, or HUD. It remembers the last score it drew in hud_last_score and returns without drawing when score still matches. A second byte, hud_drawn, is zero after the screen has been cleared, and then hud draws regardless. Both bytes sit in the run block, so a new run starts with hud_drawn at zero.

The program calls hud in three places. new_game calls it last, after drawing the ship. The main loop calls it once per update at race_continue, after the contact and completion checks. result clears the screen, sets hud_drawn to zero and calls hud, so the final score stays visible above the outcome. result saves DE around that call, because the drawing changes DE and DE still holds the outcome text.

Skipping display work must not skip the score update itself: contact changes state first, and the HUD observes that state later.

For a controlled display test, find new_game and insert ld a,7 followed by ld (score),a immediately before its call hud. Assemble: the inspector should read 7 and the screen should show 0070. This deliberately changes the starting score in source; it is not a claim that you collected seven stars. Remove those two instructions or Revert before continuing.

Sources

Zilog, Z80 CPU User Manual, UM008011-0816, instruction descriptions for the operations shown in this lesson. The running experiment supplies the program-specific observations.

Run it here

This unit's program, built for you.

Program
meteor-storm.tap
Machine
ZX Spectrum 48K (PAL)

Download meteor-storm.tap