数据部分对齐问题

时间:2019-04-08 22:54:02

标签: assembly mips

我完成了MARS Java应用程序中的MIPS中的项目编码。它运行完美,但是现在当我在将要测试的计算机上运行它时,它们的编译器与我使用的编译器略有不同。。。哎呀,我解决了大多数问题,但是现在我遇到了对齐错误。

错误:

Error #16:  Attempt to use nonexistent memory

Fault address:  004001b4

Register contents:

$zero: 00000000   $at: 00000004   $v0: 00000004   $v1: 00000000
  $a0: 100001d8   $a1: 00000000   $a2: 00000000   $a3: 00000000
  $t0: 00000006   $t1: 00000000   $t2: 10000280   $t3: 00000000
  $t4: 100000b7   $t5: 00000020   $t6: 00000000   $t7: 00000001
  $s0: 00000000   $s1: 00000001   $s2: 00000004   $s3: 00000006
  $s4: 00000029   $s5: 00000000   $s6: 00000000   $s7: 00000000
  $t8: 00000006   $t9: 00000000   $k0: 00000000   $k1: 00000000
  $gp: 00000000   $sp: 7fffeb48   $fp: 00000000   $ra: 0040005c
   pc: 004001b8    hi: 00000000    lo: 000000a8

Current instruction:    8d4e0000 (at 004001b4)

Decoded instruction:  lw        $t6, 0($t2)     (0x10000280)

我的数据部分代码:

# Constants Declaration
X_ASCII_NUM = 88
O_ASCII_NUM = 79
SPACE = 32
DATA_SIZE = 4
NEGATIVE_ONE = -1
PLAYER_ONE_WON = 1
PLAYER_TWO_WON = 2

        .data
        .align 2
Banner:
        .asciiz "   ************************\n"
Welcome:
        .asciiz "   **    Connect Four    **\n"
NewLine:
        .asciiz "\n"
Columns:
        .asciiz "   0   1   2   3   4   5   6\n"
Border:
        .asciiz "+-----------------------------+\n"
MiddleRow:
        .asciiz "|+---+---+---+---+---+---+---+|\n"
Slots:
        .asciiz "||   |   |   |   |   |   |   ||\n"
User1Prompt:
        .asciiz "Player 1: select a row to place your coin (0-6 or -1 to quit): "
User2Prompt:
        .asciiz "Player 2: select a row to place your coin (0-6 or -1 to quit): "
P1Quit:
        .asciiz "Player1 quit.\n"
P2Quit:
        .asciiz "Player2 quit.\n"
IllegalMove:
        .asciiz "Illegal column number.\n"
OutOfRoom:
        .asciiz "Illegal move, no more room in that column.\n"
P1Wins:
        .asciiz "Player 1 wins!"
P2Wins:
        .asciiz "Player 2 wins!"
DrawString:
        .asciiz "The game ends in a tie."



# Creates a 2d-array of 0's
mdArray:        .word 0,0,0,0,0,0,0  # row 0
                .word 0,0,0,0,0,0,0  # row 1
                .word 0,0,0,0,0,0,0  # row 2
                .word 0,0,0,0,0,0,0  # row 3
                .word 0,0,0,0,0,0,0  # row 4
                .word 0,0,0,0,0,0,0  # row 5
                #         (6X7)

        .text
        .align  2
    #    .globl  main

其余的代码...最后一行是导致错误的那一行: 顺便说一下,我正在创建一个Connect 4游戏,如果有 您需要更多有关https://www.cs.rit.edu/~csci250/project/185/proj185.html

的信息
#################################
# Name:         main
# Args:         N/A
# Description:  This function calls the functions to display the board
#               and start the game.
main:
                jal     printWelcome    # displays welcome message
                jal     printBoard      # displays board

##############################
# Name:         input
# Args:         N/A
# Description:
#
input:
                # Infinite loop that takes user input until an error or -1
                while:
                        # Player 1
                        li      $v0, 4
                        la      $a0, User1Prompt
                        syscall

                        li      $v0, 5  # ask for integer
                        syscall

                        # Error checking
                        la      $t0, NEGATIVE_ONE
                        beq     $v0, $t0, P1Quits # Checks to see if P1 quits
                        bgt     $v0, 6,  Invalid1 # Checks to see if input > 6
                        blt     $v0, -1, Invalid1 # Checks to see if input < -1

                        la      $a0, mdArray    # the address of the array; arg for func
                        move    $a1, $v0        # the valid col; arg for func
                        jal     validPlayer1    # passes all error checks; play move
                        li      $v0, 4

                        jal     printBoard      # updates and prints the board

                        la      $a0, mdArray    # load the array as an arg for func call
                        jal     checkForWinner  # Searches the entire board for a winner
                        la      $t0, PLAYER_ONE_WON     # loads a 1
                        beq     $v0, $t0, Player1Won    # if a 1 is returned P1 won
                        la      $t0, PLAYER_TWO_WON     # loads a 2
                        beq     $v0, $t0, Player2Won    # if a 2 is returned P2 won
                        la      $t0, NEGATIVE_ONE
                        beq     $v0, $t0, Draw          # if a -1 is returned it was a draw
                Player2:
                        # Player 2
                        li      $v0, 4
                        la      $a0, User2Prompt
                        syscall

                        li      $v0, 5          # ask for integer
                        syscall

                        la      $t0, NEGATIVE_ONE
                        beq     $v0, $t0, P2Quits # Checks to see if P2 quits
                        bgt     $v0, 6,  Invalid2 # Checks to see if input > 6
                        blt     $v0, -1, Invalid2 # Checks to see if input < -1

                        la      $a0, mdArray    # the address of the array; arg for func
                        move    $a1, $v0        # the valid col; arg for func
                        jal     validPlayer2    # passes all error checks; play move
                        jal     printBoard

                        la      $a0, mdArray    # load the array as an arg for func call
                        jal     checkForWinner # Searches the entire board for a winner
                        la      $t0, PLAYER_ONE_WON     # loads a 1
                        beq     $v0, $t0, Player1Won    # if a 1 is returned P1 won
                        la      $t0, PLAYER_TWO_WON     # loads a 2
                        beq     $v0, $t0, Player2Won    # if a 2 is returned P2 won
                        la      $t0, NEGATIVE_ONE
                        beq     $v0, $t0, Draw          # if a -1 is returned it was a draw

                        j       while

                        Invalid1:
                                li      $v0, 4
                                la      $a0, IllegalMove
                                syscall
                                j       while
                        Invalid2:
                                li      $v0, 4
                                la      $a0, IllegalMove
                                syscall
                                j       Player2
                        Player1Won:
                                li      $v0, 4
                                la      $a0, P1Wins     # print p1 wins
                                syscall

                                li      $v0, 10 # End the program
                                syscall
                        Player2Won:
                                li      $v0, 4
                                la      $a0, P2Wins     # print p2 wins
                                syscall

                                li      $v0, 10 # End the program
                                syscall
                        Draw:
                                li      $v0, 4
                                la      $a0, DrawString # print draw
                                syscall

                                li      $v0, 10 # End the program
                                syscall

##################################
# Name:         checkForWinner
# Args:         a0 - The memory address of the array
# Returns:      v0 - 1 if (X)player1 won, 2 if (O)player2 won, 0 if no winner
#               or will return -1 if the game ends in a tie
# Description:  Checks ever spot on the board to see if there are any winners
#               or if there is a tie. Checks top right, right, bottom right
#               and down for each space on the board
#
checkForWinner:
                # Save the return address
                addi    $sp, $sp, -20
                sw      $ra, 0($sp)
                sw      $s1, 4($sp)
                sw      $s2, 8($sp)
                sw      $s3, 12($sp)
                sw      $s4, 16($sp)

                # Storing variables for comparison
                li      $s2, 4
                li      $s3, 6
                li      $s4, 41

                # Keeping track of the position of the space
                li      $t0, 0  # row = 0
                li      $t1, 0  # col = 0

                # Temp row and col values used for testing other spaces
                li      $t8, 0  # tempRow = 0
                li      $t9, 0  # tempCol = 0

                li      $s1, 0  # Counter for how many spaces are occupied

                whileNotEnd:
                        # Check to see if were at position (5,6)
                        beq     $t0, $s3, checkCol      # if(row = 5)
                        notend:         # place to jump back to if col != 6

                        # Finding starting value
                        mul     $t2, $t0, 7     # t2 = rowIndex * amount of cols
                        mflo    $t2
                        add     $t2, $t2, $t1   #               + colIndex
                        mul     $t2, $t2, DATA_SIZE #           * DATA_SIZE
                        mflo    $t2
                        add     $t2, $t2, $a0   #               + Base Address
                        lw      $t6, ($t2)      # Grab the starting digit

                        bne     $t6, $zero, occupiedSpace       # if (val != 0) -> occupiedSpace
                        beq     $t6, $zero, movePosition        # check to see if the space is a 0
                        here:                           # The label to jump back to

                        li      $t7, 1          # A counter for how many in a row
                        upDiagnol:
                                beq     $t7, $s2, win   # check if 4 in a row
                                # subtract 1 from the row; Add 1 to the column
                                blt     $t8, 1, rightStraight   # if (tempRow < 1) then dont sub 1
                                bgt     $t9, 5, rightStraight   # if (tempCol > 5) then dont add 1
                                #else
                                addi    $t8, $t8, -1    # set the new quardinates
                                addi    $t9, $t9, 1

                                # Grab the value at the up right position
                                mul     $t2, $t8, 7     # t2 = rowIndex * amount of cols
                                mflo    $t2
                                add     $t2, $t2, $t9   #               + colIndex
                                mul     $t2, $t2, DATA_SIZE #           * DATA_SIZE
                                mflo    $t2
                                add     $t2, $t2, $a0   #               + Base Address
                                lw      $t3, ($t2)      # Grabbing the value at the idx


老实说,我真的不知道如何使用.align,但是我可以确定问题出在创建2d数组的地方。

0 个答案:

没有答案