Lab Session 4
Lab Session 4
This lab will teach students the fundamentals of data movement, basic input/output operations, and register
manipulation in assembly language. By using tools like EMU8086, students will learn how low-level
programming works and how to interact directly with hardware. The primary focus will be on:
Understand the use of MOV, XCHG, and INT 21H instructions in assembly language.
Gain practical experience in manipulating data using registers and memory.
Learn how to handle basic I/O operations in DOS.
Understand how .COM programs are structured and executed.
Theory Background:
Assembly language is a low-level programming language that is closely tied to a computer's machine
architecture. Programs written in assembly are converted into machine code through an assembler, and they
interact directly with the hardware, making them highly efficient for tasks that require precise control over a
system's resources. This lab focuses on the Intel 8086 architecture and using EMU8086, a widely used
emulator to understand and execute assembly programs.
Assembly language provides an interface to control a computer's hardware through mnemonic codes that
correspond to the processor's instructions. The Intel 8086 microprocessor uses a set of instructions, called the
Instruction Set Architecture (ISA), which allows programmers to execute various operations, such as data
movement, arithmetic, logic, and control flow.
EMU8086 is an emulator that simulates the Intel 8086 processor, making it an ideal tool for learning assembly
language programming. By using EMU8086, programmers can write and test assembly code in a simulated
environment without requiring access to physical hardware.
Registers are small, fast storage locations inside the CPU that hold data temporarily. The Intel 8086 has several
registers that can be grouped into different categories:
Data Registers: These are general-purpose registers used for various operations.
o AX (Accumulator): Used in arithmetic and data manipulation.
o BX (Base Register): Can store base addresses and be used in address calculations.
o CX (Count Register): Often used as a counter for loops.
o DX (Data Register): Involved in I/O operations.
Pointer and Index Registers: Used to point to memory locations.
o BP (Base Pointer), SP (Stack Pointer): Hold memory addresses.
o SI (Source Index), DI (Destination Index): Used in string operations.
Segment Registers: Define the memory segments in use.
o CS (Code Segment): Points to the current code segment.
o DS (Data Segment): Points to the data segment where variables are stored.
o SS (Stack Segment): Used for stack operations.
The 8086 processor operates in a segmented memory model. This means that memory is divided into segments
(typically of 64KB each), and different segments are used for code, data, and the stack. The combination of a
segment register (e.g., CS, DS) and an offset within that segment provides the physical address in memory.
In assembly language, memory locations can be accessed by using segment registers along with specific offsets.
The Intel 8086 microprocessor interacts with the operating system through interrupts. In DOS-based systems,
interrupt 21H is used for a wide range of input/output operations, such as reading characters from the keyboard,
displaying characters on the screen, and working with files.
Function 01H (AH = 01H): Reads a single character from the keyboard and stores it in the AL register.
Function 02H (AH = 02H): Outputs a character stored in the DL register to the screen.
These basic input/output functions allow assembly programs to communicate with the user via the command
line.
One of the core operations in assembly language is data movement between registers, memory locations, and
input/output devices. The following instructions are commonly used for data transfer:
In this lab, we will use the MOV instruction to load characters from memory into registers, and the XCHG
instruction to swap the values of two characters.
6. Retaining Program Flow: RET and Program Termination:
In assembly programs, especially those using DOS interrupts, the RET instruction is used to return from a
procedure or terminate the program. Assembly programs are terminated by calling DOS interrupt 21H, function
4CH, to return control to the operating system.
In this lab, we are working with .COM programs. A .COM program is a simple executable file format with no
header information, meaning it starts execution directly at memory offset 100H. These programs are limited to
64KB in size and must fit the code, data, and stack within this single memory segment.
The key steps of the assembly program being studied in this lab are as follows:
1. Initialize Data Segment: The program sets up the data segment using the MOV instruction.
2. Read Characters: Two characters are read from the keyboard using INT 21H and stored in memory locations A
and B.
3. Swap Values: The values of A and B are swapped using the XCHG instruction.
4. Display Swapped Characters: The swapped values are printed to the console using INT 21H.
By the end of this lab, students will have a clearer understanding of how to handle character input/output,
perform register operations, and swap data in assembly language.
This program reads two characters from the keyboard, stores them in memory locations A and B, and then swaps
their values. Finally, the swapped characters are printed back to the console.
1. org 100h
o Sets the program's starting point at the offset 100h. This is typical for .COM programs.
2. .DATA
o Defines the data segment, where variables are stored.
3. A DB ?
o Declares a variable A as a byte (DB = Define Byte) to store one character input by the user.
4. B DB ?
o Declares another variable B as a byte to store a second character input by the user.
5. .CODE
o Starts the code segment where the executable instructions are placed.
6. main PROC
o Declares the start of the procedure named main.
7. MOV AX,@DATA
o Moves the address of the data segment (@DATA) into the AX register.
8. MOV DS,AX
o Moves the value from AX (which contains the address of the data segment) into the DS register.
DS is now pointing to the data segment.
9. MOV AH,1
o Moves 1 into the AH register, preparing for interrupt 21H to read a single character from the
keyboard.
10. INT 21H
o Calls interrupt 21H, function 01H, which waits for the user to input a character from the
keyboard. The character is stored in the AL register.
11. MOV A,AL
o Stores the character from AL (the first character input by the user) into the memory location A.
12. INT 21H
o Repeats the keyboard input process to get a second character from the user. Again, the character is
stored in the AL register.
13. MOV B,AL
o Stores the second character input by the user into memory location B.
14. MOV BL,A
o Moves the value stored in memory location A into the BL register.
15. MOV CL,B
o Moves the value stored in memory location B into the CL register.
16. XCHG BL,CL
o Swaps the values between the BL and CL registers. Now BL contains the value from B and CL
contains the value from A.
17. MOV AH,2
o Prepares for interrupt 21H to display a character (function 02H).
18. MOV DL,BL
o Moves the value in BL (which now contains the original value of B) into the DL register.
19. INT 21H
o Calls interrupt 21H, function 02H, to display the character in DL (which is the original B).
20. MOV DL,CL
o Moves the value in CL (which now contains the original value of A) into the DL register.
21. INT 21H
o Calls interrupt 21H, function 02H, to display the character in DL (which is the original A).
22. ret
o Returns from the main procedure and ends the program.
Lab Exercise:
1. Exercise 1:
Modify the program to swap the values of three characters ( A, B, and C). Write the modified assembly
code and explain the changes you made.
2. Exercise 2:
Modify the program to accept a 4-character string from the user. Reverse the characters (e.g., input:
"ABCD", output: "DCBA") and display them in reverse order. Write the assembly code for this program.
3. Exercise 3:
Explain the difference between using XCHG and manually moving values between registers and memory
locations.
Lab Exercise:
1. Exercise 1:
Modify the program to swap the values of three characters (A, B, and C). Write the modified assembly code
and explain the changes you made.
SOURCE CODE:
org 100h
.DATA
A DB ?
B DB ?
C DB ?
D DW "Enter numbers$"
E DW "Swapped numbers are:$"
.CODE
main PROC
mov AX,@DATA
mov DS,AX
int 21h
mov B,AL
int 21h
mov C,AL
mov AH,2
mov DL,AL
int 21h
mov DL,BL
int 21h
mov DL,CL
int 21h
ret
OUTPUT:
2. Exercise 2:
Modify the program to accept a 4-character string from the user. Reverse the characters (e.g., input:
"ABCD", output: "DCBA") and display them in reverse order. Write the assembly code for this program.
SOURCE CODE:
org 100h
.DATA
A DB ?
B DB ?
C DB ?
D DB ?
E DW "Enter Characters$"
F DW "Reversed Characters are:$"
.CODE
main PROC
mov AX,@DATA
mov DS,AX
int 21h
mov B,AL
int 21h
mov C,AL
int 21h
mov D,AL
mov AL,A
mov BL,B
mov CL,C
mov BH,D
XCHG AL,BH ;reversing
XCHG BL,CL
mov AH,2
mov DL,AL
int 21h
mov DL,BL
int 21h
mov DL,CL
int 21h
mov DL,BH
int 21h
ret
OUTPUT:
3. Exercise 3:
Explain the difference between using XCHG and manually moving values between registers and memory
locations.
Answer:
The XCHG (Exchange) instruction swaps the contents of two operands (usually two registers or a register
and memory) directly in one instruction, without needing a temporary storage location.
It only requires one instruction:
e.g. XCHG AX, BX
Since XCHG is a single CPU instruction, it is efficient and concise, saving on code length and reducing the
chance of errors that could occur with manual swapping.
While manually, you need an additional register or memory location as a temporary variable to hold one
of the values temporarily.
With manual swapping, the code size increases, and there’s a slight increase in the chance of errors (e.g.,
if you forget a step or overwrite the temporary variable incorrectly).