Lab Manual Computer Organization & Assembly Language

Download as pdf or txt
Download as pdf or txt
You are on page 1of 89

LAB MANUAL

COMPUTER
ORGANIZATION &
ASSEMBLY LANGUAGE

1
Contents

Lab Session No. Objectives Page No.


Lab Session 01 Explore debugger using different 3
commands.
Lab Session 02 Assembly Language program using 10
Emu8086
Lab Session 03 Basic concept and functionality of 17
Assembly Language.
Lab Session 04 Basic Elements of Assembly Language, 24
Understanding Constants, Identifiers, Directives
and Instructions.
Lab Session 05 Assembling, editing, linking, and executing 34
Assembly code examples using EMU8086.
Lab Session 06 Familiar with the Logical instructions in 37
Assembly Language.

Lab Session 07 COMPARE and ASCII & BCD Conversion of 43


unsigned numbers.
Lab Session 08 To understand the use of Shift and 48
Rotate instructions.
To be able to differentiate between
Arithmetic shift and Logical shift
Lab Session 09 Signed Number Arithmetic Operations. 55
Lab Session 10 The main objective of this lab is to learn how to 63
add and subtract signed number in signed and
magnitude, 1’s and 2’s complements.
Lab Session 11 Signed Numbers and Signed Number 66
Operations (IMUL, IDIV).
Lab Session 12 Shift, Compare and String Operations for signed 69
numbers.
Lab Session 13 BIOS and DOS programming in 75
Assembly
BIOS INT 10H
DOS INT 21H

Lab Session 14 Keyboard Programming and MACRO in 82


Assembly Language

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.

TYPES OF PROGRAMMING LANGUAGE

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:

1. HIGH LEVEL LANGUAGES


A programming language that enables a programmer to write
programs that are more or less independent of a particular type of computer and are
designed to give a better program efficiency. Such languages are considered high-level
because they are closer to human languages.

2. LOW LEVEL LANGUAGES


These are designed to have both: a relatively good programming
efficiency and relatively good machine efficiency.

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.

REGISTERS CLASSIFICATION: The registers inside the microprocessor are classified


according to the function they perform.

In general, they are classified as


1. Data registers
2. Address registers
3. Segment register
4. Offset registers
5. Status register

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)

It is used to store the data also it serves as an address 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)

It is used in multiplication and division operations.


It is used in IO operation like DL in character output and DX in string output functions.

REGISTER SIZE:

We have three different sizes of registers:


8-bit register: AH, AL, BH, BL, CH, CL, DH, DL
16-bit registers: AX, BX, CX, DX, SP, BP, SI, DI, SS, DS, CS, ES, FS, GS, IP, FLAGS

BASIC MOV INSTRUCTION

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.

Syntax: MOV Destination, Sourc

EXAMPLES:

MOV AH, BL ; 8-bits register to register


MOV BX, AX ; 16-bits register to register
MOV byte1, BL ; 8-bit register to memory
MOV AX, word1 ;16-bit memory to register

SOME ARITHMETIC INSTRUCTIONS

4
ADD:Add the contents of source operand1 to source operand 2 and result store in
the source operand1.

Syntax: ADD Source operand1, Source operand2

EXAMPLE: ADD AL, BL

SUB: Subtract the contents of source operand1 to source operand 2 and result
store in the source operand1.

Syntax: SUB Source operand1, Source operand2

EXAMPLE: SUB AL,BL


DEBUG PROGRAM:

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

A Assemble symbolic instructions into machine code

D Display the contents of an area of memory

E Enter data into memory, beginning at a specific location

G Run the executable program in memory.

N Name a program

P Proceed, or execute a set of related instructions

5
Q Quit the debug program

R Display the contents of one or more registers

T Trace the contents of one instruction

U Unassembled machine code into symbolic code

W Write a program onto disk

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]

AX=0000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000

DS=0D62 ES=0D62 SS=0D62 CS=0D62 IP=0100 NV EI PL NZ NA PO NC

0D62:0100 2E CS:

0D62:0101 803ED3DF00 CMP BYTE PTR [DFD3],00 CS:DFD3=03

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

Sometimes instructions are used as follows:

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.

Creating basic assembler program:

a 100[enter]

mov ax,0002[enter]

mov bx,0004[enter]

add ax,bx[enter]

nop[enter][enter]

C:\>debug

-a 100

0D62:0100 mov ax,0002

0D62:0103 mov bx,0004

0D62:0106 add ax,bx

0D62:0108 nop

0D62:0109

Type the command "t" (trace), to execute each instruction of this program, example

-t

AX=0002 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000

DS=0D62 ES=0D62 SS=0D62 CS=0D62 IP=0103 NV EI PL NZ NA PO NC

0D62:0103 BB0400 MOV BX,000

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

AX=0002 BX=0004 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000

DS=0D62 ES=0D62 SS=0D62 CS=0D62 IP=0106 NV EI PL NZ NA PO NC

0D62:0106 01D8 ADD AX,BX

Type the command "t" (trace) to see the instruction add is executed, you will see the
follow lines:

-t

AX=0006 BX=0004 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000

DS=0D62 ES=0D62 SS=0D62 CS=0D62 IP=0108 NV EI PL NZ NA PE NC

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.

TASK#6: Perform the following debug activities

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.

Assembly Code Machine Code


mov ax,2864h
add ax,3749h
mov bx,ax
sub bx,2805

8
Nop

ACT 1.2 : How many bytes does it need to represent each instruction in binary.

Assembly Code No. Of Byte


mov ax,2864h
add ax,3749h
mov bx,ax
sub bx,2805
Nop

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

Register Mov ax,2864 Add ax,3794 Mov bx,ax


CS
IP
AX
BX

9
Lab Session 02
OBJECTIVE:

Assembly Language program using Emu8086

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.

x86 assembly language is a family of backward-compatible assembly languages, which


