On the n’th day of Christmas, my (assert love=true) love sent to me…

25 12 2009

As an excuse to get all the IDEs and compilers I might possibly need running on my Vostro, plus to get in the Christmas spirit and code something in languages I haven’t used in a while, enjoy the Twelve Days of Christmas. I got bored after MIPs, maybe I’ll get around to adding on a SKILL/LISP implementation and VHDL later.

In TCL,

set days [list\
	"first" "second" "third"\
	"fourth" "fifth" "sixth"\
	"seventh" "eighth" "ninth"\
	"tenth" "eleventh" "twelfth"]

set gifts [list\
	"partridge in a pear tree"\
	"Two turtle doves"\
	"Three French hens"\
	"Four calling birds"\
	"Five golden rings"\
	"Six geese a-laying"\
	"Seven swans a-swimming"\
	"Eight maids a-milking"\
	"Nine ladies dancing"\
	"Ten lords a-leaping"\
	"Eleven pipers piping"\
	"Twelve drummers drumming"]

for {set i 0} {$i < 12} {incr i} {
           puts "On the [lindex $days $i] day of Christmas,"
           puts "My true love sent to me,"
for {set j $i} {$j > 0} {incr j -1} {
		puts "[lindex $gifts $j],"
	}

	if {$i == 0} {
		puts "A [lindex $gifts 0].\n"
	} else {
		puts "And a [lindex $gifts 0].\n"
	}
}

In C,

#include 

int main() {
	const char *days[12] = {"first", "second", "third",
		"fourth", "fifth", "sixth",\
		"seventh", "eighth", "ninth",\
		"tenth", "eleventh", "twelfth"};

	const char *gifts[12] = {"partridge in a pear tree",\
	"Two turtle doves",\
	"Three French hens",\
	"Four calling birds",\
	"Five golden rings",\
	"Six geese a-laying",\
	"Seven swans a-swimming",\
	"Eight maids a-milking",\
	"Nine ladies dancing",\
	"Ten lords a-leaping",\
	"Eleven pipers piping",\
	"Twelve drummers drumming"};

	int i, j;
	for (i = 0; i < 12;i++) {
            printf("On the %s day of Christmas,\n", days[i]);
            printf("My true love sent to me,\n");
 		for (j = i; j > 0; j--)
			printf("%s,\n", gifts[j]);

		if (i == 0)
			printf("A %s.\n\n", gifts[0]);
		else
			printf("And a %s.\n\n", gifts[0]);
	}
}

In Java,

public class song {
	private final static String days[] = {
		"first", "second", "third",
		"fourth", "fifth", "sixth",
		"seventh", "eighth", "ninth",
		"tenth", "eleventh", "twelvth"};

	private final static String gifts[] = {
		"partridge in a pear tree",
		"Two turtle doves",
		"Three French hens",
		"Four calling birds",
		"Five golden rings",
		"Six geese a-laying",
		"Seven swans a-swimming",
		"Eight maids a-milking",
		"Nine ladies dancing",
		"Ten lords a-leaping",
		"Eleven pipers piping",
		"Twelve drummers drumming"};

	public static void main(String args[]) {
		for (int i = 0; i < 12;i++) {
                      System.out.println("On the " + days[i] + " day of Christmas,");
                      System.out.println("My true love sent to me,");
		      for (int j = i; j > 0; j--)
			  System.out.println(gifts[j]);

			if (i == 0)
				System.out.println("A " + gifts[0] + ".");
			else
				System.out.println("And a " + gifts[0]);
			System.out.println();
		}

	}
}

In MIPS,

.data
	day1:	.asciiz	"first"
	day2:	.asciiz	"second"
	day3:	.asciiz	"third"
	day4:	.asciiz	"fourth"
	day5:	.asciiz	"fifth"
	day6:	.asciiz	"sixth"
	day7:	.asciiz	"seventh"
	day8:	.asciiz	"eighth"
	day9:	.asciiz	"ninth"
	day10:	.asciiz	"tenth"
	day11:	.asciiz	"eleventh"
	day12:	.asciiz	"twelvth"

	gift1:	.asciiz	"partridge in a pear tree"
	gift2:	.asciiz	"Two turtle doves"
	gift3:	.asciiz	"Three French hens"
	gift4:	.asciiz	"Four calling birds"
	gift5:	.asciiz	"Five golden rings"
	gift6:	.asciiz	"Six geese a-laying"
	gift7:	.asciiz	"Seven swans a-swimming"
	gift8:	.asciiz	"Eight maids a-milking"
	gift9:	.asciiz	"Nine ladies dancing"
	gift10:	.asciiz "Ten lords a-leaping"
	gift11:	.asciiz	"Eleven pipers piping"
	gift12:	.asciiz	"Twelve drummers drumming"

	onThe: .asciiz "On the "
	dayOf: .asciiz " day of Christmas,\nMy True love sent to me,\n"
	trueLove: .asciiz "My true love sent to me,\n"
	period: .asciiz ".\n\n"
	comma: .asciiz ",\n"
	newline: .asciiz "\n"
	andA: .asciiz "And a "
	justA: .asciiz "A "

	dayAddressesArray: .word 0,0,0,0,0,0,0,0,0,0,0,0
	giftAddressesArray: .word 0,0,0,0,0,0,0,0,0,0,0,0

