0% found this document useful (0 votes)
9 views9 pages

Lab Session 4

Uploaded by

toobafar004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Lab Session 4

Uploaded by

toobafar004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

LAB SESSION 4

“Swapping Characters in Assembly Language Using EMU8086”

Purpose of the Lab:

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:

 Basic input and output using interrupts.


 Register manipulation and data swapping.
 Practical applications of assembly language in the 8086 architecture.

After completing this lab, students will:

 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.

1. Overview of Assembly Language and EMU8086:

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.

2. Registers in the Intel 8086 Microprocessor:

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.

3. Memory Segmentation and Addressing in 8086:

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.

4. Input and Output in Assembly Language (INT 21H):

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.

5. Data Movement and Swapping in Assembly Language:

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:

 MOV: Moves data from one location (register or memory) to another.


o Example: MOV AX, BX copies the value in BX into AX.
 XCHG: Swaps the values of two registers or between a register and a memory location.
o Example: XCHG AX, BX swaps the contents of AX and BX.

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.

 RET: Returns from the current procedure or program.


 INT 21H (AH = 4CH): Ends the program and returns control to DOS.

7. Understanding the .COM Program Format:

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.

8. Steps Involved in the Program Execution:

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.

Explanation of the Code

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.

Code Explanation (Line by Line)

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

lea dx,D ;for string output


mov ah,9
int 21h

mov AH,1 ;for input


int 21h
mov A,AL

int 21h
mov B,AL

int 21h
mov C,AL

mov DL,10 ;newline


mov ah,2h
int 21h

mov DL,13 ;carriage


mov ah,2h
int 21h

mov ah,9 ;string output


lea dx,E
int 21h
mov AL,A
mov BL,B
mov CL,C
XCHG AL,BL ;swapping
XCHG BL,CL

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

lea dx,E ;for string output


mov ah,9
int 21h

mov AH,1 ;for input


int 21h
mov A,AL

int 21h
mov B,AL

int 21h
mov C,AL

int 21h
mov D,AL

mov DL,10 ;newline


mov ah,2h
int 21h

mov DL,13 ;carriage


mov ah,2h
int 21h

mov ah,9 ;string output


lea dx,F
int 21h

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.

Manual swapping generally requires three instructions:

 Move the value from one register to the temporary storage.


 Move the second value to the first register.
 Move the value from the temporary storage to the second register.

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).

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy