Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

COMPUTER ORGANIZATION AND

ASSEMBLY LANGUAGE

CEL-324

BSCS 3B – Fall 2021

NUMBER GUESSING GAME

DEPARTMENT OF COMPUTER SCIENCE


BAHRIA UNIVERSITY, KARACHI CAMPUS

|Page
SUBMITTED TO:
Mr. MUSLIM
SUBMITTED BY:
SHAHEER IMRAN
(02-134202-049)

MUHAMMAD AHMED TARIQ


(02-134202-028)

FAISAL JAMIL AHMED


(02-134202-081)

COURSE INSTRUCTOR:
MISS AISHA DANISH
LAB INSTRUCTOR:
MR. MUSLUM
CLASS:
BSCS-3B Fall 2021

|Page
Table of Contents

ABSTRACT........................................................................................................................................................1
1. INTRODUCTION..........................................................................................................................................0
1.1 What is a Microprocessor.........................................................................................................................0
1.2 What is 8086 Microprocessor...................................................................................................................0
1.3 Emulator...................................................................................................................................................1
1.4 Guessing Game.........................................................................................................................................1
1.5 OBJECTIVES:..............................................................................................................................................2
1.6 FUTURE WORK..........................................................................................................................................2
2. SYSTEM REQUIREMENTS...........................................................................................................................4
2.1 HARDWARE REQUIREMENTS....................................................................................................................4
2.2 SOFTWARE REQUIREMENTS.....................................................................................................................4
3. SYSTEM ANALYSIS......................................................................................................................................0
3.1 INTRODUCTIONS.......................................................................................................................................0
Data Transfer Instructions..........................................................................................................................0
Arithmetic Instructions...............................................................................................................................1
Bit Manipulation Instructions.....................................................................................................................2
String Instructions......................................................................................................................................3
Program Execution Transfer Instructions (Branch and Loop Instructions)..................................................3
3.2 Code:........................................................................................................................................................5
3.3 Screen Shots...........................................................................................................................................14

|Page
ABSTRACT

According to our course computer organization and Assembly Language offered by


our institution supervised by our beloved teacher Sir Muslim, we student of Bahria
University are able and given a chance to optimize the concept of assembly language
and implementation of it by performing it on EMU8086 Assembly language emulator.
In this project we are implementing concepts of assembly language on a game called
“Guessing Game”. It a very basic game but when it comes to assembly language it
become a little bit challenging because when working with assembly opcode there
are some limitations. So, by this project we will be understanding the concept that
how things work and operate.
By this course, we deal with assembly language, Assembly-level programming is still
written, and fairly often, as well. The most traditional groups of people who write
assembly are compiler and OS programmers, but it's also used in a wide swath of
other applications, and how Microprocessor Functions.

Keywords—Emulator, Microprocesor,8086,Guessing-GAME

|Page
1. INTRODUCTION
1.1 What is a Microprocessor
A microprocessor, also simply called a processor, [1][2] or CPU[3] is a computer processor
that is implemented on a single (or more) integrated circuit (IC)[4][5] dies of MOSFET
construction in a single package. The microprocessor is a multipurpose, clock-driven,
register-based, digital integrated circuit that accepts binary data as input, processes it
according to instructions stored in its memory, and provides results (also in binary
form) as output. Microprocessors contain both combinational logic and sequential
digital logic. Microprocessors operate on numbers and symbols represented in the
binary number system.

The integration of a whole CPU onto a single or a few integrated circuits using Very-
Large-Scale Integration (VLSI) greatly reduced the cost of processing power.
Integrated circuit processors are produced in large numbers by highly automated
metal-oxide-semiconductor (MOS) fabrication processes, resulting in a relatively low
unit price. Single-chip processors increase reliability because there are many fewer
electrical connections that could fail. As microprocessor designs improve, the cost of
manufacturing a chip (with smaller components built on a semiconductor chip the
same size) generally stays the same according to Rock's law.

Before microprocessors, small computers had been built using racks of circuit boards
with many medium- and small-scale integrated circuits, typically of TTL type.
Microprocessors combined this into one or a few large-scale ICs. The first
microprocessor was the Intel 4004.

Continued increases in microprocessor capacity have since rendered other forms of


computers almost completely obsolete (see history of computing hardware), with
one or more microprocessors used in everything from the smallest embedded
systems and handheld devices to the largest mainframes and supercomputers.

1.2 What is 8086 Microprocessor


The 8086 (also called iAPX 86) is a 16-bit microprocessor chip designed by Intel
between early 1976 and June 8, 1978, when it was released. The Intel 8088, released
July 1, 1979, is a slightly modified chip with an external 8-bit data bus (allowing the
use of cheaper and fewer supporting ICs), and is notable as the processor used in the
original IBM PC design.

|Page
The 8086 gave rise to the x86 architecture, which eventually became Intel's most
successful line of processors. On June 5, 2018, Intel released a limited-edition CPU
celebrating the 40th anniversary of the Intel 8086, called the Intel Core i7-8086K.

1.3 Emulator
In computing, an emulator is hardware or software that enables one computer
system (called the host) to behave like another computer system (called the guest).
An emulator typically enables the host system to run software or use peripheral
devices designed for the guest system. Emulation refers to the ability of a computer
program in an electronic device to emulate (or imitate) another program or device.
Many printers, for example, are designed to emulate HP LaserJet printers because so
much software is written for HP printers. If a non-HP printer emulates an HP printer,
any software written for a real HP printer will also run in the non-HP printer
emulation and produce equivalent printing. Since at least the 1990s, many video
game enthusiasts have used emulators to play classic (and/or forgotten) arcade
games from the 1980s using the games' original 1980s machine code and data, which
is interpreted by a current-era system and to emulate old video game consoles.

A hardware emulator is an emulator which takes the form of a hardware device.


Examples include the DOS-compatible card installed in some 1990s-era Macintosh
computers, such as the Centris 610 or Performa 630, that allowed them to run
personal computer (PC) software programs and FPGA-based hardware emulators. In
a theoretical sense, the Church-Turing thesis implies that (under the assumption that
enough memory is available) any operating environment can be emulated within any
other environment. However, in practice, it can be quite difficult, particularly when
the exact behavior of the system to be emulated is not documented and has to be
deduced through reverse engineering. It also says nothing about timing constraints; if
the emulator does not perform as quickly as the original hardware, the emulated
software may run much more slowly than it would have on the original hardware,
possibly triggering timer interrupts that alter behavior.

1.4 Guessing Game


The game is two-person game called "Guess the Number". The first player thinks of
an integer within a known range. The second player tries to guess the number. If the
guess is incorrect, then the first player tells the second player whether the guess was
too high or too low. Eventually, the second player guesses the correct number. The
second player's score equals the number of guesses he made. The players then

|Page
reverse their roles and repeat the game. The winner is the player who gets the
correct answer with the fewest guesses.

The key strategy in this game is to generate a clever guess. If, for example, the
second player knows the number is between 0 and 100, then a reasonable first guess
is 50. This choice evenly splits the range, giving you the maximum amount of
information about the next guess. If the first player says the guess is too low, then
the second player splits the reduced range and guesses 75. If the player says the
guess is too high, then the optimal guess is 25. It can be shown that by splitting the
remaining range in half after each guess, it will, at worst, take the second player no
more than guesses to find the unknown number where is the initial range. So
if the unknown number lies between 0 and 7, then it can be guessed in no more than
guesses.

1.5 OBJECTIVES:
Here the objective of this project is to implement the logic of assembly language.
Therefore to understand the fact that how assembly language works.
Points for this project for understanding:
 t allows complex jobs to run in a simpler way.
 It is memory efficient, as it requires less memory.
 It is faster in speed, as its execution time is less.
 It is mainly hardware oriented.
 It requires less instruction to get the result.
 It is used for critical jobs.
 It is not required to keep track of memory locations.

