You are on page 1of 30

This code appears to be written in assembly language, specifically for the x86 architecture.

It is a
small program that displays ASCII art and provides a menu with several options.

Let's go through the code step by step:

1. The code begins with the definition of the model and the stack size.

2. The `.data` section contains various data declarations. These include strings (`message`, `art1`,
`art2`, etc.) that represent ASCII art, messages, and options for the menu. There are also
variables (`l1`, `selector`, `color_selector`, `star_pos`, `snake_pos`, `snake_pos_x`,
`snake_direction`, `cursor_pos`, `cursor_pos_x`, `body`, `score_0`, `score_1`, etc.) that hold
different values throughout the program.

3. The `.code` section starts with the `main` procedure.

4. The program begins by clearing the screen and setting the text color to green (using interrupt
10h).

5. ASCII art is displayed using interrupt 21h and the `09h` function. The art is printed line by
line using the `lea` instruction to load the address of each line and then invoking interrupt 21h.

6. A start instruction is displayed using interrupt 21h and the `09h` function.

7. The program enters a loop labeled as `controls` to check for key presses using interrupt 21h
and the `0bh` function. If a key is pressed, it checks which key was pressed and performs
different actions based on the key.

8. If the key pressed was 'Down', the screen is cleared, the text color is set to green, and the
ASCII art is displayed again. Then, the message "Down is Pressed" is displayed using interrupt
21h and the `09h` function.

9. The program checks the value of the `selector` variable and jumps to different labels based on
its value. Each label corresponds to an option in the menu.

10. For example, if the `selector` is 1, the program changes the text color to green, highlights the
"Play Game!" option, and displays the other options in white.

11. The program waits for a key press again, checks which key was pressed, and updates the
`selector` accordingly.

12. The program continues to display the menu options, handle key presses, and update the
`selector` value based on the selected option.

It's important to note that without a complete understanding of the program's context or purpose,
it's difficult to provide a comprehensive explanation. However, this walkthrough should give you
a general understanding of the code's structure and its main operations.
The provided code appears to be assembly code, specifically for the x86 architecture. Let's go
through it section by section to understand its functionality.

The code is divided into several sections labeled as `op1`, `op2`, `op3`, `op4`, and
`ColorChange`. These sections seem to represent different options or operations within a larger
program.

In each section, the code performs various operations, such as displaying text on the screen,
taking user input, setting values to registers, and making interrupts. The `int` instruction is used
to make software interrupts, which allow the program to interact with the underlying operating
system.

Here's a breakdown of the code:

**op2:**

This section starts by clearing the screen (`int 10h`) and displaying the text stored at `option1`
using `int 21h` (presumably a DOS interrupt). Then, it sets the text color to green with blink
(`0Aah`) and displays the text stored at `option2a`. After that, it displays the text stored at
`option3` and `option4`.

The code then waits for user input by calling `int 16h`, which reads a keystroke. Depending on
the value in `ah` (the high byte of the AX register), it compares it with different values (`50h`,
`48h`, and `1Ch`) and performs specific actions based on the comparison.

If `ah` is equal to `50h`, it jumps to `md2a` section. If `ah` is equal to `48h`, it jumps to `md2b`
section. If `ah` is equal to `1Ch`, it jumps to `nameenter` section. Otherwise, it jumps to the
`downButtn` section.

**op3:**

Similar to `op2`, this section starts by clearing the screen and displaying the text stored at
`option1` and `option2`. Then, it sets the text color to green with blink and displays the text
stored at `option3a` and `option4`.

The code then waits for user input and performs similar comparisons as in `op2` to determine the
jump location.

**op4:**

This section is similar to the previous sections. It clears the screen, displays the text stored at
`option1`, `option2`, and `option3`, sets the text color to green with blink, and displays the text
stored at `option4a`. Then, it waits for user input and performs the same comparisons as before to
determine the jump location.

**ColorChange:**
This section seems to handle changing colors. It clears the screen, displays the text stored at
`color_ins1`, waits for user input, and checks if the input is 's' or 'S'. Depending on the
comparison result, it likely performs some actions related to changing colors.

