0% found this document useful (0 votes)
53 views

MSI Lab Manual ss

The document is a lab manual for a course on Microprocessor Systems and Interfacing (EEE342), detailing various experiments using the EMU8086 emulator. It includes objectives, theoretical background, program codes, procedures, and post-lab tasks for multiple labs focused on assembly language programming, data transfer operations, memory access, arithmetic instructions, binary arithmetic, and logical operations. Each lab aims to familiarize students with different aspects of assembly language programming on the Intel 8086 microprocessor.
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)
53 views

MSI Lab Manual ss

The document is a lab manual for a course on Microprocessor Systems and Interfacing (EEE342), detailing various experiments using the EMU8086 emulator. It includes objectives, theoretical background, program codes, procedures, and post-lab tasks for multiple labs focused on assembly language programming, data transfer operations, memory access, arithmetic instructions, binary arithmetic, and logical operations. Each lab aims to familiarize students with different aspects of assembly language programming on the Intel 8086 microprocessor.
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/ 20

LAB MANUAL: MICROPROCESSOR SYSTEMS AND INTERFACING (EEE342)

Lab 1: Familiarization with EMU8086

Experiment Title:

Familiarize with EMU8086 and Display "Hello, World!" on the Screen

Objective:

To learn the basic functionality of the EMU8086 emulator, understand its user interface, and
write a simple assembly language program to display "Hello, World!" on the screen.

Theory:

The EMU8086 emulator is a microprocessor emulator for the Intel 8086, allowing users to write,
debug, and execute assembly language programs. It provides a graphical debugger to step
through code and observe register, memory, and flag states.

In this lab, students will write their first assembly language program to display a message on the
screen using interrupts. The int 21h DOS interrupt provides various functions, and the ah =
09h service displays a string terminated by '$'.

Apparatus/Software:

1. EMU8086 Emulator installed on a PC.


2. Windows Operating System.

Program Code:
; Program to display "Hello, World!" on the screen
.model small
.stack 100h
.data
message db 'Hello, World!$' ; Define the string to display
.code
main proc
; Initialize data segment
mov ax, @data
mov ds, ax

; Display the message


mov ah, 09h ; DOS interrupt service for displaying strings
lea dx, message ; Load the address of the string into DX
int 21h ; Call DOS interrupt

; Terminate the program


mov ah, 4ch ; DOS interrupt service to terminate program
int 21h
main endp
end main

_____________________________________________________________________________________
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, CUI ATTOCK CAMPUS
LAB MANUAL: MICROPROCESSOR SYSTEMS AND INTERFACING (EEE342)

Procedure:

1. Launch the EMU8086 emulator.


2. Create a new assembly file and name it hello.asm.
3. Copy the provided code into the editor.
4. Assemble the program by clicking the Compile button.
5. Run the program using the Run option.
6. Observe the output in the emulator console.
7. Use the debugger to step through the code, watching changes in the registers and memory.

Observations:

Instruction Action Register/Memory State


mov ax, @data Load the data segment address AX contains the data segment.
mov ds, ax Initialize data segment register DS set to the data segment.
mov ah, 09h Set function for string display AH = 09h.
lea dx, message Load the address of the message DX points to the message.
int 21h Call DOS interrupt to display string String is printed on the screen.

Post Lab Tasks:

• Write a program to display your name on the screen.


• Record your observations in a table.

_____________________________________________________________________________________
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, CUI ATTOCK CAMPUS
LAB MANUAL: MICROPROCESSOR SYSTEMS AND INTERFACING (EEE342)

Lab 2: Simple Data Transfer Operations

Objective:

To understand and perform basic data transfer operations using assembly language instructions
on the Intel 8086 microprocessor.

Theory:

Data transfer instructions are used to move data between registers, memory, and I/O devices.
Common instructions include MOV, XCHG, PUSH, and POP.

Program Code:

; Program to demonstrate simple data transfer operations


.model small
.stack 100h
.data
var1 dw 1234h ; Variable 1 with initial value 0x1234
var2 dw 0 ; Variable 2 initialized to 0
.code
main proc
; Initialize data segment
mov ax, @data
mov ds, ax

; Move data between registers


mov ax, var1 ; Load var1 into AX
mov bx, ax ; Copy AX into BX

; Exchange data between registers


xchg ax, bx ; Exchange AX and BX

; Move data from register to memory


