1. Introduction to Assembly Language
Programming
By – Apeksha Shinde
From- Mucchala Polytechnic
Email – shndapeksha23@gmail.com
2. Introduction
Each personal computer has a microprocessor that manages the computer’s
arithmetical, logical, and control activities .
Each family of processor has its own set of instructions for handling various
operations.
getting input from keyboard- displaying information on screen – performing
various other jobs.
These sets of instructions are called “ Machine Language Instruction”
Processor understands only machine language instructions which are strings of
1s and 0s .
The low-level assembly language is designed for a specific family of processors
that represents various instructions in symbolic code and more understandable
form.
3. What is Assembly Language
An assembly language is low-level programming language for a computer, or other
programmable device.
It requires less memory and execution time.
It allows hardware-specific complex jobs in an easier way.
An understanding of assembly lang provides knowledge of :
i. Interface of programs with OS, processor and BIOS.
ii. Representation of data in memory and other external devices.
iii. How processor accesses and executes instruction.
iv. How instruction accesses and process data.
4. Assembly Language Development Tools
Editor:
It is a program which is used to construct the assembly language program in
correct format so that the Assembler will translate it correctly to machine
language.
An Editor is a program which allows us to create a file containing the assembly
language statement for the program.
If any typing mistake is done editor will alert us to correct it.
If we leave out a program statement an editor will let you move everything down
and insert a line.
After typing all the program we have to save the program for a hard disk.
This we call it as source file.
The next step is to process the source file with an assembler.
While using TASM or MASM we should give a file name and extension .ASM.
Ex: Sample. Asm
Editors are EDIT,Word Star,Norton Edit
5. Assembler:
An Assembler is used to translate the assembly language mnemonics into machine
language( i.e binary codes).
When you run the assembler it reads the source file of your program from where
you have saved it.
The assembler generates two files . The first file is the Object file with the
extension .OBJ.
The object file consists of the binary codes for the instructions and information
about the addresses of the instructions.
After further processing, the contents of the file will be loaded in to memory and
run. The second file is the assembler list file with the extension .LST.
Assemblers are TASM(Borland’s Turbo Assembler) MASM(Microsoft Macro Assembler)
6. Linker :
A linker is a program used to connect several object files into one large object
file.
While writing large programs it is better to divide the large program into smaller
modules.
Each module can be individually written, tested and debugged.
Then all the object modules are linked together to form one, functioning
program.
These object modules can also be kept in library file and linked into other
programs as needed. A linker produces a link file which contains the binary codes
for all the combined modules.
The linker also produces a link map file which contains the address information
about the linked files.
The linkers which come with TASM or MASM assemblers produce link files with the
.EXE extension.
Linkers are TLINK(Borland’s Turbo Linker) LINK (Microsoft’s Linker)
7. Debugger:
A debugger is a program which allows to load your object code program into
system memory, execute the program, and troubleshoot or debug it.
Debuggers allows to stop the program after each instruction so that you can check
or alter memory and register contents. This is called single step debug.
The debugger allows to look into the contents of registers and memory locations
after the program runs. We can also change the contents of registers and memory
locations and rerun the program.
A debugger also allows to set a breakpoint at any point in the program.
If we insert a break point, the debugger will run the program up to the instruction
where the breakpoint is put and then stop the execution.
Debuggers are Borland’s Turbo Debugger TD, Microsoft Debugger Code View(CV).
8. 8086 Microprocessor and its feature
a. 8086 has 16-bit ALU
b. It has 16-bit data bus , so it can read data or write data to memory or I/O ports
either 16 bits or 8 bits at a time .
c. It has 20 address lines , so it can address up to 1048576 = 1Mbytes of memory
d. 8086 includes few features , which enhance multiprocessing capability.
e. Operates on +5v supply and single phase (single line) clock frequency .(Clock is
generated by separate peripheral chip 8284)
f. 8086 comes with different versions. 8086 runs at 5 MHz, 8086-2 runs at 8 MHz, 8086-
1 runs at 10 MHz.
g. It comes in 40-pin configuration with HMOS technology having around 20,000
transistors in its circuitry.
9.
10. Addressing modes of 8086
Every instruction of a program has to operate on a date. The different ways in
which a source operand is denoted in any instruction are known as addressing
modes.
There are different types of addressing modes
1. Immediate Addressing eg: MOV DL, 08H
2. Register Addressing eg: MOV (CL), (DH)
3. Direct Addressing eg: MOV BX, [1354H]
4. Register Indirect Addressing eg: MOV AX, [BX + 08H]
5. Register relative addressing eg: MOV AX, [BX + 44]
6. Based Indexed addressing eg: MOV CX , [SI + BX]
7. Relative Based Index Addressing eg: MOV DX, [BX + SI + 0AH]
8. Implied / Implicit Addressing eg: CLC
11. Instruction Sets
8086 supports 8 types of instruction types of instructions
1. Data Transfer Instructions :
Instructions that are used to transfer data/ address in to registers, memory
locations and I/O ports.
Generally involve two operands: Source operand and Destination operand of the
same size.
Mnemonics: MOV, XCHG, PUSH, POP, IN, OUT
2. Arithmetic Instructions :
• ADD , ADC , INC , AAA , DAA , SBB , SUB , DEC , NEG , CMP , AAS , DAS , MUL
IMUL , AAM , DIV, IDIV , AAD , CBW , CDW
12. 3. Bit Manipulation Instruction :
i. AND - Used for adding each bit in a byte/word with the corresponding bit in
another byte/word.
ii. OR - Used to multiply each bit in a byte/word with the corresponding bit in another
byte/word.
iii. NOT - Used to invert each bit of a byte or word.
iv. XOR - Used to perform Exclusive-OR operation over each bit in a byte/word with
the corresponding bit in another byte/ word.
v. TEST - Used to add operands to update flags, without affecting operands.
vi. SHR - Used to shift bits of a byte/word towards the right and put zero(S) in MSBs.
vii. SHL/SAL - Used to shift bits of a byte/word towards left and put zero(S) in LSBs.
viii. ROR - Used to rotate bits of byte/word towards the right, i.e. LSB to MSB and to
Carry Flag [CF].
ix. ROL - Used to rotate bits of byte/word towards the left, i.e. MSB to LSB and to
Carry Flag [CF].
x. RCR - Used to rotate bits of byte/word towards the right, i.e. LSB to CF and CF to
MSB.
xi. RCL - Used to rotate bits of byte/word towards the left, i.e. MSB to CF and CF to
LSB.
13. 4. String Instructions :
String is a group of bytes/words and their memory is always allocated in a sequential
order.
Following is the list of instructions under this group –
•REP − Used to repeat the given instruction till CX ≠ 0.
•REPE/REPZ − Used to repeat the given instruction until CX = 0 or zero flag ZF = 1.
•REPNE/REPNZ − Used to repeat the given instruction until CX = 0 or zero flag ZF = 1.
•MOVS/MOVSB/MOVSW − Used to move the byte/word from one string to another.
•COMS/COMPSB/COMPSW − Used to compare two string bytes/words.
•INS/INSB/INSW − Used as an input string/byte/word from the I/O port to the provided
memory location.
•OUTS/OUTSB/OUTSW − Used as an output string/byte/word from the provided memory
location to the I/O port.
•SCAS/SCASB/SCASW − Used to scan a string and compare its byte with a byte in AL or
string word with a word in AX.
•LODS/LODSB/LODSW − Used to store the string byte into AL or string word into AX.
14. 5. Program Execution Transfer Instructions (Branch and Loop Instructions)
These instructions are used to transfer/branch the instructions during an
execution. It includes the following instructions −
•CALL − Used to call a procedure and save their return address to the stack.
•RET − Used to return from the procedure to the main program.
•JMP − Used to jump to the provvided address to proceed to the next instruction.
•JA/JNBE − Used to jump if above/not below/equal instruction satisfies.
•JAE/JNB − Used to jump if above/not below instruction satisfies.
•JBE/JNA − Used to jump if below/equal/ not above instruction satisfies.
•JC − Used to jump if carry flag CF = 1
•JE/JZ − Used to jump if equal/zero flag ZF = 1
•JG/JNLE − Used to jump if greater/not less than/equal instruction satisfies.
•JGE/JNL − Used to jump if greater than/equal/not less than instruction satisfies.
•JL/JNGE − Used to jump if less than/not greater than/equal instruction satisfies.
•JLE/JNG − Used to jump if less than/equal/if not greater than instruction satisfies.
•JNC − Used to jump if no carry flag (CF = 0)
•JNE/JNZ − Used to jump if not equal/zero flag ZF = 0
•JNO − Used to jump if no overflow flag OF = 0
15. 6. Processor Control Instruction
These instructions are used to control the processor action by
setting/resetting the flag values.
Following are the instructions under this group −
•STC − Used to set carry flag CF to 1
•CLC − Used to clear/reset carry flag CF to 0
•CMC − Used to put complement at the state of carry flag CF.
•STD − Used to set the direction flag DF to 1
•CLD − Used to clear/reset the direction flag DF = 0
•STI − Used to set the interrupt enable flag to 1, i.e., enable INTR input.
•CLI − Used to clear the interrupt enable flag to 0, i.e., disable INTR input.
WAIT - Wait for TEST pin active
16. 7. Iteration Control Instruction
These instructions are used to execute the given instructions for number of times.
Following is the list of instructions under this group −
•LOOP − Used to loop a group of instructions until the condition satisfies, i.e., CX = 0
•LOOPE/LOOPZ − Used to loop a group of instructions till it satisfies ZF = 1 & CX =
0
•LOOPNE/LOOPNZ − Used to loop a group of instructions till it satisfies ZF = 0 & CX
= 0
•JCXZ − Used to jump to the provided address if CX = 0
Interrupt Instructions
17. 8. Interrupt Instructions :
These instructions are used to call the interrupt during program execution.
•INT − Used to interrupt the program during execution and calling service
specified.
•INTO − Used to interrupt the program during execution if OF = 1
•IRET − Used to return from interrupt service to the main program
18. Assembler Directive
Assembler Directives are the statement that give hint or direction to the Assembler to
perform the task. The Assembler Directives are also known as ‘Pseudo instructions’
because they are not translated into machine codes.
• Used to : › specify the start and end of a program › attach value to variables ›
allocate storage locations to input/ output data › define start and end of segments,
procedures, macros, etc.
19.
20.
21.
22.
23.
24. The Art of Assembly Language
Programming
Program Development Steps
1. Defining the program
2. Algorithm
3. Initialization checklist
4. Choosing instruction
5. Converting algorithm to assembly language program
25. Defining the problem: The first step in program developing is the careful analysis of
problem. The objective of the program should be clear to the user.
Algorithm: It is the step by step procedure designed to perform or sovle the task.
Initialization of checklist: Initialize variables, constants etc also initialize various
segments.
Choosing Instruction: It is important to select the proper instruction that will perform
the desired task.
Converting algorithm to assembly language program: In this step instructions are
arranged in the proper sequence according to algorithm and then converting into
machine language.
26. Here’s a example of an assembly language program
8 bit numbers subtraction
data segment
a db 09h
b db 05h
c dw ?
data ends
code segment
assume cs:code, ds:data
start:
mov ax, data
mov ds, ax
mov al, a
mov bl, b
sub al, bl
mov c, ax
int 3
code ends
end start o/p = al = 09 , bl = 02
al = 07