COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
LAB MANUAL 3
DEPARTMENT OF COMPUTING
1
© Copy Rights, Department of Computer Science, STMU Islamabad
All Rights Reserved
Prepared By: Mr. Shahid Raza
Revised By. Engr. Muhammad Haris Farooq
DEPARTMENT OF COMPUTING
3
List of Experiments
• Basic Instruction Format
• Basic Assembly language program Structure
• Arithmetic Instructions Programs
• How to take input and displaying output.
• How to print a Single character or multiple characters on screen
4
Lab Number: 3
Lab Title:
Basic Instruction Format, Basic Code Structure, Arithmetic
Instructions, Common instructions, taking input and
Displaying output
v
DEPARTMENT OF COMPUTING
Code Structure of Assembly Language, and Assembly language
instructions
Simple Assembly Language Programs 8086
The assembly language programming 8086 has some rules such as:
• The assembly level programming 8086 code must be written in upper case
letters
• The labels must be followed by a colon, for example: label:
• All labels and symbols must begin with a letter
• All comments are typed in lower case
• The last line of the program must be ended with the END directive
• 8086 processors have two other instructions to access the data, such as WORD
PTR – for word (two bytes), BYTE PTR – for byte.
Figure1. Op-Code and Operand
Op-code:
A single instruction is called as an op-code that can be executed by the CPU. Here the
‘MOV’ instruction is called as an op-code.
Operands:
A single piece data are called operands that can be operated by the op-code. Example,
subtraction operation is performed by the operands that are subtracted by the operand.
Syntax:
SUB b, c
47
Code structure
We can simply write the assembly code and emulate it in emu8086, and it’ll run.
However, without calling the exit statements or halt instruction, the program will
continue executing the next instruction in memory until it is halted by OS or emu8086
itself. The assembly code is saved in a .asm file type.
There are also some good practices like defining the model and stack memory size at the
very beginning. For small model, define data and code segment after the stack.
The code segment contains the code to execute. In the example structure given here, I
have created a main procedure (also called function or methods in other programming
languages), in which the code execution starts. At the end of it, I have called a specific
predefined statement with interrupt to indicate the code has finished executing.
.model small
.stack 100H
; Data segment
.data ; if there is nothing in the data segment, you can omit this line.
; Code segment
.code
main PROC
; Write your code here
exit:
MOV AH, 4CH
INT 21H
main ENDP
END main
The first line, .model small, defines the memory model to use. Some recognized
memory models are tiny, small, medium, compact, large, and so on.
The small memory model supports one data segment and one code segment that are
usually enough to write small programs. The following line .stack 100H defines the
stack size in hexadecimal numbers. The equivalent decimal number is 256. The lines
48
starting with, or part of the line after, ; are comments that the assembler ignores.
Assembly language instructions
A total of 116 instructions are available for the Intel 8086 microprocessor. All these
instructions with related examples are provided in this link.
We will discuss a few instructions necessary for understanding the later parts.
Copy data (MOV):
This instruction copies a byte (8-bit) or a word (16-bit) from source to destination.
Both operands should be of the same type (byte or word). The syntax of this
instruction is:
MOV destination, source
The destination operand can be any register or a memory location, whereas
the source operand can be a register, memory address, or a constant/immediate
value.
Addition (ADD) and Subtraction (SUB):
ADD adds the data of the destination and source operand and stores the result in
destination. Both operands should be of the same type (words or bytes), otherwise,
the assembler will generate an error. The subtraction instruction subtracts the
source from destination and stores the result in destination.
; Addition
ADD destination, source
ADD BL, 10
; Subtraction
SUB destination, source
SUB BL, 10
49
Label:
A label is a symbolic name for the address of the instruction that is given immediately
after the label declaration. It can be placed at the beginning of a statement and serve
as an instruction operand. The exit: used before is a label. Labels are of two types.
Symbolic Labels:
A symbolic label consists of an identifier or symbol followed by a colon (:). They
must be defined only once as they have global scope and appear in the object file's
symbol table.
Numeric Labels:
A numeric label consists of a single digit in the range zero (0) through nine (9)
followed by a colon (:). They are used only for local reference and excluded in the
object file's symbol table. Hence, they have a limited scope and can be re-defined
repeatedly.
; Symbolic label
label:
MOV AX, 5
; Numeric label
1:
MOV AX, 5
Compare (CMP):
This instruction takes two operands and subtracts one from the other, then sets OF,
SF, ZF, AF, PF, and CF flags accordingly. The result is not stored anywhere.
CMP operand1, operand2
The operand1 operand can be a register or memory address, and operand2 can be a
register, memory, or immediate value.
Jump instructions:
The jump instructions transfer the program control to a new set of instructions
50
indicated by the label provided as an operand. There are two types of jump
instructions.
Unconditional jump (JMP):
It directly jumps to the provided label.
Conditional jump:
`These instructions are used to jump only if a condition is satisfied and called after
CMP instruction. This instruction first evaluates if the condition is satisfied through
flags, then jumps to the label given as operand. It is pretty similar to if statements in
other programming languages. There are 31 conditional jump instructions available
in 8086 assembly language.
Taking User Input
The emu8086 assembler supports user input by setting a predefined value 01 or 01H
in the AH register and then calling interrupt (INT). It will take a single character from
the user and save the ASCII value of that character in the AL register. The emu8086
emulator displays all values in hexadecimal.
; input a character from user
MOV AH, 1
INT 21h ; the input will be stored in AL register
Displaying output
The emu8086 supports single character output. It also allows multi-character or string
output. Similar to taking input, we have to provide a predefined value in the AH
register and call interrupt. The predefined value for single character output is 02 or
02H and for string output 09 or 09H. The output value must be stored in the general-
purpose data register before calling interrupt.
; Output a character
MOV AH, 2
MOV DL, 35
INT 21H
51
; Output a string
MOV AH, 9
LEA DX, output
INT 21H
As shown in the code, for a single character output, we store the value in the DL
register because a character is one byte or 8 bits long. However, for string output it is
a bit different. We must load the effective address (address with offset) of the string
variable in the DX register using LEA instruction. The string variable must be defined
in data segment.
52
Lab Activities
Arithmetic and Logic Instructions
The 8086 processes of arithmetic and logic unit has separated into three groups such
as addition, division, and increment operation. Most Arithmetic and Logic
Instructions affect the processor status register.
The assembly language programming 8086 mnemonics are in the form of op-code,
such as MOV, MUL, JMP, and so on, which are used to perform the operations.
Assembly language programming 8086 examples
Addition
ORG0000h
MOV DX, #07H // move the value 7 to the register AX//
MOV AX, #09H // move the value 9 to accumulator AX//
Add AX, 00H // add CX value with R0 value and stores the result in AX//
END
Multiplication
ORG0000h
MOV DX, #04H // move the value 4 to the register DX//
MOV AX, #08H // move the value 8 to accumulator AX//
MUL AX, 06H // Multiplied result is stored in the Accumulator AX //
END
Subtraction
ORG 0000h
MOV DX, #02H // move the value 2 to register DX//
MOV AX, #08H // move the value 8 to accumulator AX//
SUBB AX, 09H // Result value is stored in the Accumulator A X//
END
MOV Ah, 0ch
MOV Bl, 08h
sub ah, bl
53
Division
ORG 0000h
MOV DX, #08H // move the value 3 to register DX//
MOV AX, #19H // move the value 5 to accumulator AX//
DIV AX, 08H // final value is stored in the Accumulator AX //
END
1. Write a Program for Read a Character from The Keyboard
MOV ah, 1h //keyboard input subprogram
INT 21h // character input
// character is stored in al
MOV c, al //copy character from alto c
2. Write a Program for Reading and Displaying a Character
MOV ah, 1h // keyboard input subprogram
INT 21h //read character into al
MOV dl, al //copy character to dl
MOV ah, 2h //character output subprogram
INT 21h // display character in dl
3. Write a Program For Reading and Displaying a Character (Student Name).
.model small
.stack 100h
.data
name db 'Enter your name: $'
output db 0ah, 0dh, 'Your name is: $'
buffer db 20 dup('$') ; buffer to store the name input
.code
main:
; Initialize data segment
mov ax, @data
mov ds, ax
; Display prompt for entering the name
mov ah, 09h
54
lea dx, name
int 21h
; Read the name from keyboard
mov ah, 0Ah
lea dx, buffer
int 21h
; Display the output message
mov ah, 09h
lea dx, output
int 21h
; Display the entered name
mov ah, 09h
lea dx, buffer + 1 ; skip the first byte which stores the buffer length
int 21h
; Terminate program
mov ah, 4Ch
int 21h
end main
4. Your Task is to execute the provided Assembly programs in EMU 8086, and
carefully document the output, noting any changes in registers, memory
operations, and any displayed results during execution
ADD
MOV AX, 1234H
MOV BX, 5678H
ADD BX, AX
MOV AX, 1234H
MOV BX, 5678H
ADD BX, AX
ADD BH, 24H
INC
MOV AX, 5679h
INC AX
ADC
MOV BX, 1234h
55
Add BX, 0F450h
MOV CX, 4502h
ADC BX, CX
AAA
MOV AX, 34H ; 4
MOV BX, 35H ; 5
Add AX, BX
AAA
DAA
MOV AX, 09h
MOV BX, 52H
Add AX, BX
DAA
SUB
MOV Ah, 0ch
MOV Bl, 08h
SUB Ah, bl
SUB 16 Bit
MOV CX, 4578h
SUB CX, 456Ah
SBB
MOV BX, 1234H
MOV CX, 4520H
SUB BX, CX
SBB BX, 2508H
DEC
MOV AH, 25h
DEC ah
;NEG
MOV AL, 07H
NEG AL
DAS
MOV AX,24H
MOV BX,16H
SUB AX, BX
DAS
56
MUL
MOV AL, 14H
MOV BL, 24H
MUL BL
MUL 16 Bit
MOV AX, 1234H
MOV BX, 45F4H
MUL BX
IMUL
MOV AL, -4
MOV BL, -16
IMUL BL
IMUL 16 Bit
MOV AX, 0FFFFH
MOV CX, 0FFF0h
IMUL CX
AAM
MOV AL, 04H
MOV BL, 04H
MUL BL
AAM
57
Lab Exercise and Summary
Summary should cover Introduction, Procedure, Data Analysis and Evaluation.
58
Student’s Signature: Date:
59
Evaluation Criteria
Method of Evaluation: Viva, Practical Quiz
Excellent Good Satisfactory Unsatisfactory Poor Marks
Obtained
10 9-7 6-4 3-1 0
Assignment All tasks Most tasks Some tasks Most tasks All tasks were
completed were were were incomplete or
correctly in completed completed incomplete or incorrect.
given time correctly. correctly and incorrect and
and have a Tasks could have an have no Didn’t
complete be improved incomplete understanding perform tasks
understanding further and understanding and have no
have a understanding
complete
understanding
Total
References:
1. [Link]
2. [Link]
3. [Link]
4. [Link]
60