mov var2, ax ; Store AX value into var2

; Use PUSH and POP


push ax ; Push AX onto the stack
pop cx ; Pop stack value into CX

; End program
mov ah, 4ch ; Terminate program
int 21h
main endp
end main

_____________________________________________________________________________________
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, CUI ATTOCK CAMPUS
LAB MANUAL: MICROPROCESSOR SYSTEMS AND INTERFACING (EEE342)

Procedure:

1. Open the EMU8086 emulator.


2. Create a new file and paste the above code into the editor.
3. Assemble and run the program using the Compile and Run options.
4. Use the debugger to observe:
o Register values (AX, BX, CX) after each operation.
o The memory locations of var1 and var2.
5. Record your observations in a table.

Post Lab Tasks:

• Write a program to move a value from one register to another and verify using the
debugger.
• Record your observations in a table.

_____________________________________________________________________________________
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, CUI ATTOCK CAMPUS
LAB MANUAL: MICROPROCESSOR SYSTEMS AND INTERFACING (EEE342)

Lab 3: Memory Access Operations

Experiment Title:

Memory Access Operations – Storing and Retrieving Data from Memory

Objective:

To understand how to access memory in assembly language using Intel 8086 instructions and
perform operations to store and retrieve values between memory locations and registers using
MOV, LEA, LDS, and LES instructions.

Theory:

Memory access operations in assembly involve moving data between memory and registers. The
MOV instruction is commonly used to transfer data. Other instructions like LEA (Load Effective
Address), LDS (Load Pointer into DS Register), and LES (Load Pointer into ES Register) are
specialized for addressing and pointer handling.

In this experiment:

1. A value is stored at a specific memory location.


2. The value is retrieved and loaded into a register.
3. Instructions like LEA are used to load the address of the memory location.

Program Code:

; Program to store a value in memory and retrieve it into a register


.model small
.stack 100h
.data
mem_value dw 1234h ; Define a word in memory with value 0x1234
.code
main proc
; Initialize data segment
mov ax, @data
mov ds, ax

; Store a value into memory


mov ax, 0ABCDh ; Load AX with 0xABCD
mov mem_value, ax ; Store AX value into mem_value

; Retrieve the value from memory


mov ax, mem_value ; Load mem_value into AX register

; Use LEA to load the address of mem_value


lea bx, mem_value ; Load the address of mem_value into BX

_____________________________________________________________________________________
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, CUI ATTOCK CAMPUS
LAB MANUAL: MICROPROCESSOR SYSTEMS AND INTERFACING (EEE342)

; Use LDS to load a pointer into DS and a register


lds si, mem_value ; Load memory pointer into DS and SI

; Use LES to load a pointer into ES and a register


les di, mem_value ; Load memory pointer into ES and DI

; End program
mov ah, 4ch ; DOS interrupt to terminate program
int 21h
main endp
end main

Procedure:

1. Open the EMU8086 emulator.


2. Create a new assembly file named memory_access.asm.
3. Copy the provided code into the editor.
4. Assemble the program using the Compile option.
5. Execute the program using the Run option.
6. Use the debugger to verify:
o The value stored at the memory location mem_value.
o The retrieved value in registers (e.g., AX).
o The address loaded into registers using LEA, LDS, and LES.

Post Lab Tasks:

• Write a program to store a value 0x1234 at a memory location and load it into a
register.
• Record your observations in a table.

_____________________________________________________________________________________
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, CUI ATTOCK CAMPUS
LAB MANUAL: MICROPROCESSOR SYSTEMS AND INTERFACING (EEE342)

Lab 4: Arithmetic Instructions - Addition and Subtraction

Experiment Title:

Perform Addition and Subtraction of Two 16-bit Numbers

Objective:

To understand and implement basic arithmetic operations in assembly language using Intel 8086
instructions like ADD, SUB, INC, and DEC. This program demonstrates how to perform addition and
subtraction on 16-bit numbers.

Theory:

Arithmetic operations are fundamental in assembly language programming. The ADD and SUB
instructions are used for addition and subtraction, respectively. The INC (increment) and DEC
(decrement) instructions increase or decrease the value of a register or memory location by 1
without affecting the carry flag.
This lab focuses on performing the following:

1. Adding two 16-bit numbers stored in registers.


2. Subtracting one 16-bit number from another.
3. Incrementing and decrementing register values.

