# William Meyer
# CSCI 370
# M/T/W/Th 11:15-12:05

#
######################### Mark the Game Board #########################
#

    .data
promptText: .asciiz  "\nEnter the next move: (0..z) "
duplicateMoveText: .asciiz "\n***** Duplicate move! *****"
continueText: .asciiz "\nContinue? (y/n) "
wrongMoveText: .asciiz "\n***** Wrong move! *****"
newGameText: .asciiz "\nNew game? (y/n) "

empty:  .ascii   "\n\n    . . . . . .      0 1 2 3 4 5"
    	.ascii     "\n    . . . . . .      6 7 8 9 a b"
    	.ascii     "\n    . . . . . .      c d e f g h"
    	.ascii     "\n    . . . . . .      i j k l m n"
    	.ascii     "\n    . . . . . .      o p q r s t"
    	.asciiz    "\n    . . . . . .      u v w x y z\n"

board:  .ascii   "\n\n    . . . . . .      0 1 2 3 4 5"
    	.ascii     "\n    . . . . . .      6 7 8 9 a b"
    	.ascii     "\n    . . . . . .      c d e f g h"
    	.ascii     "\n    . . . . . .      i j k l m n"
    	.ascii     "\n    . . . . . .      o p q r s t"
    	.asciiz    "\n    . . . . . .      u v w x y z\n"

offset: .half     6,   8,  10,  12,  14,  16
    	.half    39,  41,  43,  45,  47,  49
    	.half    72,  74,  76,  78,  80,  82
    	.half   105, 107, 109, 111, 113, 115
    	.half   138, 140, 142, 144, 146, 148
    	.half   171, 173, 175, 177, 179, 181

#
########################## Main Program #######################
#
    .text
    .globl main
    
main:
    #li    $t0, 10             		#Mark 6 moves only.
    li 	$s4, 0

#print default board
printBoard:
    la    $a0, board
    li    $v0, 4            		#Print the game board.
    syscall

#loop to continue looping through everything

continue:
    la    $a0, continueText
    li    $v0, 4			#printing text asking continue?
    syscall
    li    $v0, 12			#Read input of y/n
    syscall
    li    $a2, 'n'           		#loading register a2 with char n to check
    beq   $a2, $v0, playAgain		#checking if they are equal, if they are the jump to playAgain loop
    li    $a2, 'y'          	 	#loading register a2 with char y to check
    beq   $a2, $v0, correctMove 	#if equal then go onto next loop
    li    $v0, 4
    syscall
    bne   $a2, $v0, continue 		#else try again


#checking if entered move is correct
correctMove:
    la    $a0, promptText
    li    $v0, 4
    syscall                  		#Asking what move
    li   $v0, 12            		#Read a move (0..z)
    syscall                  		#The syscall 12 is read char that user entered

    #li $a1, '.'			#user has move in $v0, so load a register with '.' and check if they are equal
    #beq $t2, $a1, Duplicate		#if equal jump to branch that says equal

    li   $a1,  '/'			#load ascii value of slash into register a1
    blt $v0, $a1, notCorrectMove	#if user entered value has ascii value less than ascii value of slash then it is wrong
    bgt $v0, $a1, checkNumAgain		#if user entered value of ascii value greater than ascii value of slash then jump to check num loop


#checking if move is a duplicate move
#couldn't get this function to work at all
#duplicateMove:
	
     #lb $t7, $t7
     #find the index of that on the board
     
    #la    $a0, duplicateMoveText
    #li    $v0, 4             # Print the promptText
    #syscall
    #j continue


#telling user it is wrong and go back to loop asking what move
notCorrectMove:
    la    $a0, wrongMoveText
    li    $v0, 4            		#telling not correct move
    syscall
    j correctMove			#jump back to asking what move


#checking again to make sure it's in boundaries
checkNumAgain:
    li 	$a1, ':'			#load ascii value of colon into register a1
    bgt $a1, $v0, inRange		#if ascii value of colon is greater than what user enters then jump to inRange
    j checkLetter			#otherwise jump to checkLetter loop

#checking letter to make sure it's within boundaries
checkLetter:
    li  $a1, 'a'			#load 'a' into register a1
    blt $v0, $a1, notCorrectMove	#if $v0 is less than 'a' then jump to notCorrectMove
    li  $a1,  'z'			#load 'z' into register a1
    bgt $v0, $a1, notCorrectMove	#if  $v0 is greater than 'z' then jump to notCorrectMove


#confirmed it is in range in this function so marking the appropriate move
inRange:
    move $a0, $v0
    jal   MarkMove           		#Call the subroutine MarkMove.
    sub   $t0, $t0, 1        		#Decrease the counter $t0 by 1.
    bnez  $t0, printBoard            	#If not end of loop, go to L1.

