PLEASE USE MIPS ASSEMBLY LANGUAGE TO CREATE A CHECKERS GAME For example t o prin
ID: 3665195 • Letter: P
Question
PLEASE USE MIPS ASSEMBLY LANGUAGE TO CREATE A CHECKERS GAME
For example to print a string:
la $a0,stringaddr
syscall $print_string
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The BoardObject is a data type that stores the locations of all the checkers on the board and the information associated with it. This is referred to as the “state” of the board. When you declare your function in MIPS assembly, please pass the variable board by reference. Do not try to use the stack to pass by value.
In this assignment, you will have to create a subroutine to display a full checker board. The checker board is a grid of 8x8 squares. Figure 1 shows what such a grid might look like in a regular game. Figure 1 The flow of the program should go as follows: 1. allocate space for board state initialize board state to all zeros 2. 3. Begin loop a. b. c. d. e. f. print board state ask for row ask for column ask for value set board (row,column) to value go to (3a) e set boar (row,column) to value The input parameter to this function is the board state. The function prototype would be defined as follows: void display_board(BoardObject board) (Explanation / Answer
# Write a simple assembly language program to # determine all legal moves for a Knight. ##################################################################### # data segment .data # ----- # Define constants. BOARD_SIZE = 8 TRUE = 1 FALSE = 0 # ----- # Local variables for main. title: .asciiz "MIPS Assignment #3 " hdr: .ascii " Knight Moves Problem " .asciiz "-------------------- " .align 4 board: .space 256 .align 4 count: .word 0 cnt_msg: .asciiz "Count: " newLine2: .asciiz " " krow: .word 0 kcol: .word 0 kDone: .asciiz " Game Over " # ----- # Local variables for getInitial() procedure. irowprompt: .ascii "Enter initial Knights position " .asciiz " Row: " icolprompt: .asciiz " Column: " errmsg: .ascii "Error, must be between 0 and 7." .asciiz " Please re-enter. " knight: .ascii "K" star: .ascii "*" # ----- # Local variables for calcMoves() procedure. badBoard: .asciiz "Error, invalid board " # ----- # Local variables for retry() procedure. qst: .asciiz " Enter Another Position (y/n): " ans: .space 4 ansErr: .asciiz " Error, must respond with "y" or "n". " # ----- # Local variables for prtBoard() procedure. brdHdr: .asciiz " Possible Knight Moves: " nums: .asciiz " 0 1 2 3 4 5 6 7 " lines: .asciiz " --------------- " bar: .asciiz "|" spc3: .asciiz " " spcn: .asciiz " " tmpChar: .asciiz " " newLine: .asciiz " " ##################################################################### # text/code segment .text .globl main .ent main main: # ----- # Display header. la $a0, title li $v0, 4 syscall # ----- # Initial Set-up. # Resets Chess Board (to all 0's) # Display Header # Prompts user for initail location. knightMoves: la $a0, board jal clearBoard la $a0, hdr li $v0, 4 syscall la $a0, board la $a1, krow la $a2, kcol jal getInitial # ----- # Calculate all possible knight moves. la $a0, board lw $a1, krow lw $a2, kcol jal calcMoves # ----- # Print board. la $a0, board jal prtBoard # ----- # Try again? jal retry beq $v0, TRUE, knightMoves # ----- # Done, terminate program. la $a0, kDone li $v0, 4 syscall li $v0, 10 syscall # au revoir... .end main ##################################################################### # Get initial move from user. # Place knight on the baord. # knight position is marked with integer 1. # addr(row,col) = baseAddr + (row * colSize + col) * dataSize # ----- # Arguments: # $a0 - address of board # $a1 - address of row variable # $a2 - address of col variable # YOUR CODE GOES HERE # Print Header # Print question to user # Print row: wait for answer # Check its between 0-7 # Print column: wait for answer # Check its between 0-7 # User answer to calculate where to insert "K" into array .globl getInitial .ent getInitial getInitial: # Push array addr subu $sp, $sp, 4 sw $a0, ($sp) getInput: # Print row header la $a0, irowprompt li $v0, 4 syscall # Grab row value li $v0, 5 syscall # Error Check bgt $v0, 7, inputError blt $v0, 0, inputError # Store row Value sw $v0, ($a1) # Print new line la $a0, newLine li $v0, 4 syscall # Print column header la $a0, icolprompt li $v0, 4 syscall # Grab column value li $v0, 5 syscall # Error check bgt $v0, 7, inputError blt $v0, 0, inputError # Store column value sw $v0, ($a2) j inputComplete # Error message inputError: la $a0, errmsg li $v0, 4 syscall j getInput inputComplete: # Insert K into array # arr(a1, a2) = a0 + ( (a1*BOARDSIZE) + a2)*4 # Pop array addr lw $a0, ($sp) addu $sp, $sp, 4 move $t0, $a0 # board into t0 lw $t1, ($a1) # row into t1 lw $t2, ($a2) # col into t2 mul $t1, $t1, BOARD_SIZE add $t1, $t1, $t2 mul $t1, $t1, 4 add $t0, $t0, $t1 # $t0 is now pointing to location to place K li $t3, 'K' sw $t3, ($t0) jr $ra .end getInitial ##################################################################### # Calculate all possible knight moves. # Possible move is marked with '*'. # board(row,col) # addr(row,col) = baseAddr + (row * colSize + col) * dataSize # ----- # Arguments: # $a0 - address of board # YOUR CODE GOES HERE # Calculate all moves .globl calcMoves .ent calcMoves calcMoves: # +2 row, -1 col move $t0, $a0 # board addr move $t1, $a1 # row move $t2, $a2 # col add $t1, $t1, 2 bgt $t1, 7, err1 # if either location not on board, skip sub $t2, $t2, 1 blt $t2, 0 err1 mul $t1, $t1, BOARD_SIZE add $t1, $t1, $t2 mul $t1, $t1, 4 add $t0, $t0, $t1 # $t0 is now pointing to location li $t3, '*' sw $t3, ($t0) err1: # +2 row, +1 col move $t0, $a0 # board addr move $t1, $a1 # row move $t2, $a2 # col add $t1, $t1, 2 bgt $t1, 7, err2 # if either location not on board, skip add $t2, $t2, 1 bgt $t2, 7 err2 mul $t1, $t1, BOARD_SIZE add $t1, $t1, $t2 mul $t1, $t1, 4 add $t0, $t0, $t1 # $t0 is now pointing to location li $t3, '*' sw $t3, ($t0) err2: # -2 row, -1 col move $t0, $a0 # board addr move $t1, $a1 # row move $t2, $a2 # col sub $t1, $t1, 2 blt $t1, 0, err3 # if either location not on board, skip sub $t2, $t2, 1 blt $t2, 0 err3 mul $t1, $t1, BOARD_SIZE add $t1, $t1, $t2 mul $t1, $t1, 4 add $t0, $t0, $t1 # $t0 is now pointing to location li $t3, '*' sw $t3, ($t0) err3: # -2 row, +1 col move $t0, $a0 # board addr move $t1, $a1 # row move $t2, $a2 # col sub $t1, $t1, 2 blt $t1, 0, err4 # if either location not on board, skip add $t2, $t2, 1 bgt $t2, 7 err4 mul $t1, $t1, BOARD_SIZE add $t1, $t1, $t2 mul $t1, $t1, 4 add $t0, $t0, $t1 # $t0 is now pointing to location li $t3, '*' sw $t3, ($t0) err4: # +2 col, +1 row move $t0, $a0 # board addr move $t1, $a1 # row move $t2, $a2 # col add $t1, $t1, 1 bgt $t1, 7, err5 # if either location not on board, skip add $t2, $t2, 2 bgt $t2, 7 err5 mul $t1, $t1, BOARD_SIZE add $t1, $t1, $t2 mul $t1, $t1, 4 add $t0, $t0, $t1 # $t0 is now pointing to location li $t3, '*' sw $t3, ($t0) err5: # +2 col, -1 row move $t0, $a0 # board addr move $t1, $a1 # row move $t2, $a2 # col sub $t1, $t1, 1 blt $t1, 0, err6 # if either location not on board, skip add $t2, $t2, 2 bgt $t2, 7 err6 mul $t1, $t1, BOARD_SIZE add $t1, $t1, $t2 mul $t1, $t1, 4 add $t0, $t0, $t1 # $t0 is now pointing to location li $t3, '*' sw $t3, ($t0) err6: # -2 col, +1 row move $t0, $a0 # board addr move $t1, $a1 # row move $t2, $a2 # col add $t1, $t1, 1 bgt $t1, 7, err7 # if either location not on board, skip sub $t2, $t2, 2 blt $t2, 0 err7 mul $t1, $t1, BOARD_SIZE add $t1, $t1, $t2 mul $t1, $t1, 4 add $t0, $t0, $t1 # $t0 is now pointing to location li $t3, '*' sw $t3, ($t0) err7: # -2 col, -1 row move $t0, $a0 # board addr move $t1, $a1 # row move $t2, $a2 # col sub $t1, $t1, 1 blt $t1, 0, err8 # if either location not on board, skip sub $t2, $t2, 2 blt $t2, 0 err8 mul $t1, $t1, BOARD_SIZE add $t1, $t1, $t2 mul $t1, $t1, 4 add $t0, $t0, $t1 # $t0 is now pointing to location li $t3, '*' sw $t3, ($t0) err8: jr $ra .end calcMoves ##################################################################### # Re-set board (to all spaces). # board(row,col) # addr(row,col) = baseAddr + (row * colSize + col) * dataSize # ----- # Arguments: # $a0 - address of board # YOUR CODE GOES HERE # place spaces into entire array to clear board .globl clearBoard .ent clearBoard clearBoard: li $t1, 64 # counter move $t0, $a0 # board addr li $t3, ' ' # no idea if this works zeroBoard: sw $t3, ($t0) add $t0, $t0, 4 sub $t1, $t1, 1 bne $t1, 0, zeroBoard jr $ra .end clearBoard ##################################################################### # Print chess board to screen showing # the position of the queens. # ----- # Arguments # $a0, address, board # YOUR CODE GOES HERE # BROKEN .globl prtBoard .ent prtBoard prtBoard: subu $sp, $sp, 4 sw $s0, ($sp) move $s0, $a0 # board # HEADER # Print header la $a0, brdHdr li $v0, 4 syscall # Linefeed la $a0, newLine li $v0, 4 syscall # NUMBERS HEADER # Spaces la $a0, spc3 li $v0, 4 syscall # Numbers la $a0, nums li $v0, 4 syscall # LINES # Spaces la $a0, spc3 li $v0, 4 syscall # Lines la $a0, lines li $v0, 4 syscall #Linefeed la $a0, newLine li $v0, 4 syscall # Board Print # s0 holds board li $t1, 0 # counter 1 li $t2, 0 # counter 2 li $t3, 0 # line num? lineNum: # Spaces la $a0, spcn li $v0, 4 syscall # Line Num move $a0, $t3 li $v0, 1 syscall add $t3, $t3, 1 # Line number output boardPrt: # Bar la $a0, bar li $v0, 4 syscall # Board array la $t4, ($s0) move $a0, $t4 li $v0, 4 syscall # Increment add $s0, $s0, 4 add $t1, $t1, 1 beq $t1, 64, printDone # When board is done printing add $t2, $t2, 1 bgt $t2, 7, endLine # end of line j boardPrt # continue printing board endLine: # Bar la $a0, bar li $v0, 4 syscall # Linefeed la $a0, newLine li $v0, 4 syscall li $t2, 0 j lineNum printDone: # Bar la $a0, bar li $v0, 4 syscall # Linefeed la $a0, newLine li $v0, 4 syscall # Spaces la $a0, spc3 li $v0, 4 syscall # Lines la $a0, lines li $v0, 4 syscall #Linefeed la $a0, newLine li $v0, 4 syscall lw $s0, ($sp) addu $sp, $sp, 4 jr $ra .end prtBoard ##################################################################### # Ask if user wants to enter another initial position. # ----- # Arguments: # none .globl retry .ent retry retry: # ----- # Ask question: re_ask: la $a0, qst li $v0, 4 syscall la $a0, ans li $a1, 2 li $v0, 8 syscall li $t0, 0 lb $t0, ans li $v0, FALSE beq $t0, 110, retry_done # 110 = "n" li $v0, TRUE beq $t0, 121, retry_done # 110 = "y" la $a0, ansErr li $v0, 4 syscall j re_ask # ----- # Done, return to main. retry_done: jr $ra .end retry
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.