Tic Tac Toe Implementation in Assembly 8086
It is normal tic tac toe but it is implemented in Assembly Code of emu 8086. It is fun to check how assembly works over bit complex codes. So in this code, I try to implement various macros in assembly such as Print Message at prifix location, Clear Scree and checking conditions to put 'O' or 'X'.
In Figure 1, you can see there is two macros created for printing and clearing screen. there are four parameters in print macro.
- x :- Location of x in screen or column of main screen where our text start printing.
- y :- Location of y in screen or row of main screen where our text start printing.
- attrib :- is used to define foreground color and background color. 1st 4bits for background and 2nd 4bits for foreground.
- sdat :- Data used to print on screen.
There are local variable to make printing more operational. used as pointers to print data s_dcl - locate starting position of string skip_dcl - Part which contain string s_dcl_end - locate ending position of string
after that there is command called "pusha" it stores previous information in stack.
Now following things will happen in code.
- move Code Segment to extra segment for display
- setting Interupt 13 hex High and 1 hex low for display on screen with location
- put formatting information in Base Register
- provide string character counter for printing data
- provide print location in data register
- provide start location to base pointer for printing
- now generate interupt of 10 hex for print
- after print done restore register value from stack and macro over.
- Store all register values to stack
- Set intterupt routine to clear screen
- set formatting setting in base register such as forecolor and backcolor
- set cx to zero because nothing to print just have to clear
- so here we scroll thing in terms of DL = Right Column Number and DH Lower Row Number to clean.
- I used approximate value according need
- Now provide 10h interupt routine to run code.
- after interup routine, code restore all register value
for more info check this link:- https://en.wikipedia.org/wiki/INT_10H
So over here nothing so special is done, Here we store state of game markers in data segment and print output 'O' and 'X' on screen. Cool thing is that 8086 support mouse input with 33h interupt check this. link:- http://www.ablmcc.edu.hk/~scy/CIT/8086_bios_and_dos_interrupts.htm#int33h_0000h In today's world, it is quite easy to use mouse but still it is joyful to use mouse in assembly.
- steps one copy data from macro variable to register
- store those value in datasegment's variable ox,oy
- it is location to place marker
- done
Note:- this variable is later used for checking
- first get turn variable to check who's turn. it is either 'O' or 'X'.
- if turn is for 'O' then it is used to set status flag and according instruction pointer jumps
- after jumping it set 'X' or 'O' marker at location which is store in ox,oy of Data Segment
- it is stored by place macro
- Done.
Data segment has minimal variables used to store state of game. such as turn, 9 block's state, last print location,
- a - used to store 1st row of game.
- b - used to store 2nd row of game.
- c - used to store 3rd row of game.
- ox,oy - used to store location of print 'O' and 'X'.
- turn - used to store turn of user.
- chck - for chceking ending of game or not.
Here are very main logic behind the game.
1. So very first, Code intialize or put data in data segment, code in code segment 2. after code initialize, it moves data segment to data register for accessing data 3. after that we set or on mouse input for provide input 4. Now it print message at location as "Press enter any key to start game" 5. And wait until some input is come. 6. Clear the screen using above macro 7. then called tictactoe procedure to print table 8. main event loop start to check game condition. for more detail check code. 9. on every interation it is called check and winner function to close game. 10. if someone is wins, it close game with message.So here is our main table of game
Code prints table for tic tac toe