Task

Write a program that outputs the lyrics of the Christmas carol The Twelve Days of Christmas.
The lyrics can be found here.

(You must reproduce the words in the correct order, but case, format, and punctuation are left to your discretion.)




Other tasks related to string operations:

Metrics
 Array length
 String length
 Copy a string
 Empty string  (assignment)
Counting
 Word frequency
 Letter frequency
 Jewels and stones
 I before E except after C
 Bioinformatics/base count
 Count occurrences of a substring
 Count how many vowels and consonants occur in a string
Remove/replace
 XXXX redacted
 Conjugate a Latin verb
 Remove vowels from a string
 String interpolation (included)
 Strip block comments
 Strip comments from a string
 Strip a set of characters from a string
 Strip whitespace from a string -- top and tail
 Strip control codes and extended characters from a string
Anagrams/Derangements/shuffling
 Word wheel
 ABC problem
 Sattolo cycle
 Knuth shuffle
 Ordered words
 Superpermutation minimisation
 Textonyms (using a phone text pad)
 Anagrams
 Anagrams/Deranged anagrams
 Permutations/Derangements
Find/Search/Determine
 ABC words
 Odd words
 Word ladder
 Semordnilap
 Word search
 Wordiff  (game)
 String matching
 Tea cup rim text
 Alternade words
 Changeable words
 State name puzzle
 String comparison
 Unique characters
 Unique characters in each string
 Extract file extension
 Levenshtein distance
 Palindrome detection
 Common list elements
 Longest common suffix
 Longest common prefix
 Compare a list of strings 
 Longest common substring
 Find common directory path
 Words from neighbour ones
 Change e letters to i in words
 Non-continuous subsequences
 Longest common subsequence
 Longest palindromic substrings
 Longest increasing subsequence
 Words containing "the" substring
 Sum of the digits of n is substring of n
 Determine if a string is numeric
 Determine if a string is collapsible
 Determine if a string is squeezable
 Determine if a string has all unique characters
 Determine if a string has all the same characters
 Longest substrings without repeating characters
 Find words which contains all the vowels
 Find words which contains most consonants
 Find words which contains more than 3 vowels
 Find words which first and last three letters are equals
 Find words which odd letters are consonants and even letters are vowels or vice_versa
Formatting
 Substring
 Rep-string
 Word wrap
 String case
 Align columns
 Literals/String
 Repeat a string
 Brace expansion
 Brace expansion using ranges
 Reverse a string
 Phrase reversals
 Comma quibbling
 Special characters
 String concatenation
 Substring/Top and tail
 Commatizing numbers
 Reverse words in a string
 Suffixation of decimal numbers
 Long literals, with continuations 
 Numerical and alphabetical suffixes
 Abbreviations, easy
 Abbreviations, simple
 Abbreviations, automatic
Song lyrics/poems/Mad Libs/phrases
 Mad Libs
 Magic 8-ball
 99 Bottles of Beer
 The Name Game (a song)
 The Old lady swallowed a fly
 The Twelve Days of Christmas
Tokenize
 Text between
 Tokenize a string
 Word break problem
 Tokenize a string with escaping
 Split a character string based on change of character
Sequences
 Show ASCII table
 De Bruijn sequences
 Self-referential sequences
 Generate lower case ASCII alphabet




waitChar equ &BB06   ;wait for a key press
PrintChar equ &BB5A   ;print accumulator to screen
 
org &8000
 
	ld ix,VerseTable
	inc ix
	inc ix
	ld iy,SongLookup
 
	ld b,12          ;12 verses total
outerloop_song:
	push af          ;new line
		ld a,13
		call PrintChar
		ld a,10
		call PrintChar
	pop af
	push bc
		push ix
			ld ix,VerseTable
			ld a,(ix+0)
			ld c,a        ;get the low byte of verse ptr
			ld a,(ix+1)
			ld b,a        ;get the high byte
		pop ix
		;bc = the memory location of Verse1
		call loop_meta_PrintString
		push ix
		push iy
 
			ld iy,Verse0
			inc (IY+1)
			inc (IY+1)
 
			ld a,(IX+2)
			ld (IY+7),a
			ld a,(IX+3)
			ld (IY+8),a
		pop iy
		pop ix
		inc ix
		inc ix
	pop bc
 
 
	call WaitChar   ;wait for user to press any key before 
	;continuing so they have time to read it.
 
 
	djnz outerloop_song
 
ReturnToBasic:
		ret		
 
