Write a function isLegalPosition() that takes as input a board location then ret
ID: 3667841 • Letter: W
Question
Write a function isLegalPosition() that takes as input a board location then returns “1” if it is a place where a game piece can be played, or returns a “0” if it is a place where a game piece cannot be placed. The function prototype in pseudocode is as follows: int isLegalPosition(int row, int column) { … } The board location consists of a (row,column) pair, and the return value is an integer. Your program should check to see that 0<=row,column<=7. Also, you must return “0” if the square is a black square.
MIPS Assembly Language
Explanation / Answer
$s0 = 32;
top: cout << “Input a value from 1 to 3”
cin >> $v0
switch ($v0)
{case(1): {$s0 = $s0 << 1; break;}
case(2): {$s0 = $s0 << 2; break;}
case(3): {$s0 = $s0 << 3; break;}
default: goto top; }
cout << $s0
A translation of this pseudocode into MIPS assembly language appears below:
.data
.align 2
jumptable: .word top, case1, case2, case3
prompt : .asciiz “ Input a value from 1 to 3: ”
.text
top:
li $v0, 4 # Code to print a string
la $a0, prompt
syscall
li $v0, 5 # Code to read an integer
syscall
blez $v0, top # Default for less than one
li $t3, 3
bgt $v0, $t3, top # Default for greater than 3
la $a1, jumptable # Load address of jumptable
sll $t0, $v0, 2 # Compute word offset
add $t1, $a1, $t0 #Form a pointer into jumptable
lw $t2, 0($t1) # Load an address from jumptable
jr $t2 # Jump to specific case “switch”
case1: sll $s0, $s0, 1 # Shift left logical one bit
b output
case2: sll $s0, $s0, 2 # Shift left logical two bits
b output
case3: sll $s0, $s0, 3 # Shift left logical three bits
output:
li $v0, 1 # Code to print an integer is 1
move $a0, $s0 # Pass argument to system in $a0
syscall # Output results
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.