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

MIC All Experiment

The document contains multiple assembly language programs (ALPs) demonstrating various operations such as block transfer using string instructions, summing hexadecimal and BCD numbers, finding the smallest and largest numbers in an array, sorting an array using bubble sort, string operations (length, concatenation, copy, reverse), and comparing two strings. Each program is structured with a data segment for variables and a code segment for execution logic. The examples illustrate fundamental concepts in assembly language programming and memory management.

Uploaded by

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

MIC All Experiment

The document contains multiple assembly language programs (ALPs) demonstrating various operations such as block transfer using string instructions, summing hexadecimal and BCD numbers, finding the smallest and largest numbers in an array, sorting an array using bubble sort, string operations (length, concatenation, copy, reverse), and comparing two strings. Each program is structured with a data segment for variables and a code segment for execution logic. The examples illustrate fundamental concepts in assembly language programming and memory management.

Uploaded by

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

Exp:7

An 8086 Assembly Language Program (ALP) to perform a block transfer


operation using string instructions (MOVSB/MOVSW) or general-purpose
registers(without string instruction).
Method 1: Using string instructions (MOVSB/MOVSW)

DATA SEGMENT
SOURCE DB 10H, 20H, 30H, 40H, 50H ; Source block (Example Data)
DEST DB 5 DUP(?) ; Destination block (Uninitialized)
LENGTH EQU 5 ; Number of bytes to transfer DATA
ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA

START:
MOV AX, DATA
MOV DS, AX ; Initialize DS with Data Segment Address
MOV ES, AX ; Initialize ES (same segment for simplicity)

MOV SI, OFFSET SOURCE ; SI points to source


MOV DI, OFFSET DEST ; DI points to destination
MOV CX, LENGTH ; Number of bytes to transfer
CLD ; Clear direction flag for forward movement

REP MOVSB ; Repeat moving CX bytes from DS:SI to ES:DI

HLT ; Halt execution

CODE ENDS
END START

Method 2: Using General-Purpose Registers

DATA SEGMENT
SOURCE DB 10H, 20H, 30H, 40H, 50H ; Source block (Example Data)
DEST DB 5 DUP(?) ; Destination block (Uninitialized)
LENGTH EQU 5 ; Number of bytes to transfer DATA
ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA

START:
MOV AX, DATA
MOV DS, AX ; Load data segment in DS
MOV ES, AX; Load extra segment in ES (same segment for simplicity)

MOV SI, OFFSET SOURCE ; SI points to source


MOV DI, OFFSET DEST; DI points to destination
MOV CX, LENGTH ; Load count of bytes to transfer

TRANSFER_LOOP:
MOV AL, [SI] ; Load byte from source
MOV [DI], AL ; Store byte to destination
INC SI ; Move to next source byte
INC DI ; Move to next destination byte
LOOP TRANSFER_LOOP ; Repeat until CX becomes 0

HLT ; Halt execution

CODE ENDS
END START

Exp. 8 ALP to find sum of series


1.Write an assembly language program to find sum of series of n
Hexadecimal numbers.

DATA SEGMENT
NUMBERS DB 05H, 0AH, 0FH, 02H, 08H ; Example hex numbers (5 bytes)
COUNT DB 05H ; Number of elements in the series
SUM DW 0000H ; Variable to store sum (word size) DATA
ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA

START:
MOV AX, DATA
MOV DS, AX ; Load data segment

MOV CX, COUNT ; Load number of elements into CX


MOV SI, OFFSET NUMBERS ; Point to the first number
XOR AX, AX ; Clear AX (Accumulator for
SUM_LOOP: sum)

MOV BL, [SI] ; Load byte from array into BL


ADD AL, BL ; Add it to AL
ADC AH, ; Add carry (for 16-bit sum)
00H
INC SI ; Move to the next element
LOOP SUM_LOOP ; Repeat until CX = 0

MOV SUM, AX ; Store the final sum in memory

HLT ; Halt execution

CODE ENDS
END START
2.Write an assembly language program to find sum of series of n BCD
numbers.
DATA SEGMENT
NUMBERS DB 12H, 25H, 37H, 19H, 45H ; Example BCD numbers
COUNT DB 05H ; Number of elements in the series
SUM DB 00H ; Variable to store the sum (BCD) DATA
ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA

START:
MOV AX, DATA
MOV DS, AX ; Load data segment

MOV CX, COUNT ; Load number of elements into CX


MOV SI, OFFSET NUMBERS ; Point to the first
number XOR AL, AL ; Clear AL (Accumulator for
sum)

SUM_LOOP:
ADD AL, [SI] ; Add BCD number to AL
DAA ; Adjust AL for valid BCD sum
INC SI ; Move to next number in the list LOOP
SUM_LOOP ; Repeat until CX = 0

MOV SUM, AL ; Store final BCD sum in memory

HLT ; Halt execution

CODE ENDS
END START

Exp. 9 ALP to find smallest and largest number from array of numbers

DATA SEGMENT
N DB 06H ; Number of elements in the array
NUMBERS DB 25H, 63H, 12H, 89H, 07H, 45H ; Array of numbers
LARGEST DB 00H ; Variable to store the largest number
SMALLEST DB 00H ; Variable to store the smallest number
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX ; Load data segment
; Load count of numbers
MOV CX, N into CX
MOV SI, OFFSET NUMBERS ; SI points to the first element
MOV AL, [SI] ; Load first element into AL
(Largest)
MOV BL, [SI] ; Load first element into BL
(Smallest)
INC SI ; Move SI to the next element
DEC CX ; Decrement CX as first element is already considered

FIND_LOOP:
MOV DL, [SI] ; Load the current number into DL

; Check for Largest


CMP AL, DL ; Compare AL (Largest so far) with DL
JAE SKIP_LARGEST ; If AL >= DL, skip updating largest
MOV AL, DL ; Else, update AL as new largest

SKIP_LARGEST:
; Check for Smallest
CMP BL, DL ; Compare BL (Smallest so far) with DL
JBE SKIP_SMALLEST ; If BL <= DL, skip updating smallest
MOV BL, DL ; Else, update BL as new smallest

SKIP_SMALLEST:
INC SI ; Move to the next number
LOOP FIND_LOOP ; Repeat until CX = 0

MOV LARGEST, AL ; Store largest number in memory


MOV SMALLEST, BL ; Store smallest number in memory

HLT ; Halt execution

CODE ENDS
END START

Exp.10 Assembly Language Program (ALP) using an iterative method (Bubble


Sort) to arrange numbers in an array in ascending or descending order.

DATA SEGMENT
N DB 06H ; Number of elements in the array
NUMBERS DB 25H, 63H, 12H, 89H, 07H, 45H ; Array of
numbers DATA ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX ; Load Data Segment
; CX = Total elements (Outer loop
MOV CX, N counter)
DEC CX ; Outer loop runs N-1 times
ITERATIVE_SORT:
MOV SI, OFFSET NUMBERS ; SI points to the first element
MOV DX, CX ; DX = CX (Inner loop counter)
COMPARE_SWAP:
MOV AL, [SI] ; Load current element into AL
MOV BL, [SI+1] ; Load next element into BL

CMP AL, BL ; Compare AL and BL


JBE NO_SWAP ; If AL <= BL, no swap needed (Ascending
Order)
; JAE NO_SWAP ; Uncomment this for Descending Order
; Swap AL and BL
MOV [SI], BL ; Store BL in current position
MOV [SI+1], AL ; Store AL in next position

NO_SWAP:
INC SI ; Move SI to next pair
DEC DX ; Decrement inner loop counter
JNZ COMPARE_SWAP ; Repeat inner loop if DX != 0

LOOP ITERATIVE_SORT ; Repeat outer loop if CX != 0

HLT ; Halt execution

CODE ENDS
END START

Exp 11.ALP to find the length of string and concatanate two strings

DATA SEGMENT
STR1 DB 'HELLO$', 0 ; First string (Null-terminated using '$')
STR2 DB ' WORLD$', 0 ; Second string (Null-terminated using '$')
LENGTH DB 00H ; Variable to store string length
CONCAT DB 20 DUP('$') ; Buffer for concatenated string (Large
enough) DATA ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA

START:
MOV AX, DATA
MOV DS, AX ; Load Data Segment

; FIND LENGTH OF STR1


MOV SI, OFFSET STR1 ; Point SI to STR1
XOR CX, CX ; Clear CX (Counter)

FIND_LENGTH:
CMP BYTE PTR [SI], '$' ; Check for end of string
JE STORE_LENGTH ; If '$' found, store length
INC SI ; Move to next character
INC CX ; Increment length count
JMP FIND_LENGTH ; Repeat
STORE_LENGTH:
MOV LENGTH, CL ; Store length of STR1

; CONCATENATE STR1 + STR2


MOV DI, OFFSET CONCAT; Destination buffer for concatenation
MOV SI, OFFSET STR1 ; Source: STR1
COPY_STR1:
MOV AL, [SI] ; Load character from STR1
MOV [DI], AL ; Store it in CONCAT
INC SI ; Move to next char in STR1
INC DI ; Move to next position in
CONCAT
CMP AL, '$' ; Check for end of STR1
JNE COPY_STR1 ; If not, continue copying

DEC DI ; Remove '$' before concatenation


MOV SI, OFFSET STR2 ; Source: STR2
COPY_STR2:
MOV AL, [SI] ; Load character from STR2
MOV [DI], AL ; Store it in CONCAT
INC SI ; Move to next char in STR2
INC DI ; Move to next position in
CONCAT
CMP AL, '$' ; Check for end of STR2
JNE COPY_STR2 ; If not, continue copying

HLT ; Halt execution

CODE ENDS
END START

Exp12.ALP for string operations such as string reverse and string copy

DATA SEGMENT
STR1 DB 'HELLO$', 0; Input string (Null-terminated using '$')
STR_COPY DB 20 DUP('$') ; Buffer to store copied string
STR_REV DB 20 DUP('$') ; Buffer to store reversed
string DATA ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA

START:
MOV AX, DATA
MOV DS, AX ; Load Data Segment

; ======= STRING COPY =======


MOV SI, OFFSET STR1 ; Source: STR1
MOV DI, OFFSET STR_COPY ; Destination: STR_COPY

COPY_LOOP:
MOV AL, [SI] ; Load character from
STR1
MOV [DI], AL ; Store it in STR_COPY
INC SI ; Move to next character
INC DI ; Move to next position
CMP AL, '$' ; Check for end of string
JNE COPY_LOOP ; If not '$', continue copying

; ======= FIND STRING LENGTH =======


MOV SI, OFFSET STR1 ; Point to the start of STR1
XOR CX, CX ; Clear CX (Counter)

FIND_LENGTH:
CMP BYTE PTR [SI], '$' ; Check for end of string
JE STORE_LENGTH ; If found, store length
INC SI ; Move to next character
INC CX ; Increment length count JMP
FIND_LENGTH ; Repeat

STORE_LENGTH:
DEC CX ; Adjust length (to exclude '$')
MOV SI, OFFSET STR1 ; Point to STR1 (Start)
ADD SI, CX ; Move SI to last character of STR1
MOV DI, OFFSET STR_REV ; Destination for reversed string

; ======= STRING REVERSE =======


REVERSE_LOOP:
MOV AL, [SI] ; Load character from STR1 (last char)
MOV [DI], AL ; Store in STR_REV
DEC SI ; Move backward in STR1
INC DI ; Move forward in STR_REV
LOOP REVERSE_LOOP ; Repeat until CX = 0

MOV BYTE PTR [DI], '$' ; Add null-terminator ('$') at end of STR_REV

HLT ; Halt execution

CODE ENDS
END START
Exp 13. ALP to compare two strings
Compare Two Strings (Without String Instructions)
DATA SEGMENT
STR1 DB 'HELLO$', 0 ; First string (Null-terminated with '$')
STR2 DB 'HELLO$', 0 ; Second string (Modify to test differences)
MSG_EQUAL DB 'STRINGS ARE EQUAL$', 0
MSG_NOT_EQUAL DB 'STRINGS ARE NOT EQUAL$',
0 DATA ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX ; Load Data Segment

MOV SI, OFFSET STR1 ; SI points to STR1


MOV DI, OFFSET STR2 ; DI points to STR2
COMPARE_LOOP:
MOV AL, [SI] ; Load character from
STR1
MOV BL, [DI] ; Load character from
STR2
CMP AL, BL ; Compare characters
JNE NOT_EQUAL ; If mismatch found, jump to NOT_EQUAL

CMP AL, '$' ; Check for end of string


JE EQUAL ; If end reached and no mismatch, strings are equal

INC SI ; Move to next character in STR1


INC DI ; Move to next character in STR2 JMP
COMPARE_LOOP ; Repeat comparison

EQUAL:
MOV DX, OFFSET MSG_EQUAL ; Load message "STRINGS ARE
EQUAL" JMP DISPLAY_MSG

NOT_EQUAL:
MOV DX, OFFSET MSG_NOT_EQUAL ; Load message "STRINGS ARE NOT
EQUAL"

