Lab Manual Computer Organization & Assembly Language
Lab Manual Computer Organization & Assembly Language
Lab Manual Computer Organization & Assembly Language
COMPUTER
ORGANIZATION &
ASSEMBLY LANGUAGE
1
Contents
2
Lab Session 01
OBJECTIVE:
Explore debugger using different commands.
THEORY:
PROGRAMMING LANGUAGE
A programming language is an artificial language that can be used to control the behavior of
a machine, particularly a computer. Programming languages, like human languages, have
syntactic and semantic rules to define meaning.
Programming languages can be classified into three basic categories on the basis of
understanding level of users as well as the machine to which instructions has been given:
3. MACHINE LANGUAGE
Machine language is at the lowest level, because it is the actual
binary code of 1s and 0s that the computer understands. These are designed to give a
better machine efficiency.
3
SOME GENERAL PURPOSE REGISTERS:
AX (ACCUMULATOR REGISTER)
It is the preferred register to use in the arithmetic, logic and data transfer instructions
because its use generates the shortest machine code.
In multiplication and division operations, one of the numbers involved must be in AX
or AL.
Input and output operation also requires the use of AX and AL.
BX (BASE REGISTER)
CX (COUNT REGISTER)
Program loop instructions are facilitated by the use of CX register, serves as a loop
counter.
Also used as a counter in the string operations.
CL is used as count in instructions that shift and rotate bits.
DX (DATA REGISTER)
REGISTER SIZE:
The basic MOV instruction is used to transfer data between registers, between and
memory locations, or to have a number directly to a register or memory location.
EXAMPLES:
4
ADD:Add the contents of source operand1 to source operand 2 and result store in
the source operand1.
SUB: Subtract the contents of source operand1 to source operand 2 and result
store in the source operand1.
To create a program in assembler two options exist, the first one is to use the assembler program
such as TASM or Turbo Assembler from Borland, and the second one is to use the debugger - on
this first section we will use this last one since it is found in any PC with the MS-DOS, which
makes it available to any user who has access to a machine with these characteristics.
Debug can only create files with a .COM extension, and because of the characteristics of
these kinds of programs they cannot be larger that 64 kb, and they also must start with
displacement, offset, or 0100H memory direction inside the specific segment.
Debug provides a set of commands that lets you perform a number of useful operations
N Name a program
5
Q Quit the debug program
It is possible to visualize the values of the internal registers of the CPU using the
Debug program. To begin working with Debug, type the following prompt in
your computer:
C:/>Debug [Enter]
On the next line a dash will appear, this is the indicator of Debug, at this moment
the instructions of Debug can be introduced using the following command:
-r[Enter]
0D62:0100 2E CS:
All the contents of the internal registers of the CPU are displayed; an alternative of
viewing them is to use the "r" command using as a parameter the name of the
register whose value wants to be seen.
For example:
-rbx
BX 0000
ASSEMBLER STRUCTURE
In assembly language code lines have two parts, the first one is the name of the instruction which
is to be executed, and the second one are the parameters of the command. For example:
add ah,bh
Here "add" is the command to be executed; in this case an addition, and "ah" as well as "bh"
are the parameters.For example:
mov al, 25
6
In the above example, we are using the instruction mov, it means move the value 25 to al
register.The name of the instructions in this language is made of two, three or four letters.
These instructions are also called mnemonic names or operation codes, since they represent a
function the processor will perform.
DIRECT ADDRESSING
add al,[170]
The brackets in the second parameter indicate to us that we are going to work with the content of
the memory cell number 170 and not with the 170 value, this is known as direct addressing.
a 100[enter]
mov ax,0002[enter]
mov bx,0004[enter]
add ax,bx[enter]
nop[enter][enter]
C:\>debug
-a 100
0D62:0108 nop
0D62:0109
Type the command "t" (trace), to execute each instruction of this program, example
-t
7
You see that the value 2 move to AX register. Type the command "t" (trace),
again, and you see the second instruction is executed.
-t
Type the command "t" (trace) to see the instruction add is executed, you will see the
follow lines:
-t
0D62:0108 90 NOP
LAB OBJECTIVES:
TASK#1: Display the contents of the defined memory locations 120, 133, 122 using D
command.
TASK#2: Edit the contents of the above memory locations 120 ,133,122 by 02,04,03
respectively using E command.
TASK#3: Then again display the contents of the memory locations which we edit in the
Task# 2.
TASK#4: Add the contents of the above defined memory location using mov instruction.
TASK#5: Subtract the content of 120 location by 133 and then store the result in
the120 location and add the new 120 location contents with the content of 122 location.
ACT 1.1 : Use debug command U100 to un-assemble the instructions in ACT 1.1. What is the
machine code corresponding to each assembly code instruction.
8
Nop
ACT 1.2 : How many bytes does it need to represent each instruction in binary.
ACT 1.3 : What are the contents of CS , IP , AX & BX ?. Use debug command R to display these
information.
Register Content
CS
IP
AX
BX
ACT 1.4 : Predict the contents of the following registers after execution of each instruction CS
, IP,AX,BX
9
Lab Session 02
OBJECTIVE:
Theory:
Assembly Language: An assembly language is a low-level programming language for a
computer, or other programmable device, in which there is a very strong (generally one-to-
one) correspondence between the language and the architecture's machine code
instructions. Each assembly language is specific to a particular computer architecture, in
contrast to most high-level programming languages, which are generally portable across
multiple architectures, but require interpreting or compiling
Assembly language is converted into executable machine code by a utility program referred
to as an assembler; the conversion process is referred to as assembly, or assembling the code.
Assembly language consists of statements written with short mnemonics such as ADD, MOV,
SUB, and CALL. Assembly language has a one-to-one relationship with machine language: Each
assembly language instruction corresponds to a single machine-language instruction.
10
High-level languages such as C++ and Java have a one-to-many relationship with
assembly language and machine language.
A single statement in C++ expands into multiple assembly language or machine
instructions. We can show how C++ statements expand into machine code. Most people
cannot read raw machine code, so we will use its closest relative, assembly language.
A language whose source programs can be compiled and run on a wide variety of computer
systems is said to be portable. A C++ program, for example, should compile and run on just
about any computer, unless it makes specific references to library functions that exist under
a single operating system. A major feature of the Java language is that compiled programs
run on nearly any computer system.
Assembly language is not portable because it is designed for a specific processor family.
There are a number of different assembly languages widely used today, each based on a
processor family. Some well-known processor families are Motorola 68x00, x86, SUN Sparc,
Vax, and IBM-370. The instructions in assembly language may directly match the
computer’s architecture or they may be translated during execution by a program inside the
processor known as a microcode interpreter
10110000 01100001
B0 61
11
Here, B0 means 'Move a copy of the following value into AL', and 61 is a hexadecimal
representation of the value 01100001, which is 97 in decimal. Intel assembly language
provides the mnemonic MOV (an abbreviation of move) for instructions such as this, so the
machine code above can be written as follows in assembly language, complete with an
explanatory comment if required, after the semicolon. This is much easier to read and to
remember.
Syntax:
x86 assembly language has two main syntax branches: Intel syntax, originally used for
documentation of the x86 platform, and AT&T syntax.[1] Intel syntax is dominant in the MS-
DOS and Windows world, and AT&T syntax is dominant in the Unix world, since Unix was
created at AT&T Bell Labs. Many x86 assemblers use Intel syntax including MASM, TASM,
NASM, FASM and YASM.
Statement syntax
Name operation operand(s) comment
Name field
Used for instruction labels, procedure names, and variable
names Assembler translates names into memory addresses
Names are 1-31 characters including letters, numbers and special characters ? .
@ _ $ % . Names may not begin with a digit. If a period is used, it must
be first character. Names are Case insensitive
• COUNTER1
• @character
• SUM_OF_DIGITS
• $1000
• Done?
• .TEST
Examples of illegal names
• TWO WORDS
• 2abc
• A45.28
12
Operation field
Instruction
Assembler directive
An assembler directive is not translated into machine code. It tells the assembler to do
something.
Operand field
It Specifies data to be acted on. There can be Zero, one, or two operands.
Examples
• NOP
• INC AX
• ADD AX, 2
Comment field
A semicolon marks the beginning of a comment. A semicolon in beginning of a line
makes it all a comment line. Good programming practice dictates comment on
every line Examples
• MOV CX, 0 ; move 0 to CX
• Do not say something obvious
• MOV CX, 0 ; CX counts terms, initially 0
• Put instruction in context of program
• ; initialize registers
Applications:
13
Procedure:
Start Emu8086 by selecting its icon.
editor
Program 01:
org 100h
; 5 +10 = 15 (decimal) or
hex=0Fhorbin=00001111b
add al, bl
ret
Register Value
AX
BX
CS
IP
Program 02:
14
org 100h
mov al, 5 ; al = 5
add al, -3 ; al = 2
ret
Register Value
AX
BX
CS
IP
Program 03:
Org 100h
mov bl, 5 ; bl = 5
add bl, -3 ; bl = 2
ret
Register Value
AX
BX
CS
IP
Program 04:
Org 100h
mov al, 5
15
sub al, 1 ; al =
4
ret
Register Value
AX
BX
CS
IP
Program 05:
Org
100h
mov al,
7 mov
bl, 4
sub
al,bl ret
Register Value
AX
BX
CS
IP
16
Lab Session 03
OBJECTIVE:
Basic concept and functionality of Assembly Language.
THEORY:
ASSEMBLY LANGUAGE
USES:
Assembly language is used most often when either communicating with the
operating system or directly accessing computer hardware.
ASSEMBLER
An assembler is a program that converts source code programs from the assembly
language into machine language. The assembler can optionally generate a source- listing file
with line numbers, memory addresses, source code statements and a cross-reference listing of
symbols and variables used in a program.
The most popular assemblers for the Intel family are MASM (Microsoft Assembler), TASM
(Turbo Assembler).
LINKER
DEBUGGER
MICROPROCESSOR ARCHITECTURE
8086 MICROPROCESSOR:
17
We will be studying the assembly language for the microprocessor 8086.
8086 microprocessor is a 16-bit microprocessor (a 16-bit microprocessor can operate
on the 16-bits of data at a time) having 16- bit registers.
16-bit data bus and 20-bit address bus.
It can access 1 MB of total memory.
To write the assembly language programs for 8086 microprocessor, we are required to know
the internal organization at least programming model
PROGRAMMING MODEL:
The programming model of the 8086 through Pentium II is considered to be program visible
because its registers are used during application programming and are specified by the
instruction.Other registers are considered to be program invisible because they are not
addressed directly during application programming, but may be used indirectly during system
programming.
PROGRAM SEGMENTS
A machine language program consists of instructions (code) and data.
A data structure called Stack is used by the microprocessor to implement procedure
calls.
18
The program’s code, data and stack are loaded into different memory segments, called
code segment, data segment and the stack segment.
SEGMENT REGISTERS
The registers SP, BP, SI and DI normally points to (contain the offset address of)
memory location.
These registers can be used in arithmetic and other operations.
The SI register is used to point to memory locations in the data segment addressed by
DS.
This register takes its name from the string movement instructions, in which the
source string is pointed to by the SI register.
IP (INSTRUCTION POINTER)
The IP register contains the offset of the next instruction to be executed within the
current code segment.
The IP and CS registers combine to form the complete address of the next instruction.
This is a special register with individual bit positions assigned to show he status of the
CPU or the results of arithmetic operations.
19
Each relevant bit is given a name and others are undefined.
ASSEMBLY PROGRAM SYNTAX
Program instructions are placed in the code segment. Instructions are actually
organized into units called procedures. Every procedure starts with a line.
Exp:
Main Proc;
Main is the name of procedure and PROC is the directive identify the start of the procedure
Main Endp;
Main is again the name of the procedure and Endp is the direcitive ; identifies the
end of the procedure
STACK SEGMENT
The stack segment is used for temporary storage of addresses and data. If no stack
segment is declared, an error message is generated, so there must be a stack segment
even if the program doesn’t utilize the stack.
These segments begin with the directives .stack, .code, and .data
PROGRAM SYNTAX
TITLE first program syntax
.Model Small ;Specifies the memory model used
.Stack 100H ;allocate 100H memory locations for stack
.Data ;start of the data segment
; Data definitions here
ADB?
……..
.Code ;start of the code segment
Main Proc ;start of the first procedure
; instructions here
……
Main Endp ;end of the first procedure
; Other procedures here
End Main ;end of the complete assembly program
BASIC DIRECTIVES
20
Following are the description of commonly used directives;
MODEL: It is used to specify the memory model used for program to identify the size of
code and data segments.
DATA: Defines the data segments for data used in the program. Mark the beginning of the
data segment
CODE: Identifies the code segment which contains all the statements. Also .code marks the
beginning of the code segment.
RESTRICTIONS:
INPUT/OUTPUT OPERATIONS
We actually want to display single character from the microprocessor to the screen.
We don’t have any instruction that perform this operation like printf in C language.
We actually use the service routines, which are already designed and assembled programs
to perform the IO operations.
The BIOS routines are stored in ROM and interact directly with the I/O ports. We carry out
basic screen operations.
The DOS routines are the part of the operating system running into the system.
DOS routines can carry out more complex tasks, e.g. printing a character string.
21
DOS routines actually use the BIOS routines to perform direct I/O operations.
INT INSTRUCTION
Q:When we call a routine, it means we are invoking a large number of DOS functions
but which function?
INT 21h:
This can be used to invoke large number of DOS functions. A particular function is
requested by placing a function number in the AH register and invoking INT 21h.
Sample Program
OBJECT:
SOURCE CODE:
22
.data
mov ax,@data
mov ds,ax
int 21h
main endp
end main
STEPS TO FOLLOW:
ASSEMBLING:
→ →
1-Go to preferences Assembly write your file name with .asm extension.
23
Lab Session 4
Objective:
Basic Elements of Assembly Language, Understanding Constants, Identifiers, Directives
and Instructions.
Theory:
Integer Constants
An integer constant (or integer literal) is made up of an optional leading sign, one or more digits,
and an optional suffix character (called a radix ) indicating the number’s base:
If no radix is given, the integer constant is assumed to be decimal. Here are some examples
using different radixes:
h Hexadecimal
q/o Octal d
Decimal b Binary
Note: A hexadecimal constant beginning with a letter must have a leading zero to prevent
the assembler from interpreting it as an identifier.
Integer Expressions
An integer expression is a mathematical expression involving integer values and arithmetic
operators. The expression must evaluate to an integer, which can be stored in 32 bits (0 through
FFFFFFFFh). The arithmetic operators are listed in Table according to their precedence order, from
highest (1) to lowest (4).
Precedence refers to the implied order of operations when an expression contains two or more
24
operators. The order of operations is shown for the following expressions:
The following are examples of valid exp ressions and their value s :
2.
+3.0
-44.2E+05
26.E5
Character Constants
A character constant is a single character enclosed in single or double quotes. Examples are
'A'
"d"
String Constants
A string constant is a sequence of characters (including spaces) enclosed in single or double quotes:
25
'ABC'
'X'
'4096'
Reserved Words
Reserved words have special meaning in Assembler and can only be used in their correct context.
Identifiers
An identifier is a programmer-chosen name. It might identify a variable, a constant, a procedure, or
a code label. Keep the following in mind when creating identifiers:
Directives
A directive is a command embedded in the source code that is recognized and acted upon by the
assembler. Directives do not execute at runtime. Directives can define variables, macros, and
procedures. They can assign names to memory segments and perform many other
housekeeping tasks related to the assemble. Examples are:
.DATA
.MODEL
.CODE
The following example helps to show the difference between directives and instructions. The
DWORD directive tells the assembler to reserve space in the program for a doubleword variable. The
26
MOV instruction, on the other hand, executes at runtime, copying the contents of myVar to the
EAX register:
Although all assemblers for Intel processors share the same instruction set, they have completely
different sets of directives. The Microsoft assembler’s REPT directive, for example, is not recognized
by some other assemblers.
Defining Segments
One important function of assembler directives is to define program sections, or segments. The
.DATA directive identifies the area of a program containing variables:
.data
The .CODE directive identifies the area of a program containing executable instructions:
.code
The .STACK directive identifies the area of a program holding the runtime stack, setting its size:
.stack 100h
Instructions
An instruction is a statement that becomes executable when a program is assembled. Instructions
are translated by the assembler into machine language bytes, which are loaded and executed by
the CPU at runtime. An instruction contains four basic parts:
• Label (optional)
• Instruction mnemonic (required)
• Operand(s) (usually required) • Comment (optional)
This is the basic syntax:
Let’s explore each part separately, beginning with the label field.
Label
A label is an identifier that acts as a place marker for instructions and data. A label placed just before
an instruction implies the instruction’s address. Similarly, a label placed just before a variable implies
the variable’s address.
Data Labels
A data label identifies the location of a variable, providing a convenient way to reference the
variable in code. The following, for example, defines a variable named count: count DWORD 100
27
The assembler assigns a numeric address to each label. It is possible to define multiple data items
following a label. In the following example, array defines the location of the first number (1024).
The other numbers following in memory immediately afterward:
Code Labels
A label in the code area of a program (where instructions are located) must end with a colon
(:) character. Code labels are used as targets of jumping and looping instructions.
For example, the following JMP (jump) instruction transfers control to the location marked by
the label named target, creating a loop:
target:
mov ax,bx
...
jmp target
A code label can share the same line with an instruction, or it can be on a line by itself: L1:
mov ax,bx L2:
You can use the same code label more than once in a program as long as each label is unique within
its enclosing procedure. (A procedure is like a function.)
Instruction Mnemonic
An instruction mnemonic is a short word that identifies an instruction. In English, a mnemonic is a
device that assists memory. Similarly, assembly language instruction mnemonics such as mov,
add, and sub provide hints about the type of operation they perform. Following are examples of
instruction mnemonics:
Operands
Assembly language instructions can have between zero and three operands, each of which can be a
register, memory operand, constant expression, or input-output port. A memory operand is
specified by the name of a variable or by one or more registers containing the address of a variable.
A variable
28
name implies the address of the variable and instructs the computer to reference the contents
of memory at the given address. The following table contains several sample operands:
Comments
Comments are an important way for the writer of a program to communicate information about the
program’s design to a person reading the source code. The following information is typically
included at the top of a program listing: • Description of the program’s purpose
• Single-line comments, beginning with a semicolon character (;). All characters following the
semicolon on the same line are ignored by the assembler.
• Block comments, beginning with the COMMENT directive and a user-specified symbol. All
subsequent lines of text are ignored by the assembler until the same user-specified
symbol appears.
For example,
COMMENT !
COMMENT &
&
29
Of course, it is important to provide comments throughout your program, particularly where
the intent of your code is not obvious.
Procedure:
Start Emu8086 by selecting its icon.
Program
1: org
100h
bytea db
15d mov
al,bytea
ret
Modify Program 1 to include comments, labels and identifiers wherever necessary.
………………………………………………………………………………………………………
………………………………………………………………………………………………………
………………………………………………………………………………………………………
………………………………………………………………………………………………………
30
Fill in the following table for Program 1
Constants
Variables
Directive(s)
Mnemonic
Program 2:
org 100h
bytea db
15d byteb
db 06d
mov
al,bytea
mov
ah,byteb
ret
………………………………………………………………………………………………………
………………………………………………………………………………………………………
………………………………………………………………………………………………………
………………………………………………………………………………………………………
Constants
Variables
Directive(s)
Mnemonic
31
Program 3:
org 100h
worda dw
12d wordb
dw 13d
mov
ax,worda
mov
bx,wordb
ret
………………………………………………………………………………………………………
………………………………………………………………………………………………………
………………………………………………………………………………………………………
………………………………………………………………………………………………………
Constants
Variables
Directive(s)
Mnemonic
Program 4:
org 100h
worda dw
120d wordb
32
dw 121d mov
ax,worda mov
bx,wordb
ret
………………………………………………………………………………………………………
………………………………………………………………………………………………………
………………………………………………………………………………………………………
………………………………………………………………………………………………………
Constants
Variables
Directive(s)
Mnemonic
33
LAB SESSION 5
Objective
Assembling, editing, linking, and executing Assembly code examples
using EMU8086
Theory
Sample Code :
Org
100h
MOV AX,@DATA
MOV DS,AX
;subtract the
numbers
34
MOV AX,A ;AX has A again
SUB AX,B ; AX has A-B
MOV DIFFERENCE,AX ; DIFFERENCE = A - B
MAIN ENDP
END MAIN
RET
Procedure:
1. Calculate manually the value of variables seen in the data segment of the code above.
2. Write and run the code in EMU8086 environment.
3. Open „Emulator‟ window , run your code and click „vars‟ button to watch your variables. You
should see the screens in the Figure 1
35
MULTIPLICATION
DIVISION
36
Lab Session 06
Objective:
Theory:
In this Lab, the four basic operations of Boolean algebra: AND, OR, XOR, and NOT are introduced.
These operations can be carried at the binary bit level, using assembly language instructions. These
operations are also important at the Boolean expression level, in IF statements, for example. The
techniques used here could be used to manipulate control bits for hardware devices, implement
communication protocols, or encrypt data, just to name a few applications. The Intel instruction
set contains the AND, OR, XOR, and NOT instructions, which directly implement Boolean
operations on binary bits, shown.
•The Zero flag is set when the result of an operation equals zero.
•The Carry flag is set when an operation generates a carry out of the highest bit of the
destination operand.
•The Sign flag is a copy of the high bit of the destination operand, indicating that it is negative If set
and positive if clear. (Zero is assumed to be positive.)
•The Overflow flag is set when an instruction generates an invalid signed result.
•The Parity flag is set when an instruction generates an even number of 1 bits in the low byte
of the destination operand.
37
AND Instruction
The AND instruction performs a boolean (bitwise) AND operation between each pair of matching
bits in two operands and places the result in the destination operand:
AND destination,source
The following operand combinations are permitted:
AND reg,reg
AND reg,mem
AND reg,imm
AND mem,reg
AND mem,imm
The operands can be 8, 16 bits, and they must be the same size. For each matching bit in the two
operands, the following rule applies: If both bits equal 1, the result bit is 1; otherwise, it is 0. The
AND instruction lets you clear 1 or more bits in an operand without affecting other bits. The
technique is called bit masking, much as you might use masking tape when painting a house to cover
areas (such as windows) that should not be painted. Suppose, for example, that a control byte is
about to be copied from the AL register to a hardware device. Further, we will assume that the
device resets itself when bits 0 and 3 are cleared in the control byte. Assuming that we want to reset
the device without modifying any other bits in AL, we can write the following: and AL,11110110b ;
clear bits 0 and 3, leave others unchanged
For example, suppose AL is initially set to 10101110 binary. After ANDing it with 11110110, AL
equals 10100110:
mov al,10101110b
and al,11110110b ; result in AL = 10100110
OR Instruction
The OR instruction performs a boolean OR operation between each pair of matching bits in
two operands and places the result in the destination operand:
OR destination,source
The OR instruction uses the same operand combinations as the AND instruction: OR
reg,reg
OR reg,mem
OR reg,imm
OR mem,reg
OR mem,imm
The operands can be 8, 16, or 32 bits, and they must be the same size. For each matching bit
in the two operands, the output bit is 1 when at least one of the input bits is 1.
The OR instruction is particularly useful when you need to set 1 or more bits in an operand without
affecting any other bits. Suppose, for example, that your computer is attached to a servo motor,
which is activated by setting bit 2 in its control byte. Assuming that the AL register contains a control
byte in which each bit contains some important information, the following code only sets the bit in
position 2.
38
or AL,00000100b ; set bit 2, leave others unchanged
XOR Instruction
The XOR instruction performs a boolean exclusive-OR operation between each pair of matching
bits in two operands and stores the result in the destination operand: XOR destination,source
The XOR instruction uses the same operand combinations and sizes as the AND and OR instructions.
For each matching bit in the two operands, the following applies: If both bits are the same (both 0
or both 1), the result is 0; otherwise, the result is 1. The following truth table describes the boolean
expression x y:
A bit exclusive-ORed with 0 retains its value, and a bit exclusive-ORed with 1 is toggled
(complemented). XOR reverses itself when applied twice to the same operand. The following truth
table shows that when bit x is exclusive-ORed with bit y twice, it reverts to its original value:
NOT Instruction
The NOT instruction toggles (inverts) all bits in an operand. The result is called the one’s
complement.
NOT reg
NOT mem
Procedure:
39
Start Emu8086 by selecting its icon.
Program 01:
org 100h
Program 02:
org 100h
RET
Register Value Value
AX AH= AL=
Program 03:
org 100h
NOT AL ; AL = 1110010 0 b
RET
Register Value Value
AX AH= AL=
40
Program 04:
org 100h
RET
Register Value Value
AX AH= AL=
Exercise:
1. In the following instruction sequence, show the resulting value of AL where indicated, in binary:
MOV AL,01101111b
MOV AL,6Dh
MOV AL,00001111b
OR AL,61h ; c. AL=_________________
MOV AL,94h
2. Write a single instruction using 16-bit operands that clears the high 8 bits of AX and does
not change the low 8 bits.
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
3. Write a single instruction using 16-bit operands that sets the high 8 bits of AX and does not
change the low 8 bits.
41
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
4. Modify Program 01 to store two 16-bit values in AX and BX register and perform AND operation.
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
5. Modify Program 02 to store two 16-bit values in AX and BX register and perform OR operation.
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
42
Lab Session 7
Objectives
COMPARE and ASCII & BCD Conversion of unsigned numbers
Theory:
The dest operand can be in register or memory. The source operand can be in register,
memory or an immediate number.
CMP instruction compares two operands and changes the flags accordingly. Although CF,AF,SF,PF,ZF
and OF flags reflect the result of the comparison, only the CF and ZF are affected.
Compare operands CF ZF
Destination >source 0 0
Destination = source 0 1
MOV AX,CCCCH
43
SUB AX,AX
BCD(Binary Coded Decimal and ASCII (American Standard Code for Information Interchange)
Instructions
Binary representation of 0 to 9 (used by human beings) is called BCD. Digit BCD
There are two types of BCD numbers,
(1) unpacked BCD (2) packed BCD 0 0000
1 0001
Unpacked BCD: 1 byte is used to store 4 bit BCD code. E.g. 0000 1001 is 2 0010
unpacked BCD for 9.
3 0011
4 0100
Packed BCD: 1 byte is used to store two 4 bit BCD codes. E.g. 0101 1001 is
packed BCD for 59. More efficient in storing data. 5 0101
6 0110
7 0111
8 1000
9 1001
ASCII numbers:
44
4 34 011 0100 0000 0100
In order to convert ASCII to BCD the programmer must get rid of tagged “011” in the higher four bits of the
ASCII.
To do that each ASCII number is ANDed with ‘0000 1111’ (0FH).
ORG 0010H
UNPACK DB 10 DUP(?)
LOOP AGAIN
45
ASCII to packed BCD Conversion
To convert ASCII to packed BCD, it is first converted to unpacked BCD (to get rid of the 3)
and then combined to make packed BCD.
4 34 00000100
7 37 00000111 01000111 or
47H
ORG 0010H
SHL AH,CL ;shift left AH to get AH=40H OR AL,AH ;OR them to get packed BCD MOV VAL_BCD,AL
save the result
To convert packed BCD to ASCII, it must be first converted to unpacked and then the
unpacked BCD is tagged with 011 0000 (30H).
0010 1001 0000 0010 & 0000 1001 0011 0010 & 0011 1001
46
Ex:
….
MOV AL,VAL1_BCD
47
Lab Session 08
Objective:
Theory:
SHL Instruction
The SHL (shift left) instruction performs a logical left shift on the destination operand, filling the lowest
bit with 0. The highest bit is moved to the Carry flag, and the bit that was in the Carry flag is discarded:
The first operand in SHL is the destination and the second is the shift count:
48
SHL reg , CL SHL
mem, CL
x86 processors permit imm8 to be any integer between 0 and 255. Alternatively, the CL register can
contain a shift count. Formats shown here also apply to the SHR, SAL, SAR, ROR, ROL, RCR, and RCL
instructions.
Example
In the following instructions, BL is shifted once to the left. The highest bit is copied into the Carry flag
and the lowe+9st bit position is assigned zero:
Multiple Shifts
When a value is shifted leftward multiple times, the Carry flag contains the last bit to be shifted
out of the most significant bit (MSB). In the following example, bit 7 does not end up in the Carry
flag because it is replaced by bit 6 (a zero):
mov al,10000000b
Similarly, when a value is shifted rightward multiple times, the Carry flag contains the last bit to
be shifted out of the least significant bit (LSB).
SHR Instruction
The SHR (shift right) instruction performs a logical right shift on the destination operand, replacing
the highest bit with a 0. The lowest bit is copied into the Carry flag, and the bit that was previously in
the Carry flag is lost:
SHR uses the same instruction formats as SHL. In the following example, the 0 from the lowest bit in
AL is copied into the Carry flag, and the highest bit in AL is filled with a zero:
Multiple Shifts
In a multiple shift operation, the last bit to be shifted out of position 0 (the LSB) ends up in the
Carry flag:
mov al,00000010b
49
shr al,2 ; AL = 00000000b, CF = 1
If you shift binary 11001111 to the left by one bit, it becomes 10011110:
The SAR (shift arithmetic right) instruction performs a right arithmetic shift on its
destination operand:
The operands for SAL and SAR are identical to those for SHL and SHR. The shift may be
repeated, based on the counter in the second operand:
SAR destination,count
ROL Instruction
The ROL (rotate left) instruction shifts each bit to the left. The highest bit is copied into the Carry flag
and the lowest bit position. The instruction format is the same as for SHL:
Bit rotation does not lose bits. A bit rotated off one end of a number appears again at the other end.
Note in the following example how the high bit is copied into both the Carry flag and bit position 0:
50
Multiple Rotations
When using a rotation count greater than 1, the Carry flag contains the last bit rotated out of the
MSB position:
mov al,00100000b
ROR Instruction
The ROR (rotate right) instruction shifts each bit to the right and copies the lowest bit into the
Carry flag and the highest bit position. The instruction format is the same as for SHL:
In the following examples, note how the lowest bit is copied into both the Carry flag and the
highest bit position of the result: mov al,01h ; AL = 00000001b ror al,1 ; AL = 10000000b, CF =
1 ror al,1 ; AL = 01000000b, CF = 0
Multiple Rotations
When using a rotation count greater than 1, the Carry flag contains the last bit rotated out of the
LSB position:
mov al,00000100b
Procedure:
Start Emu8086 by selecting its icon.
Program 01:
Algorithm:
Shift all bits left, the bit that goes off is set to CF.
51
org 100h
SHL AL, 1
RET
AL
CF
Program 02:
Algorithm:
Shift all bits right, the bit that goes off is set to CF.
Org 100h
MOV AL, 00000111b
SHR AL, 1
RET
AL
CF
Program 03:
Algorithm:
Shift all bits left, the bit that goes off is set to CF.
Org 100h
MOV AL, 0E0h
SAL AL, 1
RET
AL
CF
52
Program 04:
Algorithm:
Shift all bits right, the bit that goes off is set to CF.
The sign bit that is inserted to the left-most position has the same value as before shift.
Org 100h
MOV AL, 0E0h
SAR AL, 1
MOV BL, 4Ch
SAR BL, 1
RET
AL
CF
Program 05:
Algorithm:
Org 100h
MOV AL, 1Ch
ROL AL, 1
RET
AL
CF
Program 06:
Algorithm:
53
Org 100h
MOV AL, 1Ch
ROR AL, 1
RET
AL
CF
Exercise:
1. Which instruction shifts each bit in an operand to the left and copies the highest bit into
both the Carry flag and the lowest bit position?
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
2. Which instruction shifts each bit to the right, copies the lowest bit into the Carry flag, and
copies the Carry flag into the highest bit position?
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
3. In the following code sequence, show the value of AL after each shift or rotate instruction
has executed:
Org 100h
mov al,0D4h
shr al,1 ; a. AL=__________________
mov al,0D4h
sar al,1 ; b. AL=__________________
mov al,0D4h
sar al,4 ; c. AL=__________________
mov al,0D4h
rol al,1 ; d. AL=__________________
54
Lab Session 09
Objective
Signed Number Arithmetic Operations
Theory:
Until now we have seen unsigned numbers where entire 8-bit or 16-bit operand was used
for the magnitude.
In order to represent positive and negative numbers signed numbers have been introduced.
The representation of signed numbers:
The MSB is set aside for the sign (+ or –) and the rest of the bits are used for the magnitude.
The sign is represented by 0 for positive (+) numbers and 1 for (–) negative numbers.
D7 D6 D5 D4 D3 D2 D1 D0
sign
Positive Numbers:
The range of positive numbers that can be represented as a signed byte operand is 0 to
+127.
55
size operand must be +5 0000 0101 used.
:: :::::::::::::::
Negative Numbers:
For negative signed numbers D7=1, but the magnitude operand is represented in 2’s
complement.
Although the assembler does the conversion, it is important to understand how the
conversion works.
1. 0011 0100
2. 1100 1011
3. 1100 1100 (CCH)
Ex: Show the representation of –12810
1. 1000 0000
2. 0111 1111
3. 1000 0000 (80H) Notice this is not negative zero (–0)
56
:: :::: :::: ::
–2 1111 1110 FE
–1 1111 1111 FF
0 0000 0000 00
+1 0000 0001 01
+2 0000 0010 02
:: :::: :::: ::
sign
Can be used for the representation of numbers between –32768 to +32767. Larger numbers
must be treated as a multiword numbers as unsigned numbers.
:: :::: :::: ::
57
0 0000 0000 0000 0000 0000
:: :::: :::: ::
DATA2 DB +70
… …
+ 96 0110 0000
+ 70 0100 0110
+166 1010 0110 According to the CPU this is –90, which is wrong.(OF=1, SF=1, CF=0)
As defined before max positive signed number for an 8-bit register is +127. Because +166
is greater than +127 the problem is arising. The overflow flag is set to inform the programmer that
there is erroneous result from the signed number operation above.
58
1. There is a carry out from D6 to D7, but no carry out from D7 (CF=0).
2. There is a carry out from D7 (CF=1), but no carry out from D6 to D7.
+ -2 1111 1110
According to the CPU, the result is +126, which is wrong. The error is indicated by the fact
that OF=1.
-2 1111 1110
+ -5 1111 1011
-7 1111 1001 OF=0, SF=1 (negatieve) , CF=1 : The result is correct since OF=0.
+7 0000 0111
59
+ +18 0001 0010
+25 0001 1001 OF=0, SF=0 (positive) , CF=0 : The result is correct since OF=0.
OF in 16-bit operations
In 16-bit signed number operations, OF is set to 1 in either of the cases:
1. There is a carry out from D14 to D15, but no carry out from D15 (CF=0).
2. There is a carry out from D15 (CF=1), but no carry out from D14 to D15.
8203 1000 0010 0000 0011 = –32,253 incorrect! OF=1, SF=1, CF=0
In order to avoid the problem of signed number operations we can sign extend the operand. Sign
extension copies the sign bit (D7) of the lower byte of a register to the upper byte bits of of the
register, or copies the sign bit of a 16-bit register into another register.
There are two commands used for sign extension.
CBW will copy D7 (the sign flag) of AL to all bits of AH. Notice that the operand is assumed
to be AL and the contents of AH is destroyed.
60
CBW ;now AH= 0000 0000 and AL=0110 0000
CWD will copy D15 (the sign flag) of AX to all bits of DX. Notice that the operand is assumed
to be AX and the contents of DX is destroyed.
Ex: MOV AX,–32766 ;AX = 1000 0000 0000 0010B or AX=8002H CWD
How can these instructions help correct the overflow error?
Lets give an example program which takes into consideration of correction of signed byte
addition operation.
DATA2 DB +70
RESULT DW ?
61
MOV BL,DATA2 ;get operand 2
MOV BX,AX
62
Lab Session 10
Objective
The main objective of this lab is to learn how to add and subtract signed number in
signed and magnitude, 1’s and 2’s complements.
Theory:
Note that subtraction is the same as addition with negative number. A process of addition in
signed and magnitude is as follows:
Activity 1.1: Perform the following operation in signed and magnitude in 8 bits: 17 + 35
00010001
00100011 +
00110100
Activity 1.2: Perform the following operation in signed and magnitude in 8 bits: 17 + 35
10010001
00100011 +
00010010
Activity 1.3: Perform the following operation in signed and magnitude in 8 bits: 23 44
10010111
10101100 +
11000011
63
1) Convert the two numbers to 1’s complement.
2) Perform addition.
3) If there is a carry out, add one to the sum.
00110100
11101110
00100011 +
100010001
1
00010010
11101000
11010011 +
110111011
1
10111100
00110100
Activity 3.2: Perform the following operation in 2’s complement in 8 bits: 17 + 35
11101111 +
64
00100011
100010010
00010010
11101001
11010100 +
110111101
10111101
10100000 carry
10101001
10110100+
101011101
01011101
Activity 3.5: Is the result in Activity 3.4 correct? If the answer is either yes or no, explain
why?
AC162
65
LAB Session 11
Objective
Theory
According to Intel manual IDIV means “integer division”. Note that all arithmetic
instructions of 8086 are for integer numbers. For real numbers (i.e. 5.32) 8087
coprocessor is used.
66
byte x byte AL register or memory AX1
Notes: 1) CF=1 and OF=1 if AH has part of the result, but if the result is not large enough to need
AH, the sign bit is copied to the unused bits and CPU makes CF=0 and OF=0 to
indicate that.
2) CF=1 and OF=1 if DX has part of the result, but if the result is not large enough to need DX,
the sign bit is copied to the unused bits and CPU makes CF=0 and OF=0 to indicate that.
Example shown below (Program 6-1) is an application of signed number arithmetic, which computes
the average of the following temperature measurements.
Ex: ……..
SIGN_DAT DB +13,-10,+19,+14,-18,-9,+12,-9,+16
ORG 0010H AVERAGE DW ? REMINDER DW ?
……..
67
MOV CX,AX ;save denominator in CX
…..
68
LAB Session 12
Objective
Theory:
• As the bits of the destination are shifted to the right into CF, the empty bits are filled with
the sign bit.
• SAR instruction can be used to divide a signed number by 2 as shown in the example below:
SAR AL,1 ;AL is shifted right arithmetic once to get: AL=1111 1011 =FDH =-5
SAL destination,count ;(shift arithmetic left) the same as SHL (shift left)
CF MSB LSB 0
SIGNED NUMBER COMPARISON
69
destination > source OF=SF or ZF=0 destination =
source ZF=1
destination < source OF=negation of SF
The mnemonics used to detect the conditions above are as follows:
STRING OPERATIONS
DI and SI registers are used to point the source and the destination operands. Until
now because the ES(Extra segment) is not defined within the programs both the DI and
SI registers were used the offset of DS(Data segment).
…..
MOV AX,DATSEG
MOV DS,AX
MOV ES,AX
…
then by default DI becomes the offset address of ES and SI becomes the offset address
of DS.
70
In each of the string instructions the operands can be byte or word. This is indicated by
adding B (byte) and W (word) to the end of the instruction mnemonic.
DF, the direction flag: To process operands in consecutive memory locations requires that
the pointer be incremented or decremented. DF in string operations is used to indicate if SI
and DI pointers will increment or decrement automatically.
It is the job of the programmer to specify the choice of increment or decrement by setting
the direction flag high or low.
CLD ;(clear direction flag) will reset (put to zero) DF, indicating that the string instruction should
increment the pointers (SI,DI) automatically. This automatic incrementation is sometimes referred as
autoincrement.
STD ;(set direction flag) will set (put to one) DF, indicating that the string instruction should
decrement the pointers (SI,DI) automatically.
71
REP prefix ; The REP (repeat) allows a string instruction to perform the operation repeatedly.
REP assumes that CX holds the number of times that the instruction should be repeated. As the
operation continues the CX register is decremented until it becomes zero.
REPE/ REPNE prefix ; REPE allows a string instruction to perform the operation repeatedly as long
as CX is not zero and the comparison gives equality. REPNE allows a string instruction to perform
the operation repeatedly as long as CX is not zero and the comparison gives inequality.
Ex: Using the string instructions, write a program that transfers a block of 20 bytes of data.
DATA1 DB ‘ABCDEFGHIJKLMNOPQRST’
ORG 30H
MOV AX,DATSEG
72
REP MOVSB ;repeat until CX becomes zero
After the transfer of every byte by the MOVSB instruction, both SI and DI registers are incremented
automatically once only (notice CLD).
The REP (repeat) prefix causes the CX counter to be decremented and MOVSB is repeated until CX
becomes zero.
Ex: Assuming that there is a spelling of “Europe” in an electronic dictionary and a user types in
“Euorope”, write a program that compares these two and displays the following message,
depending on a result: 1. If they are equal, display “The spelling is correct”
DAT_DIC DB ‘Europe’
DAT_TYPED DB ‘Euorope’
….
JMP DISPLAY
73
DISPLAY: MOV AH,09
INT 21H
74
Lab Session13
Objectives
Theory:
BIOS and DOS contain some very useful subroutines, which can be used through INT (interrupt)
instruction.
The INT instruction works like a FAR call. When it is invoked, it saves CS:IP and the flags on the stack and
goes to the subroutine associated with the interrupt.
INT 10H subroutines are in the ROM BIOS of the 80x86-based IBM PC.
Depending on the value put in AH many function associated with the manipulation
of screen text or graphics is performed.
Among these functions, clearing the screen, changing the cursor position, change
the screen color and drawing lines on the screen.
In normal text mode the screen is divided into 80 columns and 25 rows.
75
AH=06 Scroll window up
00,00 00,79
To clear the screen with INT 10H the following 00,4F(hex)
registers must contain certain values.
screen center
AH=06, AL=00, BH=07, CX=0000 12,39
0C,27 (hex)
DH=24, DL=79
76
DH = row
DL = column
Ex: Write the code to set the cursor position to row = 15 (= 0FH) and column = 25 (=19H).
Ex: Write a program segment to (1) clear the screen and (2) set the cursor at the center of
the screen.
77
AH=03 Read cursor position and size
Ex: MOV AH,03 ;option 03 of BIOS INT 10H (read cursor position and size)
After the execution of the above program: DH = current row, DL = current column
CX will provide info about the shape of
the cursor.
INT 21H subroutines are provided by DOS Operating system.
Depending on the value put in AH many functions such as inputting data from the keyboard
and displaying it on the screen can be performed.
The address in DX register is an offset address. Data is assumed to be the data segment.
INT 21H option 09 will display the ASCII data string pointed at by DX until it encounters the dollar sign
‘$’. Note that this option cannot display ‘$’ character on the screen.
Ex: ………………..
……………….
78
MOV DX,OFFSET DATA_ASC ;DX offset address of data
INT 21H option 01: Keyboard input with echo (inputting a single character with echo)
This function waits until a character is input from the keyboard, then echoes(displays) it to the monitor.
After the interrupt the character will be in AL.
This function waits until a character is input from the keyboard, then character is not displayed
(echoed) to the monitor.
After the interrupt the character will be in AL.
79
INT 21H option 0AH: Inputting a string of data from the keyboard
This function enables input a string of data from the keyboard and to store it in the data segment.
The register settings are: AH=0AH
DX= offset address of the string to be stored (called as the buffer area)
Buffer area must be defined in the data segment.
Ex: …………………….
ORG 0010H
……………………
The following shows the memory contents of offset 0010H: Before input is entered!!
06 00 FF FF FF FF FF FF
When the program is executed and the data is entered through the keyboard, the program will
not exit until the return key is pressed. Assume the data entered through the keyboard was,
“USA” ,RETURN>
The contents of memory locations starting at offset 0010H will be:
06 03 55 53 41 0D FF FF
U S A CR
80
The following is the step by step analysis:
0010=06 The size of the buffer must be defined in the first location
Note that the value 03 is generated and stored by DOS to indicate the number of characters
that entered.
81
Lab Session14
Objectives
Theory:
In the previous sections it was explained that INT 21H function AH=07, waits for the user to input a
character.
In some programs a task must run continuously while checking a key press? Such cases require to
use INT 16H.
In order to find out which key is pressed immediately after the above routine (INT 16H function
AH=01) the following routine (INT 16H function AH=00) must be called.
Upon return, AL contains the ASCII character of the pressed key.
82
Outline of the Lecture
Macros are predefined functions which involve a group of instructions to perform a special
task which can be used repeatedly.
For example:
• in order to print a string to the screen INT 21H together with 2 more instructions can
be used (3 lines of code).
• It doesn’t make sense to rewrite them every time they are needed.
• In order to reduce the time to write the code and reduce the length of the code macros can
be used.
• Macros allow programmer to define the task (set of codes to perform a specific job)
once only and invoke it whenever/wherever it is needed.
MACRO definition:
83
…
ENDM
Ex: Write a macro called STRING to which display a string of text to the monitor.
MOV AH,09
INT 21H
ENDM
The above code is the macro definition. You can invoke the above macro as follows:
STRING MESSAGE1 ; Assembler will invoke the macro to perform the defined function.
The Macros are defined outside the Code segment of an Assembly Language program and can be
invoked inside the code segment.
There can be comments in Macro definition
84
Example: the following program contains 3 Macro definitions which are: clear the screen, display a
string and set the cursor position.
;-------------------------------------------------
MOV
AX,0600H
MOV BH,07
MOV CX,0
ENDM
;-------------------------------------------------
MOV AH,09
INT 21H
ENDM
;-------------------------------------------------
MOV BH,00
MOV AH,02
MOV DH,ROW
MOV DL,COLUMN
INT 10H
ENDM
;-------------------------------------------------
.MODEL SMALL
.STACK 64
85
.DATA
.CODE
CLSCREEN
CURSOR 2,4
DISPSCREEN MESSAGE1
CURSOR 3,4
DISPSCREEN MESSAGE2
CURSOR 10,4
DISPSCREEN MESSAGE3
CURSOR 11,4
DISPSCREEN
MESSAGE4 MOV
AH,4CH INT
21H
END MAIN
• If a label is needed to be used in a macro (e.g. JNZ BACK) the label must be declared as
LOCAL to the macro.
• The LOCAL directive must be right after the MACRO directive.
• The local directive can be used to declare all names and labels at once as follows.
LOCAL name1 OR
86
;The Following Program Defines a Macro to multiply two words by repeated addition. Macro is used in
the main ;procedure below 3 times.
;-------------------------------------------------
LOCAL BACK ;
MOV BX,VALUE1
MOV DX,AX
BACK: ADD
AX,BX
ADC
DX,00
LOOP
BACK
MOV RESULT,AX
MOV RESULT+2,DX
ENDM
;-------------------------------------------------
.MODEL SMALL
.STACK 64
.DATA
RESULT1 DW 2 DUP(?)
RESULT2 DW 2 DUP(?)
RESULT3 DW 2 DUP(?)
.CODE
87
MAIN: MOV AX,@DATA
MOV DS,AX
MULTIPLY 2000,500,RESULT1
MULTIPLY
2500,500,RESULT2
MULTIPLY 300,400,RESULT3
MOV AH,4CH
Note: The reason why the LOCAL directive must be used is as follows: When a MACRO is
assembled in the program, the body of the MACRO is expanded as many times as the MACRO
function is invoked/called. This means that, for example in the above case, the same BACK label
will be expanded in the program 3 times. As a result, there will be the same BACK label in 3
different locations. This confuses the processor so it is an error.
However if LOCAL directive is used the label which is defined as LOCAL in a MACRO will be
the only one to be considered. So, in the above example when a jump to BACK label is needed it
will be the local BACK label not the other two.
88