I've done this to play a song that allows you to select instruments. Can anyone tell me why this is hanging?
.data
instrumentPrompt:.asciiz "Select instrument (piano,strings,or guitar): "
octavePrompt: .asciiz "Select octave (lo,med,or hi): "
msg2:.asciiz "\nInvalid instrument"
msg3:.asciiz "\nInvalid octave"
instrumentspace: .space 20
octavespace: .space 20
instrpiano: .asciiz "piano\n"
instrguitar: .asciiz "guitar\n"
instrstrings: .asciiz "strings\n"
instrquit: .asciiz "quit\n"
octavelow: .asciiz "lo\n"
octavemed: .asciiz "med\n"
octavehigh: .asciiz "hi\n"
octavequit: .asciiz "quit\n"
.text
.globl main
main:
li $s1,0 # Probably not needed but set the default instrument to piano
li $s2,0 # Probably not needed but set the default tone mutator to 0
setinstrumentprompt:
li $v0,4
la $a0,instrumentPrompt
syscall
li $v0,8
la $a0,instrumentspace
addi $a1,$zero,20
syscall #got string 1
la $a0,instrumentspace #pass address of instrumentspace
la $a1,instrpiano #pass address of octave1
jal isvalidinput #call isvalidinput
beq $v0,$zero,setpiano #check result and branch if valid
beq $v0,$zero,getoctave
la $a1,instrguitar #pass address of octave2
jal isvalidinput #call isvalidinput
beq $v0,$zero,setguitar #check result and branch if valid
beq $v0,$zero,getoctave
la $a1,instrstrings #pass address of octave3
jal isvalidinput #call isvalidinput
beq $v0,$zero,setstrings #check result and branch if valid
beq $v0,$zero,getoctave
la $a1,instrquit #pass address of octavespace
jal isvalidinput #call isvalidinput
beq $v0,$zero,endsong #check result and quit because user typed quit
bne $v0,$zero,setinstrumentprompt # Since they didn't quit and no match go back to prompt
getoctave:
li $v0,4
la $a0,octavePrompt
syscall
li $v0,8
la $a0,octavespace
addi $a1,$zero,20
syscall #got string 1
la $a0,octavespace #pass address of instrumentspace
la $a1,octavelow #pass address of octave1
jal isvalidinput #call isvalidinput
beq $v0,$zero,setlo #check result and branch if valid
beq $v0,$zero,playsong
la $a1,octavemed #pass address of octave2
jal isvalidinput #call isvalidinput
beq $v0,$zero,setmed #check result and branch if valid
beq $v0,$zero,playsong
la $a1,octavehigh #pass address of octave3
jal isvalidinput #call isvalidinput
beq $v0,$zero,sethigh #check result and branch if valid
beq $v0,$zero,playsong
la $a1,octavequit #pass address of octavespace
jal isvalidinput #call isvalidinput
beq $v0,$zero,endsong #check result and quit because user typed quit
bne $v0,$zero,getoctave # Since they didn't quit and no match go back to prompt
setpiano:
li $s1,0
j getoctave
setguitar:
li $s1,24
j getoctave
setstrings:
li $s1,40
j getoctave
setlo:
li $s2,-12
jr $ra
setmed:
li $s2,0
jr $ra
sethigh:
li $s2,12
jr $ra
ok:
li $v0,4
la $a0,msg3
syscall
exit:
li $v0,10
syscall
isvalidinput:
#The address of the each member of the option list is stored in a1 for comparison
#With input input from user
add $t0,$zero,$zero
add $t1,$zero,$a0
add $t2,$zero,$a1
bytecheck:
lb $t3($t1) #load a byte from each string
lb $t4($t2)
beqz $t3,checkt2 #instrumentspace end
beqz $t4,giveup
slt $t5,$t3,$t4 #compare two bytes
bnez $t5,giveup
addi $t1,$t1,1 #t1 points to the next byte of instrumentspace
addi $t2,$t2,1
j bytecheck
giveup:
addi $v0,$zero,1
jr $ra
checkt2:
bnez $t4,giveup
add $v0,$zero,$zero
playsong:
li $v0,33 #system service for MIDI output
add $a2,$zero,$s1
li $a3,127 #max volume
syscall
# The first sequence of notes. Repeated to play the last stanza
startsong:
li $t0,0 #60 is middle C
jal play_short
li $t0,7 #67 is G above middle C
jal play_short
li $t0,9 #69 is A above middle C
jal play_short
li $t0,7 #67 is G
jal play_long
li $t0,5 #69 is A above middle C
jal play_short
li $t0,4 #69 is A above middle C
jal play_short
li $t0,2 #69 is A above middle C
jal play_short
li $t0,0
jal play_long
# Skip to the endsong label if you've already played this twice
bgtz $t1,endsong
# This is the bridge
bridge:
bgt $t2,$t3,repeatt
add $t2,$t2,$t3
li $t0,7
jal play_short
li $t0,5
jal play_short
li $t0,4
jal play_short
li $t0,2
jal play_long
b bridge
# Increments the cycle counter and jumps to the beginning
repeatt:
add $t1,$t1,$t3
b startsong
# From the first stanza on second play through
endsong:
li $v0,10 #returning control to OS
syscall
play_short:
addu $a0,$t0,$t4
addu $a0,$a0,$s2
li $a1,400 #duration in milliseconds
syscall
syscall
jr $ra
play_long:
addu $a0,$t0,$t4
addu $a0,$a0,$s2
li $a1,800 #duration in milliseconds
syscall
jr $ra