Christmas Tree

From SizeCoding
Jump to: navigation, search

The challenge

During the Vintage Computing Christmas Challenge 2021 the goal was to create the shape of a given Christmas tree with as few bytes as possible. All platforms and languages were allowed. The tree should be centered and look exactly like shown bellow.

                         *
                        ***
                       *****
                      *******
                        ***
                      *******
                    ***********
                  ***************
                       ***** 
                    *********** 
                 ***************** 
              *********************** 
                        ***
                        ***

There were mainly two kinds of approaches:

  • calculate stars using a formula
  • save amount of stars within data

Do to the trunk of the tree, the second type was more efficient.

The length measurement for the contest was defined as the shortest file size or source code. For Assembler, typically the filesize was relevant.

Remarkable Entries

Shortest DOS version

The shortest DOS version with 44 bytes came from Hellmood:

org 100h			; define start of code for data access
db 80				; screen width, located at [SI], also "push ax"
mov bx,d			; pointer to data for XLAT
mov cx,1120			; 14 lines with 80 chars each
X: mov ax,cx		; get current sequence position
div byte [si]		; transform to [X,Y] in AL, AH
xlat				; get tree width for current line number
sub ah,40			; center tree in the middle
jnc F				; - 
neg ah				; -
F: sub ah,al		; inside or outside tree?
salc				; set AL depending on carry flag
imul ax,byte -42	; transform into STAR or nothing
int 29h				; write current char to screen
loop X				; repeat unil all chars are plotter
ret					; return to prompt (JMP to former AX=0x0000)
d:db 2,2,12,9,6,3,8,6,4,2,4,3,2,1

Overall winning entry (C64)

The shortest entry overall was for the C64 and was submitted by Serato with only 37 bytes:

		!cpu 6510			; enable unintended opcodes in ACME assembler

		chrout = $FFD2
		crlf = $AAD7
		pntr = $D3
		width = 40

		;; BASIC header
		*= $0801
		!word +, 10
		!byte $9e
		!text "2092"
		!byte 0
+		!word 0
table
		!byte -1  
		!byte -3  
		!byte -5  
		!byte -7  
		!byte -3  
		!byte -7  
		!byte -11 
		!byte -15 
		!byte -5  
		!byte -11 
		!byte -17 
		!byte -23 
		!byte -3  
		!byte -3  
code
--		lsr
		sbc #(128-width/2)
		sta pntr
		lda #'*'
-		jsr chrout
		inx
		bne -
		jsr crlf
		iny
entry
		lax table,y
		bmi --
		rts
end

Serato's entry was provided with the following comments:

This entry to the Christmas Tree challenge saves space in six ways:

 1. Using the c64 kernal routines to output individual characters and carriage return/linefeed. These routines save/restore X/Y allowing the registers to be used as loop counters.

 2. Writing to the PNTR kernal variable to indent each row.

 3. Using the unintended LAX opcode to simultaneously load A and X with the table values.

 4. Storing the row widths as 2's complement negative values, to simplify the indent calculation to divide by two (LSR) and subtract (SBC). The inner loop simply counts back up to zero for rendering the correct width.

 5. BASIC loads the Y register from address $30d. This defaults to 0 on startup, so Y does not need to be initialised.

 6. Placing the table before the code in memory, the MSB of the table entries do not match the MSB of the first opcode (LSR), allowing the outer loop exit condition to be set implicitly in the Negative flag as the table values are read.

These optimisations result in 37 bytes of machine code and data. Two further optimisations are possible each of which save 1 byte, but break the rules of this challenge:

 a - LSR and SBC can be replaced with the unintended opcode ARR, if you are willing to "centre" the tree on a virtual screen width of 32 chars.

 b - The BASIC interpreter leaves the line number in the zero page word at $39, so by setting the line number in the basic header to the address of the table, "LAX ($39),y" can be used for the table access.

Shortest BASIC entry for C64