In general, this code appears to be part of a menu-driven program that displays options, waits for
user input, and performs actions based on the chosen option. The specific functional ity and
purpose of the program are not clear from the provided code alone. Additional context or
surrounding code would be necessary to understand its complete functionality.

The code you provided appears to be a snippet of x86 assembly language code. Let's break it
down and discuss its functionality:

The code seems to define a menu system with four options: op1, op2, op3, and op4. Each option
displays a menu prompt using the `int 21h` instruction and waits for user input.

For example, let's take a closer look at the code for option 2 (op2):

```assembly
op2:
mov ah, 09h ; Set the display function to write a string
mov cx, 1000h ; Set the number of repetitions to display spaces
mov al, ' ' ; Set the character to display as a space
mov bl, 0Fh ; Set the text attribute to white
int 10h ; Call the BIOS video services to display spaces

mov ah, 09h ; Set the display function to write a string


lea dx, option1 ; Load the offset of the string "option1" into dx
int 21h ; Call the DOS interrupt to display the string

; ... more code for displaying additional menu prompts and processing user input
```

The code sets up the display attributes using the `int 10h` interrupt call, displays the menu
prompt using the `int 21h` interrupt call, and continues to the next menu option or handles user
input based on the selected option.

The code follows a similar structure for each menu option, displaying different strings for each
option and handling user input to determine the selected option.

It's worth noting that the code you provided seems to be a fragment, as some labels like
`downButtn` and `game_over1` are referenced but not defined in the provided code snippet.
Additionally, there are several undefined variables such as `selector` and `color_selector`.

Without further context or the complete program, it's challenging to provide a more detailed
explanation or discuss the code's purpose and functionality.
This code appears to be a game written in x86 assembly language. Let's go through it step by
step:

1. The code begins with setting up the data segment and initializing the video mode. It sets the
video mode to text mode 80x25 with 16 colors and 8 pages using interrupt 10h.

2. The `playme` subroutine is defined to hide the cursor. It sets the text-mode cursor shape using
interrupt 10h.

3. The `start` subroutine initializes various variables and enters the main game loop.

4. Within the game loop, the code checks for keyboard input using interrupt 21h. If a key is
pressed, it checks which key was pressed using the value stored in AH.

5. Based on the pressed key, the snake's direction is updated by modifying the `snake_direction`
variable. If the "W" key is pressed, the direction is set to 0 (up); if "S" key is pressed, the
direction is set to 2 (down); if "A" key is pressed, the direction is set to 3 (left); if "D" key is
pressed, the direction is set to 1 (right).

6. After updating the direction, the code flushes the keyboard buffer using interrupt 21h.

7. The `set_new_head_position` subroutine updates the position of the snake's head based on the
current direction. It also handles wrapping around the screen if the snake goes beyond the
boundaries.

8. The code then checks for collisions between the snake's head and its body parts. If a collision
is detected, the game ends (jump to `game_over`).

9. The code then shifts the body parts of the snake by moving the values of each body part to the
next position in the `body` array.

10. Next, the code checks if the snake's head position matches the star position. If they match,
the snake's body is extended by adding another body part at the tail. It also updates the star's
position to a new random value. Additionally, the score is incremented.

11. The `render` subroutine is responsible for drawing the game screen. It sets the cursor position
using interrupt 10h and draws the game border and other elements.

12. The code continues the game loop, repeating the process of checking keyboard input,
updating the snake's position, checking for collisions, extending the body, and rendering the
screen.

Overall, this code represents a simple snake game implemented in x86 assembly language. It
handles keyboard input, updates the game state, checks for collisions, and renders the game
screen.
The code you provided appears to be assembly code for a snake game. Let's go through the code
and discuss its functionality:

1. New Line: This section is responsible for printing a new line by using the DOS interrupt 21h.
It sets the value 10 in the DL register (which represents the ASCII code for a new line) and calls
interrupt 21h with function 06h to output the character in DL. It then sets the value 13 in the DL
register (which represents the ASCII code for a carriage return) and calls interrupt 21h again to
output the character.

2. Print Name: This section checks if the content of the memory location "buff + 2" is zero. If it
is zero, it jumps to the "scoreprint" label. Otherwise, it uses interrupt 21h with function 09h to
display the string at the memory location "buff + 2" (presumably a player's name). It then uses
interrupt 21h again to display the string "colon".

3. Print Score: This section displays the score. It takes the values stored in the variables
"score_1" and "score_0" and converts them to ASCII characters by adding the ASCII value of '0'
to each digit. It then uses interrupt 21h with function 06h to output the characters representing
the score.

4. Set Cursor: This section sets the cursor position on the screen. It uses interrupt 10h with
function 2h to set the cursor's position by providing the X-coordinate in the DL register and the
Y-coordinate in the DH register. In this case, both values are set to 0.

5. Draw Grass Top: This section draws a line of a grass character on the screen. It uses a loop to
repeatedly output the grass character using interrupt 21h with function 06h until the CX register
reaches zero. The value of CX is initially set to 15.

6. New Line (again): This section repeats the new line functionality mentioned in step 1.

7. Render Process: This section is the main rendering loop for the game. It increments the cursor
position and cursor position X values, then checks if the cursor position matches the snake's head
position or the star's position. If there is a match, it outputs the corresponding character using
interrupt 21h with function 06h.

8. Render Snake Body: This section iterates over the snake's body parts and checks if the current
body part's position matches the cursor position. If there is a match, it outputs the snake body
character using interrupt 21h with function 06h.

9. Render Star: This section checks if the cursor position matches the star's position and, if there
is a match, it outputs the star character using interrupt 21h with function 06h.

10. Render Tile: This section checks if the cursor position exceeds the maximum position (225 in
this case) and, if not, it outputs a tile character using interrupt 21h with function 06h.
11. Render After: This section checks if the cursor position X reaches 15, and if so, it jumps to
the new line section. It then checks if the cursor position reaches the maximum position (225),
and if not, it jumps back to the render process.

12. Game Over: This section handles the game over state. It uses interrupt 21h with function 09h
to display the string at the memory location "ritry". It then waits for a key press using interrupt
16h, and based on the pressed key, it either restarts the game or exits the program.

13. Soundfunc and Soundfunca: These two sections define procedures for playing sounds. They
set up the sound

frequency, enable the speaker output, play the sound for a specific duration, and stop the sound.
It uses the Programmable Interval Timer (PIT) and the speaker port to control the sound.

Overall, this code combines game logic, rendering, and sound playback to create a simple snake
game in assembly language.
.model small
.stack 200h
.data
message db 'Press Up or Down',10,13, '$'
l1 db ?
selector dw 00h
color_selector dw 00h
art0 db ' ',10,13
art1 db
' _________ __ ',10,13, '$'
art2 db ' / _____/ ____ _____ | | __
____ ',10,13, '$'
art3 db ' \_____ \ / \\__ \ | |/ // __ \
',10,13, '$'
art4 db ' / \ | \/ __ \| <\ ___/
',10,13, '$'
art5 db ' /_______ /___| (____ /__|_
\\___ >',10,13, '$'
art6 db ' \/ \/ \/ \/ \/
',10,13, '$'

start_ins1 db ' Press Up/Down Cursor


Keys!',10,13, '$'
color_ins1 db ' Press 1 for green, 2 for blue, 3 for cyan, 4
for RED!',10,13, '$'
option1 db ' Play Game!',10,13, '$'
option2 db ' Enter Name',10,13, '$'
option3 db ' Change Color!',10,13,
'$'
option4 db ' Exit',10,13, '$'
option1a db ' > Play Game! <',10,13,
'$'
option2a db ' > Enter Name <',10,13,
'$'
option3a db ' > Change Color!
<',10,13, '$'
option4a db ' >Exit<',10,13, '$'

game_na db 'Gsme NA!',10,13, '$'

upPress db 'Up is Pressed',10,13, '$'


downPress db 'Down is Pressed',10,13, '$'

exmessage db 'Exiting',10,13, '$'


blank db ' ',10,13, '$'

debug1 db 's1',10,13, '$'


debug2 db 's2',10,13, '$'
debug3 db 's3',10,13, '$'
debug4 db 's4',10,13, '$'
.code

data segment
assume ds:data
buff db 26 ;MAX NUMBER OF CHARACTERS ALLOWED (25).
db ? ;NUMBER OF CHARACTERS ENTERED BY USER.
db 26 dup(0) ;CHARACTERS ENTERED BY USER.
debug12 db 'Enter Name, press enter once finished: ', '$'
ritry db 'Retry? [Y/N] Q to quit', '$'
colon db ' score: ', '$'
soundFrequency dw 023A1h ; Frequency of the sound (440 Hz)
pointSound dw 1521h ; Frequency of the sound (440 Hz)
soundDuration dw 1 ; Duration of the sound (in milliseconds)

star_pos db 0
snake_pos db 0
snake_pos_x db 0
snake_direction db 1

cursor_pos db 0
cursor_pos_x db 0

body db 75 dup(0)

score_0 db 0
score_1 db 0

max_body_length equ 74
body_character equ 1
head_character equ 2
tile_character equ ','
star_character equ 3
border_character equ 205
data ends

main proc far


mov selector, 1
mov color_selector, 1
mov ax, 0003h ;clear screen
; mov ah,0000h
int 10h ;
;========================= Code Block Change Text Color
mov ah, 09h
mov cx, 1000h
mov al, ' '
mov bl, 0ah ; GREEN text on BLACK background
int 10h
;=========== Code Block for displaying ASCII Art Uses int 21, ah = 09 STRING
=================
mov ax, @data
mov ds, ax
mov ah, 09
lea dx, art1
int 21H
lea dx, art2
int 21H
lea dx, art3
int 21H
lea dx, art4
int 21H
lea dx, art5
int 21H
lea dx, art6
int 21H
mov ah, 09h
mov cx, 1000h
mov al, ' '
mov bl, 08ah ;
int 10h
lea dx, start_ins1
int 21H
; ============================================== END ASCII ART
controls: ; dat yung jmp controls same
indentions
;CHECK IF KEY WAS PRESSED.
mov ah, 0bh
int 21h ;◄■■ RETURNS AL=0 : NO KEY PRESSED,
AL!=0 : KEY PRESSED.
cmp al, 0

jne downButtn
je controls
color_decider:
cmp color_selector,1
je default_color
cmp color_selector,2
je color2
cmp color_selector,3
je color3
cmp color_selector,4
je color4
default_color:
;========================= Code Block Change Text Color

mov ah, 09h


mov cx, 1000h
mov al, ' '
mov bl, 0ah ;
int 10h
;
jmp playme

color2:
mov ah, 09h
mov cx, 1000h
mov al, ' '
mov bl, 09h ;
int 10h
jmp playme

color3:
mov ah, 09h
mov cx, 1000h
mov al, ' '
mov bl, 0bh ;
int 10h
jmp playme
color4:
mov ah, 09h
mov cx, 1000h
mov al, ' '
mov bl, 0ch ;
int 10h
jmp playme

downButtn:
mov ax, 03h ; for clearing screen
int 10h ; for clearing screen
;========================= Code Block Change Text Color
mov ah, 09h
mov cx, 1000h
mov al, ' '
mov bl, 00ah ; GREEN text on BLACK background
int 10h
ascii:
mov ax, @data
mov ds, ax
mov ah, 09
lea dx, art1
int 21H
lea dx, art2
int 21H
lea dx, art3
int 21H
lea dx, art4
int 21H
lea dx, art5
int 21H
lea dx, art6
int 21H
; ============================================== END ASCII ART

mov ax, @data


mov ds, ax
mov ah, 09
lea dx, downPress
int 21H

cmp selector, 1
cmp selector,2
je op2
cmp selector, 3
je op3
cmp selector,4
je op4
op1:
mov ah, 09h
mov cx, 1000h
mov al, ' '
mov bl, 0Aah ; GREEN SPACE WITH BLINK
int 10h
mov ah, 09
lea dx, option1a
int 21h

mov ah, 09h


mov cx, 1000h
mov al, ' '
mov bl, 0Fh ; White
int 10h

mov ah, 09
lea dx, option2
int 21h

mov ah, 09h


mov cx, 1000h
mov al, ' '
mov bl, 0Fh ; GREEN SPACE WITH BLINK
int 10h

mov ah, 09
lea dx, option3
int 21h
mov ax, @data
mov ds, ax
mov ah, 09
lea dx, option4
int 21H
mov ah, 09
lea dx, blank
int 21h
;==========================
mov ah, 0
int 16h
cmp ah, 50h ; If Down is pressed, should mov to
op2
je md1a
cmp ah, 48h
je md1b
cmp ah, 1Ch
je game
jne downButtn
md1a:
mov selector,02h
jmp downButtn
md1b:
mov selector,04h
jmp downButtn

;===========================

op2:

mov ah, 09h


mov cx, 1000h
mov al, ' '
mov bl, 0Fh ; White
int 10h
mov ah, 09
lea dx, option1
int 21h

mov ah, 09h


mov cx, 1000h
mov al, ' '
mov bl, 0Aah ; GREEN SPACE WITH BLINK
int 10h

mov ah, 09
lea dx, option2a
int 21h

mov ah, 09h


mov cx, 1000h
mov al, ' '
mov bl, 0Fh ; GREEN SPACE WITH BLINK
int 10h

mov ah, 09
lea dx, option3
int 21h
mov ax, @data
mov ds, ax
mov ah, 09
lea dx, option4
int 21H
mov ah, 09
lea dx, blank
int 21h
mov ah, 0
int 16h
cmp ah, 50h
je md2a
cmp ah, 48h
je md2b
cmp ah, 1Ch
je nameenter
jne downButtn
nameenter:
mov ax, data
mov ds, ax
LEA DX, debug12
MOV AH, 09h
INT 21H
mov dx,0
MOV AH, 0Ah
mov dx, offset buff
INT 21H
;CHANGE CHR(13) BY '$'.
mov si, offset buff + 1 ;NUMBER OF CHARACTERS ENTERED. SI is
source Index
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0 ;CLEAR CH TO USE CX.
inc cx ;TO REACH CHR(13).
add si, cx ;NOW SI POINTS TO CHR(13).
mov al, '$'
mov [ si ], al ;REPLACE CHR(13) BY '$'.

mov ax, @data


jmp downButtn
mov ah, 4ch
int 21h

md2a:
mov selector,03h
jmp downButtn

md2b:
mov selector,01h
jmp downButtn
;===========================
op3:
mov ah, 09h
mov cx, 1000h
mov al, ' '
mov bl, 0Fh ; White
int 10h

mov ah, 09
lea dx, option1
int 21h
mov ah, 09
lea dx, option2
int 21h

mov ah, 09h


mov cx, 1000h
mov al, ' '
mov bl, 0Aah ; GREEN SPACE WITH BLINK
int 10h

mov ah, 09
lea dx, option3a
int 21h

mov ah, 09h


mov cx, 1000h
mov al, ' '
mov bl, 0Fh ; GREEN SPACE WITH BLINK
int 10h

mov ax, @data


mov ds, ax
mov ah, 09
lea dx, option4
int 21H
mov ah, 09
lea dx, blank
int 21h
;==========================
mov ah, 0
int 16h
cmp ah, 50h
je md3a
cmp ah, 48h
je md3b
cmp ah, 1Ch
je changeme
jne downButtn
md3a:
mov selector,04h
jmp downButtn
md3b:
mov selector,02h
jmp downButtn
changeme:
mov ax, @data
mov ds, ax
mov ah, 09
lea dx, color_ins1
int 21H
mov ah, 0
int 16h
cmp ah, 01h
je downButtn
cmp ah, 02h
je a1
cmp ah, 03h
je a2
cmp ah, 04h
je a3
cmp ah, 05h
je a4
a1:
mov color_selector,1
jmp downButtn
a2:
mov color_selector,2
jmp downButtn
a3:
mov color_selector,3
jmp downButtn
a4:
mov color_selector,4
jmp downButtn

;===========================

op4:
mov ah, 09h
mov cx, 1000h
mov al, ' '
mov bl, 0Fh ; White
int 10h

mov ah, 09
lea dx, option1
int 21h
mov ah, 09
lea dx, option2
int 21h
mov ah, 09
lea dx, option3
int 21h

mov ah, 09h


mov cx, 1000h
mov al, ' '
mov bl, 0Aah ; GREEN SPACE WITH BLINK
int 10h

mov ax, @data


mov ds, ax
mov ah, 09
lea dx, option4a
int 21H

mov ah, 09h


mov cx, 1000h
mov al, ' '
mov bl, 0Fh ; GREEN SPACE WITH BLINK
int 10h

mov ah, 09
lea dx, blank
int 21h
;==========================
mov ah, 0
int 16h
cmp ah, 50h
je md4a
cmp ah, 48h
je md4b
cmp ah, 1Ch
je game_over1
jne downButtn
md4a:
mov selector,01h
jmp downButtn
md4b:
mov selector,03h
jmp downButtn
;===========================

ColorChange:
; newselector type Variable
;qwerty
;lea dx
;cmp tas je
mov ax, 0003h ;clear screen
int 10h ;

mov ax, @data


mov ds, ax
mov ah, 09
lea dx, color_ins1
int 21H

mov ah, 0
int 16h
cmp al, 's' ; Quit and save
cmp al, 'S' ; Quit and save

game:
snakeGame:
; set data segment pointer
push ax
mov ax, data
mov ds, ax
pop ax

; text mode 80x25, 16 colors, 8 pages


; ah=xxh al=03h -> ax=xx03h
; int 10h = video card intterupt
mov ah, 00h
mov al, 03h
int 10h
; AH = function that write character with attributes
; CX = how many times write character 1000 hex = 4096 decimal
; AL = character to write
; BL = background color
; upper bits stands for background e.g. 0
; lower bits for foreground e.g. 2
; 02h = black background, green foreground
;mov ah, 09h
;mov cx, 1000h
;mov al, ' '
;mov bl, 0ah
;int 10h
jmp color_decider
playme:
; hide cursor
; AH = 1 set text-mode cursor shape
; CX = 8 scan lines character of cursor
mov ah, 01h
mov cx, 2607h
int 10h

start:
mov si, 0
clear_enemies:
mov body[si], 0
inc si
cmp si, max_body_length
jl clear_enemies
; set values initially
mov star_pos, 35
mov snake_pos, 55
mov snake_pos_x, 10

game_loop:
check_key:
; sleep for 20ms (20816 microseconds -> 0c680h)
mov cx, 01h
mov dx, 0c680h
mov ah, 86h
int 15h

; check if any data is in keyboard buffer


mov ah, 0Bh
int 21h
cmp al, 0h
je is_new_key_false

is_new_key_true:
; if there is some data in keyboard buffer
; get keystroke and save ASCII character in AL, AH = scan
code
mov ah, 0
int 16h

; check if esc is pressed


cmp ah, 01h
jne not_exit
jmp game_over
not_exit:

; check if W is pressed
cmp ah, 11h
jne not_w
; check if currently snake isnt turned down
; snake shouldnt collide with themself
; just by turning 180 degree
cmp snake_direction, 2
je not_w
mov snake_direction, 0
call soundfunc
not_w:

; check if S is pressed
cmp ah, 1Fh
jne not_s
cmp snake_direction, 0
je not_s
mov snake_direction, 2
call soundfunc
not_s:

; check if A is pressed
cmp ah, 1Eh
jne not_a
cmp snake_direction, 1
je not_a
mov snake_direction, 3
call soundfunc
not_a:
; check if D is pressed
cmp ah, 20h
jne not_d
cmp snake_direction, 3
je not_d
mov snake_direction, 1
call soundfunc
not_d:

; flush keyboard buffer


mov ah, 0Ch
int 21h

is_new_key_false:

set_new_head_position:
cmp snake_direction, 0
jne not_up
sub snake_pos, 15
not_up:

cmp snake_direction, 1
jne not_right
inc snake_pos
inc snake_pos_x
not_right:

cmp snake_direction, 2
jne not_down
add snake_pos, 15
not_down:

cmp snake_direction, 3
jne not_left
dec snake_pos
dec snake_pos_x
not_left:

cmp snake_pos_x, 16
jne not_over_right
mov snake_pos_x, 1
sub snake_pos, 15
not_over_right:

cmp snake_pos_x, 0
jne not_over_left
mov snake_pos_x, 15
add snake_pos, 15
not_over_left:

cmp snake_pos, 225


jle not_over_bottom
cmp snake_pos, 240
jg not_over_bottom
sub snake_pos, 225
not_over_bottom:

cmp snake_pos, 240


jl not_over_top
cmp snake_pos, 0
jg not_over_top
add snake_pos, 225
not_over_top:

; loop for each part of body defined in body array


mov si, 0
loop_over_body_collision:

; if si is outer of body
cmp si, max_body_length
jge exit_loop_over_body_collision

mov ah, [snake_pos]


mov bh, body[si]
cmp ah, bh
jne not_collided
jmp game_over

not_collided:
inc si
jmp loop_over_body_collision
exit_loop_over_body_collision:

mov si, 0
loop_over_body_shift:

; if si is outer of body
cmp si, max_body_length
jge exit_loop_over_body_shift
inc si
mov ah, body[si]
dec si
mov body[si], ah

; move si to next value


inc si
jmp loop_over_body_shift
exit_loop_over_body_shift:

; insert new body part (twice if star has been collected)


mov si, 0
loop_over_body_add_body:

; if si is outer of body
cmp si, max_body_length
jge exit_loop_over_body_add_body

mov dl, body[si]


cmp dl, 0
jne dont_add_body
mov dl, snake_pos
mov body[si], dl

; check if head is euqal to star position


; and insert body once again
mov ah, [snake_pos]
mov bh, [star_pos]
cmp ah, bh
jne exit_loop_over_body_add_body

inc si
cmp si, max_body_length
jge exit_loop_over_body_add_body
mov dl, body[si]
cmp dl, 0
jne dont_add_body

mov dl, snake_pos


mov body[si], dl

; get random value


push dx
mov ax, dx
xor dx, dx
mov cx, 225
div cx
inc dl
mov star_pos, dl
pop dx

; add score
; increment score by 1
inc score_0
call soundfunca
cmp score_0, 9
jbe dont_icrement_tens
; if ones place is >= 9
; increment decimal by one
; so number jump from 9
; to 1,0 [score_1, score_0]
mov score_0, 0
inc score_1
dont_icrement_tens:

jmp exit_loop_over_body_add_body
dont_add_body:

; move si to next value


inc si
jmp loop_over_body_add_body
exit_loop_over_body_add_body:

render:
mov cursor_pos, 0

; set cursor at x=dl=0, y=dh=15


mov dl, 00h
mov dh, 15
mov ah, 2h
mov bh, 0
int 10h

; draw border bottom


mov dl, 10
mov ah, 06h
int 21h
mov dl, 13
int 21h
mov cx, 15
draw_grass_bottom:
dec cx
mov dl, border_character
int 21h
jnz draw_grass_bottom

; write new line


mov dl, 10
mov ah, 06h
int 21h
mov dl, 13
int 21h

;Print name
cmp buff + 2,0
je scoreprint
mov ah,09h
mov dx, offset buff + 2 ;For displaying name
int 21H
mov ah,09h
mov dx, offset colon
int 21H

; print score
scoreprint:
mov dl, score_1
add dl, '0'
mov ah, 06h
int 21h
mov dl, score_0
add dl, '0'
int 21h

; set cursor at x=dl=0, y=dh=0


mov dl, 00h
mov dh, 00h
mov ah, 2h
mov bh, 0
int 10h

; draw grass top


mov cx, 15
draw_grass_top:
dec cx
mov dl, border_character
int 21h
jnz draw_grass_top

; set cursor at x=dl=0, y=dh=0


mov dl, 00h
mov dh, 00h
mov ah, 2h
mov bh, 0
int 10h

new_line:
mov dl, 10
mov ah, 06h
int 21h
mov dl, 13
int 21h
mov cursor_pos_x, 0

render_process:
inc cursor_pos
inc cursor_pos_x

mov ah, [cursor_pos]


mov bh, [snake_pos]
cmp ah, bh
jne render_2
mov dl, head_character
mov ah, 06h
int 21h
jmp render_after

render_2:
mov si, 0
loop_over_body_render:

; if si is outer of body
cmp si, max_body_length
jge exit_loop_over_body_render

; check if current body part


; is equal to cursor position
; and draw snake body
mov ah, [cursor_pos]
mov bh, body[si]
cmp ah, bh
jne loop_over_body_render_next

mov dl, body_character


mov ah, 06h
int 21h
jmp render_after

; move si to next value


loop_over_body_render_next:
inc si
jmp loop_over_body_render
exit_loop_over_body_render:

render_3:
mov ah, [cursor_pos]
mov bh, [star_pos]
cmp ah, bh
jne render_4
mov dl, star_character
mov ah, 06h
int 21h
jmp render_after

render_4:
cmp cursor_pos, 225
ja render_after
mov dl, tile_character
mov ah, 06h
int 21h

render_after:
cmp cursor_pos_x, 15
je new_line

cmp cursor_pos, 225


jb render_process

jmp game_loop

game_over:
mov ah, 09h
mov cx, 1000h
mov al, ' '
mov bl, 0ah
int 10h
mov score_0,0
mov score_1,0
mov ah,09h
mov dx,offset ritry
int 21H
mov ah,0
int 16h

cmp ah, 15h


je snakegame
jne notyet
notyet:
cmp ah, 10h
je game_over1
jne downButtn

game_over1:
MOV AH,4ch
INT 21H

soundfunc proc
mov ax, data
mov ds, ax
; Set up the sound frequency
mov al, 182 ; Set command 182h (Play Sound) in AL
register
mov bx, soundFrequency
out 43h, al ; Send the command to the PIT
(Programmable Interval Timer)
mov al, bl
out 42h, al ; Send the low byte of the frequency
to port 42h
mov al, bh
out 42h, al ; Send the high byte of the frequency
to port 42h

; Play the sound


mov al, 3 ; Enable speaker output
out 61h, al ; Send command to port 61h
mov cx, soundDuration
mov dx, 40h
mov al, 0B6h
out dx, al ; Start playing the sound

; Wait for the sound to finish


mov ah, 86h
int 15h

; Stop the sound


mov al, 0 ; Disable speaker output
out 61h, al
ret
soundfunc endp

soundfunca proc
mov ax, data
mov ds, ax
; Set up the sound frequency
mov al, 182 ; Set command 182h (Play Sound) in AL
register
mov bx, pointSound
out 43h, al ; Send the command to the PIT
(Programmable Interval Timer)
mov al, bl
out 42h, al ; Send the low byte of the frequency
to port 42h
mov al, bh
out 42h, al ; Send the high byte of the frequency
to port 42h

; Play the sound


mov al, 3 ; Enable speaker output
out 61h, al ; Send command to port 61h
mov cx, soundDuration
mov dx, 40h
mov al, 0B6h
out dx, al ; Start playing the sound

; Wait for the sound to finish


mov ah, 86h
int 15h

; Stop the sound


mov al, 0 ; Disable speaker output
out 61h, al
ret
soundfunca endp

main endp
end main

You might also like