provide some level of compatibility all the way back to the Intel 8008. x86 assembly
languages are used to produce object code for the x86 class of processors. Like all assembly
languages, it uses short mnemonics to represent the fundamental instructions that the CPU in
a computer can understand and follow. Compilers sometimes produce assembly code as an
intermediate step when translating a high level program into machine code. Regarded as a
programming language, assembly coding is machine-specific and low level. Assembly
languages are more typically used for detailed and/or time critical applications such as small
real-time embedded systems or operating system kernels and device drivers.

How Does Assembly Language Relate to Machine Language?

Machine language is a numeric language specifically understood by a computer’s


processor (the CPU). All x86 processors understand a common machine language.

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.

How Do C++ and Java Relate to Assembly Language?

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.

Is Assembly Language Portable?

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

.Mnemonics and Opcodes:


Each x86 assembly instruction is represented by a mnemonic which, often combined with
one or more operands, translates to one or more bytes called an opcode; the NOP instruction
translate to 0x90, for instance and the HLT instruction translates to 0xF4.

A program written in assembly language consists of a series of (mnemonic) processor


instructions and meta-statements (known variously as directives, pseudo-instructions and
pseudo-ops), comments and data. Assembly language instructions usually consist of an
opcode mnemonic followed by a list of data, arguments or parameters.[4] These are
translated by an assembler into machine language instructions that can be loaded into
memory and executed. For example, the instruction below tells an x86/IA-32 processor to
move an immediate 8-bit value into a register. The binary code for this instruction is 10110
followed by a 3-bit identifier for which register to use. The identifier for the AL register is
000, so the following machine code loads the AL register with the data 01100001.[5]

10110000 01100001

This binary computer code can be made more human-readable by expressing it


in hexadecimal as follows.

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.

MOV AL, 61h ; Load AL with 97 decimal (61 hex)

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.

A program consists of statement per line. Each statement is an instruction or


assembler directive.

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

Examples of legal names

• COUNTER1
• @character
• SUM_OF_DIGITS
• $1000
• Done?
• .TEST
Examples of illegal names

• TWO WORDS
• 2abc
• A45.28

12
Operation field

Instruction

It describes operation’s function; e.g. MOV, ADD, SUB, INC.

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:

• Assembly language is typically used in a system's boot code, (BIOS on IBM-


compatible PC systems and CP/M), the low-level code that initializes and tests the
system hardware prior to booting the operating system, and is often stored in ROM.
• Some compilers translate high-level languages into assembly first before fully
compiling, allowing the assembly code to be viewed for debugging and optimization
purposes
• Assembly language is valuable in reverse engineering. Many programs are distributed
only in machine code form which is straightforward to translate into assembly
language, but more difficult to translate into a higher-level language

13
Procedure:
Start Emu8086 by selecting its icon.

Write the following code in the text

editor

Program 01:

org 100h

mov al, 5 ; bin=00000101b

mov bl, 10 ;bin=00001010b

; 5 +10 = 15 (decimal) or
hex=0Fhorbin=00001111b

add al, bl

ret

Press the emulate button and single step the code.

Observe the values in the registers.

Note the final values of registers in the following table

Register Value
AX
BX
CS
IP

Program 02:

14
org 100h

mov al, 5 ; al = 5

add al, -3 ; al = 2

ret

Observe the values in the registers.

Note the final values of registers in the following table

Register Value
AX
BX
CS
IP

Program 03:

Org 100h

mov bl, 5 ; bl = 5

add bl, -3 ; bl = 2

ret

Observe the values in the registers.

Note the final values of registers in the following table

Register Value
AX
BX
CS
IP

Program 04:

Org 100h

mov al, 5

15
sub al, 1 ; al =
4

ret

Observe the values in the registers.

Note the final values of registers in the following table

Register Value
AX
BX
CS
IP

Program 05:

Org
100h
mov al,
7 mov
bl, 4
sub
al,bl ret

Observe the values in the registers.

Note the final values of registers in the following table

Register Value
AX
BX
CS
IP

16
Lab Session 03
OBJECTIVE:
Basic concept and functionality of Assembly Language.

THEORY:

ASSEMBLY LANGUAGE

Assembly language is a machine specific programming language with


a one-to-one correspondence between its statements and the computer’s native machine
language. There are many different types of assembly language, each specific to a processor
or processor family. IBM-PC assembly language refers to instruction recognized by a number
of different microprocessors in the Intel family: 8086, 8088, 80186, 80286, 80386, 80486,
and Pentium.

USES:

Assembly language is used most often when either communicating with the
operating system or directly accessing computer hardware.

Secondly, assembly language is used to optimize certain critical areas of application


programs to speed up their runtime execution.

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

A companion program that combines individual files created by an assembler into a


single executable file

DEBUGGER

A program that provides a way for a programmer to trace the execution of a


program, and examine the contents of memory.

MICROPROCESSOR ARCHITECTURE

As assembly language is microprocessor specific, so before writing assembly


language programs it is required to know the architecture of that microprocessor.

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.

REGISTERS CLASSIFICATION: The registers inside the microprocessor are classified


according to the function they perform.
In general, they are classified as
Data registers
Address registers
Segment register
Offset registers
Status register
SOME GENERAL PURPOSE REGISTERS:
General purpose register we already defined in the Lab #1.

SEGMENT AND OFFSET REGISTERS:


Address registers store addresses of instructions and data in memory.
These values are used by the microprocessor to access memory locations.
Every memory location is actually identified by an address of 20-bit.
We are have the registers of 16-bits son that over 20-bit address (Physical Address) is
store into two parts in two 16-bit registers called segment number and offset
A memory segment is a block of 2^16 (64 K) consecutive memory bytes.
A segment number, called segment address (starting with 0) identifies each segment.
A segment number is 16 bits so the highest segment number is FFFFh.
Within segment, giving an offset number called the offset identifies a memory
location. This is the number of bytes from the beginning of the segment.
With a 64KB segment, the offset can be given as a 16-bit
number. The first byte in a segment has offset 0.
The last offset in a segment is FFFFh.

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

