Coal Lab Manual
Coal Lab Manual
Laboratory Manual
COMPUTER ORGANIZATION
&&
ASSEMBLY LANGUAGE
Prepared By
UMT,Lahore Page 1
DS Department Computer Organization & Assembly language
LAB 1
Debugger
UMT,Lahore Page 2
DS Department Computer Organization & Assembly language
⮚ To study assembler
⮚ MASM
⮚ NASM
⮚ TASM
⮚ Emulator
⮚ Linker
⮚ DOS Debugger
⮚ Turbo Assembler
⮚ Assembly Language Syntax
⮚ How to represent number and Character
⮚ How to declare variable, arrays and define constants
⮚ Few basic Assembly Language Instruction
⮚ Lab Task
UMT,Lahore Page 3
DS Department Computer Organization & Assembly language
Theory
Assembly Language
An assembly language is a low-level programming language for computers, microprocessors,
Microcontrollers, and other integrated circuits. It implements a symbolic representation of the
binary machine codes and other constants needed to program a given CPU architecture. This
representation is usually defined by the hardware manufacturer, and is based on mnemonics that
symbolize processing steps (instructions), processor registers, memory locations, and other
language features. An assembly language is thus specific to certain physical (or virtual) computer
architecture.
Assembler
It is a system program which converts the assembly language program instructions into
machine executable instructions. For example: Microsoft Macro Assembler (MASM), Borland
Turbo Assembler (TASM), Open Source Netwide Assembler (NASM) etc.
MASM
The Microsoft Macro Assembler (MASM) is an x86 assembler for MS-DOS and Microsoft
Windows. It supports a wide variety of macro facilities and structured programming idioms,
Including high-level functions for looping and procedures. Later versions added the capability of
producing programs for Windows.
TASM
Turbo Assembler (TASM) is an x86 assembler package developed by Borland. It is used with
Borland's high-level language compilers, such as Turbo Pascal, Turbo Basic and Turbo C. The
Turbo Assembler package is bundled with the linker, Turbo Linker, and is interoperable with the
Turbo Debugger.
NASM
The Net wide Assembler (NASM) is an assembler and disassembler for the Intel x86
architecture. It can be used to write 16-bit, 32-bit (IA-32) and 64-bit (x86-64) programs. NASM
is considered to be one of the most popular assemblers for Linux and is the second most popular
assembler overall, behind MASM.
NASM was originally written by Simon Tatham with assistance from Julian Hall, and is
currently maintained by a small team led by H. Peter Anvin.
Emulators
UMT,Lahore Page 4
DS Department Computer Organization & Assembly language
EMU8086 is an emulator which emulates the behavior of 8086 machine.Emu8086 IDE contains
a built-in assembler which is compatible with the syntax of MASAM, TASAM and few other
assemblers.
It also contains a debugger which can be used to debug executable programs. In emu8086 one
can emulate each instruction of the program one by one and can view the value of registers, flags
and variables changed by instruction.
Linker
Linker or link editor is a program that takes one or more objects generated by a compiler and
combines them into a single executable program. When a program comprises multiple object
.files, the linker combines these files into a unified executable program
Linkers can take objects from a collection called a library. Some linkers do not include the whole
library in the output; they only include its symbols that are referenced from other object files or
.libraries
DOS Debugger
Debug” is a command in DOS, MS-DOS, OS/2 and Microsoft Windows (only x86 versions)“
which runs the program debug.exe (or DEBUG.COM in older versions of DOS). Debug can act
as an assembler, disassemble, or hex dump program allowing users to interactively examine
memory contents in assembly language, make changes, and selectively execute COM, EXE and
.other file types
UMT,Lahore Page 5
DS Department Computer Organization & Assembly language
TURBO ASSEMBLER
The basic steps required to create, assemble, link and debug programs using MS
Notepad and the Borland Turbo C tools TASM, TLINK and TDEBUG, respectively. Each step
requires certain command parameters and programs to be run in order to accomplish on-line
.debugging
Step 1
An assembly language program should be entered with any text editor and have the extension
.filename.asm
Step 2
Assembling a Program
Once you have entered this program you need to assemble it. This is done using the following
TASM command line syntax.
TASM Filename.asm
The .asm extension is assumed for the file. The files generated by this command will be;
Step 3
Linking a program
As a final step you need to run the Turbo linker (TLINK) to create an EXE (executable) file by
the name of filename.exe. This is done by entering the following command.
TLINK Filename.obj
The .obj extension is assumed for the file. The files generated by this command will be;
UMT,Lahore Page 6
DS Department Computer Organization & Assembly language
Step 4
Running DEBUG
TD, the Turbo Debugger, is run with the following command.
TD Filename.exe
Step 5
Execution of Program
For Example:
UMT,Lahore Page 7
DS Department Computer Organization & Assembly language
For example :
ARRAYS
In assembly language an array is just a sequence of memory bytes or words. For example to
define a 3 byte array with name arr_3byte whose initial values are 10h , 20h ,30h we can write
as:
Arr_3byte DB 10h,20h,30h
The name arr_3byte is associated with the first of these bytes i.e 10h , arr_3byte+1 with second
i.e 20h , arr_3byte+2 third i.e 30h.
Constant:
For example the following statement associates the name LF with 0Ah
LF EQU 0Ah now L could be used in place of constant 0AH which is ASCII value of
Line Feed Character.
UMT,Lahore Page 8
DS Department Computer Organization & Assembly language
Lab Task
Task # 1:
● Copy the notepad file and save the file in c:\temp folder with extension .asm i.e.
First_program.asm
● Then go to the command prompt by writing cmd
● Execute the .asm file saved above by writing the instruction tasm ,tlink
● Observe the output
UMT,Lahore Page 9
DS Department Computer Organization & Assembly language
LAB 2
● Memory Model
● Code Segment
● Data Segment
● Stack Segment
▪ Data variable
▪ Lab Task
UMT,Lahore Page 10
DS Department Computer Organization & Assembly language
Memory Models
The .MODEL directive specifies the memory model for an assembler module that uses the
simplified segment directives. The .MODEL directive must precede .CODE, .DATA, and
.STACK. The format of the .MODEL directive is:
TINY One segment. Thus both program code and data together must fit within
the same 64 Kb segment. Both code and data are near.
SMALL Program code must fit within a single 64 Kb segment, and data must fit
within a separate 64 Kb segment. Both code and data are near.
MEDIUM More than one code-segment. One data-segment. Thus code may be
greater than 64K.
COMPACT One code-segment. More than one data-segment. Thus data may be greater
than 64K.
LARGE More than one code-segment. More than one data-segment. No array larger
than 64K. Thus both code and data may be greater than 64K.
HUGE More than one code-segment. More than one data-segment. Arrays may be
larger than 64K. Thus both code and data may be greater than 64K
UMT,Lahore Page 11
DS Department Computer Organization & Assembly language
All program models but TINY result in the creation of exe-format programs. The TINY
model creates com-format programs.
Stack Segment
STACK_SEG defines the stack segment and controls the size of the stack. For example,
Code Segment
CODE_SEG marks the start of your program’s code segment. It contains executable instruction.
Main PROC
Main endp
CODE_SEG ends
Data Segments
DATA_SEG marks the start of your data segment. You should place your memory variables in
this segment. For example,
DATA_SEG ends
UMT,Lahore Page 12
DS Department Computer Organization & Assembly language
DATA VARIABLES
]name] Dx expression
Directive Dx:
Expression:
can be uninitialized : ?
Example:
DATAZ DB 21, 2
UMT,Lahore Page 13
DS Department Computer Organization & Assembly language
Signed integer
DD 4 Double Word Unsigned integer
Single precision floating point
number in the range about
±10^38
Far pointer in 16-bit mode, i.e.
address in the segment: offset
form 32-bit offset
Signed integer
DQ 8 Quad Word Unsigned integer
Double precision floating point
number in the range about
±10^308
● Declare variables
● Write code
The following exe-format display the string “This is my First Program” on screen
.model small
UMT,Lahore Page 14
DS Department Computer Organization & Assembly language
data_Seg ends
mov ax,data_Seg
mov ds,ax
mov ah,9
INT 21h
MOV AH,4CH
INT 21h
MAIN endp
stack_Seg ends
end main
UMT,Lahore Page 15
DS Department Computer Organization & Assembly language
LAB TASKS
Task # 1:
1. Copy the above code in notepad and save the file with extension .asm i.e.
First_program.asm
2. Then go to the command prompt by writing cmd
3. Execute the .asm file saved above by writing the instruction TASM ,TLINK
4. Observe the output
Task # 2:
1. Declare two or three strings in data segment of above code as str1 db “First String
”,13,10,”$”
2. Display these two or three strings one by one on the output screen by using the String
Display Output Instruction in Turbo Assembler (TASM) or Emu 8086
3. Observe the output
UMT,Lahore Page 16
DS Department Computer Organization & Assembly language
LAB 3
UMT,Lahore Page 17
DS Department Computer Organization & Assembly language
CPU communication with peripherals devices through I/O registers called I/O ports.
There are two instructions, IN and OUT, that access the ports directly. These Instructions are
used when a program needs to directly communicate with peripherals like when fast I/O is
needed.
These are two categories of I/O service routines
BIOS routines are stored in ROM and communicate directly with I/O ports.
The DOS routines actually use The BIOS routines to perform direct I/O operation. They Can
carry out the more Complex tasks; for example printing a character string, getting inputs from
keyboard etc.
INT instruction
To invoke DOS or BIOS routine, the INT (interrupt) instruction is used, it has the format
When an Interrupt number is a number that specifies a routine, for example INT 16h invokes a
BIOS routine that performs keyboard input. INT 21h may be used to invoke a large number of
DOS functions.
Interrupts Recommended:
1) INT 21H, Function 04CH terminate the code properly and return to the DOS Prompt.
2) INT 21H, Function 02H Display a number or character on the screen.
3) INT 21H, Function 09H Display the string on the screen
4) INT 21H, Function 01H Get Character input with Echo.
5) INT 21h, function 08H Get Character input without Echo.
UMT,Lahore Page 18
DS Department Computer Organization & Assembly language
Reads a character from the standard input device and echoes it to the standard output device.
If no character is ready it waits until one is available.
When a character is pressed, AL will contain its ASCII code if any other key is pressed, such as
.an arrow key or F0-F10 and so on, AL will contain 0
To display a character “A‟ on screen execute the following instruction, actually DL must contain
the ASCII code of Character that is to be displayed. In this example DL will contain ASCI code
of “A‟.
:Returns Nothing
Reads a character from the standard input device without copying it to the display. If no
.character is ready it waits until one is available
UMT,Lahore Page 19
DS Department Computer Organization & Assembly language
To display a String execute the following instruction, DX register must contain the starting offset
Address of string that to be displayed
OR
Returns: Nothing.
The string must be terminated by the $ character (24h), which is not transmitted. Any ASCII
code can be embedded within the string.
These four 16-bit registers can also be treated as eight 8-bit registers:
UMT,Lahore Page 20
DS Department Computer Organization & Assembly language
Assignment
x = 42 ; mov x,42
y = 24; mov y, 24
z = x + y; add z,x
add z ,y
In assembly language we carry out the same operation but we use an instruction to denote the
assignment operator ("=" in C).
.model small
stack_Seg ends
data_seg ends
UMT,Lahore Page 21
DS Department Computer Organization & Assembly language
; initialize DS
; You also can use “LEA DX,msg” instead of ” mov dx , offset msg” ; get offset address of msg
;terminate dos
Mov ah ,04Ch ;dos exit
Int 21h
Main endp
Code_seg ends
End main
UMT,Lahore Page 22
DS Department Computer Organization & Assembly language
Lab Task # 1:
Name: xyz
ID: 1234
Section: B
Lab Task # 2:
Lab Task # 3:
UMT,Lahore Page 23
DS Department Computer Organization & Assembly language
LAB 4
● Conditional Flags
o Carry flag
o Sign flag
o Overflow flag
o Zero flag
o Auxiliary flag
o Parity flag
● Control Flags
o Trap flag
o Interrupt flag
o Directional flag
● LAB TASK
UMT,Lahore Page 24
DS Department Computer Organization & Assembly language
Flag Register
The 8086 flag register contents indicate the results of computation in the ALU. It also contains
some flag bits to control the CPU operations.
A flag can only take on the values 0 and 1. We say a flag is set if it has the value 1.
The condition code flag register is the lower byte of the 16-bit flag register. The condition code
flag register is identical to 8085 flag register, with an additional overflow flag.
The control flag register is the higher byte of the flag register. It contains three flags namely
direction flag (D), interrupt flag (I) and trap flag (T).
X X X X O D I T S Z X A X P X C
C Y
S- Sign Flag: This flag is set, when the result of any computation is negative.
Z- Zero Flag: This flag is set, if the result of the computation or comparison performed by the
previous instruction is zero.
P- Parity Flag: This flag is set to 1, if the lower byte of the result contains an even number of
1’s.
C- Carry Flag: This flag is set, when there is a carry out of MSB in case of addition or a borrow
in case of subtraction.
T- Tarp Flag: If this flag is set, the processor enters the single step execution mode.
I- Interrupt Flag: If this flag is set, the mask-able interrupts are recognized by the CPU,
otherwise they are ignored.
D- Direction Flag: This is used by string manipulation instructions. If this flag bit is ‘0’, the
string is processed beginning from the lowest address to the highest address, i.e., auto
incrementing mode. Otherwise, the string is processed from the highest address towards the
lowest address, i.e., auto incrementing mode.
UMT,Lahore Page 25
DS Department Computer Organization & Assembly language
AC-Auxiliary Carry Flag: This is set, if there is a carry from the lowest nibble, i.e, bit three
during addition, or borrow for the lowest nibble, i.e, bit three, during subtraction.
O- Overflow Flag: This flag is set, if an overflow occurs, i.e, if the result of a signed operation
is large enough to accommodate in a destination register. The result is of more than 7-bits in size
in case of 8-bit signed operation and more than 15-bits in case of 16-bit sign operations, and then
the overflow will be set.
AF---Auxiliary Flag when there is an unsigned overflow for low nibble (4 bits)
IF—Interrupt Enable Flag - when this flag is set to 1 CPU reacts to interrupts from external devices
DF---Direction Flag this flag is used by some instructions to process data chains, when this flag
is set to 0 - the processing is done forward, when this flag is set to 1the
processing is done backward
UMT,Lahore Page 26
DS Department Computer Organization & Assembly language
LAB TASK
Execute the following Programs in Turbo Debugger and fill the output table with corresponding
value of register and give the reason if the value of flag is set to 1.
Mov ah,07Fh
Output:
Mov ax,1234
Mov bh,al AX =
AX =
Mov bl,ah BX =
BX=
Mov ax,5510
Sub al,2
Output:
AX =
AX =
FLAG BITS :
UMT,Lahore Page 27
DS Department Computer Organization & Assembly language
Mov ah,10
Output:
Mov bh,10
Sub ah,bh AX =
BX=
FLAG BITS :
Mov ax,0FFFEh
Output:
Sub al,2
Mov bx,02D8Ch AX =
Add bx,ax AX =
FLAG BITS :
BX=
BX=
FLAG BITS :
Mov al,100
Add al,50
Output:
AL=
FLAG BITS :
UMT,Lahore Page 28
DS Department Computer Organization & Assembly language
Mov ax,32760
Output:
Add ax, 50
AX=
AX=
FLAG BITS :
Mov ax,65530
Output:
Add ax, 80
AX=
AX=
FLAG BITS :
UMT,Lahore Page 29
DS Department Computer Organization & Assembly language
LAB 5
⮚ BIOS INT
⮚ INT 10h –services
⮚ Screen position
⮚ Setting the cursor position- goto (x,y)
⮚ Scroll page up - clrscr window
⮚ Scroll page down
⮚ Loops
⮚ Lab tasks
UMT,Lahore Page 30
DS Department Computer Organization & Assembly language
Interrupt Handler:
1- Hardware Interrupts: A signal from some device e.g. mouse, keyboard, etc.
The INT instruction in x8086 instruction invokes the respective ISR to perform the task
assigned. Number of interrupts supported by the instruction set is 256 but every interrupt can
have up to 256 sub interrupts. So the total number of interrupts are (256 * 256) = 65,536.
1) INT 21H, Function 04CH terminate the code properly and return to the DOS Prompt.
2) INT 21H, Function 02H Display a number or character on the screen.
3) INT 21H, Function 09H Display the string on the screen
4) INT 21H, Function 01H Get Character input with Echo.
5) INT 21h, function 08H Get Character input without Echo.
For example 21H interrupt number has 108 sub interrupts like 01H for character input, 02H for
character output etc.
UMT,Lahore Page 31
DS Department Computer Organization & Assembly language
The following are the IBM PC BIOS interrupts in which we will be Interested.
INTERRUPT FUNCTION
a. 5H Print screen
b. 10H Video I/O
c. 11H Equipment check
d. 14H Serial I/O
e. 1BH Keyboard break
f. 1CH Timer tick
g. 1FH Graphics character table
INTERRUPT 10H is the most useful BIOS interrupt to the programmer. INT 10H is the 17th
Interrupt vector in an x86 based computer system. It is a real mode ISR. Interrupt 10H is used to
provide the standard character output function as well as to provide some additional control over
the video display.
How to Use
Like 21H it has many subroutines. So the steps involved in using this routine are
UMT,Lahore Page 32
DS Department Computer Organization & Assembly language
● Input AH = 2
● DH = new cursor row (0-24)
● DL = new cursor column 0-79 for 80x25 display,0-39 for 40x25
● Output none
Example :
o Mov dh,10
o Mov dl,20
o Mov ah, 2
o Int 10h
● Input AH = 6/7
● AL= number of lines to scroll (0 = whole screen)
● BH = attribute for blank lines
● DL,DH =row column for lower right corner
CL,CH = row column for upper left corner of window
● Output none
⮚ Scrolling the screen up one line means to move each display line UP one row and insert
a blank line at the bottom of the screen. The previous top row disappears from the screen.
⮚ The whole screen or any rectangular area (window) may be scrolled. AL contains the
number of lines to scroll. If AL = 0, all the lines are scrolled and this clears the screen or
window.
⮚ Function 7 Same as function 6, but lines are scrolled down instead of up.
UMT,Lahore Page 33
DS Department Computer Organization & Assembly language
Looping instruction
A loop is a sequence of instructions that is repeated the number of times to repeat may be known
.in advance, or it may depend on condition
; Instructions
LOOP Again
Lab Tasks
Task # 1
Example
Enter a character: g
ghijklmnop
UMT,Lahore Page 34
DS Department Computer Organization & Assembly language
Task # 2:
Example:
Enter a character: A
Task # 3:
Example:
Enter a character: A
UMT,Lahore Page 35
DS Department Computer Organization & Assembly language
Task # 4
4
+ 4
==========
8
UMT,Lahore Page 36
DS Department Computer Organization & Assembly language
LAB 6
⮚ Lab Task
UMT,Lahore Page 37
DS Department Computer Organization & Assembly language
Addressing Modes
The way in which an operand is specified is called the Addressing Mode. The data addressing
:modes available in 8080/8088 are following
MOV DATA,BP ; moves the content of BP to the memory location associated with variable
DATA.AS as a result BP is copied into memory location DATA and DATA+1 in the data
.segment
UMT,Lahore Page 38
DS Department Computer Organization & Assembly language
In this mode, the offset address of the operand is contained in a register. We say that the register
acts as a pointer to memory location. The operand format is
For example suppose SI contain 0100h and the word at 0100h contains 1234h.to execute
MOV AX,[SI]
:The CPU execute
Memory location is specified by Base Registers. Effective address is sum of 16-bit offset in
.given instruction
Contents of base register BX or BP
Segment register is DS or SS
]E.g. MOV AX, 4[BX
MOV [BX], CX
Memory location is specified by Index Registers. Effective address is sum of 16-bit offset
given in instruction
Segment register is DS
Memory location specified by sum of Base and index Registers. Effective address is sum of
UMT,Lahore Page 39
DS Department Computer Organization & Assembly language
Segment register is DS
In register relative addressing the data in segment of memory are addresses by adding the
)displacement to the content a base or index register (BX, BP,DI,SI
✔ [register +displacement]
✔ [register]+displacement
✔ Displacement +[register]
✔ Displacement[register]
Relative mode through Base Register.7
LAB TASK
UMT,Lahore Page 40
DS Department Computer Organization & Assembly language
stack_Seg ends
num dw 4321h
data_seg ends
main proc
Mov ax,data_seg
Mov ds,ax
mov dx,2222h
Mov si,1000h
mov [si],bl
mov [si+100h],dl
Mov bx,1000h
mov [si+bx],3333h
mov [si+bx+100h],4444h
Mov ah,4ch
Int 21h
UMT,Lahore Page 41
DS Department Computer Organization & Assembly language
Proc Endp
Code_seg ends
End main
stack_Seg ends
num db 4321h
data_seg ends
main proc
Mov ax,data_seg
Mov ds,ax
mov dl,22h
Mov si,1000h
mov [si],bl
mov [si+100h],dl
UMT,Lahore Page 42
DS Department Computer Organization & Assembly language
Mov bx,1000h
mov [si+bx],33h
mov [si+bx+100h],44h
Mov ah,4ch
Int 21h
Proc Endp
Codes_seg ends
End main
Task # 2:
Example:
Suppose User enter a string “hello” then the output should be h e l l o
Task # 3:
Example:
Suppose User enter a string “hello” then the output should be h e l l o
UMT,Lahore Page 43
DS Department Computer Organization & Assembly language
LAB 7
UMT,Lahore Page 44
DS Department Computer Organization & Assembly language
Following lines of code will input a string from the keyboard and save it to a buffer whose offset
address must exist in register DX and the first location of the buffer must contain the size of the
.buffer. This function does not allow entering more characters than the specified buffer Size
When above code executes, first byte of buffer will contain size of buffer as specified in buffer
.declaration , 2nd byte will contain actual no of character read
.must be appended at the end of the buffer to print the contents of the buffer as string ‟ $“
Buffer Actual xx xx xx xx xx xx xx xx
length length
10 5 65 67 c6 c6 f6 d0 xx xx
UMT,Lahore Page 45
DS Department Computer Organization & Assembly language
number of chars from last input which may be recalled OR number of characters
01 1
actually read, excluding CR
02 n actual characters read, including the final carriage return
LAB TASK
Task # 1:
Example:
Suppose User enter a string “hello” then the output should be h e l l o
Task # 2:
Note: To convert a capital letter to a small one, use the following instruction:
Output:
UMT,Lahore Page 46
DS Department Computer Organization & Assembly language
Task # 3:
Output:
Task # 4:
Note: The above program should be done using LOOP. And the sum should not exceed 9.
Output:
UMT,Lahore Page 47
DS Department Computer Organization & Assembly language
LAB 8
Arithmetic OPERATIOn
▪ Arithmetic operation
● Add instruction
● Sub instruction
▪ LAB TASK
UMT,Lahore Page 48
DS Department Computer Organization & Assembly language
Arithmetic Operation :-
Pentium provides several arithmetic instructions that operate on 8, 16, 32 bit operands.
i. Addition ADD, ADC , AAA , INC
ii. Subtraction SUB, SBB , AAB , DEC
iii. Multiplication MUL ,IMUL
iv. Division DIV ,IDIV
Addition Instructions:-
ADD
Basic format
ADD destination, source
Perform simple integer instruction
Destination = destination +source
Five operand combinations are possible:
Add register, register
Add register, immediate
Add memory, immediate
Add memory, register
Add register, memory
ADC
Basic format
ADD destination, source
Perform integer Addition with Carry
Destination = destination +source +CF
INC
INC requires a single operand
INC destination
Destination = destination +1
AAA
ASCII adjusts for addition.
Main use is in arithmetic operations on BCD numbers.AAA has no Operand (AL is assumed to
be the operand). It is used after an add operation to adjust the BCD value in AL.
Basic format
AAA
UMT,Lahore Page 49
DS Department Computer Organization & Assembly language
Subtraction instruction:-
SUB
Basic format
SUB destination, source
Perform simple integer instruction
Destination = destination - source
SBB
Basic format
SBB destination, source
Perform integer Addition with Carry
Destination = destination - source - CF
DEC
Dec Requires a single operand
DEC destination
Destination = destination -1
AAS
ASCII adjusts for Subtraction.
AAS has no Operand (AL is assumed to be the operand). It performs BCD subtraction
adjustment on AL.
Basic format
AAS
LabTasks
Task # 1:
Task # 2:
1. Move a 1st 2-digit hexadecimal number to a register AL
2. Move a 2nd 2-digit hexadecimal number to a register BL
3. Add 8 –bit numbers using arithmetic instructions (Add,adc,aaa).
4. Display result in the following format:
“The sum of 53 and 36 is 89”
UMT,Lahore Page 50
DS Department Computer Organization & Assembly language
Task # 3:
1. Prompt User to Enter 1st (1-digit) number
2. Prompt User to Enter 2nd (1-digit) number
3. SUB two numbers using SUB,SBB,AAS instructions.
4. Display result in the following format:
“The sum of 37 and 32 is 5”
Task #4
Write 8086 Assembly Language programs for the following operations. The
operands and the results are to be stored in specific memory locations.
(a) Add three 16-bit unsigned integers (b)Add three 16-bit signed integers
(c)Compute the function:
X = |((A+B) – (C+D))| - E. Assume 16-bit unsigned integer variables.
UMT,Lahore Page 51
DS Department Computer Organization & Assembly language
LAB 9
Stack
STACK
● The stack is implemented for temporary storage of information such as data or addresses.
● The stack is 64KBytes long.
● The contents of the SP and BP registers are used as offsets into the stack segment
memory while the segment base value is in the SS register.
● Push instructions (PUSH) and pop instructions (POP)
● Top of the stack (TOS) and bottom of the stack (BOS)
● The 8088 can push word-wide data and address information onto the stack from registers
or memory.
● Many stacks can exist but only one is active at a time.
PUSH AX
POP AX
LAB TASK
TASK # 1
UMT,Lahore Page 52
DS Department Computer Organization & Assembly language
NOTE
OUTPUT:
TASK# 2
Output:
The output should like this
UMT,Lahore Page 53
DS Department Computer Organization & Assembly language
LAB 10
UMT,Lahore Page 54
DS Department Computer Organization & Assembly language
Unconditional Jump
The basic instruction that transfers control to another point in the program is JMP.
JMP label
Conditional Jump
If the condition for the jump is the true ,the next instruction to be executed is the one at
destination label ,which may precede or follow the jump itself.
The jump condition is often by CMP instruction .it has the form
The instruction compares destination and source by computing contents minus source contents.
The result is not stored, but the flags are affected.
Branching Structure
IF-THEN
IF condition is true
Then
END-IF
IF –THEN-ELSE
IF condition is true
Then
UMT,Lahore Page 55
DS Department Computer Organization & Assembly language
ELSE
END-IF
UMT,Lahore Page 56
DS Department Computer Organization & Assembly language
Lab Task
Task#1
1. Prompt a user to enter a string of numbers
2. Using conditional and unconditional jumps Find the Minimum number in an array
3. Display the result on console
Output :
Task#2
Note:
You may use these conditional jumps JE(jump equal), JG(jump greater), JL(jump low)
Output:
UMT,Lahore Page 57
DS Department Computer Organization & Assembly language
LAB 11
UMT,Lahore Page 58
DS Department Computer Organization & Assembly language
Pseudo code
Code in C
{ if (values[i] == target)
{Return i; } }
Return -1;}
Task # 1:
Output:
UMT,Lahore Page 59
DS Department Computer Organization & Assembly language
Pseudo code
For i from 1 to N
For j from 0 to N - 1
end func
Code in C
Task # 2:
Output:
UMT,Lahore Page 60
DS Department Computer Organization & Assembly language
HOME ASSIGNMENT
/* iMin is the index of the minimum element. Swap it with the current position */
if ( iMin != j ) {
swap(a[j], a[iMin]);
}
}
Task # 3:
Output:
UMT,Lahore Page 61
DS Department Computer Organization & Assembly language
LAB 12
⮚ Multiplication
⮚ Division
UMT,Lahore Page 62
DS Department Computer Organization & Assembly language
Arithmetic Operation :-
Pentium provides several arithmetic instructions that operate on 8, 16, 32 bit operands.
v. Multiplication MUL ,IMUL
vi. Division DIV ,IDIV
Multiplication Instructions:-
Basic format
MUL source
AAM
ASCII adjusts for Multiplication.
The AAM instruction performs the second step. It divides the content of AL by 10.
To multiply the BCD digits in AL and BL, and put the BCD product in AX
Basic format
MUL BL
UMT,Lahore Page 63
DS Department Computer Organization & Assembly language
AAM
DIVISION instruction:-
AAD
ASCII adjusts for Division.
The instruction AAD does step 1.it multiples AH by 10, adds the product to AL then clear Ah.
Basic format
▪ AAD
▪ Div BL
▪ AAM
UMT,Lahore Page 64
DS Department Computer Organization & Assembly language
LAB TASK
Task#1
4. Prompt user to enter a string of number
5. Using multiplication, convert this string into integer (Max. input 65536)
6. Prompt user to enter 2nd string, and convert it to integer as well
7. Add both integers using ADC instruction.
8. Using Division, convert this integer sum back to String (ASCII).
9. Print the result on console
Output:
Task#2
89 – 80: B
79 – 70: C
UMT,Lahore Page 65
DS Department Computer Organization & Assembly language
LAB 13
▪ Logical operation
● AND instruction
● OR instruction
● XOR instruction
● Not Instruction
● Test instruction
▪ LAB TASK
UMT,Lahore Page 66
DS Department Computer Organization & Assembly language
LOGICAL OPERATIONS :-
These instructions perform the specified logical operation (logical bitwise and, or, and exclusive
or, respectively) on their operands, placing the result in the first operand location.
i. AND operation
ii. OR operation
iii. XOR operation
iv. NOT operation
v. Test operation
AND Instruction:-
OR Instruction:-
UMT,Lahore Page 67
DS Department Computer Organization & Assembly language
XOR Instruction:-
The XOR instruction can be used to complement specific destination bits while preserving the
others.
01010110 XOR 0000 1111 = 01011001
NOT Instruction:-
TEST Instruction:
The test instruction performs an Operation of the destination with the source but does not change
the destination contents.
The purpose of the test instruction is to set the status flags.
Basics format
Test destination, source
Examining Bits
The test instruction can be used to examine individual bits in an operand
UMT,Lahore Page 68
DS Department Computer Organization & Assembly language
Lab Tasks
Task # 1:
Example:
Result Output: 10100010
Display on console
Task # 2:
1. Move a 4-digit hexadecimal number to a register
2. Separate the digits by Bit Masking
3. Display them individually
Example:
Suppose we have a number 2345h
Then by AND, SHR or SHL operations, separate 2, 3, 4 and 5 and then display the
result as
2
3
4
5
UMT,Lahore Page 69
DS Department Computer Organization & Assembly language
LAB 14
o Shift operations
o SHR instruction
o SHL instruction
o SAL instruction
o SAR Instruction
o Rotate operation
o ROR instruction
o ROL instruction
o RCL instruction
o RCR instruction
o LAB TASK
UMT,Lahore Page 70
DS Department Computer Organization & Assembly language
Shift Instruction Shift and rotate instructions shift the bits in the destination operand by one or
more positions either to the left or right
OPCODE destination, 1
OPCODE destination, CL
The first shifts by one position, the second shifts by N positions, where CL contains N(CL is
)the only register which can be used
Effect on flags
AF is undefined
The SHL (shift left) instruction shifts the bits in the destination to the left
Zeros are shifted into the rightmost bit positions and the last bit shifted out goes into CF
SHL Al, 4
The SHR (shift right) instruction shifts the bits in the destination to the right
Zeros are shifted into the leftmost bit positions and the last bit shifted out goes into CF
SHR AL, 4
To emphasize the arithmetic nature of the operation, the SAL (shift arithmetic left) is used in
instances where multiplication is intended
UMT,Lahore Page 71
DS Department Computer Organization & Assembly language
The SAR (shift arithmetic right) instruction can be used to divide an operand by powers of 2
SAR operates like SHR, except the MSB retains its original value
Rotate Instructions
Rotate Left
Rotate Right
The rightmost bit is shifted into the MSB and also into the CF
The instruction RCL shifts bits to the left. The MSB is shifted into CF
The rightmost bit is shifted into CF. CF is shifted into the MSB
LAB TASK
Task # 1
UMT,Lahore Page 72
DS Department Computer Organization & Assembly language
Note
To convert a number to its binary, you will have to do Bit Masking as done earlier i.e.
.AND, OR, SHR (shift right and SHL (shift left) operations
Output
Task # 2
Output
41h=01000001
Task #3
Verify the following instructions by executing them with DEBUG under the MS- DOS
.environment
Examine and modify the contents of the 8086’s internal register, and dedicated parts of the
memory.
Assemble instructions into memory of the PC system; TRACE each instruction to determine the
operation it performs
Operations
UMT,Lahore Page 73
DS Department Computer Organization & Assembly language
Instructions
UMT,Lahore Page 74
DS Department Computer Organization & Assembly language
LAB 15
▪ Procedure Declaration
▪ The CALL Instruction
▪ Executing a CALL
▪ The RET instruction
▪ Recursion
▪ LAB TASK
UMT,Lahore Page 75
DS Department Computer Organization & Assembly language
Procedure Declaration
● type can be NEAR (in same segment) or FAR (in a different segment) -- if omitted,
NEAR is assumed
Call name
Where name is the name of a procedure, and indirect
Call address_expression (not generally recommended)
Where address_expression specifies a register or memory location containing the
address of a procedure
Executing a CALL
● The return address to the calling program (the current value of the IP) is saved on the
stack
● IP get the offset address of the first instruction of the procedure (this transfers control to
the procedure)
● FAR procedures must process CS:IP instead of just IP
ret pop_value
is executed
PROC and ENDP are compiler directives, so they are not assembled into any real machine code.
Compiler just remembers the address of the procedure.
UMT,Lahore Page 76
DS Department Computer Organization & Assembly language
Here is an example:
ORG 100h
CALL m1
MOV AX, 2
m1 PROC
MOV BX, 5
RET ; return to caller.
m1 ENDP
END
Recursion
Recursion occurs when a procedure calls itself. The following, for example, is a recursive
procedure:
Recursive proc
call Recursive
ret
Recursive endp
UMT,Lahore Page 77
DS Department Computer Organization & Assembly language
Lab Task
Task # 1:
1. Clrscr ( )
2. Getch ( )
3. Putch ( )
4. Gets( )
5. Puts( )
6. Gotoxy ( )
7. Delay ( )
8. A to I ( )
9. I to A ( )
Task # 1:
Example:
Enter a number: 4
Task # 2:
UMT,Lahore Page 78
DS Department Computer Organization & Assembly language
Example:
Task # 1:
Output:
UMT,Lahore Page 79
DS Department Computer Organization & Assembly language
Task # 2:
Output:
Input a String: 5 6 7 9 10 12 13 14 17
UMT,Lahore Page 80
DS Department Computer Organization & Assembly language
Lab 18
⮚ Moving a String
⮚ Store String
⮚ Load String
⮚ Scan String
⮚ Compare String
⮚ Rep string
⮚ Lab Task
String Instructions
UMT,Lahore Page 81
DS Department Computer Organization & Assembly language
● In this instruction, the source string is in the Data Segment and destination string is in
Extra Segment.
REP (Repeat):
● CMPS compares two strings (of equal size), say String1 and String2, a byte or word at a
time.
● CMPSB for comparing Strings Byte at a time. CMPSW for comparing Strings Word at a
time. CMPSB and CMPSW are more common than CMPS
UMT,Lahore Page 82
DS Department Computer Organization & Assembly language
● STOS is used for creating a string in memory a byte or word at a time. AL/AX contents
copied to memory pointed by ES:[DI].
● SCAS is used for scanning a string in memory for a particular byte or word.
● Operand values are not changed. Flags are affected based on the result of subtraction.
● SCASB is used for scanning string bytes at a time. SCASW is used for scanning string
words at a time.
LAB TASK
Task #1
● Strcpy( st1,st2)
UMT,Lahore Page 83
DS Department Computer Organization & Assembly language
● Strncpy( st1,st2,n)
● Strcat(st1,st2 )
● Strlen ( st1)
● Substr(string, start, length)
● Strrev(st1,st2 )
● Strcmp(st1,st2)
● Deleting a Word from the String
● Insert a word in the string
● Searching a word from a String
Task# 2
Write an assembly language program that reads a string and displays the first 10
characters on the next line.
UMT,Lahore Page 84
DS Department Computer Organization & Assembly language
Lab 19
⮚ Create File
⮚ Open File
⮚ Close File
⮚ Read File
⮚ Write File
⮚ Delete File
⮚ Changing a File Attributes
⮚ The File Pointer
⮚ Lab Task
File Handling
UMT,Lahore Page 85
DS Department Computer Organization & Assembly language
● Input:
● AH = 3Ch
● DS:DX = address of filename which is an ASCII string
● CL = attribute
● Output:
● If successful, AX = file handle
● Error IF CF=1, Error Code in AX(3,4,or 5)
● When a file is created or opened in a program, DOS assigns it a unique number
called the FILE HANDLE.
● This number is used to identify the file.
● Input:
● AH = 3Dh
● DS:DX = address of filename which is an ASCII string
● AL=Access Mode
● Mode = 0 means open for reading
o 1 means open for writing
o 2 means open for both
● Output:
● If successful, AX = file handle
● Error IF CF=1, Error Code in AX(2,4, 5,12)
UMT,Lahore Page 86
DS Department Computer Organization & Assembly language
● Input:
● AH= 3EH
● BX = File Name
● Output:
● Error IF CF=1, Error Code in AX(6)
● Input:
● AH =4OH
● BX =File Handle
● CX =No Of Bytes To Write
● DS:DX =Segment: Offset Of Buffer
● Output:
● If successful then
● AX =Bytes Transferred. If AX<CX, error (Full Disk)
● Error IF CF=1, Error Code in AX(5,6)
● Input:
● AH = 3Fh
● BX = File Handle
● CX = Number Of Bytes To Read
● DS:DX = Memory Buffer Address
● Output:
● AX = Count Of Bytes Actually Read .
● If AX=0 or AX<CX, end of File Encounter.
● Error IF CF=1, Error Code in AX(5,6)
The file pointer is used to locate a position in a file. When the file is opened, the file pointer is
positioned at the beginning of the file. After a read operation, the file pointers indicate the next
bytes to read; after writing a new file, the file pointer is at EOF (end of file).
UMT,Lahore Page 87
DS Department Computer Organization & Assembly language
● Input :
● AH= 42h
● Al= movement code:
● 0 means move relative to beginning of file
● 1 means move relative to the current file pointer location
● 2 means move relative to end of file
● BX = File Handle
● CX: DX = number of bytes to move (signed).
● Output:
● DX:AX = new pointer location in bytes from the beginning of the file
● If CF = 1 , error Code in AX (1 , 6).
● Input :
● AH= 43h
● AL = 0 to get Attributes
● 1 to Change Attributes
● DX:DX = Address of File pathname as ASCIIZ String
● CX = new File Attributes (if AL=1)
● Output :
● If successful ,CX = Current file attributes (if AL=0)
● Error If CF = 1, error Code in AX (2, 3, or 5).
File Errors
There are many opportunities for error in INT 21h file Handling; DOS identifies each error by a
code number. If errors occur then CF is set and code appears in AX. The following list contains
more common file-handling errors.
UMT,Lahore Page 88
DS Department Computer Organization & Assembly language
LAB TASK
Task #1
● fcreate()
● fopen()
● fwrite()
● fread()
● fseek( )
● fclose( )
UMT,Lahore Page 89
DS Department Computer Organization & Assembly language
LAB 20
⮚ 8088/8086 Interrupts:
⮚ Interrupt Service Routine
⮚ Interrupt Vector Table
⮚ Differences between INT and CALL
⮚ Interrupt Mechanisms, Types, and Priority
⮚ Interrupts types
⮚ Terminate and Stay Resident Programs (TSR)
⮚ Storing/Hooks an Interrupt Vector in the Vector Table
⮚ Interrupt Get Vector
⮚ Interrupt Set Vector
⮚ TSR Sample Program
⮚ Interrupts Hooks Sample Program
⮚ Memory mapped I/O for text display sample Program
⮚ Lab Task
UMT,Lahore Page 90
DS Department Computer Organization & Assembly language
8088/8086 Interrupts:
An interrupt is an external event which informs the CPU that a device needs service
In the 8088 & 8086 there are total of 256 interrupts (or interrupt types)
INT 00
INT 01
INT FF
When an interrupt is executed, the microprocessor automatically saves the flags register (FR),
the instruction pointer (IP) and the code segment register (CS) on the stack and goes to a fixed
memory location.
UMT,Lahore Page 91
DS Department Computer Organization & Assembly language
Examples
A CALL FAR instruction can jump any location within the 1 MB address range but INT nn goes
to a fixed memory location in the Interrupt Vector Table to get the address of the interrupt service
routine
A CALL FAR instruction is used by the programmer in the sequence of instruction in the
program but externally activated hardware interrupt can come at any time
A CALL FAR saves CS:IP but INT nn saves Flags and CS:IP
At the end of the subroutine RET is used whereas for Interrupt routine IRET should be the last
statement.
Interrupts types
UMT,Lahore Page 92
DS Department Computer Organization & Assembly language
● Usually a non-resident program is a file, loaded from disk by DOS. Termination of such a
program is the passing control back to DOS.
● DOS frees all memory, allocated for and by this program, and stays idle to execute the
next program.
● Resident program passes control to DOS at the end of its execution, but leaves itself in
memory whole or partially.
● Such a program termination was called TSR - Terminate-and-Stay-Resident. So resident
programs are often called TSR.
● For example, TSR can watch key presses to get passwords, INT 13h sectors operations to
substitute info, INT 21h to watch and dispatch file operations and so on.
● TSR stays in memory to have some control over the processes. Usually, TSRs takes
Interrupt vectors to its code, so, when interrupt occurs, vector directs execution to TSR
Keep( ) in c
UMT,Lahore Page 93
DS Department Computer Organization & Assembly language
Getvect( )
Data part
Oldint dd(?)
Code part
MOV AX,0
MOV ES,AX
MOV AX,ES:[int # *4]
MOV word ptr oldint , AX
MOV Ax , Es:[int # *4+2]
MOV word ptr oldint[2], AX
Setvect( )
Data part
Oldint dd(?)
Code part
MOV AX , offset my proc
MOV ES:[int # *4], AX
MOV Es:[int # *4+2],CS
UMT,Lahore Page 94
DS Department Computer Organization & Assembly language
.model tiny
Code_seg segment ‘Code’
Main proc far
.stratup
Jmp start
St1 db “this is int 65h”,13,10,’$’
Oldint dd (?)
Start:
Mov AX,0
Mov ES,AX
Mov AX,ES:[ 65h *4]
Mov word ptr cs: oldint , AX
Mov Ax , Es:[ 65h *4+2]
Mov word ptr cs:oldint[2], AX
Mov ah,31h
Mov dx,500
Int 21h
Main endp
Myproc proc
Push ax
Push ds
Push dx
Mov ax,cs
Mov ds,ax
Mov ah,9
Mov dx,offset st1
Int 21h
Pop dx
Pop ds
Pop ax
Iret
Proc endp
Code_seg ends
End
UMT,Lahore Page 95
DS Department Computer Organization & Assembly language
.model tiny
Code_seg segment ‘Code’
Main proc far
.stratup
Jmp start
St1 db “this is int 8h”,13,10,’$’
Oldint dd (?)
Start:
Mov AX,0
Mov ES,AX
Mov AX,ES:[ 8h *4]
Mov word ptr cs: oldint , AX
Mov Ax , Es:[ 8h *4+2]
Mov word ptr cs:oldint[2], AX
Mov ah,31h
Mov dx,500
Int 21h
Main endp
Myproc proc
Push ax
Push ds
Push dx
Mov ax,cs
Mov ds,ax
Mov ah,9
Mov dx,offset st1
Int 21h
Pop dx
Pop ds
Pop ax
Proc endp
Code_seg ends
end
UMT,Lahore Page 96
DS Department Computer Organization & Assembly language
MOV AX,0B800h
MOV ES , AX
MOV DI,0
MOV word ptr ES:[DI],’U’
MOV word ptr ES:[DI],07h
MOV word ptr ES:[DI],’C’
MOV word ptr ES:[DI],70h
MOV word ptr ES:[DI],’P’
MOV word ptr ES:[DI],07h
LAB TASK
Task #1
Write a Program that intercepts keyboard interrupts. Your TSR program should call the
keyboard int9h thrice each time a key is pressed.
Task# 2
Write a TSR Program that intercepts INT 9h. Your program should convert any
occurrence of ‘1’ on screen into ‘9’ each time a key is pressed.
Task # 3
Write a TSR Program that intercepts INT 8h.Your TSR program should Display your
name with the background each time when timer interrupt is invoked.
UMT,Lahore Page 97