1.6 FUTURE WORK


The future of manufacturing and more particularly flexible assembly solutions will be
driven by advances in control technologies and control strategies.  Smarter, flexible
manufacturing cells are the goal. Machines that can change from one part model to
another on the fly.  Equipment that is smart enough to adjust the order of processes
to remain productive as it waits for a missing component.  Stations that can sense it
is making a less than optimum part and can correct in real time.

Most of the individual technologies to accomplish this exist today.  The bottle neck is
that the control solutions available do not make the task friendly.  The typical control

|Page
architecture today is many individual controllers specializing in a single task all
coordinated through one central controller; normally a PLC. 

Some innovative, creative Assembly Engineers are leading the way with very cool,
leading edge assembly cells that are flexible and powerful with the ability to produce
a variety of parts and to sense the process and process variables on the fly and take
corrective actions.  These cells and stations do exist.  However most have one thing in
common.  They are a Controls Engineer’s nightmare.  Most were created with many
thousands of hours of PLC programming and debug work by a team of Controls
Engineers, typically one of which is of rare talent. 

These Engineers who created the solution are the only people who can efficiently
work on the cell.  Pity the poor maintenance guy who must try and debug a problem
a year later when the “creators of the monster” are off to their next project.

Standardized protocols and communication standards are all part of the solution. 
Some Engineers are working on solutions that move away from the centralized
control model discussed above to a more decentralized control model.  While others
are moving toward even more centralized control solutions.  The solution is probably
a high bred strategy of both.

Whatever the solution, the major innovations in manufacturing over the next 25
years will be control based innovations.  Control technologies and strategies that
unleash the technical power available today in new ways and new combinations. 
Sure, we will develop new processes and new materials, but nothing will unleash our
productivity more than better control solutions that allow us to fully and efficiently
capitalize on the technologies that already exist today.

The factories of the future, the factories in our imagination are only limited by the
control technologies available today.

|Page
2. SYSTEM REQUIREMENTS
2.1 HARDWARE REQUIREMENTS

Hardware Requirement
Processor RAM Disk space
GHz or faster 1.6 1GB of RAM (1.5 GB if RPM hard 5400
processor running on a virtual drive and 5 GB of
machine) available hard disk
space
Table (2.1): Hardware Requirement

2.2 SOFTWARE REQUIREMENTS

Software Requirement

Operating System Database User Interface


design

Windows XP, Windows 7 Windows 8, Registers EMU8086


Windows10

Table (2.2): Software Requirement

|Page
3. SYSTEM ANALYSIS
3.1 INTRODUCTIONS
Operand types:
REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP. SREG:
DS, ES, SS, and only as second operand: CS.

Memory: [BX], [BX+SI+7], variable, etc.

Immediate: 5, -24, 3Fh, 10001101b, etc...


These marks are used to show the state of the flags:

1 - instruction sets this flag to 1. 0 - instruction sets this flag to 0. r - flag value
depends on result of the instruction. ? - flag value is undefined (maybe 1 or 0).

Data Transfer Instructions


These instructions are used to transfer the data from the source operand to the
destination operand. Following are the list of instructions under this group –

Instruction to transfer a word

 MOV − Used to copy the byte or word from the provided source to the
provided destination.
 PPUSH − Used to put a word at the top of the stack.
 POP − Used to get a word from the top of the stack to the provided location.
 PUSHA − Used to put all the registers into the stack.
 POPA − Used to get words from the stack to all registers.
 XCHG − Used to exchange the data from two locations.
 XLAT − Used to translate a byte in AL using a table in the memory.

Instructions for input and output port transfer

 IN − Used to read a byte or word from the provided port to the accumulator.
 OUT − Used to send out a byte or word from the accumulator to the provided
port.

|Page
Instructions to transfer the address

 LEA − Used to load the address of operand into the provided register.
 LDS − Used to load DS register and other provided register from the memory
 LES − Used to load ES register and other provided register from the memory.