.text

main:

	# Put all the addresses of the day strings in an array
	la $s0, dayAddressesArray
	la $t1, day1
	sw $t1, 0($s0)
	la $t1, day2
	sw $t1, 4($s0)
	la $t1, day3
	sw $t1, 8($s0)
	la $t1, day4
	sw $t1, 12($s0)
	la $t1, day5
	sw $t1, 16($s0)
	la $t1, day6
	sw $t1, 20($s0)
	la $t1, day7
	sw $t1, 24($s0)
	la $t1, day8
	sw $t1, 28($s0)
	la $t1, day9
	sw $t1, 32($s0)
	la $t1, day10
	sw $t1, 36($s0)
	la $t1, day11
	sw $t1, 40($s0)
	la $t1, day12
	sw $t1, 44($s0)

	# Put all the addresses of the gift strings in an array
	la $s1, giftAddressesArray
	la $t1, gift1
	sw $t1, 0($s1)
	la $t1, gift2
	sw $t1, 4($s1)
	la $t1, gift3
	sw $t1, 8($s1)
	la $t1, gift4
	sw $t1, 12($s1)
	la $t1, gift5
	sw $t1, 16($s1)
	la $t1, gift6
	sw $t1, 20($s1)
	la $t1, gift7
	sw $t1, 24($s1)
	la $t1, gift8
	sw $t1, 28($s1)
	la $t1, gift9
	sw $t1, 32($s1)
	la $t1, gift10
	sw $t1, 36($s1)
	la $t1, gift11
	sw $t1, 40($s1)
	la $t1, gift12
	sw $t1, 44($s1)

	li $t0, 0 # $t0 <- outer loop index
	outerLoopStart:
		la $a0,onThe
		li $v0,4
		syscall

		move $t1, $t0
		sll $t1, $t1, 2
		add $t2, $s0, $t1 	# t2 <- address of array element with address of string
		lw $a0, 0($t2) 		# t2 <- address of string
		li $v0,4
		syscall
		la $a0, dayOf
		li $v0, 4
		syscall

		move $t1, $t0
		innerLoopStart:
			beq $t1, $zero, innerLoopEnd
			move $t3, $t1
			sll $t3, $t3, 2
			add $t3, $t3, $s1
			lw $a0, 0($t3)
			li $v0, 4
			syscall

			la $a0, comma
			li $v0, 4
			syscall

			addi $t1, -1
			beq $t1, $zero, innerLoopEnd
			j innerLoopStart
		innerLoopEnd:

		beq $t0, $zero, printA

		printAnd:
			la $a0, andA
			li $v0, 4
			syscall
			j donePrinting

		printA:
			la $a0, justA
			li $v0, 4
			syscall
		donePrinting:

		lw $a0, 0($s1)
		li $v0, 4
		syscall
		la $a0, period
		li $v0, 4
		syscall

		addi $t0, 1
		beq $t0, 12, outerLoopEnd
		j outerLoopStart

	outerLoopEnd:
		jr $ra

In SKILL,


days = ‘(“first” “second” “third” “fourth” “fifth” “sixth”\
” seventh” “eighth” “ninth” “tenth” “eleventh” “twelvth”)

gifts = ‘(“partridge in a pear tree”\
“Two turtle doves”\
“Three French hens”\
“Four calling birds”\
“Five golden rings”\
“Six geese a-laying”\
“Seven swans a-swimming”\
“Eight maids a-milking”\
“Nine ladies dancing”\
“Ten lords a-leaping”\
“Eleven pipers piping”\
“Twelve drummers drumming”)

(defun sing ()
(for i 0 11
(printf “On the %s day of Christmas,\n” nth(i days))
(printf “My true love sent to me,\n”)

j = i
(while j > 0
(printf “%s,\n” nth(j gifts))
j = j – 1
)

(if i == 0 then
(printf “A %s.\n\n” (car gifts))
else
(printf “And a %s.\n\n” (car gifts))
)
)
)

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: