Advertisement
sylvieseal

Untitled

Feb 25th, 2023
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. game:
  2.  
  3. push ecx ; save the value in ecx
  4.  
  5. xor byte [board + 19], 1 ; player_number is turned to its opposite (1 -> 0 or 0 -> 1)
  6. mov eax, 4 ; SYS_WRITE board
  7. mov ebx, 1
  8. mov ecx, board
  9. mov edx, 19
  10. int 0x80
  11.  
  12. mov eax, 3 ; SYS_READ user_input
  13. mov ebx, 2
  14. mov ecx, board + 20
  15. mov edx, 2
  16. int 0x80
  17.  
  18. sub byte [board + 20], 49 ; user_input is converted from ascii to equivalent integer and lowered by 1 (it is lowered so that input is 1-9 and not 0-8)
  19. mov eax, board + 7 ; board's address moved into eax register
  20. add al, [board + 20] ; adjust address based on user_input
  21.  
  22. cmp al, 10 ; since the board has two new-lines in it, this adjusts the address based on them. (AL starts at 00 for the first address in .data)
  23. jl n
  24. inc al
  25. cmp al, 14
  26. jl n
  27. inc al
  28.  
  29. n:
  30. mov bl, [board + 19] ; move player_number into bl, turn it into ascii, then place in the spot on the board that corresponds to player_input
  31. add bl, 48
  32. mov byte [eax], bl
  33.  
  34. mov ecx, 8 ; prepare horizontal loop
  35.  
  36. h:
  37. mov al, [board + ecx + 7] ; adds horizontal spaces together and checks them against 144 and 147 (if they equal either one they are all the same)
  38. add al, [board + ecx + 8]
  39. add al, [board + ecx + 9]
  40. cmp al, 144
  41. je exit
  42. cmp al, 147
  43. je exit
  44.  
  45. sub ecx, 4 ; horizontal loop
  46. cmp ecx, 0
  47. jge h
  48.  
  49. mov ecx, 2 ; prepare vertical loop
  50.  
  51. v:
  52. mov al, [board + ecx + 7] ; same as previous but for vertical
  53. add al, [board + ecx + 11]
  54. add al, [board + ecx + 15]
  55. cmp al, 144
  56. je e
  57. cmp al, 147
  58. je e
  59.  
  60. dec ecx ; vertical loop
  61. cmp ecx, 0
  62. jge v
  63.  
  64. mov al, [board + 7] ; checks first diagonal
  65. add al, [board + 12]
  66. add al, [board + 17]
  67. cmp al, 144
  68. je e
  69. cmp al, 147
  70. je e
  71.  
  72. mov al, [board + 9] ; checks second diagonal
  73. add al, [board + 12]
  74. add al, [board + 15]
  75. cmp al, 144
  76. je e
  77. cmp al, 147
  78. je e
  79.  
  80. pop ecx ; loop
  81. inc cl
  82. cmp cl, 9
  83. jl g
  84. exit:
  85. mov eax, 0x1 ; sys_exit
  86. xor ebx, ebx
  87. int 0x80
  88. section .data
  89. board db 27,"[H",27,"[2J___",10,"___",10,"___",10,1,1
  90.  
  91. ; 27,"[H",27,"[2J" is the code for a console clear. Having it at the start effectively clears the console each time the board is displayed
  92. ; "___",10,"___",10,"___",10 is the board
  93. ; 1,1 are 'variables' (player_number and user_input). Normally they would be in the .bss section, but to conserve space, I made them .data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement