You are on page 1of 10

PAMANTASAN NG LUNGSOD NG MAYNILA

(University of the City of Manila)


Intramuros, Manila

Computer Architecture and Organization Lab

APPLICATION OF INTERRUPT AND FUNCTIONS IN ASSEMBLY PROGRAMMING

Score

Submitted by:

Rosete, Mark Anthony C.

Sundiam, Ferdinand Francis

Mendoza, James Noah

Orduna, Camille

Dumalag, Joshua
Monday, 6:00 pm – 9:00 pm
CPE 0323.1-3 Computer Architecture and Organization

Date Submitted
06-05-2023

Submitted to:

Engr. Maria Rizette H. Sayo

CH

I. Objectives

• To produce a simple 16-bit program using 8086 assembly language.


• To utilize 10 interrupts with functions
• To present and execute 10 interruptions.
• To promote the 16-bit program architecture.
• To provide main menu system
• To allow player to change color of the game
• To enable player to enter their name
• To provide a user friendly navigation for the menu

II. Methods

This program is a snake game made using 8086 assembly language. It contains several
data, code segments and labels to define variables and implement functions that serve as the game
logic.

The program uses interrupts for setting the video mode, key press, and sound. It also uses
input/output operations to interact with the screen and keyboard. It follows very basic game logic
and structure where a player controls a character(snake) where it can navigate space, it can eat
objects while avoiding colliding to itself. The points increase with each successful eat the dot. The
game ends when the snake eats its own body.

Data Segment:

The data segment defines various variables used in the snakeGame , including:

• star_pos: Stores the position of the star in the game.


• snake_pos: Stores the current position of the snake's head.
• snake_pos_x: Stores the current position of the snake's head on the x-axis.
• snake_direction: Stores the current direction of the snake (up, down, left, or right).
• cursor_pos: Stores the current position of the cursor.
• cursor_pos_x: Stores the current position of the cursor on the x-axis.
• body: Array that represents the body parts of the snake.
• score_0 and score_1: Variables to store the player's score.
Computer Architecture and Organization Lab Page 2 PLM_CpE
• Various constants and characters used in the game.

Main proc far


• This is where the main code happens
• It initialized the ASCII art in the press any key to continue screen
• Initializes variable selector, and color_selector with the default value of 01h
• The label controls is the loop where the initial screen with the ASCII art will show. It
uses interrupt -21 (AH = 0BH CHECK STANDARD INPUT STATUS) where AL = 00h
if no character is inputted. If there is no character, it jumps to itself to continuously
display the text.
• Label DownButton is where the menu and the other parts of the code apart from the
soundfunct procedures. It checks what value the selector variable is and jumps from
either op1, op2, op3, or op4 depending on its value.

DownButtn label
• The label checks the value of variable selector. When it is equal to 1, then it jumps in to
op1 where the screen is cleared, changing the colors of which it would be the only one
highlighted using the attribute 0Ah on bl (int 10, AH = 09) to present the highlighted
option for the user in bright green colors.
• The labels op1, op2, op3, and op4 uses interrupt 16h to get character input from the user.
The code uses a pseudo AND gate logic where if the selector is a current value (1-4) and
the user pressed enter (scan code 1ch), then it will jump to the function as to which the
screen will the be cleared in order for that function to be used
• The function for op1 when entered is to play a game, thus it jumps to label snakeGame
where the game is found. snakeGame label points back to the data segment where the
variables for the game are used. Upon exiting the code, it reverts the ax to use the .data
segment of the code.
• The function for op2 when enter is pressed is for the user to enter their name which
would be shown when playing the game. It uses variable buff to store the maximum
character the program accepts, the name of the user entered, and the length of the string
entered by the user. Upon pressing enter after finishing the user input, the code will jump
to label downButtn , looping back for players to select options 1 to 4.
• The function for op3 when enter is pressed is to change the color of the snake game when
played. It uses the label changeme to allow character input from the player to select their
desired color (1 = default green, 2 = blue, 3 = cyan, and 4 = red). Upon character input,
variable color_selector is updated for label color_decider to use.
• Finally, op4 when entered, will invoke the ah = 4ch of interrupt 21 of DOS, exiting the
program

playmeLoop:

• The main game loop is started with the label game_loop.


• Inside the loop, the program checks for keyboard input using interrupt 21h with function
0Bh. If a key is pressed, it processes the input and updates the snake's direction accordingly.
• The snake's head position is updated based on the current direction.
• Collision detection is performed by comparing the head position with the positions of the
snake's body parts. If a collision is detected, the game is over.
• The body of the snake is shifted and updated based on the current head position.
• If the snake's head position matches the star's position, the snake grows longer, and the
score is incremented. The star's position is randomly generated.
• The game screen is rendered, including the snake, the star, and the score.

color_decider label:
• It will be used when snakeGame is initialized. After calling snakeGame label from op1,
the code will jump to color_decider where it compares the value of the variable
color_selector, jumping when equivalent to either color1, color2, color3, and color4 where
the int 10, ah = 09 is used to modify the attributes of the text to be displayed
snakeGame:

Computer Architecture and Organization Lab Page 3 PLM_CpE


• the label contains the game of the classic snake game in assembly
• The segment will first jump into label color_decider to check which color in video mode
will the game will play. It will jump back to the playme label that contains the rest of the
game hereinafter
• Whenever the esc key is pressed, or the snake character collides with its body, the game
will constitute this as a game_over and will jump accordingly
• The game_over label will display the message, asking the user to retry the game, exit to
menu, or exit the program entirely.
• The game uses the controls w,a,s, and d, to control the snake character and flushes the
buffer for scalability purposes when using the same key s for other functions.
• The game will jump to different areas of the snakeGame label upon wasd input to change
the direction of the snake, using variable snake_direction, and will not jump if the player
is attempting to move from left to right instantaneously as this will collide to the body of
the snake, resulting in a game over.
• Score is printed on the bottom of the rendered bottom grass, alongside the player name on
its left. If no player name is entered, then the score will only be printed.
• Upon collliding, the code will jump to game_over, where the user is then asked to retry by
pressing Y/N. Additionally, pressing Q will exit the program entirely
Name enter label:
• The label is invoked when option2 to change name is selected.
• It will display the message to enter name using the variable buff.
• Upon pressing enter (character 13 in ascii means EOL), the string is inputted to the buff
variable, alongside its count, jumping afterwards
• The code then checks the size of the string, increments it, and puts ‘$’ for it to be
successfully read as a string.

Summary:

The code implements a main menu for a basic snake game in assembly language. The menu
handles the additional parts of the game, including changing its colors, customizing player name,
as well as selecting whether to play again or not. The game itself handles keyboard input, updates
the game state, performs collision detection, renders the game screen, and manages the player's
score. The game will terminate when the player presses the esc key, the snake character collides
with the body and selected to press Q

Interrupts used by the program:


Int 10

Computer Architecture and Organization Lab Page 4 PLM_CpE


Int 21h

Int 16h

Computer Architecture and Organization Lab Page 5 PLM_CpE


Int 15h

Interrupt 10
AH = 09h VIDEO - WRITE ATTRIBUTES/CHARACTERS AT CURSOR POS
Where AL = character, BH = display page, BL = attributes of char and CX = number of
times to write character

AH = 03h VIDEO - READ CURSOR POSITION


Where AL = 03H for text mode, but having no resolution set, this will fill the screen with
blank characters

AH = 01h VIDEO - SET CURSOR CHARACTERISTICS CH


Where bits 5-6 = blink attribute (00=normal, 01=invisible, 10=slow, 11=fast) used to
hide cursor
Interrupt 21h
AH = 09h DOS - PRINT STRING
Where DX = address of string terminated by "$"

AH = 0Bh DOS - CHECK STANDARD INPUT STATUS