To keep track of the various program segments, the 8086 microprocessor is


equipped with four segment registers to hold the segment numbers.
The CS, DS and SS registers contain the code, data and stack segment numbers
respectively.
If a program needs to access a second data segment, it can use the ES (Extra Segment)
register.
POINTER AND INDEX 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 SP register contains the offset of the top of stack.


The SP and SS registers combine to form the complete address of the top of the stack.
BP (BASE POINTER)
The BP register contains an assumed offset from the SS register, as does the stack
pointer.
The BP register is often used by a subroutine to locate variables that were passed on
the stack by a calling program.
The BP register can be used for other segment unlike SP register.

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.

The DI register performs the same operation as the SI register.


The DI register acts as the destination for string movement instructions.
CONTROL 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.

FLAGS (FLAGS REGISTER)

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

Assembly language program consists of statements.


A statement is either an instruction to be executed when the program runs or a
directive for the assembler.
A program normally consists of three parts or segments.
DATA SEGMENT

Variables are declared in the data segment.


Each variable is assigned space in memory and may be initialized.
Exp:
A DW 3501H

A DW (?) ; un- initialized variable


CODE SEGMENT

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.

STACK: Defines the size of stack used in program

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.

PROC: Beginning of the procedure

ENDP: End of the procedure

END: End of assembly language program

BASIC MOV INSTRUCTION:

We already defined in the Lab#1

RESTRICTIONS:

Move between memory to memory is not allowed.


A number directly inside a segment register is not allowed.
Segment to segment registers, move is not allowed.

INPUT/OUTPUT OPERATIONS

To display a character is an output operation.

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.

There are two categories of service routines

Basic Input Output System (BIOS) routines


DOS routines

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

INT instruction is used to invoke a DOS or BIOS routine.

Syntax INT 10H ( BIOS routine call)

INT 21H (DOS routine call)

FUNCTION NUMBER & AH REGISTER

A routine is actually a collection of different functions.

A function is a set of basic instructions, used to actually perform the operation.

Q:When we call a routine, it means we are invoking a large number of DOS functions
but which function?

A:A particular function is requested by placing a function number in the AH register


and invoking INT 21H.

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.

For E.g. following functions can be used in our program:

Function No. Routine

02h Single character output

Single Character Output

MOV AH,02H ; display character function

MOV DL,’?’ ; character is ‘?’

INT 21h ; character will be displayed

Sample Program

OBJECT:

Title a program that displays single character on screen.

SOURCE CODE:

.model small ;specify memory model

.stack 64h ; specify size of stack

22
.data

.code ; start of the code segment

main proc far ; start of the first procedure

mov ax,@data

mov ds,ax

mov ah,02h ; display a single character

mov dl,'A' ; transfer A to register dl

int 21h ; DOS routine interrupt

mov ah,4ch ; exit DOS function

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:

[{+ | -}] digits [radix]

Radix may be one of the following (uppercase or lowercase):

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

If no radix is given, the integer constant is assumed to be decimal.

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 :

Real Number Constants


Real number constants are represented as decimal reals or encoded (hexadecimal) reals. A Decimal
real contains an optional sign followed by an integer, a decimal point, an optional integer that
expresses a fraction, and an optional exponent:

[ sign ] integer .[ integer ][ exponent ]


Following are the syntax for the sign and exponent:

Sign {+,-} exponent E[{+,-}] integer


Following are examples of valid real number constants:

2.

+3.0

-44.2E+05

26.E5

At least one digit and a decimal point are required.

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'

"Good night, Gracie"

'4096'

Reserved Words
Reserved words have special meaning in Assembler and can only be used in their correct context.

There are different types of reserved words:

• Instruction mnemonics, such as MOV, ADD, and MUL


• Register names
• Directives, which tell Assembler how to assemble programs
• Attributes, which provide size and usage information for variables and operands. Examples are
BYTE and WORD

• Operators, used in constant expressions

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:

• They may contain between 1 and 247 characters.


• They are not case sensitive.
• The first character must be a letter (A..Z, a..z), underscore (_), @ , ?, or $. Subsequent characters
may also be digits.
• An identifier cannot be the same as an assembler reserved word.

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:

myVar DWORD 26 ; DWORD directive mov


ax,myVar ; MOV instruction

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:

[label:] mnemonic [operands] [;comment]

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:

array DWORD 1024, 2048

DWORD 4096, 8192

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

• Names of persons who created and/or revised the program


• Program creation and revision dates
• Technical notes about the program’s implementation

Comments can be specified in two ways:

• 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 !

This line is a comment.

This line is also a comment.

We can also use any other symbol:

COMMENT &

This line is a comment.

This line is also a comment.

&

29
Of course, it is important to provide comments throughout your program, particularly where
the intent of your code is not obvious.

The NOP (No Operation) Instruction


The safest (and the most useless) instruction you can write is called NOP (no operation). It takes up
1 byte of program storage and doesn’t do any work. It is sometimes used by compilers and
assemblers to align code to even-address boundaries. In the following example, the first MOV
instruction generates three machine code bytes. The NOP instruction aligns the address of the third
instruction to a doubleword boundary (even multiple of 4):

00000000 66 8B C3 mov ax,bx

00000003 90 nop ; align next instruction 00000004


8B D1 mov edx,ecx

Procedure:
Start Emu8086 by selecting its icon.

Write the following codes in the text editor Modify

the codes according to given instructions

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

Modify Program 2 to include comments, labels and identifiers wherever necessary.

………………………………………………………………………………………………………

………………………………………………………………………………………………………

………………………………………………………………………………………………………

………………………………………………………………………………………………………

Fill in the following table for Program 2

Constants
Variables
Directive(s)
Mnemonic

31
Program 3:

org 100h

worda dw

12d wordb

dw 13d

mov

ax,worda

mov

bx,wordb

ret

Modify Program 3 to include comments, labels and identifiers wherever necessary.

………………………………………………………………………………………………………

………………………………………………………………………………………………………

………………………………………………………………………………………………………

………………………………………………………………………………………………………

Fill in the following table for Program 3

Constants
Variables
Directive(s)
Mnemonic
Program 4:

org 100h

worda dw

120d wordb

32
dw 121d mov

ax,worda mov

bx,wordb

ret

Modify Program 4 to include comments, labels and identifiers wherever necessary.

………………………………………………………………………………………………………

………………………………………………………………………………………………………

………………………………………………………………………………………………………

………………………………………………………………………………………………………

Fill in the following table for Program 4

Constants
Variables
Directive(s)
Mnemonic

33
LAB SESSION 5

Objective
Assembling, editing, linking, and executing Assembly code examples
using EMU8086

Theory

Practice 8086 Emulator


• Loading, verifying and saving machine code
• Executing instructions and tracing programs
• Writing a complete assembly program

Sample Code :

;;;;; This program does basic arithmetic operations for two


variables ;A and B are the variables

Org
100h

;;;;; Data segment starts


.DATA
A D
W
1
1
B DW
SU
M
DW
?
DIFFERENCE DW ?
MULTIPLICATION DW ?
DIVISION DW ?

;;;;; Code segment starts


.CODE

MAIN PROC FAR


;initialize DS

MOV AX,@DATA
MOV DS,AX

;add the numbers


MOV AX,A ;AX has A
ADD AX,B ;AX has A+B
MOV SUM, AX ;SUM = A+B

;subtract the
numbers
34
MOV AX,A ;AX has A again
SUB AX,B ; AX has A-B
MOV DIFFERENCE,AX ; DIFFERENCE = A - B

;multiply the numbers


MOV AX,A ;AX has A again
MOV BX,B
MUL BX ; AX has A*B
MOV MULTIPLICATION,AX ; MULTIPLICATION = A * B

;divide the numbers


MOV AX,A ;AX has A again
DIV BX ; AX has A/B and DX has modulus
MOV DIVISION,AX ; DIVISION = 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

Figure 1: Emulator window and Variables window

4. Fill the following table.


Variables Calculated Actual
A
B
SUM
DIFFERENCE

35
MULTIPLICATION
DIVISION

36
Lab Session 06

Objective:

Familiar with the Logical instructions in Assembly Language

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 CPU Flags


Boolean instructions affect the Zero, Carry, Sign, Overflow, and Parity flags.

•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.

The following operand types are permitted:

NOT reg

NOT mem

For example, the one’s complement of F0h is 0Fh:

mov al,11110000b not


al ; AL = 00001111b

Flags: No flags are affected by the NOT instruction.

Procedure:
39
Start Emu8086 by selecting its icon.

Write the following codes in the text editor

Fill the observation tables accordingly

Program 01:

org 100h

MOV AL, 'a' ; AL = 01100001b

AND AL, 11011111b ; AL = 01000001b ('A')


RET
Register Value Value
AX AH= AL=

Program 02:

org 100h

MOV AL, 'A' ; AL = 01 000001 b

OR AL, 00100000b ; AL = 01100001b ('a')

RET
Register Value Value
AX AH= AL=

Program 03:

org 100h

MOV AL, 00011011b

NOT AL ; AL = 1110010 0 b

RET
Register Value Value
AX AH= AL=

40
Program 04:

org 100h

MOV AL, 00000111b

XOR AL, 00000010b ; A L =


00000101b

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

AND AL,00101101b ; a. AL= _________________

MOV AL,6Dh

AND AL,4Ah ; b. AL=_________________

MOV AL,00001111b

OR AL,61h ; c. AL=_________________

MOV AL,94h

XOR AL,37h ; d. AL=_________________

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:

COMPARE OF UNSIGNED NUMBERS

CMP dest,source ;compare dest and source.

The operands themselves remain unchanged.

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

Destination < source 1 0

Flag settings of the CMP instruction.

Ex: DATA1 DW 235FH

MOV AX,CCCCH

CMP AX,DATA1 ;compare CCCC with


235F

JNC OVER ;jump if CF=0

43
SUB AX,AX

OVER: INC DATA1

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:

Key ASCII(Hex) Binary BCD (Unpacked)

0 30 011 0000 0000 0000

1 31 011 0001 0000 0001

2 32 011 0010 0000 0010

3 33 011 0011 0000 0011

44
4 34 011 0100 0000 0100

5 35 011 0101 0000 0101

6 36 011 0110 0000 0110

7 37 011 0111 0000 0111

8 38 011 1000 0000 1000

9 39 011 1001 0000 1001

• ASCII to BCD Conversion

ASCII to Unpacked BCD Conversion


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).

Ex: ASC DB ‘9562481273’

ORG 0010H

UNPACK DB 10 DUP(?)

MOV CX,5 ;CX is the loop counter

MOV BX,OFFSET ASC ;BX points to ASCII data

MOV DI,OFFSET UNPACK ;DI points to unpacked BCD data

AGAIN: MOV AX,WORD PTR [BX] ;move next 2 ASCII numbers to AX

AND AX,0F0F ;remove ASCII 3s (011)

MOV WORD PTR [DI],AX ;store unpacked BCD

ADD DI,2 ;point to next unpacked BCD data

ADD BX,2 ;point to next ASCII data

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.

Key ASCII Unpacked BCD Packed BCD

4 34 00000100

7 37 00000111 01000111 or
47H

ORG 0010H

VAL_ASC DB ‘47’ VAL_BCD DB


?

;reminder: the DB will put 34 in 0010H location and 37 in 0011H.

MOV AX,WORD PTR VAL_ASC ;AH=37 AL=34

AND AX,0F0FH ;mask 3 to get unpacked BCD

XCHG AH,AL ;swap AH and AL

MOV CL,4 ;CL=04 to shift 4 times

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

Packed BCD to ASCII Conversion

To convert packed BCD to ASCII, it must be first converted to unpacked and then the
unpacked BCD is tagged with 011 0000 (30H).