Instructions to transfer flag registers

 LAHF − Used to load AH with the low byte of the flag register.
 SAHF − Used to store AH register to low byte of the flag register.
 PUSHF − Used to copy the flag register at the top of the stack.
 POPF − Used to copy a word at the top of the stack to the flag register.

Arithmetic Instructions
These instructions are used to perform arithmetic operations like addition,
subtraction, multiplication, division, etc.

Following is the list of instructions under this group –

Instructions to perform addition

 ADD − Used to add the provided byte to byte/word to word.


 ADC − Used to add with carry.
 INC − Used to increment the provided byte/word by 1.
 AAA − Used to adjust ASCII after addition.
 DAA − Used to adjust the decimal after the addition/subtraction operation.

Instructions to perform subtraction

 SUB − Used to subtract the byte from byte/word from word.


 SBB − Used to perform subtraction with borrow.
 DEC − Used to decrement the provided byte/word by 1.
 NPG − Used to negate each bit of the provided byte/word and add 1/2’s
complement.
 CMP − Used to compare 2 provided byte/word.
 AAS − Used to adjust ASCII codes after subtraction.
 DAS − Used to adjust decimal after subtraction.

|Page
Instruction to perform multiplication

 MUL − Used to multiply unsigned byte by byte/word by word.


 IMUL − Used to multiply signed byte by byte/word by word.
 AAM − Used to adjust ASCII codes after multiplication.

Instructions to perform division

 DIV − Used to divide the unsigned word by byte or unsigned double word by
word.
 IDIV − Used to divide the signed word by byte or signed double word by word.
 AAD − Used to adjust ASCII codes after division.
 CBW − Used to fill the upper byte of the word with the copies of sign bit of the
lower byte.
 CWD − Used to fill the upper word of the double word with the sign bit of the
lower word.

Bit Manipulation Instructions


These instructions are used to perform operations where data bits are involved, i.e.
operations like logical, shift, etc.

Following is the list of instructions under this group –

Instructions to perform logical operation

 NOT − Used to invert each bit of a byte or word.


 AND − Used for adding each bit in a byte/word with the corresponding bit in
another byte/word.
 OR − Used to multiply each bit in a byte/word with the corresponding bit in
another byte/word.
 XOR − Used to perform Exclusive-OR operation over each bit in a byte/word
with the corresponding bit in another byte/word.
 TEST − Used to add operands to update flags, without affecting operands.

Instructions to perform shift operations

 SHL/SAL − Used to shift bits of a byte/word towards left and put zero(S) in
LSBs.
 SHR − Used to shift bits of a byte/word towards the right and put zero(S) in
MSBs.

|Page
 SAR − Used to shift bits of a byte/word towards the right and copy the old MSB
into the new MSB.

Instructions to perform rotate operations

 ROL − Used to rotate bits of byte/word towards the left, i.e. MSB to LSB and to
Carry Flag [CF].
 ROR − Used to rotate bits of byte/word towards the right, i.e. LSB to MSB and
to Carry Flag [CF].
 RCR − Used to rotate bits of byte/word towards the right, i.e. LSB to CF and CF
to MSB.
 RCL − Used to rotate bits of byte/word towards the left, i.e. MSB to CF and CF
to LSB.

String Instructions
String is a group of bytes/words, and their memory is always allocated in a sequential
order.

Following is the list of instructions under this group −

 REP − Used to repeat the given instruction till CX ≠ 0.


 REPE/REPZ − Used to repeat the given instruction until CX = 0 or zero flag ZF =
1.
 REPNE/REPNZ − Used to repeat the given instruction until CX = 0 or zero flag
ZF = 1.
 MOVS/MOVSB/MOVSW − Used to move the byte/word from one string to
another.
 COMS/COMPSB/COMPSW − Used to compare two string bytes/words.
 INS/INSB/INSW − Used as an input string/byte/word from the I/O port to the
provided memory location.
 OUTS/OUTSB/OUTSW − Used as an output string/byte/word from the
provided memory location to the I/O port.
 SCAS/SCASB/SCASW − Used to scan a string and compare its byte with a byte
