-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkerTopRight.asm
120 lines (101 loc) · 3.77 KB
/
checkerTopRight.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
################### Checker for top right of the board ###################
# Overall Program Functional Description:
#This function is meant to check whether a player 1 or player 2 has won the game
#or neither has won the game for the top right of the board.
#returns -1 if player has 4 in a row,
#0 if no 4 in a row, or 1 if computer has 4 in a row
################################################################
# Register Usage in Main:
#No in parameters
#Out parameters:
#$v0 = -1 if computer has 4 in a row,
#$v0 = 1 if human has 4 in a row,
#$v0 = 0 neither has 4 in a row
################################################################
# Pseudocode Description:
# t0 = 0; //row
# t1 = 3; //col
#
# while(t0 < 3){//rows
# while(t1 < 7){//cols
# t7 = 0;//total sum
# t2 = t0 * 7 + t1;//starting index
# t3 = board[t2];
# t7 += t3;
# t2 += 6;
# t4 = board[t2];
# t7 += t4;
# t2 += 6;
# t5 = board[t2];
# t7 += t5;
# t2 += 6;
# t6 = board[t2];
# t7 += t6;
# if(t7 == -4)
# goto _checker_end;
# if(t7 == 4)
# goto _checker_end;
# t1++;
# }
# t0++;
# }
#################################################################
.text
.globl check_top_right
check_top_right:
#Set row and col to 0
add $t0, $0, $0
li $t1, 3
while1:
#If row is less than 3
bge $t0, 3, endWhile1 # if $t0 >= $t1 then target
while2:
#If col is less than 7
bge $t1, 7, endWhile2 # if $t0 >= $t1 then target
#Set t7 to 0; t7 is the total sum
add $t7, $0, $0
#Set t2 to t0 * 7 + t1
#t2 is starting index
mul $t2, $t0, 7 # $t0 * 7 = Hi and Lo registers; Get the index based on row
add $t2, $t2, $t1 # $t0 = $t1 + $t2; Add col to index
#Make $t2 word aligned
sll $t2, $t2, 2 # $t2 = $t2 << 0
lw $t3, board($t2) # $t3 = board[t2]; Get the value of the board at the index
add $t7, $t7, $t3 # $t7 = $t7 + $t3; Add the value of the board at the index to the total sum
add $t2, $t2, 24 # $t2 = $t2 + 6*4; Add 6 to the index
#Make $t2 word aligned
# sll $t2, $t2, 2 # $t2 = $t2 << 0
lw $t3, board($t2) # $t3 = board[t2]; Get the value of the board at the index
add $t7, $t7, $t3 # $t7 = $t7 + $t3; Add the value of the board at the index to the total sum
add $t2, $t2, 24 # $t2 = $t2 + 6*4; Add 6 to the index
lw $t3, board($t2) # $t3 = board[t2]; Get the value of the board at the index
add $t7, $t7, $t3 # $t7 = $t7 + $t3; Add the value of the board at the index to the total sum
add $t2, $t2, 24 # $t2 = $t2 + 6*4; Add 6 to the index
lw $t3, board($t2) # $t3 = board[t2]; Get the value of the board at the index
add $t7, $t7, $t3 # $t7 = $t7 + $t3; Add the value of the board at the index to the total sum
#If t7 is -4
beq $t7, -4, _computer_wins # if $t7 == -4 then computer wins
#If t7 is 4
beq $t7, 4, _human_wins # if $t7 == 4 then human wins
#Increment col
add $t1, $t1, 1
j while2 # jump to while2
endWhile2:
#Increment row
add $t0, $t0, 1
#Reset col
li $t1, 3
j while1 # jump to while1
endWhile1:
#No 4 in a row
li $v0, 0
sw $v0, 0($sp)
jr $ra # jump to $ra
_computer_wins:
li $v0, -1
sw $v0, 0($sp)
jr $ra # jump to $ra
_human_wins:
li $v0, 1
sw $v0, 0($sp)
jr $ra # jump to $ra