[Icarus Productions] - Click for main page The team Projects Articles Features
This is the current problem to solve:

Optimization: Size

Unless otherwise specified, all strings are Z-strings.

Problem  

Number Spellout

Input:HL ® free space
BCDE = number between 0 and 999999999
Output:BCDE will contain a 32 bit number (LSB in E, MSB in B) with at most 9 decimal digits (you may assume this), which should be spelled out, according to the rules below. The string should be created at HL, which will point to enough free memory for the correct answer. The string should terminate with a null byte. When the routine is done, HL should point to the beginning of the string (ie, HL should be preserved).
Special:You may not use any temporary memory! You'll have to include any temporary space in the program code itself.

Rules for spelling

  • Spelling for ones: zero, one, two, three, four, five, six, seven, eight, nine
  • Spelling for teens: ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen
  • Spelling for tens: twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety
  • Spelling for hundreds: hundred, thousand, million
  • A white blank should separate each of the numbers above, except between ones and tens, where are hyphen ('-') should be used.
  • No pluralization should be done.
Examples

Input:BCDE = 50312
Output:HL -> "fifty thousand three hundred twelve"

Input:BCDE = 198000
Output:HL -> "one hundred ninety-eight thousand"

Input:BCDE = 10000000
Output:HL -> "ten million"

Input:BCDE = 123456789
Output:HL -> "one hundred twenty-three million four hundred fifty-six thousand seven hundred eighty-nine"

Hint and advice

Algorithm

This page has a simple algorithm (in Java) for the spellout procedure.

Extracting digits

The following piece of code will extract the last digit in BCDE and store it in A, while at the same time dividing BCDE with ten. Please note that you do not have to use this routine! If you do, the size will count.

; Input:    BCDE = 32 bit
; Output:   BCDE = BCDE/10, A = BCDE mod 10

Extract:
 push hl
 xor a 
 ld hl,$200A 
ELoop:
 sla e
 rl d
 rl c
 rl b
 rla
 cp l
 jr c,ESkip
 sub l
 inc e
ESkip:
 dec h
 jr nz,ELoop
 pop hl
 ret

Defining 32 bit numbers

The following simple macro can be used to store a 32 bit number in two 16 bit registers.

#define ld_bcde(x) ld bc,(x)>>16 \ ld de,(x)&$FFFF
The current round ended on 09/05/1999, 6:00 pm CET!
 
 
Page last updated on Sun May 09 23:42:18 1999 E-mail us!Top of page