loop_meta_PrintString:
		ld a,(bc)
		or a		;compare A to 0. 0 is the null terminator for verses.
		ret z
		cp 255		;255 means "goto the verse specified after the 255"
		jr z,GotoPreviousVerse
		ld (smc_loop_meta_PrintString_alpha+2),a
		;use self modifying code to point IY's offset to the correct 
		;	song line, without changing IY itself.
		inc a
		ld (smc_loop_meta_PrintString_beta+2),a
smc_loop_meta_PrintString_alpha:
		ld a,(iy+0)	;the "+0" gets clobbered with the desired lyric low byte
		ld L,a
smc_loop_meta_PrintString_beta:
		ld a,(iy+0)	;the "+0" gets clobbered with the desired lyric high byte
		ld H,a
		call PrintString	;now print the string in HL.
		inc bc
		jp loop_meta_PrintString
 
GotoPreviousVerse:
	inc bc		;advance past &FF opcode
	ld a,(bc)   ;get low byte
	ld e,a
	inc bc		;advance to high byte
	ld a,(bc)
	ld d,a
	push de
	pop bc
	jp loop_meta_PrintString
 
 
PrintString:
	ld a,(hl)
	or a
	ret z
	call PrintChar
	inc hl
	jr PrintString
 
;;;; data
VerseTable:
	word Verse0
	word Verse1
	word Verse2
	word Verse3
	word Verse4
	word Verse5
	word Verse6
	word Verse7
	word Verse8
	word Verse9
	word Verse10
	word Verse11
	word Verse12
 
Verse0:
	byte 2
	byte 32			;increment this by 2 after each verse.
	byte 4,56,6,56
	byte 255
	word Verse1		;look up next verse and write that here too.
					;repeat until a hardcoded 12 verses are "sung"
 
Verse1:
	byte 8,56,0
Verse2:
	byte 10,56,255
	word Verse1
Verse3:
	byte 12,56,255
	word Verse2
Verse4:
	byte 14,56,255
	word Verse3
Verse5:
	byte 16,56,255
	word Verse4
Verse6:
	byte 18,56,255
	word Verse5
Verse7:
	byte 20,56,255
	word Verse6
Verse8:
	byte 22,56,255
	word Verse7
Verse9:
	byte 24,56,255
	word Verse8
Verse10:
	byte 26,56,255
	word Verse9
Verse11:
	byte 28,56,255
	word Verse10
Verse12:
	byte 30,56,255
	word Verse11
 
 
 
SongLookup:
		word null		;0
		word Day_Part1	;2
		word Day_Part2	;4
		word Day_Part3	;6
		word Day1		;8
 
		word Day2		;10
		word Day3		;12
		word Day4		;14
		word Day5		;16
		word Day6		;18
 
		word Day7		;20
		word Day8		;22
		word Day9		;24
		word Day10		;26
		word Day11		;28
 
		word Day12		;30
		word First		;32
		word Second		;34
		word Third		;36
		word Fourth		;38
 
		word Fifth		;40
		word Sixth		;42
		word Seventh	;44
		word Eighth		;46
		word Ninth		;48
		word Tenth		;50
 
		word Eleventh	;52
		word Twelfth	;54
 
		word Song_NewLine	;56
 
null:
		byte 0
Day_Part1:
		byte "On the",0
Day_Part2:
		byte "day of Christmas,",0
Day_Part3:
		byte "my true love gave to me",0
Day1:
		byte "a partridge in a pear tree.",0
Day2:
		byte "two turtle doves, and",0
Day3:
		byte "three french hens",0
Day4:
		byte "four calling birds",0
Day5:
		byte "five golden rings",0
Day6:
		byte "six geese a-laying",0
Day7:
		byte "seven swans a-swimming",0
Day8:
		byte "eight maids a-milking",0
Day9:
		byte "nine ladies dancing",0
Day10:
		byte "ten lords a-leaping",0
Day11:
		byte "eleven pipers piping",0
Day12:
		byte "twelve drummers drumming",0
First:
		byte " first ",0
Second:
		byte " second ",0
Third:
		byte " third ",0
Fourth:
		byte " fourth ",0
Fifth:
		byte " fifth ",0
Sixth:
		byte " sixth ",0
Seventh:
		byte " seventh ",0
Eighth:
		byte " eighth ",0
Ninth:
		byte " ninth ",0
Tenth:
		byte " tenth ",0
Eleventh:
		byte " eleventh ",0
Twelfth:
		byte " twelfth ",0
 
Song_NewLine:
		byte 13,10,0		;control codes for a new line.