DISPLAY_MSG:
; Simulate printing the message (for emulator)
HLT ; Halt execution

CODE ENDS
END START

Method 2:Compare Two Strings Using CMPSB


DATA SEGMENT
STR1 DB 'HELLO$', 0 ; First string (Null-terminated with '$')
STR2 DB 'HELLO$', 0 ; Second string (Modify to test differences)
MSG_EQUAL DB 'STRINGS ARE EQUAL$', 0
MSG_NOT_EQUAL DB 'STRINGS ARE NOT EQUAL$',
0 DATA ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA

START:
MOV AX, DATA
MOV DS, AX ; Load Data Segment
MOV ES, AX ; Load Extra Segment (for STR2)

MOV SI, OFFSET STR1 ; SI points to STR1 (source)


MOV DI, OFFSET STR2 ; DI points to STR2 (destination)

COMPARE_LOOP:
LODSB ; Load AL with byte from STR1 (SI increments)
SCASB ; Compare AL with byte in STR2 (DI increments)

JNE NOT_EQUAL ; If mismatch found, jump to NOT_EQUAL

CMP AL, '$' ; Check for end of string


JNE COMPARE_LOOP ; If not end, continue comparing

EQUAL:
MOV DX, OFFSET MSG_EQUAL ; Load message "STRINGS ARE
EQUAL" JMP DISPLAY_MSG

NOT_EQUAL:
MOV DX, OFFSET MSG_NOT_EQUAL ; Load message "STRINGS ARE NOT
EQUAL"

DISPLAY_MSG:
; Simulate printing the message (for emulator)
HLT ; Halt execution

CODE ENDS
END START

Exp.14 ALP to check a given number is odd or even

DATA SEGMENT
NUM DB 5 ; Change this value to test different numbers
MSG_EVEN DB 'NUMBER IS EVEN$', 0
MSG_ODD DB 'NUMBER IS ODD$', 0
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX ; Load Data Segment

MOV AL, NUM ; Load the number into AL


TEST AL, 01H ; Check the least significant bit
(LSB)
JZ EVEN ; If LSB is 0, number is even
ODD:
MOV DX, OFFSET MSG_ODD ; Load "NUMBER IS ODD"
JMP DISPLAY_MSG
EVEN:
MOV DX, OFFSET MSG_EVEN ; Load "NUMBER IS EVEN"

DISPLAY_MSG:
; Simulate printing the message (for emulator)
HLT ; Halt execution

CODE ENDS
END START

15. ALP to check a given number is positive or negative


Method 1:Use rotate instructions to check the given number is positive or
negative.
.model small
.stack 100h
.data msg_pos db 'The number is
Positive.$' msg_neg db 'The
number is Negative.$'
.code
main:
mov ax, @data mov ds, ax mov al, -8 ; Load a number
to check (you can change this)

; Rotate MSB into Carry Flag


rol al, 1 ; MSB -> CF, AL is rotated left by
1 bit ; Now CF = original MSB

jc is_negative; If CF = 1 (i.e., original MSB = 1), it's negative

is_positive:
lea dx,
msg_pos mov
ah, 09h int
21h jmp exit
is_negative:
lea dx,
msg_neg mov
ah, 09h int
21h

exit:
mov ah, 4ch
int 21h
end main

Methode 2: Using Test instruction


.model small
.stack 100h
.data msg_pos db 'The number is
Positive.$' msg_neg db 'The
number is Negative.$'
.code
main:
mov ax, @data ; Initialize data segment mov ds, ax mov
al, -5 ; Load a number to test (you can change this value)

; Check sign of AL
test al, al ; Sets flags based on AL (including Sign flag)
jns is_positive ; Jump if Sign Flag = 0 (i.e., number is
positive)

is_negative:
lea dx,
msg_neg mov
ah, 09h int
21h jmp exit

is_positive:
lea dx,
msg_pos mov
ah, 09h int
21h

exit:
mov ah, 4ch
int 21h
end main

16. ALP to count number of' ‘0' and '1's in a given number
.model small
.stack 100h
.data
ones_msg db 'Number of 1s: $'
zeros_msg db 'Number of 0s: $'
.code
main:
mov ax, @data mov ds, ax mov al, 0F3h ; Example
number (11110011b), change as needed
mov bl, 8 ; Counter for 8 bits
mov ch, 0 ; Count of 1's
mov cl, 0 ; Count of 0's

