03 May 2010 by Published in: Code Tags: 1 comment
##############################
####DREW CRAWFORD#############
####ASSIGNMENT 2##############
####COMPUTER ARCHITECTURE#####
####FEB 2 2010########
##############################
.data
.align 2
A: .asciiz "A\n"
.align 2
B: .asciiz "B\n"
.align 2
C: .asciiz "C\n"
prompt: .asciiz "How many disks?"
.align 2
newline: .asciiz "\n"
.align 2
print:  .asciiz "Move from \n"
.align 2
to: .asciiz " to \n"
.align 2
movet: .asciiz "The total number of moves is: \n"

.text
la $a0,prompt
li $v0,51
syscall
move $t0,$a0

#####t0 - n
#####a1 - "A"
#####a2 - "B"
#####a3 - "C"
la $a1,A
la $a2,B
la $a3,C
jal hanoi
#print totals
la $s1,movet
jal printstr
move $a0,$v1
li $v0,1
syscall
li $v0,10
syscall

li $v1,0

#A hanoi solution in 32 instructions
#(11 print instructions, 1 move count instruction)
#So only 20 algorithmic instructions.
#And only one expensive lw/sw pair per call.
#Beat that.
hanoi:
beq $t0,0,silentreturn
addi $t0,$t0,-1
xor $a2,$a2,$a3
xor $a3,$a2,$a3
xor $a2,$a2,$a3
addi $sp,$sp,-4
sw $ra,0($sp)
jal hanoi
##############non-algoirthmic section
#An implementation detail prevents me from using the printstr syscall on my emulator
#Feel free to replace with the syscall
la $s1, print
jal printstr
move $s1, $a1
jal printstr
la $s1,to
jal printstr
move $s1, $a3
jal printstr
la $a0,newline
la $v0,4
syscall
addi $v1,$v1,1
#############end non-algorithmic section
move $t1,$a3
move $a3,$a1
move $a1,$a2
move $a2,$t1
jal hanoi
xor $a1,$a1,$a3
xor $a3,$a1,$a3
xor $a1,$a1,$a3
lw $ra,0($sp)
addi $sp,$sp,4
addi $t0,$t0,1
silentreturn: jr $ra

printstr: #my hack around a printstr issue on my emulator
ld $s2,($s1)
andi $a0,$s2,255 #select xxxX
li $v0,11
beq $a0,10,ret
syscall
andi $a0,$s2,65280 #select xxXx
srl $a0,$a0,8
beq $a0,10,ret
syscall
andi $a0,$s2,16711680 #select xXxx
srl $a0,$a0,16
beq $a0,10,ret
syscall
andi $a0,$s2,4278190080 #select Xxxx
srl $a0,$a0,24
beq $a0,10,ret
syscall
addi $s1,$s1,4
j printstr
ret: jr $ra

Want me to build your app / consult for your company / speak at your event? Good news! I'm an iOS developer for hire.

Like this post? Contribute to the coffee fund so I can write more like it.

Comments

  1. Dogus yigit ozcelik
    Sun 30th Oct 2011 at 5:44 pm

    my compiler(QTspim) says:
    andi $a0,$s2,16711680

    andi $a0,$s2,4278190080

    are out of range

Comments are closed.

Powered by WordPress