Assembly Language Programming
Assembly Language Programming
Assembly Language Programming
UNIT –II
(ASSEMBLY LANGUAGE PROGRAMMING)
Syllabus: Assembly language programs involving logical, branch and call instructions, sorting, evaluation of
arithmetic expressions, string manipulation.
Programming Languages: To run a program, a microcomputer must have the program stored in binary form in
successive memory locations. There are three language levels that can be used to write a program for a
microcomputer.
1. Machine Language
2. Assembly Language
3. High-level Languages
Machine Language: You can write programs as simply a sequence of the binary codes for the instructions you want
the microcomputer to execute. This binary form of the program is referred to as machine language because it is the
form required by the machine. However, it is very difficult, not possible, for a programmer to memorize the
thousands of binary instruction codes for a microprocessor. Also, it is very easy for an error to occur when working
with long series of 1’s and 0’s. Using hexadecimal representation for the binary codes might help some, but there are
still thousands of instruction codes to cope with.
Assembly Language: To make programming easier, many programmers write programs in assembly language. They
then translate the assembly language program to machine language so that it can be loaded into memory and run.
Assembly language uses 2, 3, or 4- letter mnemonics to represent each instruction type. A mnemonic is advice to help
you remember something. The letters in an assembly language mnemonic are usually initials or shortened form of the
English word(s) for the operation performed by the instruction. For example, the mnemonic for addition is ADD, the
mnemonic for subtraction is SUB and the mnemonic for the instruction to copy data from one location to another is
MOV. Assembly language statements are usually written in a standard form that has four fields, as shown in fig.
below.
The first field in an assembly language statement is the Label field. A label is a symbol or group of symbols
used to represent an address which is not specially known at the time the statement is written. Labels are usually
followed by a colon.
The opcode field of the instruction contains the mnemonic for the instruction to be performed. Instruction
mnemonics are sometimes called operation codes or opcodes.
The operand field of the statement contains the data, the memory address. The port address, or the name of
the register on which the instruction is to be performed. Operand is just another name for the data item(s) acted on
by the instruction. In the above example there are two operands, AL and 07H, specified in the operand field. AL
represents the AL register, and 07H represents the number 07H. This assembly language statement thus says, “Add
the number 07H to the contents of the AL register.” By Intel convention, the result of the addition will be put in the
register or the memory location specified before the comma in the operand field. For the example, the result will be
left in the register AL.
K SUDHAKAR Unit-2
Microprocessors and Microcontrollers Page |2
The final field in an assembly language statement is comment field, which starts with a semicolon. Comments
do not become the part of the machine language program, but they are very important.
High-level Language: Another way of writing a program for a microcomputer is with a high-level language, such as
BASIC, Pascal, or C. These language use program statements which are even more English-like than those of assembly
language. Each high level statement may represent many machine code instructions. An interpreter or a compiler
program is used to translate higher-level language statements to machine codes. Programs can usually be written
faster in high level languages than in assembly language because a high –level language work with bigger building
blocks. However, programs written in a high –level language and interpreted or compiled almost always execute
more slowly and require more memory than the same program written in assembly language.
Programs that involve a lot of hardware control, such as robots and factory control systems, or programs that
must run as quickly as possible are usually best written assembly language. Complex data processing programs that
manipulate massive amounts of data, such as insurance company records, are usually best written in a high-level
language.
Developing a program however requires more than just writing down series of instructions. When you write a
computer program, it is good idea to start by developing a detailed plan or outline for the entire program. You should
never start writing an assembly language program by just writing down instructions!
Assembler: An assembler is programming tool which is used to translate the assembly language mnemonics for
instructions to the corresponding binary codes. The assembler generates two files. The first file, called the object file,
is given the extension .OBJ. The object file contains the binary codes for the instructions and information about the
addresses of the instructions. After further processing the contents of this file will be loaded into memory and run.
The second file generated by the assembler is called the assembler list file and is given the extension .LST.
K SUDHAKAR Unit-2
Microprocessors and Microcontrollers Page |3
Linker: The linker is program used to join several object files into one large object file. The linkers which come with
the TASM or MASM assemblers produce link files with the .EXE extension.
Locator: A locator is a program used to assign the specific addresses of where the segments of object code are to be
loaded into memory.
Debugger: If your program requires no external hardware or requires only hardware accessible directly from your
microcomputer, then you can use debugger to run and debug your program. A debugger is a program which allows
you to load your object code program into system memory, execute the program, and troubleshoot or’ debug’ it.
Emulator: Another way to run your program is with an emulator. An emulator is a mixture of hardware and software.
It is usually used to test and debug the hardware and software of an external system.
Simple programs
K SUDHAKAR Unit-2
Microprocessors and Microcontrollers Page |6
10. Write an ALP in 8086 to perform BCD subtraction of two 16-bit numbers.
K SUDHAKAR Unit-2
Microprocessors and Microcontrollers Page |7
13. Write an ALP in 8086 to perform multiplication of given two numbers using
1. MUL instruction
2. Repeated addition method
1. MUL instruction
K SUDHAKAR Unit-2
Microprocessors and Microcontrollers Page |8
17. Write an ALP in 8086 to find the minimum number from the given array of N numbers.
ASSUME CS: CODE
ORG 2000H
CODE SEGMENT
MOV SI, 3000H
MOV CL, [SI]
INC SI
MOV AX, [SI]
DEC CL
UP: INC SI
INC SI
CMP AX, [SI]
JB DOWN
MOV AX, BX
DOWN: LOOP UP
INT 03H
CODE ENDS
END
18. Write an ALP in 8086 to count no. of even and odd numbers from the given array.
ASSUME CS: CODE
ORG 2000H
CODE SEGMENT
MOV SI, 3000H
MOV CL, [SI]
MOV BX, 0000H
MOV DX, 0000H
K SUDHAKAR Unit-2
Microprocessors and Microcontrollers Page |9
19. Write an ALP in 8086 to find no. of positive and negative numbers from the given array.
ASSUME CS: CODE
ORG 2000H
CODE SEGMENT
MOV BX, 0000H
MOV DX, 0000H
MOV AX, 0000H
MOV SI, 3000H
MOV CL, [SI]
UP: INC SI
MOV AL, [SI]
ROL AL, 01H
JC NEG
INC BX
JMP DOWN
NEG: INC DX
DOWN: LOOP UP
INT 03H
CODE ENDS
END
20. Write an ALP in 8086 to count no. of 1’s and 0’s in a given 16-bit number.
ASSUME CS: CODE
ORG 2000H
CODE SEGMENT
XOR AX, AX
XOR BX, BX
XOR DX, DX
MOV SI, 3000H
MOV CL, 10H
MOV AX, [SI]
UP: ROR AX, 01H
JC ONE
INC BX
JMP DOWN
ONE: INC DX
DOWN: LOOP UP
INT 03H
CODE ENDS
END
K SUDHAKAR Unit-2
Microprocessors and Microcontrollers P a g e | 10
21. Write a Recursive program in 8086 to find the sum of first N integers.
ASSUME CS: CODE
ORG 5000H
CODE SEGMENT
MOV SI, 3000H
MOV CX, [SI]
CALL ADD
INT 03H
CODE ENDS
END
ADD: PROC NEAR
CMP CX, 0000H
JE EXIT
ADD AX, CX
DEC CX
CALL ADD
EXIT: RET
ENDP
Evaluation of arithmetic expressions
2.
2.
Sorting
23. Write an ALP in 8086 to arrange a given array of N bytes in ascending order.
Strings
K SUDHAKAR Unit-2