Program Code:

; Program to perform addition and subtraction of 16-bit numbers


.model small
.stack 100h
.data
num1 dw 1234h ; First number
num2 dw 5678h ; Second number
.code
main proc
; Initialize data segment
mov ax, @data
mov ds, ax

; Load numbers into registers


mov ax, num1 ; Load num1 into AX
mov bx, num2 ; Load num2 into BX

; Perform addition
add ax, bx ; Add AX and BX, result in AX
mov cx, ax ; Store the result in CX

; Perform subtraction
sub cx, bx ; Subtract BX from CX, result in CX

_____________________________________________________________________________________
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, CUI ATTOCK CAMPUS
LAB MANUAL: MICROPROCESSOR SYSTEMS AND INTERFACING (EEE342)

; Increment and Decrement operations


inc bx ; Increment BX by 1
dec ax ; Decrement AX by 1

; End program
mov ah, 4ch ; DOS interrupt to terminate program
int 21h
main endp
end main

Procedure:

1. Open the EMU8086 emulator.


2. Create a new assembly file named arithmetic.asm.
3. Copy the provided code into the editor.
4. Assemble the program using the Compile option.
5. Execute the program using the Run option.
6. Use the debugger to observe the changes in register values after each instruction.

Post Lab Tasks:

• Write a program add AX and BX and store the result in CX.


• Record your observations in a table.

_____________________________________________________________________________________
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, CUI ATTOCK CAMPUS
LAB MANUAL: MICROPROCESSOR SYSTEMS AND INTERFACING (EEE342)

Lab 5: Binary Arithmetic Operations - Square of a Number Using Repetitive


Addition

Experiment Title:

Calculate the Square of a Number Using Repetitive Addition

Objective:

To calculate the square of a given number using repetitive addition in assembly language. This
program uses the ADD instruction to perform addition and the LOOP instruction for iteration
control.

Theory:

• Repetitive Addition: The square of a number N can be calculated as N×N by adding the
number N to itself N times.
• Instructions Used:
1. ADD: Adds the source operand to the destination operand.
2. LOOP: Decrements the CX register and jumps to a specified label until CX
becomes zero.

This approach demonstrates how basic instructions like ADD and LOOP can replace more complex
multiplication operations.

Program Code:

; Program to calculate the square of a number using repetitive addition


.model small
.stack 100h
.data
num db 5 ; Number to square
result dw 0 ; Variable to store the result
.code
main proc
; Initialize data segment
mov ax, @data
mov ds, ax

; Load the number into a register


mov al, num ; Load the number into AL
mov cx, al ; Set CX as the loop counter (number of repetitions)
xor ax, ax ; Clear AX to store the result
xor bx, bx ; Clear BX
mov bl, num ; Load the number into BL

repeat_addition:
add ax, bx ; Add BL (num) to AX (accumulated result)

_____________________________________________________________________________________
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, CUI ATTOCK CAMPUS
LAB MANUAL: MICROPROCESSOR SYSTEMS AND INTERFACING (EEE342)

loop repeat_addition ; Decrement CX and jump if CX is not zero

; Store the result in memory


mov result, ax ; Store the final result in the result variable

; End program
mov ah, 4ch ; DOS interrupt to terminate program
int 21h
main endp
end main

Procedure:

1. Open the EMU8086 emulator.


2. Create a new assembly file named binary_arithmetic.asm.
3. Copy the provided code into the editor.
4. Assemble the program using the Compile option.
5. Execute the program using the Run option.
6. Use the debugger to observe the loop iterations and the final result stored in the result
variable.

Post Lab Tasks:

• Write a program to calculate the fourth power of a number using repetitive addition.
• Record your observations in a table.

_____________________________________________________________________________________
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, CUI ATTOCK CAMPUS
LAB MANUAL: MICROPROCESSOR SYSTEMS AND INTERFACING (EEE342)

Lab 6: Logical Instructions - Bitwise Operations

Experiment Title:

Perform Bitwise Operations on Two Numbers

Objective:

To perform logical bitwise operations (AND, OR, XOR, and NOT) on two numbers using
assembly language.

Theory:

• Bitwise Operations manipulate individual bits of binary numbers.


