Sunday, March 27, 2011

Battleship Code in LC-3 Assembly

                                             .ORIG x3000 ;This program Runs a game of battleship between two players ;There are Subroutines for Displaying the Board,Checking of player wins ;Getting Input from user,Checking if input is valid and updating board ;The program calls these subroutines whenever required ;R1 is the Flag for Player, if R1=0, ; it is player 1, and its board will be loaded ;If R1=1, it activates all subroutines for Player 2   AND R1,R1,0  ;Initialises R1, the flag  LEA R0,PLAYER1  ;Loads R0 with "Player 1" String  TRAP x22  ;Prints String  JSR display  ;Jumps to Display Subroutine(Player 1 Board)  LEA R0,PLAYER2  ;Loads R0 with "Player 2" String  TRAP x22  ;Prints String         ADD R1,R1,1  ;Adds 1 to R1         JSR DISPLAY  ;Jumps to Subroutine Display(Player 2 Board)         ADD R1,R1,-1  ;Subtracts 1 from R1, makes it 0         JSR CHECK_WIN  ;Check win for Player1         ADD R1,R1,1  ;R1<-1         JSR CHECK_WIN  ;CheckWin for player1         AND R1,R1,0  ;R1<-0  ;This Subroutine Controls the whole program, Calls Various Subroutines ;And updates value of R1 and decides whose Turn it is MAINPROG  ADD R1,R1,0  ;R1<-R1+0  BRp PL2   ;Jumps to PL2 if Positive  LEA R0,PLAYER1  ;Loads R0 with "Player 1" String  TRAP x22   ;Prints String  JSR DISPLAY  ;Jumps to Display Subroutine  BRnzp INPUT  ;Jumps to Input Label PL2 LEA R0,PLAYER2  ;Loads R0 with "Player 1" String  TRAP x22  ;Prints String  JSR DISPLAY  ;Jumps to Display Subroutine   INPUT   JSR GET_INPUTS  ;Jumps to GET_INPUTS Subroutine UPDATE  JSR UPDATE_BOARD ;Jumps To Update Board Subroutine            AND R5,R5,0   ;Initiliazes R5          LD R5,GUESSED  ;Loads r5 with Valid/Invalud guess notice          BRnp INPUT  ;Branches to Input if Invalid        ADD R1,R1,0  ;Add 0 to r1         BRz FST_PLY  ;Branches to Nest Player if zero         JSR DISPLAY  ;Jumps to Display Subroutine   JSR CHECK_WIN  ;Jump to Check_WIN Subroutine   AND R1,R1,0  ;Initializes R1          BRnzp MAINPROG  ;Branches back to MAINPROG  FST_PLY     JSR DISPLAY  ;Jumps to Display Subroutine      JSR CHECK_WIN ;Jump to Check_WIN Subroutine               ADD R1,R1,1  ;R1<-R1+1      BRnzp MAINPROG ;Branches back to MAINPROG    ; DISPLAY subroutine ;I have decided to run 2 loops,labelled LOOP1 and LOOP ;LOOP is the inner LOOP and will first check memory address if it is X0000 ;Using z condition code  ;If Zero. it jumps to label ELSE3 and prints " " on screen ;If not 0, there may be 3 numbers, x0010,x0011, or x0001 in database address; ;x0001 should print M for Miss,x0002 should print " " space, ;X0011 should print H for Hit ;The program then subtracts 2 from the value stored in memory location, ;after it is put in R1,    ;Subtracting 2 from x0001 gives us a negative no, ;using n condition code, we direct it to print "M" ;Subtracting 2 from x0011 gives us a positive no, ;using p condition code, we direct it to print "H" ;Subtracting 2 from x0010, gives us zero, ;using z condition code, we direct it to print a space " " CELL .STRINGZ " |" CELL1 .STRINGZ "|" COLUMN_NUMBERS .STRINGZ "\n     0   1   2   3   4   5   6   7   8   9\n" HORIZONTAL_RULE .STRINGZ "   +---+---+---+---+---+---+---+---+---+---+\n"  NEWLINE .FILL x0A       ; \n CHAR_H .STRINGZ " H" CHAR_M .STRINGZ " M"                        ; 'M' CHAR_SPACE .STRINGZ " "    ; ' ' DOUBLE_SPACE .STRINGZ "  "  PLAYER1 .STRINGZ "\nPlayer 1" PLAYER2 .STRINGZ "\nPlayer 2" PLAYER1_WIN .STRINGZ "\nPlayer 1 wins\n" PLAYER2_WIN .STRINGZ "\nPlayer 2 wins\n" PLAYER1_BOARD .FILL x4000        ;stores address x4000 for player one's board PLAYER2_BOARD .FILL x5000        ;stores address x5000 for player two's board  SAVER0 .BLKW 1 SAVER1 .BLKW 1 SAVER2 .BLKW 1 SAVER3 .BLKW 1 SAVER4 .BLKW 1 SAVER5 .BLKW 1 SAVER7 .BLKW 1  GUESSED .BLKW 1 DISPLAY    ST R0, SAVER0 ;Stores Values Of all registers in LaBels   ST R6, SAVER1       ST R2, SAVER2   ST R3, SAVER3   ST R4, SAVER4   ST R5, SAVER5   ST R7, SAVER7      ADD R1,R1,0   ;R1<-R1+0            BRp PLAYER_2           ;Branches to Player_2 of positive            LD R3, PLAYER1_BOARD   ;3 is pointer to memory X4000, PLayer1 Board     BRnzp DONE    ;Branches to DONE, Skips next line  PLAYER_2   LD R3, PLAYER2_BOARD   ;3 is pointer to memory X4000, PLayer1 Board  DONE    LEA R0, COLUMN_NUMBERS      ;loads R0 with column nummbers 0 to 9            TRAP x22         ;Prints the string of column numbers on screen               LEA R0, HORIZONTAL_RULE     ;loads R0 with horizontal rule             TRAP x22                    ;prints first horizontal rule on string            AND R0,R0,0                 ;Initializes R0             AND R5,R5,0                 ;Initializes R5- Counter for Loop1              ADD R5,R5,10               ;Adds 10 to R5, Loop 1 will run 10 times            LD R2, CHAR_LC_A                 ;Loads r2 with ascii character 'a'                                ;Will be incremented to show b,c,d and so on LOOP1      AND R4,R4,0                 ;R4 is counter for LOOP, Initialized=0            ADD R4,R4,10                ;Sets R4 to 10, Loop will run 10 Times            LEA R0, CHAR_SPACE           ;Loads R0 with char_space            TRAP x22                    ;prints a space on screen            ADD R0,R2,0                 ;Loads R0 with value of R2                                        ;R2 contains ascii character            TRAP x21                    ;Displays character on screen            ADD R2,R2,1                 ;Increments R2                                        ;next character will be displayed            LEA R0, CHAR_SPACE           ;loads R0 with space                TRAP x22                    ;Displays " " on screen            LEA R0, CELL1               ;Loads R0 with Cell1 string "| "            TRAP x22                    ;Displays "| " on screen  LOOP       AND R6,R6,0                 ;R1 is initialized                  LDR R6,R3,0                 ;R1 <-R3(contains address to memory"            BRnp ELSE                   ;If not zero, jump to ELSE conditon                                        ;checks wether the memory contains 0 ELSE3      LEA R0, DOUBLE_SPACE           ;Fills R0 with space value            TRAP x22                    ;Prints space on screen            BRnzp PRINTCELL             ;Prints " | " for divider                                        ;Skips labels ELSE and ELSE2   ELSE       ADD R6,R6,-2                ;Subtracts 2 from R1            BRp ELSE2                   ;Jumps to ELSE2 if positive             BRz ELSE3                   ;Jumps to ELSE3 if Zero            LEA R0, CHAR_M               ;If negative, Fills with M            TRAP x22                    ;Prints M for Miss            BRnzp PRINTCELL             ;Prints " | " for cell divider                                        ;Skips label ELSE2       ELSE2      LEA R0, CHAR_H               ;ELSE 2 - Fills R0 with H             TRAP x22                    ;Prints H for Hit PRINTCELL   LEA R0, CELL                ;Loads R0 with Cell divider         TRAP x22                      ;Prints cell string " | "             ADD R3,R3,1             ;Increment pointer R3, Address to memory                                        ;Next memory Location is stored in R3            ADD R4,R4,-1                ;Decrements R4 with 1,                                         ;R4 is counter for inner loop               BRp LOOP                   ;If R4 is not zero, Jumps to LOOP,                                        ;LOOP runs again if R4 is not zero ;End of inner labeled LOOP, Horizontal Spaces filled with Hit miss or Space             LD R0, NEWLINE              ;Loads R0 with new line "\n"             TRAP x21                    ;Displays next line on screen             LEA R0, HORIZONTAL_RULE     ;Loads R0 with Horizontal Rule String             TRAP x22                    ;Prints Horizontal Rule on screen             ADD R5,R5,-1                ;Decrements R5, Counter for LOOP1             BRz RETURN              ;Jumps to exit if R5=0             BRnp LOOP1                  ;Jumps to LOOP1 and runs it again                                         ;If value of R5 is not zero,runs LOOP1    RETURN  LD R0, SAVER0  ;Loads Back register Values                 LD R6, SAVER1                 LD R2, SAVER2                 LD R3, SAVER3                 LD R4, SAVER4                 LD R5, SAVER5                 LD R7, SAVER7                 RET   ;return To Program  INV_GS_MSG .STRINGZ "\nCell has been guessed already.  Choose another cell.\n" CHAR_ZERO .FILL x30     ; '0' CHAR_LC_A .FILL x61     ; 'a'    ; CHECK_WIN subroutine  SAVER01 .BLKW 1 SAVER11 .BLKW 1 SAVER21 .BLKW 1 SAVER31 .BLKW 1 SAVER41 .BLKW 1 SAVER51 .BLKW 1 SAVER71 .BLKW 1 CHECK_WIN                  ST R0, SAVER01 ;Stores register values in labels                 ST R6, SAVER11                 ST R2, SAVER21                 ST R3, SAVER31                 ST R4, SAVER41                 ST R5, SAVER51                 ST R7, SAVER71      ADD R1,R1,0  ;R1<-R1+0          BRp CHECK_PLAY2  ;Branches To CHECK_PLAY2 if positive           LD R2,PLAYER1_BOARD ;Loads Player 1's Board in R2   BRnzp LOAD  ;Skips next line, Branches to Load CHECK_PLAY2     LD R2,PLAYER2_BOARD ;Loads Player 2's Board in R2            LOAD  LD R6,HUNDRED  ;LOADS VALUE OF HUNDRED IN R1,COUNTER WINLOOP  AND R4,R4,0  ;INITIALIZES R4    LDR R4,R2,0  ;LOADS VALUE OF R2 ADDRESS IN R4    ADD R4,R4,-2  ;SUBTRACTS 2 FROM VALUE STORED IN ADDRESS   BRz NOTWIN  ;JUMPS TO NOT WIN, AS IF 10 IS THERE,    ADD R2,R2,1  ;SHIP REMAINS UNGUESSED   ADD R6,R6,-1  ;DECREMENTS R1(COUNTER)   BRp WINLOOP  ;RUNS LOOP IF POSITIVE,I.E. NOT ZERO   ADD R1,R1,0  ;R1<-R1+0           BRp PLAY2_WIN  ;If R1 is +ve, Branches to PLAY2_WIN                 LEA R0,PLAYER1_WIN ;Loads Player 1 wins Message           TRAP x22  ;Prints String           TRAP x25  ;Halts Program PLAY2_WIN   LEA R0,PLAYER2_WIN ;Loads Player 2 wins message            TRAP x22  ;Prints String           TRAP x25  ;halts Program  NOTWIN   LD R0, SAVER01  ;Loads Back Register values                 LD R6, SAVER11                 LD R2, SAVER21                 LD R3, SAVER31                 LD R4, SAVER41                 LD R5, SAVER51                 LD R7, SAVER71   RET   ;returns to where it was called from   ;your code goes here  HUNDRED .FILL x64  ROWVAL .BLKW 1 COLVAL .BLKW 1  ; UPDATE_BOARD subroutine ;ROWVAL AND COLVAL CONTAINS INPUTS OF ROW AND COLUMN TAKE FRM USER SAVER72 .BLKW 1 UPDATE_BOARD    ST R7, SAVER72   LD R5,COLVAL  ;LOADS COLUMN VALUE IN R5   LD R4,ROWVAL  ;LOADS ROW VALUE IN R4   LD R6,CHAR_LC_A  ;LOADS R1 WITH ASCII CHARACTER a   NOT R6,R6  ;PERFORMS NOT ON R1   ADD R6,R6,1  ;ADDS 1, THIS GIVE NEGATIVE VALUE   ADD R6,R6,R4  ;SUBTRACTS 'a'VALUE FROM ROWVAL ;THIS IMPLIES THAT WE HAVE VALUES 0-9 CORRESPONDING TO a-j(REDUCED ROW VALUE) ;WE WILL MULTIPLY THIS VALUE BY 10 AND ADD 0-9 CORRESPOINT TO COLUMN VALUE ;I.E. REDUCED COLUMN VALUE ;THIS WOULD GIVE ADDRESS OF SQUARE     LD R2,CHAR_ZERO  ;LOADS R1 WITH ASCII CHARACTER a   NOT R2,R2  ;PERFORMS NOT ON R1   ADD R2,R2,1  ;ADDS 1, THIS GIVE NEGATIVE VALUE   ADD R2,R2,R5  ;SUBTRACTS '0'VALUE FROM COLVAL ;THIS IMPLIES THAT WE HAVE VALUES 0-9 CORRESPONDING TO ASCII 0-9       AND R4,R4,0  ;INITIALIZES R4   ADD R4,R4,10 ;R4 IS COUNTER TO MULTIPLY ROWVAL BY 10   AND R5,R5,0  ;WE WILL SAVE MULTIPLIED VALUE IN R5 MULTIPLY ADD R5,R5,R6  ;LOADS REDUCED ROW VALUE IN R5   ADD R4,R4,-1  ;DECREMENTS COUNTER   BRp MULTIPLY  ;RERUNS MULTIPLY LOOP IF COUNTER +VE   ADD R5,R5,R2  ;ADDS REDUCED COLUMN VALUE TO ;10*REDUCED ROWVAL. EG. j,9 WILL GIVE US x63        ADD R1,R1,0  ;R1

2 comments:

SM said...

useful code for beginners

jaina said...
This comment has been removed by the author.