check_bit:
ror al, 1 ; Rotate right, LSB -
> CF
jc count_one ; If CF=1, it's a '1'

inc cl ; Count 0s
jmp next_bit

count_one:
inc ch ; Count 1s

next_bit: dec
bl jnz
check_bit

; Print number of
1s lea dx,
ones_msg mov
ah, 09h int 21h

mov dl, ch ; Move 1s count to


DL add dl, 30h ; Convert to
ASCII mov ah, 02h int 21h

; Print number of
0s lea dx,
zeros_msg mov
ah, 09h int 21h

mov dl, cl ; Move 0s count to


DL add dl, 30h ; Convert to
ASCII mov ah, 02h ;Start of
text int 21h

; Exit
mov ah, 4ch
int 21h
end main
17.ALP to Perform Arithmetic Operations Using Procedures

.model small
.stack 100h
.data
num1 db 10
num2 db 5

msg_add db 'Addition: $'


msg_sub db 'Subtraction: $'
msg_mul db 'Multiplication:
$' msg_div db 'Division: $'

.code
main: mov ax,
@data mov
ds, ax

; ------------ Addition ------------


lea dx,
msg_add mov
ah, 09h int
21h

call addition
call
print_result

; ------------ Subtraction ------------


lea dx,
msg_sub mov
ah, 09h int
21h

call subtraction
call print_result

; ------------ Multiplication ------------


lea dx,
msg_mul mov
ah, 09h int
21h

call multiplication
call print_result

; ------------ Division ------------


lea dx,
msg_div mov
ah, 09h int
21h

call division
call
print_result

; Exit
program
mov ah,
4ch int 21h

; =============================
; Procedures
; =============================

; --- Addition --addition proc


mov al, num1 add al, num2
mov bl, al ; Store result in BL
ret
addition endp
; ---
Subtraction --
subtraction
proc mov al,
num1 sub al,
num2
mov bl, al
ret
subtraction endp

; --- Multiplication --multiplication


proc mov al, num1 mov bl, num2
mul bl ; AL = AL * BL, result in AL
mov bl, al
ret
multiplication endp

; --- Division --
division proc
mov al,
num1 mov
ah, 0
mov bl,
num2
div bl ; AL = AL / BL, remainder in
AH mov bl, al
ret
division endp
; --- Print Result --
print_result proc
mov dl, bl add dl, 30h ;
Convert to ASCII mov ah,
02h
int 21h
mov dl, 10 ; New line
mov ah, 02h int 21h
mov dl, 13 int 21h
ret
print_result endp

end main

18.ALP to perform arithmetic operations on given numbers using macro


.model small
.stack 100h
.data
num1 db 12
num2 db 4
msg_add db
'Addition: $'
msg_sub db
'Subtraction: $'
msg_mul db
'Multiplication:
$' msg_div db
'Division: $'
.code

; ================================
; MACROS
; ================================

ADD_MACRO MACRO
a, b mov al, a add
al, b
mov bl, al
ENDM

SUB_MACRO MACRO
a, b mov al, a sub
al, b mov bl, al
ENDM

MUL_MACRO MACRO
a, b mov al, a mov
bl, b
mul bl ; AL = AL * BL
mov bl, al
ENDM
DIV_MACRO MACRO a, b mov
al, a mov ah, 0 mov bl, b
div bl ; AL = AL / BL, result in AL
mov bl, al
ENDM

PRINT_RESULT MACRO mov dl, bl


add dl, 30h ; Convert result to
ASCII mov ah, 02h int 21h

; New
line mov
dl, 10 int
21h mov
dl, 13 int
21h
ENDM

main:
mov ax, @data
mov ds, ax

; ----------- Addition -----------


lea dx, msg_add mov
ah, 09h int 21h
ADD_MACRO num1,
num2 PRINT_RESULT

; ----------- Subtraction --------


lea dx, msg_sub mov
ah, 09h int 21h
SUB_MACRO num1,
num2 PRINT_RESULT

; ----------- Multiplication -----


lea dx, msg_mul mov
ah, 09h int 21h
MUL_MACRO num1,
num2 PRINT_RESULT

; ----------- Division -----------


lea dx, msg_div mov
ah, 09h int 21h
DIV_MACRO num1,
num2 PRINT_RESULT

; ----------- Exit ---------------


mov ah,
4ch int 21h
end main

You might also like