[solved]-Task Lab Write Piece Mips Code Prints Instructions Move Well Ordered Stack Discs One Peg O Q39079444
Your task in this lab is to write a piece of MIPS code thatprints instructions on how to move a well ordered stack of discsfrom one peg to one of the other two – for an arbitrary number ofdiscs. In doing so, you may certainly assume that all of the discsbegin on peg #1, and label the other pegs #2 and #3.
A framework code is provided at the end of this document. Thecode is currently set to begin (note: just begin) with 4 discs onthe first peg.
Complete the following steps.
- Carefully study the “main:” procedure and try to figure out howit operates.
- Go to the “hanoi:” procedure and fill in the prologue andepilogue. Edit the remainder of the code as you see necessary.[Note that the procedure “hanoi” receives the source peg in $a0,”spare” peg in $a1, destination peg in $a2, and number of discs (n)in $a3.]
- Use the “print_message” procedure for printing moves.
- Design your program so that it works for any arbitrarynon-negative number of starting disks. For example, your programmust work for n=0 but not n=-1.
Sample Output
A sample out for n = 4.
Move disk from one to two
Move disk from one to three
Move disk from two to three
Move disk from one to two
Move disk from three to one
Move disk from three to two
Move disk from one to two
Move disk from one to three
Move disk from two to three
Move disk from two to one
Move disk from three to one
Move disk from two to three
Move disk from one to two
Move disk from one to three
Move disk from two to three
The Framework Assembly Code
.data
nDisks: .word 4
peg1: .asciiz “one”
peg2: .asciiz “two”
peg3: .asciiz “three”
.text
main: la $a0,peg1
la $a1,peg2
la $a2,peg3
lw $a3,nDisks
jal hanoi
li $v0,10 # system call code for_exit
syscall
# procedure hanoi
# Register assignments
#
# Required: you must write out your
# register assignments here
# before writing your program
#
hanoi: beq $a3,$zero,getOut # if no work to do, return
# prologue
# save EveryThing that may be destroyed
# Your code goes here.
# next, make copies of source, spare, destination, n
move $s0,$a0
move $s1,$a1
move $s2,$a2
move $s3,$a3
# prepare, and call hanoi
move $a0,$s0
move $a1,$s2
move $a2,$s1
addi $a3,$s3,-1
jal hanoi
# call print_message
move $a0,$s0
move $a1,$s2
jal print_message
# call hanoi again
move $a0,$s1
move $a1,$s0
move $a2,$s2
addi $a3,$s3,-1
jal hanoi
# epilogue
#
# Your code goes here.
#
getOut: jr $ra
# print_message
.data
m1: .asciiz “Move disk from”
m2: .asciiz ” to “
CR: .asciiz “.n”
.text
print_message: move $t2,$a0 # For safety: thesyscalls will destroy $a0
move$t3,$a1 # More safety.
li $v0,4 # system call code for _print_string
la $a0,m1 # address of string
syscall
li $v0,4 # system call code for _print_string
move $a0,$t2
syscall
li $v0,4 # system call code for _print_string
la $a0,m2 # address of string
syscall
li $v0,4 # system call code for _print_string
move $a0,$t3
syscall
li $v0,4 # system call code for _print_string
la $a0,CR # address of string
syscall
jr $ra
Expert Answer
Answer to Your task in this lab is to write a piece of MIPS code that prints instructions on how to move a well ordered stack of d… . . .
OR