1. AND: Performs a logical AND between each bit of two operands.
2. OR: Performs a logical OR between each bit of two operands.
3. XOR: Performs a logical exclusive OR between each bit of two operands.
4. NOT: Inverts each bit of the operand (one's complement).

These operations are fundamental in embedded systems for masking, toggling, and testing bit
values.

Program Code:

; Program to perform bitwise operations on two numbers


.model small
.stack 100h
.data
num1 db 0Fh ; First number: 15 (0x0F in hexadecimal)
num2 db 05h ; Second number: 5 (0x05 in hexadecimal)
result_and db 0 ; To store result of AND
result_or db 0 ; To store result of OR
result_xor db 0 ; To store result of XOR
result_not db 0 ; To store result of NOT (one's complement)
.code
main proc
; Initialize data segment
mov ax, @data
mov ds, ax

; Perform AND operation


mov al, num1 ; Load num1 into AL
and al, num2 ; Perform AL AND num2
mov result_and, al ; Store the result in result_and

; Perform OR operation
mov al, num1 ; Reload num1 into AL
or al, num2 ; Perform AL OR num2
mov result_or, al ; Store the result in result_or

_____________________________________________________________________________________
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, CUI ATTOCK CAMPUS
LAB MANUAL: MICROPROCESSOR SYSTEMS AND INTERFACING (EEE342)

; Perform XOR operation


mov al, num1 ; Reload num1 into AL
xor al, num2 ; Perform AL XOR num2
mov result_xor, al ; Store the result in result_xor

; Perform NOT operation


mov al, num1 ; Reload num1 into AL
not al ; Perform one's complement on AL
mov result_not, al ; Store the result in result_not

; End program
mov ah, 4ch ; DOS interrupt to terminate program
int 21h
main endp
end main

Procedure:

1. Open the EMU8086 emulator.


2. Create a new assembly file named logical_instructions.asm.
3. Copy the provided code into the editor.
4. Assemble the program using the Compile option.
5. Execute the program using the Run option.
6. Use the debugger to verify the results of AND, OR, XOR, and NOT operations stored in
the respective variables.

Post Lab Tasks:

• Write a program to perform bitwise operations (AND, OR, XOR, and NOT
operations) on two 16-bit numbers.
• Record your observations in a table.

_____________________________________________________________________________________
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, CUI ATTOCK CAMPUS
LAB MANUAL: MICROPROCESSOR SYSTEMS AND INTERFACING (EEE342)

Lab 7: Masking Operations - Extracting Specific Bits

Experiment Title:

Extract Specific Bits Using Masking Techniques

Objective:

To extract specific bits from a byte using masking techniques by applying bitwise operations
such as AND, SHL (Shift Left), and SHR (Shift Right).

Theory:

• Masking is a process in bitwise operations used to isolate, manipulate, or extract specific


bits within a binary number.
• Commonly used operations:
o AND: Used with a mask to isolate specific bits.
o SHL/SHR: Shifts bits left or right to align the desired bits.
• Example:
o To extract the lower nibble (4 bits) of a number, use a mask 0x0F (binary:
00001111) with the AND operation.

Program Code:

; Program to extract specific bits using masking techniques


.model small
.stack 100h
.data
number db 0xAB ; Example number: 0xAB (binary: 10101011)
mask_lower db 0x0F ; Mask for lower nibble (binary: 00001111)
mask_upper db 0xF0 ; Mask for upper nibble (binary: 11110000)
result_lower db 0 ; To store the lower nibble
result_upper db 0 ; To store the upper nibble
shifted_upper db 0 ; To store upper nibble after right shift
.code
main proc
; Initialize data segment
mov ax, @data
mov ds, ax

; Mask lower nibble


mov al, number ; Load number into AL
and al, mask_lower ; Apply mask to extract lower nibble
mov result_lower, al ; Store the lower nibble in result_lower

; Mask upper nibble


mov al, number ; Reload number into AL
and al, mask_upper ; Apply mask to extract upper nibble
mov result_upper, al ; Store the upper nibble in result_upper

_____________________________________________________________________________________
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, CUI ATTOCK CAMPUS
LAB MANUAL: MICROPROCESSOR SYSTEMS AND INTERFACING (EEE342)

; Shift upper nibble to the right


mov al, result_upper ; Load upper nibble into AL
shr al, 4 ; Shift right by 4 bits
mov shifted_upper, al ; Store the shifted result

; End program
mov ah, 4ch ; DOS interrupt to terminate program
int 21h
main endp
end main

Procedure:

1. Open the EMU8086 emulator and create a new assembly file named
masking_operations.asm.
2. Copy the provided code into the editor.
3. Assemble the program using the Compile option.
4. Execute the program using the Run option.
5. Observe the values of result_lower, result_upper, and shifted_upper in the
memory or debugger window.

Post Lab Tasks:

• Write a program to reverse the bit pattern of the upper half of the AX register while
keeping the lower half unchanged.
• Record your observations in a table.

_____________________________________________________________________________________
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, CUI ATTOCK CAMPUS
LAB MANUAL: MICROPROCESSOR SYSTEMS AND INTERFACING (EEE342)

Lab 8: Conditional Jumps - Comparing Two Numbers

Experiment Title:

Using Conditional Jumps to Compare Two Numbers

Objective:

To compare two numbers and display the larger one using conditional jump instructions such as
CMP, JE, JNE, JG, and JL.

Theory:

• Conditional Jumps allow a program to branch to a different location in the code based
on the result of a comparison or flag settings.
• Common instructions:
o CMP: Compares two values by subtracting one from the other but does not store
the result.
o JG: Jump if greater (signed comparison).
o JL: Jump if less (signed comparison).
o JE: Jump if equal.
o JNE: Jump if not equal.
• This experiment uses the CMP instruction and conditional jumps to identify the larger of
two numbers.

Program Code:

; Program to compare two numbers and display the larger one


.model small
.stack 100h
.data
num1 dw 0x15 ; First number (decimal 21)
num2 dw 0x20 ; Second number (decimal 32)
result db 'Larger number is: ', 0
num_msg db '0', 0
.code
main proc
; Initialize data segment
mov ax, @data
mov ds, ax

; Load the numbers into registers


mov ax, num1 ; Load first number into AX
mov bx, num2 ; Load second number into BX

; Compare the two numbers


cmp ax, bx ; Compare AX and BX
jg greater ; Jump to 'greater' if AX > BX

_____________________________________________________________________________________
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, CUI ATTOCK CAMPUS
LAB MANUAL: MICROPROCESSOR SYSTEMS AND INTERFACING (EEE342)

jl lesser ; Jump to 'lesser' if AX < BX

equal:
mov num_msg, '=' ; If numbers are equal, store '=' in num_msg
jmp display ; Jump to display result

greater:
mov num_msg, '>' ; If AX > BX, store '>' in num_msg
jmp display ; Jump to display result

lesser:
mov num_msg, '<' ; If AX < BX, store '<' in num_msg

display:
; Display result message
lea dx, result ; Load result message address
mov ah, 9 ; DOS interrupt for string display
int 21h

; Display comparison result


mov dl, num_msg ; Load the comparison result character
mov ah, 2 ; DOS interrupt for character display
int 21h

; Terminate program
mov ah, 4ch ; DOS interrupt to terminate program
int 21h
main endp
end main

Procedure:

1. Open the EMU8086 emulator and create a new assembly file named
conditional_jumps.asm.
2. Copy the provided code into the editor.
3. Assemble the program using the Compile option.
4. Execute the program using the Run option.
5. Observe the displayed message and the comparison result on the screen.

Post Lab Tasks:

• Write a program that jumps to label L1 if either bit 4, 5, or 6 is set in the BL register.
• Record your observations in a table.

_____________________________________________________________________________________
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, CUI ATTOCK CAMPUS
LAB MANUAL: MICROPROCESSOR SYSTEMS AND INTERFACING (EEE342)

Lab 9: Unconditional Jumps - Implementing an Infinite Loop

Experiment Title:

Implementing an Infinite Loop Using an Unconditional Jump

Objective:

To create an infinite loop using the JMP instruction and display a message repeatedly on the
screen.

Theory:

• Unconditional Jumps: The JMP instruction is used to unconditionally transfer control to


a specified label or address. It allows for creating loops or redirecting execution to
different parts of the program.
• An infinite loop can be implemented by using JMP to continuously jump to the start of
the same instruction or block of code, thus causing the program to repeat indefinitely.
• Use case: Displaying a message repeatedly on the screen using an infinite loop.

Program Code:

; Program to implement an infinite loop using unconditional jump


.model small
.stack 100h
.data
msg db 'Hello, this is an infinite loop!$', 0
.code
main proc
; Initialize data segment
mov ax, @data
mov ds, ax

loop_start:
; Display message
lea dx, msg ; Load address of message into DX
mov ah, 9 ; DOS interrupt for string display
int 21h ; Display the message

; Unconditional jump to the start of the loop


jmp loop_start ; Jump back to loop_start to repeat the message

main endp
end main

_____________________________________________________________________________________
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, CUI ATTOCK CAMPUS
LAB MANUAL: MICROPROCESSOR SYSTEMS AND INTERFACING (EEE342)

Procedure:

1. Open the EMU8086 emulator and create a new assembly file named
infinite_loop.asm.
2. Copy the provided code into the editor.
3. Assemble the program using the Compile option.
4. Execute the program using the Run option.
5. Observe that the message "Hello, this is an infinite loop!" will be displayed repeatedly
on the screen.
6. Terminate the program by closing the EMU8086 window or using a break command.

Post Lab Tasks:

• Write a program to calculate the sum of two numbers, using JMP instruction.
• Record your observations in a table.

_____________________________________________________________________________________
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, CUI ATTOCK CAMPUS
LAB MANUAL: MICROPROCESSOR SYSTEMS AND INTERFACING (EEE342)

Lab 10: Loops and Counters - Counting the Number of 1’s in a Binary Number

Experiment Title:

Counting the Number of 1's in a Binary Number Using a Loop and Counter

Objective:

To count the number of 1’s in a given binary number using a loop and counter. This is achieved
by processing each bit of the number using the LOOP, DEC, and JNZ instructions.

Theory:

• Looping and Counters: Loops and counters are commonly used in assembly language to
perform repetitive tasks. The LOOP instruction helps repeat an operation a specific number
of times, and the DEC instruction decrements a value, while JNZ (Jump if Not Zero)
checks whether the counter has reached zero.
• Algorithm: The process involves:
1. Checking each bit of the binary number.
2. Incrementing the counter if the bit is 1.
3. Moving to the next bit and repeating the process.
• Instructions:
o LOOP: Decreases the counter (CX) and jumps to the specified label if CX is non-
zero.
o DEC: Decrements the specified register or memory location.
o JNZ: Jumps to a label if the specified value is not zero (commonly used to
continue the loop).

Program Code:

; Program to count the number of 1's in a binary number


.model small
.stack 100h
.data
number dw 0xB6F2 ; Binary number 1011011011110010 (16-bit number)
count db 0 ; To store the count of 1's
.code
main proc
; Initialize data segment
mov ax, @data
mov ds, ax

; Load the number into AX register


mov ax, number ; Load the binary number into AX

; Initialize counter in CX (number of bits to check, 16 bits for a word)


mov cx, 16 ; Set loop counter to 16 (16-bit number)

_____________________________________________________________________________________
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, CUI ATTOCK CAMPUS
LAB MANUAL: MICROPROCESSOR SYSTEMS AND INTERFACING (EEE342)

mov dl, 0 ; Clear DL to use it for counting 1's

count_loop:
; Check the least significant bit (LSB)
test ax, 1 ; Perform AND with 1 to check LSB (AX AND 1)
jz skip_increment ; If LSB is 0, jump to skip_increment

; If the LSB is 1, increment the counter (DL)


inc dl

skip_increment:
; Shift AX right by 1 to check the next bit
shr ax, 1 ; Shift right by 1

; Decrement counter and check if it's zero


dec cx ; Decrement CX (number of bits to process)
jnz count_loop ; If CX is not zero, repeat the loop

; Store the result in 'count' variable


mov count, dl ; Store the number of 1's in 'count'

; Display the result (for simplicity, this example assumes output via the
program's termination)
; Terminate program
mov ah, 4ch ; DOS interrupt to terminate program
int 21h
main endp
end main

Procedure:

1. Open the EMU8086 emulator and create a new assembly file named count_ones.asm.
2. Copy the provided code into the editor.
3. Assemble the program using the Compile option.
4. Execute the program using the Run option.
5. Observe the program output (the program will finish execution without direct output
display, but the number of 1's is stored in the count variable, which can be checked via
debugging or extending the program to print the result).

Post Lab Tasks:

• Write a program to sort an array of numbers in ascending order using loops and
conditional jumps.
• Write a program to calculate the factorial of a given number using a loop.

_____________________________________________________________________________________
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, CUI ATTOCK CAMPUS

You might also like