Do My Homework / Homework Help Answers / Computer Science Homework Help / fully translating a c program to MIPS assembler using the provided mips assembler starter code of a connect_four game

fully translating a c program to MIPS assembler using the provided mips assembler starter code of a connect_four game

Need help with this question or any other Computer Science assignment help task?

For this assignment, I have to use the fully provided c code and fully translate it to MIPS assembly language using the provided mips assembler starter code. This is a c program that runs a full connect_four board game, and the mips assembler starter code provided has a total of 7 functions that have to be completed and are titled #todo...complete this function. The c code provided is titled connect_four.c, and the main work file for the mips assembler starter code is titled connect_four.s. Additionally, in the MIPS assembler starter code, there are comments to guide you on the functions that have to be completed based on the provided c program attached below IMPORTANT: note that the full c program and mips assembler starter code are attached one after the other in the same word document below and are titled in red
Additional Instructions:
#include #include #include /*-- Constants --*/ /// How many pieces we're trying to connect #define CONNECT 4 /// The minimum and maximum board dimensions #define MIN_BOARD_DIMENSION 4 #define MAX_BOARD_WIDTH 9 #define MAX_BOARD_HEIGHT 16 /// The three cell types #define CELL_EMPTY '.' #define CELL_RED 'R' #define CELL_YELLOW 'Y' /// The winner conditions #define WINNER_NONE 0 #define WINNER_RED 1 #define WINNER_YELLOW 2 /// Whose turn is it? #define TURN_RED 0 #define TURN_YELLOW 1 /*-- Function prototypes --*/ void assert_board_dimension(int dimension, int min, int max); void initialise_board(void); void play_game(void); int play_turn(int whose_turn); int check_winner(void); int check_line(int start_row, int start_col, int offset_row, int offset_col); bool is_board_full(void); // provided for you: void print_board(void); /*-- Global variables --*/ char board[MAX_BOARD_HEIGHT][MAX_BOARD_WIDTH]; int board_width; int board_height; /*-- Code --*/ int main(void) { printf("Enter board width: "); scanf("%d", &board_width); assert_board_dimension(board_width, MIN_BOARD_DIMENSION, MAX_BOARD_WIDTH); printf("Enter board height: "); scanf("%d", &board_height); assert_board_dimension(board_height, MIN_BOARD_DIMENSION, MAX_BOARD_HEIGHT); initialise_board(); print_board(); play_game(); return 0; } /// Make sure the board dimensions we're /// given fit within the correct bounds void assert_board_dimension(int dimension, int min, int max) { if (dimension < min) { printf("Board dimension too small (min %d)\n", min); exit(1); } if (dimension > max) { printf("Board dimension too large (max %d)\n", max); exit(1); } } /// Initialise the board to all empty cells void initialise_board(void) { for (int row = 0; row < board_height; row++) { for (int col = 0; col < board_width; col++) { board[row][col] = CELL_EMPTY; } } } /// The main game loop void play_game(void) { int whose_turn = TURN_RED; int winner; // Play a single turn do { whose_turn = play_turn(whose_turn); print_board(); winner = check_winner(); } while (winner == WINNER_NONE && !is_board_full()); // Either we have a winner, or the board is full! if (winner == WINNER_NONE) { printf("The game is a draw!\n"); } else if (winner == WINNER_RED) { printf("Game over, Red wins!\n"); } else { printf("Game over, Yellow wins!\n"); } } /// Play a single turn! /// This in sequence: /// - Reads in the column to insert, /// - Makes sure it is valid, /// - Inserts the color into that column, /// - Switches the turn to the next player. int play_turn(int whose_turn) { if (whose_turn == TURN_RED) printf("[RED] "); else printf("[YELLOW] "); printf("Choose a column: "); int target_col = 0; scanf("%d", &target_col); target_col--; // user input is 1-indexed if (target_col < 0 || target_col >= board_width) { printf("Invalid column\n"); return whose_turn; } int target_row = board_height - 1; while (target_row >= 0 && board[target_row][target_col] != CELL_EMPTY) { target_row--; if (target_row < 0) { printf("No space in that column!\n"); return whose_turn; } } if (whose_turn == TURN_RED) { board[target_row][target_col] = CELL_RED; return TURN_YELLOW; } else { board[target_row][target_col] = CELL_YELLOW; return TURN_RED; } } /// Checks if the game has a winner yet! /// For each position on the board, the /// loop will check vertical, horizontal, /// North-East vertical, and /// North-West vertical for a connection. int check_winner(void) { for (int row = 0; row < board_height; row++) { for (int col = 0; col < board_width; col++) { int check; check = check_line(row, col, 1, 0); if (check != WINNER_NONE) return check; check = check_line(row, col, 0, 1); if (check != WINNER_NONE) return check; check = check_line(row, col, 1, 1); if (check != WINNER_NONE) return check; check = check_line(row, col, 1, -1); if (check != WINNER_NONE) return check; } } return WINNER_NONE; } /// Checks if a particular line represents /// a connect 4! /// It uses a start_row and start_col, /// and then checks if there are 3 further /// tokens of the same color, offsetting by /// offset_row and offset_col each time. int check_line(int start_row, int start_col, int offset_row, int offset_col) { char first_cell = board[start_row][start_col]; if (first_cell == CELL_EMPTY) return WINNER_NONE; int row = start_row + offset_row; int col = start_col + offset_col; for (int i = 0; i < CONNECT - 1; i++) { if (row < 0 || col < 0) return WINNER_NONE; if (row >= board_height || col >= board_width) return WINNER_NONE; char cell = board[row][col]; if (cell != first_cell) return WINNER_NONE; row += offset_row; col += offset_col; } if (first_cell == CELL_RED) return WINNER_RED; else return WINNER_YELLOW; } /// Checks if the board is completely full /// i.e. there is no free space for a turn. /// This is the condition that causes a draw bool is_board_full(void) { for (int row = 0; row < board_height; row++) { for (int col = 0; col < board_width; col++) { if (board[row][col] == CELL_EMPTY) return false; } } return true; } /// Print the board out to the terminal, /// with numbers at the top to indicate /// the column indicies. void print_board(void) { printf("\n"); for (int col = 0; col < board_width; col++) { printf("%d ", col + 1); } printf("\n"); for (int row = 0; row < board_height; row++) { for (int col = 0; col < board_width; col++) { printf("%c ", board[row][col]); } printf("\n"); } }
######################################################################## # COMP1521 22T2 -- Assignment 1 -- Connect Four! # # # !!! IMPORTANT !!! # Before starting work on the assignment, make sure you set your tab-width to 8! # It is also suggested to indent with tabs only. # Instructions to configure your text editor can be found here: # https://cgi.cse.unsw.edu.au/~cs1521/22T2/resources/mips-editors.html # !!! IMPORTANT !!! # # # This program was written by YOUR-NAME-HERE (z5555555) # on INSERT-DATE-HERE # # Version 1.0 (05-06-2022): Team COMP1521 # ######################################################################## #![tabsize(8)] # Constant definitions. # DO NOT CHANGE THESE DEFINITIONS # MIPS doesn't have true/false by default true = 1 false = 0 # How many pieces we're trying to connect CONNECT = 4 # The minimum and maximum board dimensions MIN_BOARD_DIMENSION = 4 MAX_BOARD_WIDTH = 9 MAX_BOARD_HEIGHT = 16 # The three cell types CELL_EMPTY = '.' CELL_RED = 'R' CELL_YELLOW = 'Y' # The winner conditions WINNER_NONE = 0 WINNER_RED = 1 WINNER_YELLOW = 2 # Whose turn is it? TURN_RED = 0 TURN_YELLOW = 1 ######################################################################## # .DATA # YOU DO NOT NEED TO CHANGE THE DATA SECTION .data # char board[MAX_BOARD_HEIGHT][MAX_BOARD_WIDTH]; board: .space MAX_BOARD_HEIGHT * MAX_BOARD_WIDTH # int board_width; board_width: .word 0 # int board_height; board_height: .word 0 enter_board_width_str: .asciiz "Enter board width: " enter_board_height_str: .asciiz "Enter board height: " game_over_draw_str: .asciiz "The game is a draw!\n" game_over_red_str: .asciiz "Game over, Red wins!\n" game_over_yellow_str: .asciiz "Game over, Yellow wins!\n" board_too_small_str_1: .asciiz "Board dimension too small (min " board_too_small_str_2: .asciiz ")\n" board_too_large_str_1: .asciiz "Board dimension too large (max " board_too_large_str_2: .asciiz ")\n" red_str: .asciiz "[RED] " yellow_str: .asciiz "[YELLOW] " choose_column_str: .asciiz "Choose a column: " invalid_column_str: .asciiz "Invalid column\n" no_space_column_str: .asciiz "No space in that column!\n" ############################################################ #### #### #### Your journey begins here, intrepid adventurer! #### #### #### ############################################################ ######################################################################## # # Implement the following 7 functions, # and check these boxes as you finish implementing each function # # - [ ] main # - [ ] assert_board_dimension # - [ ] initialise_board # - [ ] play_game # - [ ] play_turn # - [ ] check_winner # - [ ] check_line # - [X] is_board_full (provided for you) # - [X] print_board (provided for you) # ######################################################################## ######################################################################## # .TEXT .text main: # Args: void # Returns: # - $v0: int # # Frame: [$ra, ...] # Uses: [...] # Clobbers: [...] # # Locals: # - [...] # # Structure: # main # -> [prologue] # -> body # -> [epilogue] main__prologue: begin # begin a new stack frame push $ra # | $ra main__body: # TODO ... complete this function main__epilogue: pop $ra # | $ra end # ends the current stack frame li $v0, 0 jr $ra # return 0; ######################################################################## # .TEXT .text assert_board_dimension: # Args: # - $a0: int dimension # - $a1: int min # - $a2: int max # Returns: void # # Frame: [...] # Uses: [...] # Clobbers: [...] # # Locals: # - [...] # # Structure: # assert_board_dimension # -> [prologue] # -> body # -> [epilogue] assert_board_dimension__prologue: assert_board_dimension__body: # TODO ... complete this function assert_board_dimension__epilogue: jr $ra # return; ######################################################################## # .TEXT .text initialise_board: # Args: void # Returns: void # # Frame: [...] # Uses: [...] # Clobbers: [...] # # Locals: # - [...] # # Structure: # initialise_board # -> [prologue] # -> body # -> [epilogue] initialise_board__prologue: initialise_board__body: # TODO ... complete this function initialise_board__epilogue: jr $ra # return; ######################################################################## # .TEXT .text play_game: # Args: void # Returns: void # # Frame: [...] # Uses: [...] # Clobbers: [...] # # Locals: # - [...] # # Structure: # play_game # -> [prologue] # -> body # -> [epilogue] play_game__prologue: play_game__body: # TODO ... complete this function play_game__epilogue: jr $ra # return; ######################################################################## # .TEXT .text play_turn: # Args: # - $a0: int whose_turn # Returns: void # # Frame: [...] # Uses: [...] # Clobbers: [...] # # Locals: # - [...] # # Structure: # play_turn # -> [prologue] # -> body # -> [epilogue] play_turn__prologue: play_turn__body: # TODO ... complete this function play_turn__epilogue: jr $ra # return; ######################################################################## # .TEXT .text check_winner: # Args: void # Returns: # - $v0: int # # Frame: [...] # Uses: [...] # Clobbers: [...] # # Locals: # - [...] # # Structure: # check_winner # -> [prologue] # -> body # -> [epilogue] check_winner__prologue: check_winner__body: # TODO ... complete this function check_winner__epilogue: jr $ra # return; ######################################################################## # .TEXT .text check_line: # Args: # - $a0: int start_row # - $a1: int start_col # - $a2: int offset_row # - $a3: int offset_col # Returns: # - $v0: int # # Frame: [...] # Uses: [...] # Clobbers: [...] # # Locals: # - [...] # # Structure: # check_line # -> [prologue] # -> body # -> [epilogue] check_line__prologue: check_line__body: # TODO ... complete this function check_line__epilogue: jr $ra # return; ######################################################################## # .TEXT # YOU DO NOT NEED TO CHANGE THE IS_BOARD_FULL FUNCTION .text is_board_full: # Args: void # Returns: # - $v0: bool # # Frame: [] # Uses: [$v0, $t0, $t1, $t2, $t3] # Clobbers: [$v0, $t0, $t1, $t2, $t3] # # Locals: # - $t0: int row # - $t1: int col # # Structure: # is_board_full # -> [prologue] # -> body # -> loop_row_init # -> loop_row_cond # -> loop_row_body # -> loop_col_init # -> loop_col_cond # -> loop_col_body # -> loop_col_step # -> loop_col_end # -> loop_row_step # -> loop_row_end # -> [epilogue] is_board_full__prologue: is_board_full__body: li $v0, true is_board_full__loop_row_init: li $t0, 0 # int row = 0; is_board_full__loop_row_cond: lw $t2, board_height bge $t0, $t2, is_board_full__epilogue # if (row >= board_height) goto is_board_full__loop_row_end; is_board_full__loop_row_body: is_board_full__loop_col_init: li $t1, 0 # int col = 0; is_board_full__loop_col_cond: lw $t2, board_width bge $t1, $t2, is_board_full__loop_col_end # if (col >= board_width) goto is_board_full__loop_col_end; is_board_full__loop_col_body: mul $t2, $t0, MAX_BOARD_WIDTH # row * MAX_BOARD_WIDTH add $t2, $t2, $t1 # row * MAX_BOARD_WIDTH + col lb $t3, board($t2) # board[row][col]; bne $t3, CELL_EMPTY, is_board_full__loop_col_step # if (cell != CELL_EMPTY) goto is_board_full__loop_col_step; li $v0, false b is_board_full__epilogue # return false; is_board_full__loop_col_step: addi $t1, $t1, 1 # col++; b is_board_full__loop_col_cond # goto is_board_full__loop_col_cond; is_board_full__loop_col_end: is_board_full__loop_row_step: addi $t0, $t0, 1 # row++; b is_board_full__loop_row_cond # goto is_board_full__loop_row_cond; is_board_full__loop_row_end: is_board_full__epilogue: jr $ra # return; ######################################################################## # .TEXT # YOU DO NOT NEED TO CHANGE THE PRINT_BOARD FUNCTION .text print_board: # Args: void # Returns: void # # Frame: [] # Uses: [$v0, $a0, $t0, $t1, $t2] # Clobbers: [$v0, $a0, $t0, $t1, $t2] # # Locals: # - `int col` in $t0 # - `int row` in $t0 # - `int col` in $t1 # # Structure: # print_board # -> [prologue] # -> body # -> for_header_init # -> for_header_cond # -> for_header_body # -> for_header_step # -> for_header_post # -> for_row_init # -> for_row_cond # -> for_row_body # -> for_col_init # -> for_col_cond # -> for_col_body # -> for_col_step # -> for_col_post # -> for_row_step # -> for_row_post # -> [epilogue] print_board__prologue: print_board__body: li $v0, 11 # syscall 11: print_int la $a0, '\n' syscall # printf("\n"); print_board__for_header_init: li $t0, 0 # int col = 0; print_board__for_header_cond: lw $t1, board_width blt $t0, $t1, print_board__for_header_body # col < board_width; b print_board__for_header_post print_board__for_header_body: li $v0, 1 # syscall 1: print_int addiu $a0, $t0, 1 # col + 1 syscall # printf("%d", col + 1); li $v0, 11 # syscall 11: print_character li $a0, ' ' syscall # printf(" "); print_board__for_header_step: addiu $t0, 1 # col++ b print_board__for_header_cond print_board__for_header_post: li $v0, 11 la $a0, '\n' syscall # printf("\n"); print_board__for_row_init: li $t0, 0 # int row = 0; print_board__for_row_cond: lw $t1, board_height blt $t0, $t1, print_board__for_row_body # row < board_height b print_board__for_row_post print_board__for_row_body: print_board__for_col_init: li $t1, 0 # int col = 0; print_board__for_col_cond: lw $t2, board_width blt $t1, $t2, print_board__for_col_body # col < board_width b print_board__for_col_post print_board__for_col_body: mul $t2, $t0, MAX_BOARD_WIDTH add $t2, $t1 lb $a0, board($t2) # board[row][col] li $v0, 11 # syscall 11: print_character syscall # printf("%c", board[row][col]); li $v0, 11 # syscall 11: print_character li $a0, ' ' syscall # printf(" "); print_board__for_col_step: addiu $t1, 1 # col++; b print_board__for_col_cond print_board__for_col_post: li $v0, 11 # syscall 11: print_character li $a0, '\n' syscall # printf("\n"); print_board__for_row_step: addiu $t0, 1 b print_board__for_row_cond print_board__for_row_post: print_board__epilogue: jr $ra # return;
#include #include #include /*-- Constants --*/ /// How many pieces we're trying to connect #define CONNECT 4 /// The minimum and maximum board dimensions #define MIN_BOARD_DIMENSION 4 #define MAX_BOARD_WIDTH 9 #define MAX_BOARD_HEIGHT 16 /// The three cell types #define CELL_EMPTY '.' #define CELL_RED 'R' #define CELL_YELLOW 'Y' /// The winner conditions #define WINNER_NONE 0 #define WINNER_RED 1 #define WINNER_YELLOW 2 /// Whose turn is it? #define TURN_RED 0 #define TURN_YELLOW 1 /*-- Function prototypes --*/ void assert_board_dimension(int dimension, int min, int max); void initialise_board(void); void play_game(void); int play_turn(int whose_turn); int check_winner(void); int check_line(int start_row, int start_col, int offset_row, int offset_col); bool is_board_full(void); // provided for you: void print_board(void); /*-- Global variables --*/ char board[MAX_BOARD_HEIGHT][MAX_BOARD_WIDTH]; int board_width; int board_height; /*-- Code --*/ int main(void) { printf("Enter board width: "); scanf("%d", &board_width); assert_board_dimension(board_width, MIN_BOARD_DIMENSION, MAX_BOARD_WIDTH); printf("Enter board height: "); scanf("%d", &board_height); assert_board_dimension(board_height, MIN_BOARD_DIMENSION, MAX_BOARD_HEIGHT); initialise_board(); print_board(); play_game(); return 0; } /// Make sure the board dimensions we're /// given fit within the correct bounds void assert_board_dimension(int dimension, int min, int max) { if (dimension < min) { printf("Board dimension too small (min %d)\n", min); exit(1); } if (dimension > max) { printf("Board dimension too large (max %d)\n", max); exit(1); } } /// Initialise the board to all empty cells void initialise_board(void) { for (int row = 0; row < board_height; row++) { for (int col = 0; col < board_width; col++) { board[row][col] = CELL_EMPTY; } } } /// The main game loop void play_game(void) { int whose_turn = TURN_RED; int winner; // Play a single turn do { whose_turn = play_turn(whose_turn); print_board(); winner = check_winner(); } while (winner == WINNER_NONE && !is_board_full()); // Either we have a winner, or the board is full! if (winner == WINNER_NONE) { printf("The game is a draw!\n"); } else if (winner == WINNER_RED) { printf("Game over, Red wins!\n"); } else { printf("Game over, Yellow wins!\n"); } } /// Play a single turn! /// This in sequence: /// - Reads in the column to insert, /// - Makes sure it is valid, /// - Inserts the color into that column, /// - Switches the turn to the next player. int play_turn(int whose_turn) { if (whose_turn == TURN_RED) printf("[RED] "); else printf("[YELLOW] "); printf("Choose a column: "); int target_col = 0; scanf("%d", &target_col); target_col--; // user input is 1-indexed if (target_col < 0 || target_col >= board_width) { printf("Invalid column\n"); return whose_turn; } int target_row = board_height - 1; while (target_row >= 0 && board[target_row][target_col] != CELL_EMPTY) { target_row--; if (target_row < 0) { printf("No space in that column!\n"); return whose_turn; } } if (whose_turn == TURN_RED) { board[target_row][target_col] = CELL_RED; return TURN_YELLOW; } else { board[target_row][target_col] = CELL_YELLOW; return TURN_RED; } } /// Checks if the game has a winner yet! /// For each position on the board, the /// loop will check vertical, horizontal, /// North-East vertical, and /// North-West vertical for a connection. int check_winner(void) { for (int row = 0; row < board_height; row++) { for (int col = 0; col < board_width; col++) { int check; check = check_line(row, col, 1, 0); if (check != WINNER_NONE) return check; check = check_line(row, col, 0, 1); if (check != WINNER_NONE) return check; check = check_line(row, col, 1, 1); if (check != WINNER_NONE) return check; check = check_line(row, col, 1, -1); if (check != WINNER_NONE) return check; } } return WINNER_NONE; } /// Checks if a particular line represents /// a connect 4! /// It uses a start_row and start_col, /// and then checks if there are 3 further /// tokens of the same color, offsetting by /// offset_row and offset_col each time. int check_line(int start_row, int start_col, int offset_row, int offset_col) { char first_cell = board[start_row][start_col]; if (first_cell == CELL_EMPTY) return WINNER_NONE; int row = start_row + offset_row; int col = start_col + offset_col; for (int i = 0; i < CONNECT - 1; i++) { if (row < 0 || col < 0) return WINNER_NONE; if (row >= board_height || col >= board_width) return WINNER_NONE; char cell = board[row][col]; if (cell != first_cell) return WINNER_NONE; row += offset_row; col += offset_col; } if (first_cell == CELL_RED) return WINNER_RED; else return WINNER_YELLOW; } /// Checks if the board is completely full /// i.e. there is no free space for a turn. /// This is the condition that causes a draw bool is_board_full(void) { for (int row = 0; row < board_height; row++) { for (int col = 0; col < board_width; col++) { if (board[row][col] == CELL_EMPTY) return false; } } return true; } /// Print the board out to the terminal, /// with numbers at the top to indicate /// the column indicies. void print_board(void) { printf("\n"); for (int col = 0; col < board_width; col++) { printf("%d ", col + 1); } printf("\n"); for (int row = 0; row < board_height; row++) { for (int col = 0; col < board_width; col++) { printf("%c ", board[row][col]); } printf("\n"); } }
There are no answers to this question.
Login to buy an answer or post yours. You can also vote on other others

Get Help With a similar task to - fully translating a c program to MIPS assembler using the provided mips assembler starter code of a connect_four game

Related Questions

Similar orders to fully translating a c program to MIPS assembler using the provided mips assembler starter code of a connect_four game
Popular Services
Tutlance Experts offer help in a wide range of topics. Here are some of our top services: