Professional Documents
Culture Documents
Codes
Codes
It is a
small program that displays ASCII art and provides a menu with several options.
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.
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.
**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
; ... 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, '$'
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
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
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
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, 09
lea dx, option2
int 21h
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, 09
lea dx, option2a
int 21h
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 '$'.
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, 09
lea dx, option3a
int 21h
;===========================
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, 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 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
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
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 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:
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:
; if si is outer of body
cmp si, max_body_length
jge exit_loop_over_body_collision
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
; if si is outer of body
cmp si, max_body_length
jge 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
; 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:
render:
mov cursor_pos, 0
;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
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
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
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
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
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
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
main endp
end main