Sudoku Solver Using Mips Instructions

Assemble the below code in Mips Assembler to solve the sudoku below. input the sudoku in the following grid. .data .align 4 #variable of the board board: .byte 0, 6, 0, 1, 0, 4, 0, 5, 0 .byte 0, 0, 8, 3, 0, 5, 6, 0, 0 .byte 2, 0, 0, 0, 0, 0, 0, 0, 1 .byte 8, 0, 0, 4, 0, 7, 0, 0, 6 .byte 0, 0, 6, 0, 0, 0, 3, 0, 0 .byte 7, 0, 0, 9, 0, 1, 0, 0, 4 .byte 5, 0, 0, 0, 0, 0, 0, 0, 2 .byte 0, 0, 7, 2, 0, 6, 9, 0, 0 .byte 0, 4, 0, 5, 0, 8, 0, 7, 0 new_row: .ascii "\n" .text .globl main main: #$s0 has the offset #$s1 has the row #s2 has the coloum #$s3 for the number currently testing move $a0, $zero #first cell with offset 0 jal guess jal print_grid j exit exit: move $a0, $s0 li $v0, 17 syscall guess: #Setup the stack frame and save all the required register's values to be used later sub $sp, $sp, 20 sw $ra, 16($sp) sw $s0, 12($sp) sw $s1, 8($sp) sw $s2, 4($sp) sw $s3, 0($sp) move $s0, $a0 beq $s0, 81, guess_comp #Chekc if it has reached the end of the grid #iF NOT THE END li $s3, 9 div $s0, $s3 mflo $s1 #lo holds the quotient mfhi $s2 #hi has the remainder lb $t0, board+0($s0) beq $t0, 0, guess_it # if equal to zero then guess the number to be placed in it addi $a0, $s0, 1 jal guess j guess_return #$a0 has the number #$a1 has the row #$a2 has the coloumn guess_it: move $a0, $s3 move $a1, $s1 move $a2, $s2 jal check_num bnez $v0, num_exist #else sb $s3, board + 0($s0) addi $a0, $s0, 1 jal guess beqz $v0, guess_return num_exist: sub $s3, $s3, 1 bnez $s3, guess_it sb $zero, board + 0($s0) #else No number is satisfied as already checked from 9 to 1 go back change the previous number and come back li $v0, 1 j guess_return guess_comp: move $v0, $zero guess_return: lw $ra, 16($sp) lw $s0 , 12($sp) lw $s1, 8($sp) lw $s2, 4($sp) lw $s3, 0($sp) addi $sp, $sp, 20 jr $ra #set t1 to an element. decrease t0 by 1 and check it with t1 check_num: li $t0, 9 mul $t1, $a1, $t0 # offset of the first element of the row of the element to guess check_num_row: lb $t2, board+0($t1) beq $t2, $a0, present #Found the element in the row #else addi $t1, $t1, 1 sub $t0, $t0, 1 bnez $t0, check_num_row #else move $t1, $a2 check_num_col: lb $t2, board+0($t1) beq $t2, $a0, present #found the element in the coloumn #else addi $t1, $t1, 9 ble $t1, 81, check_num_col #else #(success) move $v0, $zero j check_success present: li $v0, 1 check_success: jr $ra #Printing #s0 holds the offset #s1 hold row #s2 hols coloumn print_grid: sub $sp, $sp, 16 sw $ra, 12($sp) sw $s2, 8($sp) sw $s1, 4($sp) sw $s0, 0($sp) la $s0, board #number-> board[0,0] move $s1, $zero #row ->0 move $s2, $zero #coloum ->0 print_cell: lb $a0, ($s0) #To print a int li $v0, 1 syscall addi $s0, $s0, 1 #add to offset addi $s2, $s2, 1 #add to coloumn blt $s2, 9, print_cell #if the coloumn is complted la $a0, new_row #to print asciiz li $v0, 4 syscall move $s2, $zero #reset coloumn to zero addi $s1, $s1, 1 #add to row #Check if the row is printed blt $s1,9, print_cell lw $s0, 0($sp) # Restore the $s0 register lw $s1, 4($sp) # Restore the $s1 register lw $s2, 8($sp) # Restore the $s2 register lw $ra, 12($sp) # Restore the return address addi $sp, $sp, 16 # Clean up the stack jr $ra

Share this

Related Posts

Latest
Previous
Next Post »