Packed BCD Unpacked BCD ASCII

29H 02& 09 32&39

0010 1001 0000 0010 & 0000 1001 0011 0010 & 0011 1001

46
Ex:

VAL1_BCD DB 29H VAL3_ASC


DW ?

….

MOV AL,VAL1_BCD

MOV AH,AL ;copy AL to AH. Now AH=29 and AL=29

AND AX,F00FH ;mask 9 from AH and 2 from AL


MOV CL,04 ;CL=04 for shift

SHR AH,CL ;shift right AH to get unpacked BCD


OR AX,3030H combine with 30 to get ASCII

XCHG AH,AL ;swap for ASCII storage convention MOV


VAL3_ASC,AX ;store the ASCII

47
Lab Session 08

Objective:

• To understand the use of Shift and Rotate instructions.


• To be able to differentiate between Arithmetic shift and Logical shift.

Theory:

Shift and Rotate Instructions


Along with bitwise instructions, shift instructions are among the most characteristic of assembly
language. Shifting means to move bits right and left inside an operand. x86 processors provide
a particularly rich set of instructions in this area, all affecting the Overflow and Carry flags.

Logical Shifts and Arithmetic Shifts

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:

If you shift 11001111 left by 1 bit, it becomes 10011110:

The first operand in SHL is the destination and the second is the shift count:

SHL destination, count


The following lists the types of operands permitted by this instruction:

SHL reg, imm8


SHL mem, imm8

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:

mov bl,8Fh ; BL = 10001111b shl


bl,1 ; CF = 1, BL = 00011110b

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

shl al,2 ; CF = 0, AL = 00000000b

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:

mov al,0D0h ; AL = 11010000b

shr al,1 ; AL = 01101000b, CF = 0

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

SAL and SAR Instructions


The SAL (shift arithmetic left) instruction works the same as the SHL instruction. For each shift count, SAL
shifts each bit in the destination operand to the next highest bit position. The lowest bit is assigned
0. The highest bit is moved to the Carry flag, and the bit that was in the Carry flag is discarded:

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:

mov al,40h ; AL = 01000000b rol


al,1 ; AL = 10000000b, CF = 0 rol
al,1 ; AL = 00000001b, CF = 1 rol
al,1 ; AL = 00000010b, CF = 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

rol al,3 ; CF = 1, AL = 00000001b

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

ror al,3 ; AL = 10000000b, CF = 1

Procedure:
Start Emu8086 by selecting its icon.

Write the following codes in the text editor

Fill the observation tables accordingly

Program 01:

Shift operand1 Left. The number of shifts is set by operand2.

Algorithm:

Shift all bits left, the bit that goes off is set to CF.

Zero bit is inserted to the right-most position.

51
org 100h

MOV AL, 11100000b

SHL AL, 1

RET
AL
CF

Program 02:

Shift operand1 Right. The number of shifts is set by operand2.

Algorithm:

Shift all bits right, the bit that goes off is set to CF.

Zero bit is inserted to the left-most position.

Org 100h
MOV AL, 00000111b
SHR AL, 1

RET

AL
CF

Program 03:

Shift Arithmetic operand1 Left. The number of shifts is set by operand2.

Algorithm:

Shift all bits left, the bit that goes off is set to CF.

Zero bit is inserted to the right-most position.

Org 100h
MOV AL, 0E0h
SAL AL, 1

RET

AL
CF

52
Program 04:

Shift Arithmetic operand1 Right. The number of shifts is set by operand2.

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:

Rotate operand1 left. The number of rotates is set


by operand2.

Algorithm:

shift all bits left, the bit that goes


off is set to CF and the same bit is
inserted to the right-most position.

Org 100h
MOV AL, 1Ch
ROL AL, 1

RET

AL
CF

Program 06:

Rotate operand1 right. The number of rotates is


set by operand2.

Algorithm:

shift all bits right, the bit that goes


off is set to CF and the same bit is
inserted to the left-most position.

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:

SIGNED NUMBER ARITHMETIC OPERATIONS

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.

Signed byte operands:

D7 D6 D5 D4 D3 D2 D1 D0

sign

If D7=0 the operand is


positive If D7=1 it is
negative.

Positive Numbers:
The range of positive numbers that can be represented as a signed byte operand is 0 to
+127.

Ex: 0 0000 0000 Note: If a positive number is


larger than +1 0000 0001 +127, a word-

55
size operand must be +5 0000 0101 used.
:: :::::::::::::::

+127 0111 1111

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.

To convert to negative number representation (2’s complement) follow the steps:

1. Write the magnitude of the number in 8-bit binary (no sign)


2. Invert each bit
3. Add 1 to it

Ex: Show how the computer would represent –5

1. 0000 0101 5 in 8-bit binary


2. 1111 1010 invert each bit
3. 1111 1011 add 1 (hex = FBH)
This is the signed number representation of –5 in 2’s complement.

Ex: Show how the computer would represent –34H

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)

Byte-sized signed number ranges:


Decimal Binary Hex

–128 1000 0000 80

–127 1000 0001 81

–126 1000 0010 82

56
:: :::: :::: ::

–2 1111 1110 FE

–1 1111 1111 FF

0 0000 0000 00

+1 0000 0001 01

+2 0000 0010 02

:: :::: :::: ::

+127 0111 1111 7F

Word-sized byte operands:

D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0

sign

If D15=0 the operand is


positive If D15=1 it is
negative.

Can be used for the representation of numbers between –32768 to +32767. Larger numbers
must be treated as a multiword numbers as unsigned numbers.

Decimal Binary Hex

–32 768 1000 0000 0000 0000 8000

–32 767 1000 0000 0000 0001 8001

–32 766 1000 0000 0000 0010 8002

:: :::: :::: ::

–2 1111 1111 1111 1110 FFFE

–1 1111 1111 1111 1111 FFFF

57
0 0000 0000 0000 0000 0000

+1 0000 0000 0000 0001 0001

+2 0000 0000 0000 0010 0002

:: :::: :::: ::

+32 766 0111 1111 1111 1110 7FFE

+32 767 0111 1111 1111 1111 7FFF

Overflow problem in signed number operations


When using signed numbers Overflow problem can arise after an operation. This
problem arises if the result in a register after an operation is too large. In such a case CPU sets
the OF (Overflow Flag). The programmer must consider the overflow case.

Ex: DATA1 DB +96

DATA2 DB +70

… …

MOV AL,DATA1 ;AL=0110 0000 (60H)

MOV BL,DATA2 ;BL=0100 0110 (46H)

ADD AL,BL ;AL=1010 0110 (AL=A6H=-90 invalid!)

+ 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.

When the OF is set in 8-bit operations


In 8-bit signed number operations, OF is set to 1 is either of the following two conditions
occurs:

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.

Ex: MOV DL,–128 ;DL=1000 0000 (80H)

MOV CH, –2 ;CH=1111 1110 (FEH)

ADD DL,CH ;DL=0111 1110 (DL=FEH=+126 invalid!)

-128 1000 0000

+ -2 1111 1110

–130 0111 1110 OF=1, SF=0, CF=1

According to the CPU, the result is +126, which is wrong. The error is indicated by the fact
that OF=1.

Ex: MOV AL,–2 ;AL=1111 1110 (FEH)


MOV CL,–5 ;CL=1111 1011 (FBH)

ADD CL,AL ;CL=1111 1001 (CL=F9H=-7 which is correct!)

-2 1111 1110
+ -5 1111 1011

-7 1111 1001 OF=0, SF=1 (negatieve) , CF=1 : The result is correct since OF=0.

Ex: MOV DH,+7 ;DH=0000 0111 (FEH)


MOV BH,+18 ;BH=0001 0010 (FBH)

ADD BH,DH ;BH=0001 1001 (CL=19H=+25 which is correct!)

+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.

Ex: MOV AX,62FH ;28 207 (MOV AX,+28807))

MOV CX,13D4H ; 5076

ADD AX,CX ;=33283 is expected result (out of range)

6E2F 0110 1110 0010 1111 + 13D4


0001 0011 1101 0100

8203 1000 0010 0000 0011 = –32,253 incorrect! OF=1, SF=1, CF=0

Avoiding erroneous results in signed number operations


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 ; Convert signed Byte to signed Word

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.

Ex: MOV AL,+96 ;AL = 0110 0000

60
CBW ;now AH= 0000 0000 and AL=0110 0000

Ex: MOV AL,– ;AL = 1111 1110

2 CBW ;now AH= 1111 1111 and AL=1111 1110

CWD ; Convert signed Word to signed Doubleword

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 = 0000 0001 0000 0100 or

AX,+260 CWD AX=0104H ;DX = 0000H and AX=0104H

Ex: MOV AX,–32766 ;AX = 1000 0000 0000 0010B or AX=8002H CWD

;DX = FFFFH and AX=8002H


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.

Ex: DATA1 DB +96

DATA2 DB +70

RESULT DW ?

MOV AH,0 ;AH=0

MOV AL,DATA1 ;get operand 1

61
MOV BL,DATA2 ;get operand 2

ADD AL,BL ;add them

JNO OVER ;jump if there is no overflow (OF=0) to OVER

MOV AL,DATA2 ;otherwise get operand 2 to

CBW ;sign extend it

MOV BX,AX

MOV AL,DATA1 ; get back operand 1 to

CBW ;sign extend it

ADD AX,BX ;add them

OVER: MOV RESULT,AX ;save the result

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:

Task 1: Signed and Magnitude System

Note that subtraction is the same as addition with negative number. A process of addition in
signed and magnitude is as follows:

1) Convert the two numbers to signed and magnitude.


2) If the two numbers have the same sign, find the sum of the magnitudes and keep the sign bit.
3) If the two numbers have the different signs, find the difference of the magnitudes and keep
the sign of the larger magnitude.

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

Task 2: 1’s Complement

A process of addition in 1’s complement is as follows:

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.

Activity 2.1: Perform the following operation in 1’s complement in 8 bits: 17 + 35


00010001
00100011 +

00110100

Activity 2.2: Perform the following operation in 1’s complement in 8 bits: 17 + 35

11101110
00100011 +

100010001
1
00010010

Activity 2.3: Perform the following operation in 1’s complement in 8 bits: 23 44

11101000
11010011 +

110111011
1
10111100

Task 3: 2’s Complement

A process of addition in 2’s complement is as follows:

1) Convert the two numbers to 2’s complement.


2) Perform addition.

Activity 3.1: Perform the following operation in 2’s complement in 8 bits: 17 + 35


00010001
00100011 +

00110100
Activity 3.2: Perform the following operation in 2’s complement in 8 bits: 17 + 35

11101111 +

64
00100011
100010010
00010010

Activity 3.3: Perform the following operation in 2’s complement in 8 bits: 23 44

11101001
11010100 +

110111101
10111101

Activity 3.4: Perform the following operation in 2’s complement in 8 bits:

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?

No, since Carry in is not the same as carry out.

Task 4: Hexadecimal System

A Hexadecimal number is a number with base sixteen.

Activity 4.1: Perform the following operation:


2A9F6 +
D6B58

Activity 4.2: Perform the following operation:


D6B58 -
2A9F6

AC162

65
LAB Session 11
Objective

Signed Numbers and Signed Number Operations (IMUL , IDIV).

Theory

SIGNED NUMBER DIVISION

IDIV ; (signed number division)

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.

Signed Division Summary:


Division Numerator Denominator Quotient Remainder

byte/byte AL = byte CBW register or memory AL AH

word/word AX = word CWD register or memory AX DX

word/byte AX = word register or memory AL1 AH

doubleword/word DXAX=doubleword register or memory AX2 DX

Notes: 1) Divide error interrupt if –127>AL>+127

2) Divide error interrupt if –32767>AX>+32767

SIGNED NUMBER MULTIPLICATION

IMUL ; (signed number multiplication)

According to Intel manual IMUL means “integer multiplication”.

Signed Multiplication Summary:


Multiplication Operand 1 Operand 2 Result

66
byte x byte AL register or memory AX1

word x word AX register or memory DXAX2

word x byte AL = byte CBW register or memory DXAX2

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 ?

……..

MOV CX,9 ;load counter

SUB BX,BX ;clear BX, used as an accumulator

MOV SI,OFFSET SIGN_DAT ;set up pointer

BACK: MOV AL,[SI] ;move byte into AL

CBW ;sign extend into AX

ADD BX,AX ;ADD to BX

INC SI ;increment pointer

LOOP BACK ;loop if not finished


MOV AL,9 ;move count to AL

CBW ;sign extend into AX

67
MOV CX,AX ;save denominator in CX

MOV AX,BX ;move sum to AX

CWD ;sign extend the sum

IDIV CX ;find the average

MOV AVERAGE,AX ;store the average (quoitent)

MOV REMINDER,DX ;store the reminder

…..

68
LAB Session 12
Objective

Shift, Compare and String Operations for signed numbers

Theory:

SAR destination,count ;(shift arithmetic right)

• 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:

Ex: MOV AL,-10 ;AL=-10=F6H=1111 0110

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

• SAL and SHL are performing exactly the same task.


SIGNED NUMBER COMPARISON

CMP destination ,source ;(compare destination with source)

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:

JG Jump Greater jump if OF=SF or ZF=0


JGE Jump Greater or Equal jump if OF=SF

JL Jump Less jump if OF=inverse of SF

JLE Jump Less or Equal jump if OF=inverse of SF or ZF=1

JE Jump Equal jump if ZF=1

STRING OPERATIONS

In 8086 family of microprocessors there are a group of instructions referred to as


the string instructions.

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).

When ES is defined as:

…..

ASSUME CS:CODSEG, DS:DATSEG, SS:STASEG,ES:DATSEG

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.

String Operation Summary:


Instruction Mnemonic Destination Source Prefix

move string byte MOVSB ES:DI DS:SI REP

move string word MOVSW ES:DI DS:SI REP

store string byte STOSB ES:DI AL REP

store string word STOSW ES:DI AX REP

load string byte LODSB AL DS:SI none

load string word LODSW AX DS:SI none

compare string byte CMPSB ES:DI DS:SI REPE/REPNE

compare string word CMPSW ES:DI DS:SI REPE/REPNE

scan string byte SCASB ES:DI AL REPE/REPNE

scan string word SCASW ES:DI AX REPE/REPNE

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.

In the data segment:

DATA1 DB ‘ABCDEFGHIJKLMNOPQRST’

ORG 30H

DATA2 DB 20 DUP (?)

In the code segment:

ASSUME CS:CODESEG,DS:DATSEG,SS:STASEG, ES:DATSEG

MOV AX,DATSEG

MOV DS,AX ; initialize the data segment

MOV ES,AX ; initialize the extra segment

CLD ;clear direction flag for autoincrement

MOV SI,OFFSET DATA1 ;load the source pointer

MOV DI,OFFSET DATA2 ;load the destination pointer

MOV CX,20 ;load the counter

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”

2. If they are not equal, display “Wrong Spelling”

;from the data segment

DAT_DIC DB ‘Europe’

DAT_TYPED DB ‘Euorope’

MESSAGE1 DB ‘The spelling is correct’,‘$’

MESSAGE2 DB ‘Wrong spelling’,‘$’

;from code segment:

….

CLD ;DF=0 for increment

MOV SI,OFFSET DAT_DIC ;SI=offset of DAT_DIC

MOV DI,OFFSET DAT_TYPED ;DI=offset of DAT_TYPED

MOV CX,06 ;load the counter

REPE CMPSB ;repeat as long as equal or until CX=0


JE OVER ;if ZF=1 then display MESSAGE1

MOV DX,OFFSET MESSAGE2 ;if ZF=0 then display MESSAGE2

JMP DISPLAY

OVER: MOV DX,OFFSET MESSAGE1

73
DISPLAY: MOV AH,09

INT 21H

74
Lab Session13
Objectives

• BIOS and DOS programming in Assembly


• BIOS INT 10H
• DOS INT 21H

Theory:

BIOS AND DOS PROGRAMMING IN ASSEMBLY


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 xx ;the interrupt number can be 00 – FFH (256 possible interrupts)

BIOS INT 10H PROGRAMMING

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.

Monitor screen in text mode

In normal text mode the screen is divided into 80 columns and 25 rows.

Top left = 00,00

Bottom left = 24,00 (decimal)

Bottom right = 24,79 (decimal)

• Clearing the screen ( INT 10H function 06H)

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

The code: MOV AH,06 ;AH=06 24,00 24,79


1 8, 00 ( hex ) 1 8, 4 F ( hex )
select the scroll function

MOV AL,00 ;number of lines to scroll (if AL=00 the entire


page)

MOV BH,07 ;the display attribute (BH=07 normal)

MOV CH,00 ;row value of the start point

MOV CL,00 ;column value of the start point


MOV DH,24 ;row value of the ending point

MOV DL,79 ;column value of the ending point


INT 10H ;invoke the interrupt

More efficient coding: MOV AX,0600H ;scroll entire screen

MOV BH,07 ;normal attribute

MOV CX,0000 ;start at 00,00

MOV DX,184FH ;end at 24,79 (hex=18,4F)

INT 10H ;invoke the interrupt

• INT 10H function 02: setting the cursor to a specific location

AH=02 Set cursor position

BH= page number (BH=00) ; 00 represents the current viewed page.

76
DH = row

DL = column

Ex: Write the code to set the cursor position to row = 15 (= 0FH) and column = 25 (=19H).

MOV AH,02 ;set cursor option

MOV BH,00 ;page 0

MOV DH,15 ;row position

MOV DL,25 ;column position

INT 10H ;invoke interrupt 10H