in AL or string word with a word in AX.
 LODS/LODSB/LODSW − Used to store the string byte into AL or string word
into AX.

Program Execution Transfer Instructions (Branch and Loop


Instructions)

|Page
These instructions are used to transfer/branch the instructions during an execution.
It includes the following instructions −

Instructions to transfer the instruction during an execution without any condition −

 CALL − Used to call a procedure and save their return address to the stack.
 RET − Used to return from the procedure to the main program.
 JMP − Used to jump to the provided address to proceed to the next
instruction.

Instructions to transfer the instruction during an execution with some conditions −

 JA/JNBE − Used to jump if above/not below/equal instruction satisfies.


 JAE/JNB − Used to jump if above/not below instruction satisfies.
 JBE/JNA − Used to jump if below/equal/ not above instruction satisfies.
 JC − Used to jump if carry flag CF = 1
 JE/JZ − Used to jump if equal/zero flag ZF = 1
 JG/JNLE − Used to jump if greater/not less than/equal instruction satisfies.
 JGE/JNL − Used to jump if greater than/equal/not less than instruction
satisfies.
 JL/JNGE − Used to jump if less than/not greater than/equal instruction
satisfies.
 JLE/JNG − Used to jump if less than/equal/if not greater than instruction
satisfies.
 JNC − Used to jump if no carry flag (CF = 0)
 JNE/JNZ − Used to jump if not equal/zero flag ZF = 0
 JNO − Used to jump if no overflow flag OF = 0
 JNP/JPO − Used to jump if not parity/parity odd PF = 0
 JNS − Used to jump if not sign SF = 0
 JO − Used to jump if overflow flag OF = 1
 JP/JPE − Used to jump if parity/parity even PF = 1
 JS − Used to jump if sign flag SF = 1

|Page
3.2 Code:
.model small

.stack 100h

.data

number db 255d ;variable 'number' stores the random value

;declarations used to add LineBreak to strings

CR equ 13d

LF equ 10d

;String messages used through the application

prompt db CR, LF,'Please enter a valid number : $'

lessMsg db CR, LF,'Value if Less ','$'

moreMsg db CR, LF,'Value is More ', '$'

equalMsg db CR, LF,'You have made fine Guess!', '$'

overflowMsg db CR, LF,'Error - Number out of range!', '$'

retry db CR, LF,'Do you want to continue [y/n] ? ','$'

guess db 0d ;variable user to store value user entered

errorChk db 0d ;variable user to check if entered value is in range

param label Byte

; Declare strings

s_request DB 13,10, "Please guess a number between 1 and 5: $"

s_retryfail DB 13, 10, "Unlucky! Your guess was incorrect! :(", 13, 10, "Would you like to try again? (y/n): $"

|Page
s_retrysucc DB 13, 10, "Congratulations! Your guess was correct! :)", 13, 10, "Would you like to play again? (y/n): $"

s_bye DB 13, 10, "Thanks for playing, bye! :)", 13, 10, "$"

s_blankline DB 13, 10, "$"

s_round2 DB 13, 10, "WELCOME TO ROUND2! ",13,10,"$"

.code

start:

; --- BEGIN resting all registers and variables to 0h

MOV ax, 0h

MOV bx, 0h

MOV cx, 0h

MOV dx, 0h

MOV BX, OFFSET guess

MOV BYTE PTR [BX], 0d ; Is A Pointer Directive Used To Store 8-Bit Value

MOV BX, OFFSET errorChk

MOV BYTE PTR [BX], 0d

; --- END resting

Mid:

MOV ax, @data

MOV ds, ax

MOV dx, offset prompt

MOV ah, 9h

INT 21h

MOV cl, 0h

MOV dx, 0h

; -- BEGIN reading user input

while:

|Page
CMP cl, 5d

JG endwhile ; This Method Will Read Input One By One From User Upto 5 Decimal Values

MOV ah, 1h

INT 21h

CMP al, 0Dh ; Will Chk If User Pressed Enter, Means User Is Done With Input

JE endwhile

SUB al, 30h ; Subtracting 30 To Get Accurate Value

MOV dl, al

PUSH dx ; Loding That Input Into Stack

INC cl

JMP while

endwhile:

; -- END reading user input

DEC cl

CMP cl, 02h

JG overflow

MOV BX, OFFSET errorChk

MOV BYTE PTR [BX], cl

MOV cl, 0h

; -- BEGIN processing user input

; -- Create actual NUMERIC representation of

;-- number read from user as three characters

while2:

|Page
CMP cl,errorChk

JG endwhile2

POP dx

MOV ch, 0h

MOV al, 1d

MOV dh, 10d

; -- BEGIN loop to create power of 10 for related position of digit

; -- IF CL is 2

; -- 1st loop will produce 10^0

; -- 2nd loop will produce 10^1

; -- 3rd loop will produce 10^2

while3:

CMP ch, cl ;In Method While2, While3 and endwhile3 We Will Process The User Input And
Then After Processing Jump To endwhile2 To CMP Values

JGE endwhile3

MUL dh

INC ch

JMP while3

endwhile3:

; -- END power calculation loop

; now AL contains 10^0, 10^1 or 10^2 depending on the value of CL

MUL dl

JO overflow

|Page
MOV dl, al

ADD dl, guess

JC overflow

MOV BX, OFFSET guess

MOV BYTE PTR [BX], dl

INC cl

JMP while2

endwhile2:

; -- END processing user input

MOV ax, @data

MOV ds, ax

MOV dl, number

MOV dh, guess

; Will CMP Values And JMP

CMP dh, dl

JC greater

JE equal

JG lower

equal:

MOV dx, offset equalMsg

MOV ah, 9h

INT 21h

JMP exit

|Page
greater:

MOV dx, offset moreMsg

MOV ah, 9h

INT 21h

JMP start

lower:

MOV dx, offset lessMsg

MOV ah, 9h

INT 21h

JMP start

overflow:

MOV dx, offset overflowMsg

MOV ah, 9h

INT 21h

JMP start

exit:

; -- Ask user if he needs to try again if guess was successful

retry_while:

MOV dx, offset retry

MOV ah, 9h

INT 21h

MOV ah, 1h

|Page
INT 21h

CMP al, 6Eh

JE return_to_DOS

CMP al, 79h

JE restart

JMP retry_while

retry_endwhile:

restart:

LEA DX, s_round2

MOV AH, 9

INT 21H

JMP beg

; Start prog

beg:

JMP RANDNUM

; Generate randnum

RANDNUM:

MOV AH, 2CH

INT 21H

MOV AL,DL

MOV AH,0 ; Will Generate Random Num For Round 2

MOV CL,20

DIV CL

|Page
MOV BL, AL

INC BL

JMP INPUT

; Recieve input

INPUT:

LEA DX, s_request

MOV AH, 9

INT 21H

MOV AH, 1

INT 21H

SUB AL, 30H

JMP COMPARE

; Compare guess with randnum

COMPARE:

CMP AL, BL

JZ SUCCESS

JMP FAIL

; If guess is successful

SUCCESS:

LEA DX, s_retrysucc

MOV AH, 9

INT 21H

JMP Res

; If guess is unsuccessful

FAIL:

LEA DX, s_retryfail

MOV AH, 9

INT 21H

JMP Res

|Page
Res:

MOV AH, 1

INT 21H

CMP AL, 79H

LEA DX, s_blankline

MOV AH, 9 ; Will Again ask For Try Again

INT 21H

JZ Mid

LEA DX, s_bye

MOV AH, 9

INT 21H

return_to_DOS:

MOV ax, 4 ; DOS INT 21h (DOS interrupt)

end start

RET

|Page
3.3 Screen Shots

Press RUN Button To Proceed Further

Game Started, Enter The Guessed Number

Round 1:
|Page
Round 2:

THANKYOU
|Page

You might also like