Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

DIGITAL LOGIC &

MICROPROCESSOR THEORY
SWE-1003

DA-1
SUBMITTED BY:
SUMIT PAUDEL
22MIS0401
Write an 8086 assembly language program for the following
1.Program to get the count of even numbers from the list of n numbers.
; Program to count even numbers in a list of n numbers
ORG 100h ; Set the origin to 100h for COM file

section .data
n dw 5 ; Number of elements in the list
numbers db 1, 2, 3, 4, 5 ; List of numbers
section .text
global _start
_start:

mov ax, 0 ; Initialize the counter for even numbers


mov cx, [n] ; Load the number of elements into CX
lea si, [numbers] ; Load the address of the numbers array into SI

count_even:
mov al, [si] ; Load the current number into AL
and al, 1 ; Check if it's even (by checking the LSB)
jz is_even ; If the result of AND operation is 0, it's even

not_even:
inc si ; Move to the next number in the array
loop count_even ; Continue the loop for the next element
jmp done ; Exit the loop

is_even:
inc ax ; Increment the count of even numbers
inc si ; Move to the next number in the array
loop count_even ; Continue the loop for the next element

done:
; At this point, AX contains the count of even numbers
; You can use it for further processing or display
; (e.g., printing the count)

; Exit the program


mov ah, 4Ch ; DOS function to exit
int 21h ; Call DOS

; End of the program

2. Program to generate a Fibonacci sequence.


ORG 100h ; Set the origin to 100h for COM file

section .data
n db 10 ; Number of Fibonacci numbers to generate

section .bss
fib_numbers resw 10 ; Array to store Fibonacci numbers

section .text
global _start
_start:
mov cx, [n] ; Load the number of Fibonacci numbers to generate
mov bx, 0 ; Initialize loop counter
mov word [fib_numbers], 0 ; Initialize the first Fibonacci number
mov word [fib_numbers + 2], 1 ; Initialize the second Fibonacci number

generate_fibonacci:
; Calculate the next Fibonacci number
mov ax, [fib_numbers + bx]
add ax, [fib_numbers + bx + 2]
mov [fib_numbers + bx + 4], ax ; Store the result

; Increment the loop counter


add bx, 2

loop generate_fibonacci ; Repeat the loop until all numbers are generated

; Print the Fibonacci numbers


mov cx, [n] ; Reset the loop counter
mov si, 0

print_fibonacci:
mov ax, [fib_numbers + si]
call print_number

; Print a comma and a space


mov dl, ','
mov ah, 2
int 21h
mov dl, ' '
int 21h

add si, 2
loop print_fibonacci

; Exit the program


mov ah, 4Ch
int 21h

print_number:
; Print the number in AX
push ax

mov cx, 0
mov bx, 10

reverse_loop:
xor dx, dx
div bx
add dl, '0'
push dx
inc cx
test ax, ax
jnz reverse_loop

print_loop:
pop dx
mov ah, 2
int 21h
loop print_loop

; Print a newline
mov dl, 10
int 21h

pop ax
ret

; End of the program

3. Program to sort a given array of elements.


ORG 100h ; Set the origin to 100h for COM file

section .data
array db 7, 1, 3, 9, 2, 5, 8, 4, 6, 0 ; Array to be sorted
n db 10 ; Number of elements in the array

section .text
global _start
_start:

; Load the array and n into registers


lea si, [array]
mov cl, [n]

sort_loop:
xor bh, bh ; Reset flag to 0 (no swaps yet)
mov ch, cl ; Set the inner loop counter to n

inner_loop:
mov al, [si] ; Load the current element
mov dl, [si + 1] ; Load the next element

cmp al, dl
jle no_swap ; If al <= dl, no swap needed

; Swap the elements


mov [si], dl
mov [si + 1], al
mov bh, 1 ; Set the flag to 1 (swap occurred)

no_swap:
inc si
loop inner_loop

; If no swaps occurred in the inner loop, the array is sorted


test bh, bh
jz done

; Decrement the outer loop counter and repeat the sorting


dec cl
jmp sort_loop

done:
; The sorted array is now in memory, you can print or use it as needed

; Exit the program


mov ah, 4Ch
int 21h

; End of the program

4. Square of a 16 bit number using 8086.


ORG 100h ; Set the origin to 100h for COM file

section .text
global _start
_start:

; Assuming the 16-bit number to be squared is already in AX

; Multiply AX by itself to compute the square


imul ax, ax

; At this point, AX contains the square of the input number

; Exit the program


mov ah, 4Ch
int 21h
; End of the program

5. Program to get the count of odd numbers from the list of n numbers
ORG 100h ; Set the origin to 100h for COM file

section .data
n db 5 ; Number of elements in the list
numbers db 1, 2, 3, 4, 5, 0 ; List of numbers, terminated with 0

section .text
global _start
_start:

mov ax, 0 ; Initialize the counter for odd numbers


mov cx, [n] ; Load the number of elements into CX
lea si, [numbers] ; Load the address of the numbers array into SI

count_odd:
mov al, [si] ; Load the current number into AL
cmp al, 0 ; Check if it's the end of the list
je done ; If it's the end of the list, exit the loop

test al, 1 ; Test if the least significant bit is set (odd


number)
jnz is_odd ; If the result of the test is not zero, it's an odd
number

not_odd:
inc si ; Move to the next number in the array
loop count_odd ; Continue the loop for the next element
jmp done

is_odd:
inc ax ; Increment the count of odd numbers

inc si ; Move to the next number in the array


loop count_odd ; Continue the loop for the next element

done:
; At this point, AX contains the count of odd numbers
; You can use it for further processing or display

; Exit the program


mov ah, 4Ch ; DOS function to exit
int 21h ; Call DOS

; End of the program


Use any one open source software other than Multisim to implement the following circuits.

1. Design a Half adder and Full adder / Half Subtractor and Full Subtractor
HALF ADDER

FULL ADDER
Half Subtractor

Full Subtractor
2. Design a BCD to Excess-3 code converter using logic gates.

You might also like