#asking if they want to play again
playAgain:
    la    $a0, newGameText
    li    $v0, 4             		#Print new game?
    syscall
    li    $v0, 12			#Read input of y/n
    syscall
    li    $a1, 'n'            		#loading register a1 with char n to check
    beq   $v0, $a1, playAgainN
    li    $a1, 'y'            		#loading register a1 with char n to check
    beq   $v0, $a1, resetGame


#clearing game board
resetGame:
    #code to reset the game board
    li $t0, 0
    
resetGameloop:
    lb   $t1, empty($t0)
    sb   $t1, board($t0)
    add  $t0, $t0, 1
    blt  $t0, 192, resetGameloop
    j main				#jump back to main function at beginning

    

# 
######################### Mark a Move #########################
#
# Input:  $a0 (the move in character)   
# Output: 

MarkMove:
    # Save data in the runtime stack.
    subu  $sp, $sp, 4        		#Decrement the $sp to make space for $ra.
    sw    $ra, ($sp)         		#Push/save the return address, $ra.
    subu  $sp, $sp, 4        		#Decrement the $sp to make space for $t0.
    sw    $t0, ($sp)         		#Push/save the $t0.

    # Convert the move to an integer.
    bgt   $a0, '9', L11
    sub   $t0, $a0, '0'
    j     L12
L11:  
    sub   $t0, $a0, 'a'
    add   $t0, $t0, 10

# Find the offset.
L12:  mul   $t0, $t0, 2       		#Each offset is two-byte long.
      lh    $t1, offset($t0)  		#Load $t1 with the offset of the index $t0.

      beq $t2, 0, LX			#jump to LX to put X
      beq $t2, 1, LO			#jump to LO to put O
      j	  L13
    
#print X
LX:
    # Mark the board.
    li    $t2, 'X'          		#Put the piece ‘X’ in $t2.
    sb    $t2, board($t1)   		#Put the piece at the location, board+offset.
    li    $t2, 1
    j	L13
    
 #print O
 LO:
    li    $t2, 'O'          		#Put the piece ‘O’ in $t2.
    sb    $t2, board($t1)   		#Put the piece at the location, board+offset.
    li    $t2, 0

#given code
L13:
    # Restore the data from the runtime stack.
    lw    $t0, ($sp)         		#Pop/restore $t0.
    addu  $sp, $sp, 4        		#Increment the $sp.
    lw    $ra, ($sp)         		#Pop/restore the return address, $ra.
    addu  $sp, $sp, 4        		#Increment the $sp.
    jr    $ra                		#Return.

#syscall to exit game, entered n to continue and n to play again
playAgainN:
    li    $v0, 10            		#Exit.
    syscall
 

Assembly Online Compiler

Write, Run & Share Assembly code online using OneCompiler's Assembly online compiler for free. It's one of the robust, feature-rich online compilers for Assembly language. Getting started with the OneCompiler's Assembly compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Assembly and start coding.

About Assembly

Assembly language(asm) is a low-level programming language, where the language instructions will be more similar to machine code instructions.

Every assembler may have it's own assembly language designed for a specific computers or an operating system.

Assembly language requires less execution time and memory. It is more helful for direct hardware manipulation, real-time critical applications. It is used in device drivers, low-level embedded systems etc.

Syntax help

Assembly language usually consists of three sections,

  1. Data section

    To initialize variables and constants, buffer size these values doesn't change at runtime.

  2. bss section

    To declare variables

  3. text section

    _start specifies the starting of this section where the actually code is written.

Variables

There are various define directives to allocate space for variables for both initialized and uninitialized data.

1. To allocate storage space to Initialized data

Syntax

variable-name    define-directive    initial-value 
Define DirectiveDescriptionAllocated Space
DBDefine Byte1 byte
DWDefine Word2 bytes
DDDefine Doubleword4 bytes
DQDefine Quadword8 bytes
DTDefine Ten Bytes10 bytes

2. To allocate storage space to un-initialized data

Define DirectiveDescription
RESBReserve a Byte
RESWReserve a Word
RESDReserve a Doubleword
RESQReserve a Quadword
RESTReserve a Ten Bytes

Constants

Constants can be defined using

1. equ

  • To define numeric constants
CONSTANT_NAME EQU regular-exp or value

2. %assign

  • To define numeric constants.
%assign constant_name value

3. %define

  • To define numeric or string constants.
%define constant_name value

Loops

Loops are used to iterate a set of statements for a specific number of times.

mov ECX,n
L1:
;<loop body>
loop L1

where n specifies the no of times loops should iterate.

Procedures

Procedure is a sub-routine which contains set of statements. Usually procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

procedure_name:
   ;procedure body
   ret