Ex: Write a program segment to (1) clear the screen and (2) set the cursor at the center of
the screen.

;clearing the screen

MOV AX,0600H ;scroll the entire page

MOV BH,07 ;normal attribute

MOV CX,0000 ;row and column of the top left

MOV DX,184FH ;row and column of the bottom right

INT 10H ;invoke interrupt 10H

;setting the cursor to the center of the screen

MOV AH,02 ;set cursor option


MOV BH,00 ;page 0

MOV DH,12 ;center row position

MOV DL,39 ;center column position INT 10H


;invoke interrupt 10H

• INT 10H function 03: get current cursor position

77
AH=03 Read cursor position and size

Ex: MOV AH,03 ;option 03 of BIOS INT 10H (read cursor position and size)

MOV BH,00 ;choose current (00) page

INT 10H ;interrupt10H routine

After the execution of the above program: DH = current row, DL = current column
CX will provide info about the shape of
the cursor.

DOS INT 21H PROGRAMMING


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.

INT 21H option 09: outputting a string of data to the monitor



INT 21H can be used to send a set of ASCII data to the monitor.

Register settings before INT 21H is invoked: AH=09
DX = the offset address of the ASCII
data to be displayed.


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: ………………..

DATA_ASC DB ‘I love MICROPROCESSORS’,’$’

……………….

MOV AH,09 ;option 09 to display string of data

78
MOV DX,OFFSET DATA_ASC ;DX offset address of data

INT21H ;invoke the interrupt

INT 21H option 02: outputting a single character to the monitor



To do that: AH=02 (AH is given 02)
DL = is loaded with the ASCII character to be displayed.

INT 21H is invoked.

Ex: MOV AH,02 ;option 02 displays one character


MOV DL,’Y’ ;DL holds the character to be displayed
INT 21H ;invoke the interrupt.

* This option can be used to display ‘$’ sign on the monitor.

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.

Ex: MOV AH,01 ;option 01 inputs one character

INT 21H ;after the interrupt, AL = input character (ASCII)

INT 21H option 07: Keyboard input without echo


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.

Ex: MOV AH,07 ;keyboard input without echo

INT 21H ;after the interrupt, AL = input character (ASCII)

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

DATA1 DB 6,?,6 DUP(FF) ;0010H=06, 0012H – 0017H=FF

……………………

MOV AH,0AH ;string input option of INT 21H

MOV DX,OFFSET DATA1 ;load the offset address of buffer

INT 21H ;invoke the interrupt


The following shows the memory contents of offset 0010H: Before input is entered!!

0010 0011 0012 0013 0014 0015 0016 0017

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:

0010 0011 0012 0013 0014 0015 0016 0017

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

0011=03 The keyboard was pressed 3 times, U, S, A (excluding the RETURN)

0012=55 the hex ASCII code for letter U

0013=53 the hex ASCII code for letter S

0014=41 the hex ASCII code for letter A

0015=0D the hex ASCII code for CR (carriage return)

Note that the value 03 is generated and stored by DOS to indicate the number of characters
that entered.

81
Lab Session14
Objectives

Keyboard Programming and MACRO in Assembly Language

Theory:

INT 16H Keyboard Programming:


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.

Checking a key press: AH=01

Ex: MOV AH,01 ;check for key press

INT 16H ;using INT 16H

After the execution, ZF=0,if there is a key press;


ZF=1 if there is no key press.

Which key is pressed?


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.

Ex: MOV AH,0 ;get key pressed

INT 16H ;using INT 16H


Upon return, AL contains the ASCII character of the pressed key.
82
Outline of the Lecture

• MACROS in Assembly Language


• MACRO definition

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:

name MACRO dummy1,dummy2,dummy3,…,dummyN

83

ENDM

Ex: Write a macro called STRING to which display a string of text to the monitor.

STRING MACRO DATA1

MOV AH,09

MOV DX,OFFSET DATA1

INT 21H

ENDM

The above code is the macro definition. You can invoke the above macro as follows:

; from the data segment

MESSAGE1 DB ‘What is your name?’,’$’

;from the code segment

STRING MESSAGE1 ; Assembler will invoke the macro to perform the defined function.

Using MACROS in an Assembly Language Program:


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.

;THE FOLLOWING PROGRAM USES MACROS

;-------------------------------------------------

CLSCREEN MACRO ;THIS MACRO CLEARS THE SCREEN

MOV
AX,0600H
MOV BH,07
MOV CX,0

MOV DX184FH INT 10H

ENDM

;-------------------------------------------------

DISPSCREEN MACRO STRING ;THIS MACRO DISPLAYS A STRING OF


DATA

MOV AH,09

MOV DX,OFFSET STRING

INT 21H

ENDM

;-------------------------------------------------

CURSOR MACRO ROW,COLUMN ;THIS MACRO SETS THE CURSOR


POSITION

MOV BH,00

MOV AH,02

MOV DH,ROW

MOV DL,COLUMN

INT 10H

ENDM

;-------------------------------------------------

.MODEL SMALL

.STACK 64

85
.DATA

MESSAGE1 DB ‘My name ’,’$’


MESSAGE2 DB ‘is Ali’,’$’

MESSAGE3 DB ‘What is ‘,’$’

MESSAGE4 DB ‘your name?’,’$’

.CODE

MAIN: MOV AX,@DATA


MOV DS,AX

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

LOCAL directive and its use in macros:

• 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

LOCAL name2 <==> LOCAL name1,name2,name3 LOCAL


name3

86
;The Following Program Defines a Macro to multiply two words by repeated addition. Macro is used in
the main ;procedure below 3 times.

;-------------------------------------------------

MULTIPLY MACRO VALUE1, VALUE2, RESULT

LOCAL BACK ;

;THIS MACRO COMPUTES RESULT = VALUE1 x VALUE2

MOV BX,VALUE1

MOV CX,VALUE2 SUB


AX,AX

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

INT 21H END MAIN

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

You might also like