Returns AL = FFh if character available and 00h if no character is entered

AH = 0Ah DOS - BUFFERED KEYBOARD INPUT


Where DX = address of buffer . The first byte of buffer must contain maximum length on
entry, second byte contains actual length of previous line which may be recalled with
the DOS line-editing commands on return the second byte contains actual length, third
and subsequent bytes contain the input line.

AH = 0Ch DOS - CLEAR KEYBOARD BUFFER


Clears the keyboard buffer to not interact with the rest of the code

AH = 4Ch DOS - QUIT WITH EXIT CODE (EXIT)


Used for exiting program

Interrupt 16h
AH = 00h KEYBOARD - READ CHAR FROM BUFFER, WAIT IF EMPTY
Where AH = scan code and AL = character
Interrupt 15h
AH = 86h SYSTEM - WAIT
Where CX,DX = number of microseconds to wait (only accurate to 977 us)
Computer Architecture and Organization Lab Page 6 PLM_CpE
Program Tools

1. Visual Code Studio – Visual Studio Code is a free and widely used program that helps
programmers write and edit their code. It has a simple and user-friendly interface,
supports many programming languages, and offers features like highlighting and
suggestions. It also has a built-in terminal for running commands and interacting with
code.

2. DOSBox - DOSBox is a program that lets you play old MS-DOS games and use
applications on modern computers. It creates a virtual environment that mimics the
functionality of the old MS-DOS system, allowing you to run those programs. You can
adjust settings and mount drives to access the necessary files. It's a useful tool for retro
gaming and running older software.

3. MASM/TASM -
MASM and TASM are assemblers used for low-level programming. They convert assembly
language code into machine code. MASM is developed by Microsoft and is commonly used for
programming on x86 architecture. TASM, developed by Borland, is also used for x86
programming. They allow programmers to write efficient code that interacts directly with
hardware. They are often used for system-level programming.

III. Results

The main focus of the program is to provide a simple interactive game where the player is
directed to menu screen that allows simple customization of a classic snake game, including
changing ones name, and changing the colors of the game. The classic game of snake is where
player controls a ‘snake’ to collect stars and increase their score. The gameplay experience is
displayed in the text-based console.

The code keeps track of the player's score and updates it whenever a star is collected. The
score is displayed on the screen during gameplay. The game also displays the player's name on the
screen, which is obtained through the buff variable.

The results that the group found upon running the code achieved the objectives of the
project. The code allows for player to change their name, enabled player to choose the colors of
the game, and most importantly to play the classic game of snake. However, the group found that
jumping back to the menu screen loses the colors of the program 9/10 times. The group suspect
that this is due to memory allocation as the group experimented with largely changing the value
of cx for character attributes to the maximum to compensate for the change of characters but was

Computer Architecture and Organization Lab Page 7 PLM_CpE


unsuccessful. Fortunately, this is simply a preference put in place by the group for easier debugging
process and escaping to the menu is not part of the objectives.

Computer Architecture and Organization Lab Page 8 PLM_CpE


Computer Architecture and Organization Lab Page 9 PLM_CpE
IV. Conclusion

In conclusion, the program successfully achieves its main focus of providing a simple
interactive game based on the classic snake game. The player is able to customize their gaming
experience by changing their name and the colors of the game. The code effectively keeps track
of the player's score, updates it when stars are collected, and displays the score during gameplay.
The player's name is also displayed on the screen, obtained through the buff variable.

Overall, the objectives of the project were met, allowing players to enjoy the classic game
of snake with customization options. However, the group encountered a minor issue where
returning to the menu screen sometimes resulted in the loss of color settings. The group suspects
that this is due to memory allocation and attempted to address it by adjusting character attributes,
but the issue persisted. Fortunately, this does not affect the core objectives of the project, as
returning to the menu screen was not part of the intended gameplay flow.

Computer Architecture and Organization Lab Page 10 PLM_CpE

You might also like