From the mind of Kon Pik…

Hopeless without the internet

Happy Birthday!

So I’ve been updating the blog quite a lot recently.. but I’ve been quite reluctant to actually add any content. I’ve been adding/upgrading most of the backend stuff – such as no longer running my own SQL Server, but instead using the one provided by UW. In any case, uh, I’ve decided that once a week (at least) I’ll update the blog with something interesting I’ve found over the week. Even if I’m talking to myself, it’ll be fun to look back on this a while from now…

Here’s something I made just now out of curiosity – a happy birthday song in assembly (MIPS). This’ll only work in the MARS assembler (as SPIM doesn’t have the pseudo syscall to interact with MIDIs). It’s pretty messy but I made it in one go. The link to MARS is here. I found out that running it at roughly 23-25 instructions/second was a good fit – if I ran it at full speed, then MARS would crash. In any case here’s the code:

	.data
sixteenthnote: 	.word	500
eightnote: 		.word	1000
d8:				.word	1250		#dotted 8th
quarternote: 	.word 	1500
halfnote:		.word	2300
wholenote:		.word	3500

c:		.word	60
csharp:	.word	61
d:		.word	62
eflag:	.word	63
e:		.word	64
f:		.word	65
fsharp:	.word	66
g:		.word	67
gsharp:	.word	68
a:		.word	69
b0:		.word	70
c2:		.word	71
csharp2: .word	73

.align 2
	.text
	.globl	main

main:
	addi	$sp, $sp, -32	# stack frame is 32 bytes long
	sw	$ra, 20($sp)	# save return address
	sw	$fp, 16($sp)	# save old frame pointer
	addiu	$fp, $sp, 28	# set up frame pointer
	li $a2, 1		# instrument = piano
	li $a3, 127		# volume = max
	li $v0, 31		# type of syscall
bar1:
	jal playc
	jal p8b
	syscall		# C 8.
	jal p16
	syscall		# C 16

bar2:
	jal p4
	jal playd
	syscall		# D 4
	jal playc
	syscall		# C 4
	jal playf
	syscall		# F 4

bar3:
	jal p2
	jal playe
	syscall		# E 2
	jal playc
	jal p8b
	syscall		# C 8.
	jal p16
	syscall		# C 16

bar4:
	jal p4
	jal playd
	syscall		# D 4
	jal playc
	syscall		# C 4
	jal playg	# G 4
	syscall

bar5:
	jal p2
	jal playf
	syscall		# F 2
	jal playc
	jal p8b
	syscall		# C .8
	jal p16
	syscall		# C 16

bar6:
	jal playc2
	jal p4
	syscall		# C 4
	jal playa
	syscall		# A 4
	jal playf
	syscall		# F 4

bar7:
	jal playe
	syscall		# E 4
	jal p1
	jal playd
	syscall		# D 1
	jal p8b
	jal playb
	syscall		# B 8.
	jal	p16
	syscall		# B 16

bar8:
	jal p4
	jal playa
	syscall		# A 4
	jal playf
	syscall		# F 4
	jal playg
	syscall		# G 4

bar9:
	jal	p1
	jal playf
	syscall		# F1
	j startexit

playb:
	la $t0, b0
	lw $a0, 0($t0)
	jr $ra

playa:
	la $t0, a
	lw $a0, 0($t0)
	jr $ra

playc2:
	la $t0, c2
	lw $a0, 0($t0)
	jr $ra

playg:
	la $t0, g
	lw $a0, 0($t0)
	jr $ra
playe:
	la $t0, e
	lw $a0, 0($t0)
	jr $ra
playf:
	la $t0, f
	lw $a0, 0($t0)
	jr $ra
playc:
	la $t0, c
	lw $a0, 0($t0)
	jr $ra
playd:
	la $t0, d
	lw $a0, 0($t0)
	jr $ra
p4:
	la $t0, quarternote
	lw $a1, 0($t0)
	jr $ra
p2:
	la $t0, halfnote
	lw $a1, 0($t0)
	jr $ra
p1:
	la $t0, wholenote
	lw $a1, 0($t0)
	jr $ra
p16:
	la $t0, sixteenthnote
	lw $a1, 0($t0)
	jr $ra
p8b:
	la $t0, d8
	lw $a1, 0($t0)
	jr $ra
p8a:
	la $t0, eightnote
	lw $a1, 0($t0)
	jr $ra
startexit:
	li $v0, 10
	syscall

Attachment:
MusicASM (RAR)

Tags: , , , , ,