While BASIC programs seem to have a structural disadvantage for this kind of challenge, some entries in the spirit of oneliners are remarkably compact. The shortest BASIC entry for the C64 was submitted by Wil with 68 bytes of source code length.

0dA-1,,1,2,,2,4,6,1,4,7,10,,:rEw:?sP18-w);:fOk=-2tow+w:?"*";:nE:?:gO

Expanded source code as produced by LIST:

0 data-1,,1,2,,2,4,6,1,4,7,10,,:readw:printspc(18-w);:fork=-2tow+w:print"*";:next:print:goto

Optimized post-deadline versions

36 bytes version for C64

The C64 version was shortened by altering the calculation so the default register values on start cause the first line to be drawn, shortening the table by one entry.

		!cpu 6510			; enable unintended opcodes in ACME assembler

		chrout = $FFD2
		crlf = $AAD7
		pntr = $D3
		width = 40

		;; BASIC header
		*= $0801
		!word +, 10
		!byte $9e
		!text "2074"
		!byte 0
+		!word 0
table
;		!byte -1  
		!byte -3  
		!byte -5  
		!byte -7  
		!byte -3  
		!byte -7  
		!byte -11 
		!byte -15 
		!byte -5  
		!byte -11 
		!byte -17 
		!byte -23 
		!byte -3  
		!byte -3  
code
--		adc	#width-1
		lsr
		sta pntr
		lda #'*'
-		jsr chrout
		inx
		bmi -
		jsr crlf
		iny
		lax table-1,y
		bmi --
		rts
end

35 bytes version for C64

The C64 version was shortened by writing directly to the screen, saving one byte. This change prevents the Y register being used as a loop index, so a different approach is used to iterate over the table entries.

		!cpu 6510			; enable unintended opcodes in ACME assembler

		crlf = $AAD7
		pnt = $D1
		lnmx = $D5
		eal = $AE
		

		;; BASIC header
		*= $0801
		!word +, 10
		!byte $9e
		!text "2061"
		!byte 0
+		!word 0

code 
--		adc	lnmx
		lsr
		tay
		lda #'*'
-		sta (pnt),y
		iny
		inx
		bmi -
		jsr crlf-8
		dec	eal
		lax (eal+1,x)
		bmi --
		rts
table
		!byte -3  
		!byte -3  
		!byte -23 
		!byte -17 
		!byte -11 
		!byte -5  
		!byte -15 
		!byte -11 
		!byte -7  
		!byte -3  
		!byte -7  
		!byte -5  
		!byte -3  
;		!byte -1

34 bytes version for C64

The C64 version was shortened by removing the ADC that indents the tree. Instead the offset to centre the tree is provided "for free" by suitably locating the EAL/EAH zp vector. This is possible because the C64 tape buffer is located immediately below the default screen location, and writing to the top line of the screen can be combined with the kernal routine to scroll the screen downward. This routine always leaves the Y register as $84, which added to the tape buffer zp vector TAPE1 points to the table ending at $3c0. The table values have their MSB clear so the centre of the tree has value $7f. When right shifted this gives an indent of $3f. The structure of the code is arranged so that the end of the binary ($3d4) plus the $3f offset give $413, ie 19 chars indented from the start of the screen matrix, centring the tree on screen. Although removing the ADC saves two bytes, one byte is lost adding the first row back into the table.

		!cpu 6510			; enable unintended opcodes in ACME assembler

		scroll_down = $E981
		eal = $AE
		tape1 = $b2

*=$3c0-14
;		SYS 970 to draw tree
;		SYS 960 to draw with added "ground"

table
		!byte 128-1
		!byte 128-3
		!byte 128-5
		!byte 128-7
		!byte 128-3
		!byte 128-7
		!byte 128-11
		!byte 128-15
		!byte 128-5
		!byte 128-11
		!byte 128-17
		!byte 128-23
		!byte 128-3
		!byte 128-3
code 
--		lsr
		tay
		lda #'*'
-		sta (eal),y
		iny
		inx
		bpl -
entry
		jsr scroll_down
		dec	tape1
		lax (tape1),y
		bne --
		rts
end

33 bytes version for C64

This C64 version is most similar to the 35-byte version above. It saves 2 bytes over that version by omitting the table vector decrement, instead using the zp screen editor row variable which is incremented by the kernal with each linefeed. Fortuitously the high-byte of this vector is the "ASCII Value of Last Character Printed" which will always be $d. By locating the binary at $0dxx the table can be accessed with this vector.

		!cpu 6510			; enable unintended opcodes in ACME assembler

		crlf = $AAD7
		pnt = $D1
		lnmx = $D5
		tblx = $D6
		
*=$d0b
;		SYS 3352 to draw tree, SYS should be typed with the cursor on row 10.

table
;		!byte -1
		!byte -3
		!byte -5
		!byte -7
		!byte -3
		!byte -7
		!byte -11
		!byte -15
		!byte -5
		!byte -11
		!byte -17
		!byte -23
		!byte -3
		!byte -3
code 
--		adc lnmx
		lsr
		tay
		lda #'*'
-		sta (pnt),y
		iny
		inx
		bmi -
		jsr crlf-8
		lax (tblx+1,x)
		bmi --
		rts
end

32 bytes version for ZX Spectrum

After the deadline following entry for the ZX Spectrum with only 35 bytes was submitted by reddie and optimized by char to 32 bytes:

# Christmas Tree post-event build, ZX Spectrum version
# tnx to Manwe for info about event, better late than never =)
# first version - 35 bytes - (c) reddie, 2021.12.25
# optimized to 32 bytes by char, 2021.12.26 - huge thanks from me!
# full final object len  = 32 bytes (from label "data" to label "end")
# data array len  = 14 bytes; code len = 18 bytes, 13 Z80 instructions
# execute from Basic only! entry point  = 61455, or just run TRD image
# and then run "ctree32b" Basic-program, it will load & start the code
# TRD file also contains source text in ALASM format

data	org	-16*256+1
	defb	-5,-5,-25,-19,-13,-7,-17,-13,-9,-5,-9,-7,-5,-3

start	dec	c
	jr	z,$	;stop when finished
	ld	a,23	;set coords via ROM procedure
	rst	16
	ld	a,(bc)	;line len in negative format
	ld	e,a
	rra
	sub	b	;centering
print	rst	16
	ld	a,"*"
	inc	e
	jr	nz,print
	jr	start
end

35 bytes version for Plus/4

The following optimized version for the Commodore Plus/4 was created after the deadline:

# Plus/4 Christmas Tree by GeirS, 2021-12-29
# Assemble using 64tass (replace hash with semicolon first, except for 'lda #$2a')

BtmRow		= $0c00+$28*$18	# Address of bottom screen row
Screen		= BtmRow-$6c	# Adjustment for ($100-$28)/2
ScrollUp	= $da89			# KERNAL routine to scroll screen upwards
VarTab		= $2d			# Pointer: Start of BASIC variables

			* = $1001		# Load address

# BASIC stub
			.word (NextLine),0
			.null $9e,format("%4d", Start)
NextLine
			.word 0

# Code and data (35 bytes total)
RowLoop
			lsr				# Calculate screen offset
			tay
			lda #$2a		# Asterisk character
CharLoop
			sta Screen,y	# Put char in bottom screen row
			iny
			inx
			bne CharLoop	# Loop for all chars
Start
			jsr ScrollUp	# Scroll the screen upwards (x=0 after)
			dec VarTab		# Adjust address of char count to use next
			lax (VarTab,x)	# Load char count into both a and x
			bmi RowLoop		# Loop while not done
			rts

# Asterisk counts (negative values to optimize the code)
			.char -3,-3,-23,-17,-11,-5,-15,-11,-7,-3,-7,-5,-3,-1