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

COAL Lab 3

Summary
Items Description
Course Title Computer Organization and Assembly Language
Duration 4 Hours
Lab Title To learn how to use Registers, Input /Output, Display Strings in
Assembly
Operating System /Tool/Language Emu 8086
Objective Practice how to Input, Output and Display Characters as well as
strings
Objective

In this lab, you will learn the basic assembly of registers, flags and how you can input the data

Directives

Assembler Directives are not assembly language instructions as they do not generate any machine
code. They are special codes placed in the assembly language program to instruct the assembler to
perform a particular task or function. They can be used to define symbol values, reserve and
initialize storage space for variables and control the placement of the program code

• Tittle Directive
• MODEL directive
• .STACK directive
• .DATA directive
• .CODE directive

• Tittle Directive
It can be a maximum of 60 characters wide title.

For example:

TITLE print a Hello World

• MODEL directive

1 Prepared by Dr. Amad


Lab 2
Specify how many code and data segments are necessary for the program. There are many several
types of memory small, compact, medium, large, etc.

For example

.model small

It shows that’s no more than 64 k memory for code and 64k for data.
• STACK Directive

This directive is optional and is used to define the size of the stack segment.
For example
Syntax: .STACK <size>
.stack 100d (100 bytes stack segment)
.stack 100h (256 bytes stack segment)

• .DATA directive

Used to define data segment necessary for our program.


Variables are stored in those segments.
Data can be of different types like byte, word, double word or quad word.
DB - Define Byte
DW - Define Word
DD - Define Double Word
DQ - Define Quad Word
DT - Define Ten Bytes
message db “Hello World”,“$”
count db 20
<variable> <directive> < Value>

• .CODE directive:

Indicate the beginning of instructions i.e. the assembly code. The assembly language program is

2 Prepared by Dr. Amad


Lab 2
ended with an END directive.

INTERRUPTS

An interrupt is an event that causes the processor to suspend its present task and transfer control to
a new program called the interrupt service routine (ISR)
There are three sources of interrupts
• Processor interrupts
• Hardware interrupts generated by a special chip, for ex: 8259 Interrupt Controller.
• Software interrupts
Software Interrupt is just similar to the way the hardware interrupt actually works. The INT
Instruction requests services from the OS, usually for I/O. These services are located in the OS.
INT has a range 0 -> FFh. Before INT is executed AH usually contains a function number that
identifies the subroutine. The 8086 INT instruction can be used to cause the 8086 to do any one of
256 possible interrupt types.
The desired type is specified as part of the instruction.
Software interrupts produced by the INT instructions have many uses.
For example:

INT 3 instruction to insert breakpoints in programs for debugging.


Another use of Software interrupt INT is to test various interrupts-service procedures.
For example:

INT 0 instruction to send execution to a divide-by-zero interrupt - service procedure without


having to run the actual division program

REGISTERS

Registers are high-speed storage locations directly inside the CPU, designed to be accessed at a
much higher speed than conventional memory.

Types of registers

3 Prepared by Dr. Amad


Lab 2
General purpose registers

8086 CPU has 8 general purpose registers, each register has its own name:

AX - the accumulator register (divided into AH / AL).


BX - the base address register (divided into BH / BL).
CX - the count register (divided into CH / CL).
DX - the data register (divided into DH / DL).
SI - source index register.
DI - destination index register.
BP - base pointer.
SP - stack pointer.
Despite the name of a register, it's the programmer who determines the usage for each general-
purpose register.
The main purpose of a register is to keep a number (variable). The size of the above registers is 16
bit, it's something like: 0011000000111001b (in binary form), or 12345 in decimal (human) form.
4 general purpose registers (AX, BX, CX, DX) are made of two separate 8-bit registers.
For example
If AX= 0011000000111001b, then AH=00110000b and AL=00111001b. Therefore, when you
modify any of the 8-bit registers 16-bit registers are also updated, and vice-versa. The same is for
the other 3 registers, "H" is for high and "L" is for a low part.

Because registers are located inside the CPU, they are much faster than a memory. Accessing a
memory location requires the use of a system bus, so it takes much longer. Accessing data in a
register usually takes no time. Therefore, you should try to keep variables in the registers. Register
sets are very small and most registers have special purposes which limit their use as variables, but
they are still an excellent place to store temporary data of calculations.

SEGMENT REGISTERS

CS - points at the segment containing the current program.


DS - generally points at segment where variables are defined.
ES - extra segment register, it's up to a coder to define its usage.

4 Prepared by Dr. Amad


Lab 2
SS - points at the segment containing the stack.

Although it is possible to store any data in the segment registers, this is never a good idea. The
segment registers have a very special purpose pointing at accessible blocks of memory.
Segment registers work together with general purpose register to access any memory value. For
example if we would like to access memory at the physical address 12345h (hexadecimal), we
should set the DS = 1230h and SI = 0045h. This is good, since this way we can access much more
memory than with a single register that is limited to 16-bit values.
CPU makes a calculation of physical address by multiplying the segment register by 10h and
adding general purpose register to it (1230h × 10h + 45h = 12345h):
The address formed with 2 registers is called an effective address.
By default BX, SI and DI registers work with DS segment register; BP and SP work with SS
segment register.
Other general purpose registers cannot form an effective address! Also, although BX can form an
effective address, BH and BL cannot!

FLAG REGISTERS

STATUS FLAGS

• Allow the results of one instruction to influence later instructions.


• The arithmetic instructions use OF, SF, ZF, AF, PF, and CF

Zero Flag (ZF)

If the result is zero, zero flag is set. Once a flag is set, it remains in that state until another
instruction that affects the flags is executed Not all instructions affect all status flags.
add and sub affect all six flags
inc and dec affect all but the carry flag
mov, push, and pop do not affect any flags

Example

5 Prepared by Dr. Amad


Lab 2
Initially, assume ZF = 0
MOV AL,55H ;ZF is still zero
SUB AL,55H ;Result is 0
;ZF is set (ZF = 1)
Sign Flag (SF)

Indicates the sign of the result. Useful only when dealing with signed numbers
MOV AL,15
ADD AL,97
Clears the sign flag as sets the sign flag as the result is 112 (or 0111000 in binary)
MOV AL,15
SUB AL,97
sets the sign flag as the result is -82 (or 10101110 in binary)

Carry Flag (CF)

Records the fact that the result of an arithmetic operation on unsigned numbers is out of range. The
carry flag is set in the following example

MOV AL,0FH
ADD AL,0F1H

Overflow Flag (OF)


Indicates out-of-range result on signed numbers. Signed number counter part of the carry flag. The
following code sets the overflow flag but not the carry flag
MOV AL,72H ; 72H = 114D
MOV AL,0EH ; 0EH = 14D
8 bits -128 to +127
16 bits -32,768 to +32,767
32 bits -2,147,483,648 to +2,147,483,647

6 Prepared by Dr. Amad


Lab 2
Auxiliary flag (AF)

Indicates whether an operation produced a carry or borrow in the low-order 4 bits (nibble) of 8,
16,or 32
Bit operands (i.e. operand size doesn’t matter)
MOV AL,43 ; 43D = 0010 1011B
ADD AL,94 ; 94D = 0101 1110B
; 137D=1000 1001B

carry from lower 4 bits

MOV INSTRUCTION

Copies the second operand (source) to the first operand (destination). The source operand can be
an immediate value, general-purpose register or memory location.
The destination register can be a general-purpose register, or memory location.
Both operands must be the same size, which can be a byte or a word. These types of operands are
supported:

MOV REG, memory


MOV memory, REG
MOV REG, REG
MOV memory, immediate
MOV REG, immediate
REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP. memory: [BX],
[BX+SI+7], variable, etc...
immediate: 5, -24, 3Fh, 10001101b, etc...
For segment registers only these types of MOV are supported:
MOV SREG, memory
MOV memory, SREG
MOV REG, SREG
MOV SREG, REG
7 Prepared by Dr. Amad
Lab 2
SREG: DS, ES, SS, and only as second operand: CS.
REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP. memory: [BX],
[BX+SI+7], variable, etc...
The MOV instruction cannot be used to set the value of the CS and IP registers.

Example:

ORG 100h ; directive required for a COM program.


MOV AX, 0B800h ; set AX to hexadecimal value of B800h.
MOV DS, AX ; copy value of AX to DS.
MOV CL, 'A' ; set CL to ASCII code of 'A', it is 41h.
MOV CH, 01011111b ; set CH to binary value.
MOV BX, 15Eh ; set BX to 15Eh.
MOV [BX], CX ; copy contents of CX to memory at B800:015E
RET ; returns to operating system.

Addition of binary digits

Example # 01

.code
main proc
MOV AL,01001011b
MOV BL,00000011b
ADD AL,BL
MOV DL,AL

CX (Count Register)

• Contains the count for certain instructions e.g. shift count, rotate the number of bytes and a
counter with loop instruction.
• Can be accessed as 32 bit (ECX), 16 bit (CX) or 8 bit (CH or CL) register 32 bit
• General Purpose use in ADD, MUL, DIV, MOV
8 Prepared by Dr. Amad
Lab 2
• Special Purpose use in LOOP etc.

Example # 02

.code
main proc
MOV CL,12
MOV BL,65
AGN:INC BL
MOV DL,BL

Introduction to Input / Output

• In 8086 assembly language, we use a software interrupt mechanism for I/O.


• An interrupt signals the processor to suspend its current activity (i.e. running your
program) and to pass control to an interrupt service program (i.e. part of the operating
system).
• A software interrupt is one generated by a program (as opposed to one generated by
hardware).
• The 8086 INT instruction generates a software interrupt.
• For I/O and some other operations, the number used is 21h.
• A Specific number is placed in the register AH to specify which I/O operation (e.g. read a
character, display a character) you wish to carry out.
• When the I/O operation is finished, the interrupt service program terminates and program
will be resumed at the instruction following int.

Example # 03

Write a code fragment to display the character ‘a’ on the screen

; To Display the character 'a'

org 100h

9 Prepared by Dr. Amad


Lab 2
.data
…..
…..
…..
.code
main proc

Character Input from User

To get the input from the keyboard a subprogram at 1h will be called. First, 1h will be placed in
ah and then an interrupt 21h generated to call the subprogram. Finally, the character will be
placed in al register.

Example

mov ah,1h ; Keyboard input subprogram


INT 21H ; Call the subprogram to get character input and stored in al
Note:- Carriage Return ASCII (0DH) is the control character to bring the cursor to the start of a
line.
;display Return
mov dl, 0dh
mov ah, 2h
int 21h ; display Carriage Return
Line-feed ASCII (0Ah) is the control character that brings the cursor down to the next line on the
screen.
;display Line-feed
mov dl, 0ah
mov ah, 2h
int 21h ; display Line Feed

Example # 4: Write a program to read and display a character.

10 Prepared by Dr. Amad


Lab 2
; To read a character from keyboard
org 100h
.data
inMsg db "Enter a character ",'$' newline db 0dh,0ah,'$'
outMsg db "User enter character ",'$'

.code
main proc

;To display input message


mov ax, @data
mov ds, ax
mov dx, offset inMsg
mov ah, 9
INT 21H

;Get input from keyboard


mov ah,1h
INT 21H
mov bl, al

String Output

• A string is a list of characters treated as a unit.


• In 8086 assembly language, single or double quotes may be used to denote a string
constant.
• For Defining String Variables following 3 definitions are equivalent ways of defining a
string
"abc":
var1 db "abc" ; string constant

11 Prepared by Dr. Amad


Lab 2
var2 db ‘a’, ‘b’, ‘c’ ; character constants
var3 db 97, 98, 99 ; ASCII codes

o The first version simply encloses the string in quotes. (preferred method)
o The second version defines a string by specifying a list of the character
constants that make up the string.
o The third version defines a string by specifying a list of the ASCII codes that
make up the string
• In order to display string using MS-DOS subprogram (number 9h), the string must be
terminated with the ‘$’ character.
• In order to display a string we must know where the string begins and ends.
• The beginning of string is given by obtaining its address using the offset operator.
• The end of a string may be found by either knowing in advance the length of string or by
storing a special character at the end of the string.

EXERCISE # 3.1

Write a program that input a character from user 2 times. The program will display the character
entered by user 2 times on screen in newline (without using a loop).
Sample input vs output:
Please enter the character? A
you have entered the character A
you have entered the character A
Please enter another character? B
you have entered the character B
you have entered the character B

EXERCISE # 3.2

Write a program that input a character from the user is in lowercase, the program will convert it to
uppercase and will display it on the console after conversion.
Hint: - The ASCII codes for lowercase letters (a-z) are 97-122. In order to convert a lowercase
12 Prepared by Dr. Amad
Lab 2
letter to uppercase letter, just subtract 32 from its ASCII code.
Sample input vs output:
Please enter an alphabet in lower case? a
Upper case of the input is : A

EXERCISE # 3.3

Write an Assembly code which take an alphabet from user and print the next and previous
alphabets on the screen.
Sample input vs output:
Please enter an alphabet ? d
Previous alphabet in English grammar is : c
You have entered alphabet : d
Next alphabet in English grammar is : e

13 Prepared by Dr. Amad


Lab 2

You might also like