Monday, 15 July 2013

assembly - Homework - TICTACTOE - addressing issue -



assembly - Homework - TICTACTOE - addressing issue -

hey guys having problem understanding behavior of program. trying store address homecoming to, keeps beingness overwritten somehow. homework don't give me easy answers! perhaps explantion how jal might overwriting $s0 register. don't quite understand how possible. set comments in code can see loop occuring.

2.

.data # info segment variables , strings # these strings need outputted console title: .asciiz "tic-tac-toe!\n\n" oprompt: .asciiz "o player: please come in row/col, 1 per line:\n\n" xprompt: .asciiz "x player: please come in row/col, 1 per line:\n\n" invposstr: .asciiz " invalid position. seek again.\n\n" owinstr: .asciiz "o won game,\n\n exiting. . ." xwinstr: .asciiz "x won game,\n\n exiting. . ." catstr: .asciiz "cat game,\n\n exiting. . ." border: .asciiz "\n\n _ _ _ \n" # initialze board 9 bytes board: .byte 'x','x','x','x','x','_','x','_','_' .text # text segment instructions main: # begin main func li $v0, 4 #set v0 print string la $a0, title #set string print (in $a0) title syscall #execute li $s1, 0 #use s1 game counter (up 9 turns) playerx: #player x go first li $v0, 4 #prepare print string la $a0, xprompt #load move prompt syscall jal get_valid_move #go valid move address # having problem returning here li $t0, 'x' #load asciiz code x sb $t0, ($v0) #store x board address addi $s1, 1 #increment game counter next turn jal print_board #after getting move, print board status # , here playero: #player o go sec li $v0, 4 #prepare print string la $a0, oprompt #load move prompt syscall jal get_valid_move #go valid move address # , here li $t0, 'o' #load asciiz code o sb $t0, ($v0) #store o board address addi $s1, 1 #increment game counter next turn jal print_board #after getting move, print board status # , here j playerx #go player x turn # store current homecoming address below (first time) print_board: #function print status of board move $s0, $ra #save recent homecoming address la $t1, board #load board address li $v0, 4 #set v0 print string la $a0, border #set string print (in $a0) prompt syscall #execute li $t0, 0 #use counter loop loop: #use loop print 1 row @ time li $v0, 11 #prepare print char li $a0, 124 #asciiz code | syscall lb $a0, ($t1) #load next byte board li $v0, 11 #prepare print char syscall addi $t1, $t1, 1 #increment address of board next byte/char addi $t0, $t0, 1 #increment loop counter beq $t0, 3, rowend #go print special stuff @ end of row beq $t0, 6, rowend #go print special stuff @ end of row beq $t0, 9, rowend #go print special stuff @ end of row j loop #go top rowend: li $a0, 124 #asciiz | li $v0, 11 #prepare print char syscall li $a0, 10 #ascizz newline li $v0, 11 #prepare print char syscall blt $t0, 9, loop #unless @ lastly row, loop li $a0, 10 #ascizz newline li $v0, 11 #prepare print char syscall jr $ra #return gameplay in main entry_address: #function transform user input (row, col) board address move $t0, $a1 #make re-create of row utilize move $t1, $a2 #make re-create of col utilize addi $t0, $t0, -1 #use n-1 accessing storage address addi $t1, $t1, -1 #use n-1 mul $t0, $t0, 3 #mul row 3 la $v0, board #load base of operations address of board add together $v0, $v0, $t0 #add row offset add together $v0, $v0, $t1 #add col offset #v0 holds right board position address jr $ra #now go check if valid move valid_position: li $v0, 1 #assume valid blt $a1, 1, inv #if row less 1 bgt $a1, 3, inv # or if row greater 3 blt $a2, 1, inv # or if col less 1 bgt $a2, 3, inv # or if col greater 3 alter validity jr $ra #go get_valid_move @ lastly instruction inv: li $v0, 0 #since branched, alter inv jr $ra #go get_valid_move @ lastly instruction # store current homecoming address s0 below (the 2nd time) get_valid_move: move $s0, $ra #save recent homecoming address li $v0, 5 #prepare read row int syscall move $a1, $v0 #copy read int argument passed li $v0, 5 #prepare read col int syscall move $a2, $v0 #copy read int argument passed jal valid_position #go create sure pos valid beq $v0, 0, invalid #go tell user if valid_position returned false jal entry_address #position go entry_address # jr statement below looping here # need homecoming lastly instruction in main thought stored in $s0 li $t0, '_' #use underscore comparing lb $t1, ($v0) #load byte entry_address bne $t0, $t1, invalid #if entry taken, move jr $s0 #since move valid go gameplay in main invalid: li $v0, 1 #prepare print int move $a0, $a1 #load row numb syscall li $v0, 11 #prepare print char li $a0, 44 #load comma asciiz syscall li $v0, 1 #prepare print int move $a0, $a2 #load col numb syscall li $v0, 4 #prepare print string la $a0, invposstr #load invalid pos message syscall j get_valid_move #repeat user input loop check_all_match: print_winner_and_exit: row_winner: col_winner: diag_winner: check_for_win: # after printing results, can exit programme exit: li $v0, 10 # set v0 exit syscall # execute exit

assembly mips

No comments:

Post a Comment