0% found this document useful (0 votes)
10 views64 pages

Microprocessors Lab

The document is a record of a Microprocessors laboratory course, detailing various exercises performed by a student named Preethi S. It includes aims, source codes, and results for tasks such as addition and subtraction of numbers, character occurrences, palindrome checking, and counting binary digits. Each exercise provides a structured approach to programming in assembly language using 8086 instructions.

Uploaded by

preethisnj09
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)
10 views64 pages

Microprocessors Lab

The document is a record of a Microprocessors laboratory course, detailing various exercises performed by a student named Preethi S. It includes aims, source codes, and results for tasks such as addition and subtraction of numbers, character occurrences, palindrome checking, and counting binary digits. Each exercise provides a structured approach to programming in assembly language using 8086 instructions.

Uploaded by

preethisnj09
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

Microprocessors lab

Record

NAME : PREETHI .S
REG NO:2303717672622045
COURSE : MSC SOFTWARE SYSTEMS
COURSE CODE: 20MSS46
DATE:22-02-2025
SUBJECT:MICROPROCESSORS LABORATORY
[Link] HEADING REMARKS
1 . ADDITION OF TWO 8 BIT &
16 bit NUMBERS
2 . ADDITION OF TWO ONE
DIGIT DECIMAL NUMBERS
3. ADDITION OF 2 TWO DIGIT
DECIMAL NUMBERS
4. SUBTRACTION OF ARRAY OF
NUMBERS
5. OCCURENCES OF
CHARACTER IN THE STRING
6. PALINDROME CHECKER
7. COUNT OF 0’S AND 1’S
8. LARGEST AND SMALLEST
NUMBER
9. MIDDLE VALUE OF AN
ARRAY
10. ARRANGING AN ARRAY IN
ASCENDING & DESC ORDER
11. REPLACEMENT OF STRINGS
12. REPLACEMENT OF STRINGS
EX NO:1 16 BIT AND 8 BIT ADDITION
DATE:

AIM:
Write ALP programs using 8086 instructions to
perform:
 addition of two 8-bit numbers
 addition of two 16-bit numbers

SOURCE CODE:
LABEL SOURCE COMMENTS
8 -BIT

name "add-sub"

org 100h

mov al, 11100011b


mov bl, 10101011b

add bl, al ; print result in


mov cx, 8 binary:
print:
mov ah, 2 ;print function
mov dl, '0
test bl, 10000000b
;test first bit
jz zero
mov dl, '1'
zero: int 21h
shl bl, 1
loop print
; print binary suffix:
mov dl, 'b'
int 21h

ret ;return

LABEL SOURCE COMMENTS

16 BIT
name "add-sub"

org 100h

mov ax,
1010101010101010b
mov bx,
1100110011001100b

add bx, ax
mov cx, 16
; print result in
print: mov ah, 2
binary:
mov dx, '0'
test bx,
1000000000000000b ;
test first bit. ;print function
jz zero
mov dx, '1'
zero: int 21h
shl bx, 1
loop print
; print binary suffix:
mov dx, 'b'
int 21h ; wait for any key
press:
;mov ah, 0
;int 16h

ret

OUTPUT:
RESULT :

8 bit and 16 bit addition is done successfully.


EX NO:2 ADDING TWO NUMBERS

AIM:
Write an 8086 ALP program to add two one-digit decimal
numbers. Input the numbers during run time and generate
the one-digit arithmetic output on the emulator screen.

SOURCE CODE:
LABEL SOURCE COMMENTS

org 100h

lea dx, m1 ; Display message


mov ah, 09h for the first
int 21h number

mov ah, 01h ; Input the first


int 21h number
sub al, '0'
mov bl, al

lea dx, m2 ; Display message


mov ah, 09h for the second
int 21h number
mov ah, 01h ; Input the second
int 21h number
sub al, '0'
mov bh, al ;Add the two
numbers
add bl,bh
add bl,'0'
lea dx, m3
mov ah, 09h
int 21h

mov dl, bl
mov ah, 2
int 21h

ret
M1 m1 db "enter the
first number(0-9):
$"
M2 m2 db 0dh, 0ah,
"enter the second
number(0-9):$"
m3 db 0dh, 0ah,
M3 "the addition of
two numbers are:
$"

OUTPUT:
RESULT:
Two one digit decimal numbers added successfully.

EX NO:3 TWO DIGITS DECIMAL NUMBERS


AIM:
Write an 8086 ALP program to add two two-digit decimal
numbers. Input the numbers during run time and generate
more than two-digit arithmetic output on the emulator
screen.

SOURCE CODE:

LABEL SOURCE COMMENTS


org 100h ;orgin

mov dx, offset


m1
mov ah,9
int 21h
mov ah,1
int 21h

sub al,'0'
mov bl,al

mov dx,offset
m2
mov ah,9
int 21h
mov ah,1
int 21h

sub al,'0'

mov cx,10
mul cl
add bl,al

mov dx,offset
m3
mov ah,9
int 21h
mov ah,1
int 21h
sub al,'0'
mov cl,al

mov dx,offset
m4
mov ah,9
int 21h
mov ah,1
int 21h
sub al,'0'
mov ch,10
mul ch
add cl,al

add cl,bl
mov al,cl
aam

mov cx,ax

add ch,'0'
add cl,'0'
mov dx,offset
m5
mov ah,9
int 21h

mov dl,ch
mov ah,2
int 21h

mov dl,cl
mov ah,2
int 21h

hlt
; add your code
ret here
m1 db 0Dh,0Ah,
"Enter the unit
M1 digit of first
number:$"

m2 db 0Dh,0Ah,
"Enter the tens
M2 digit of first
number:$"
m3 db 0Dh,0Ah,
"Enter the unit
digit of second
number:$"
M3
m4 db 0Dh,0Ah,
"Enter the tens
digit of second
number:$"
m5 db 0Dh,0Ah,
M4 "Sum is :$"

OUTPUT:
RESULT:
Two digit decimal addition done successfully.

EX NO:4 SUBTRACT DECIMAL NUMBERS


AIM:
Write an 8086 ALP program to subtract N set of
decimal numbers. Input the numbers during run
[Link] and display the count 0 output on the
emulator screen.

SOURCE CODE:
LABEL SOURCE COMMENTS
org 100h

start:
lea dx, msg1
mov ah, 9
int 21h

mov ah, 1
int 21h
sub al, 30h
mov cl, al

lea dx, msg2 ; Store n in cl


mov ah, 9
int 21h

mov ah, 1
int 21h
sub al, 30h ; Read the first
mov bl, al number

dec cl

next_num:
cmp cl, 0
je
display_result

lea dx, msg2


mov ah, 9
int 21h

mov ah, 1
int 21h
sub al, 30h
sub bl, al
dec cl ; Read the
next number

jmp next_num ; Subtract the


number from BL
display_result:
;Decrement
lea dx, result
the counter
mov ah, 9
int 21h

add bl, 30h


mov dl, bl
mov ah, 2
int 21h ; Store result in
DL for printing

msg1 db "Enter
the count of
numbers(n): $"
msg2 db
0Dh,0Ah, "Enter
a number: $"
result db
M1 0Dh,0Ah, "The
result is: $"

M2 end start
OUTPUT:

RESULT:
Subtraction of n digit number is done successfully.

EX NO:5 CHARACTER OCCURANCE

AIM:
Write a program to find the occurrence(s) of two
different characters in a given string.
SOURCE CODE:
LABEL SOURCE COMMENTS
org 100h

lea dx, m1
mov ah, 9
int 21h

mov ah, 0Ah


lea dx, string
int 21h

lea dx, m2
mov ah, 9
int 21h

mov ah, 1
int 21h
mov ch, al

lea dx, m3
mov ah, 9
int 21h

mov ah, 1
int 21h
mov cl, al

mov si, 2
mov count1, 0
mov count2, 0

char_loop:
mov al, [string
+ si]
cmp al, 0Dh
je print

cmp al, ch
je
char_match1

cmp al, cl
je
char_match2

inc si
jmp char_loop

char_match1:
inc count1
inc si
jmp char_loop

char_match2:
inc count2
inc si
jmp char_loop
print:
lea dx, m4
mov ah, 9
int 21h

mov al, count1


call
print_number

lea dx, m5
mov ah, 9
int 21h

mov al, count2


call
print_number

mov ah, 4Ch


int 21h

print_number:
xor ah, ah
mov bl, 10
div bl
add ah, '0'
mov dl, ah
mov ah, 2
int 21h
cmp al, 0
je
end_number
add al, '0'
mov dl, al
mov ah, 2
int 21h
end_number:
ret

m1 db 0Dh, 0Ah,
'Enter string:$'
m2 db 0Dh, 0Ah,
'Enter 1st letter:
$'
m3 db 0Dh, 0Ah,
M1 'Enter 2nd letter:
$'
m4 db 0Dh, 0Ah,
M2 'Occurrence of
1st letter: $'
m5 db 0Dh, 0Ah,
M3 'Occurrence of
2nd letter: $'
string db 255, ?,
M4 255 dup(?)
ch1 db ?
ch2 db ?
count1 db 0
count2 db 0
M5 db 20, 0
db 20 dup('$')
OUTPUT:

RESULT:
Occurance of the letter is done successfully.

EX NO:6 PALINDROME OR NOT PALINDROME


DATE:

AIM:
Read a string from the keyboard and find whether the
input string is a palindrome or not.
SOURCE CODE:
LABEL SOURCE COMMENTS
org 100h

start:
mov si, 0250h
lea dx, m1
call prompt
mov cl, 0

read_loop:
call input
cmp al, 0dh
je loop_new
mov di, si
mov [si], al
inc si
jmp read_loop

loop_new:
mov byte ptr
[si], 00
dec si
mov di, 0250h
call
check_loop
ret
check_loop:
mov al, [si]
mov bl, [di]
cmp al, bl
jne no_pali
inc di
;
dec si
cmp di, si
jne
check_loop
lea dx, m2
call prompt
ret

no_pali:
lea dx, m3
call prompt
ret
i cs
print:
mov ah, 02h
int 21h
ret

prompt:
mov ah, 09h
int 21h
ret

input:
mov ah, 01h
int 21h
ret

m1 db "enter
the string: $"
m2 db 0dh, 0ah,
M1 "the string is
palindrome:$"
M2 m3 db 0dh, 0ah,
"the string is not
palindrome:$"

M3 ret
OUTPUT:

RESULT:
Checking palindrome or not done successfully.

EX NO:7 COUNT 1’S AND 0’S

AIM:
Read a byte from the keyboard as input. Write a code
to count the number of 1’s and 0’s in the byte. Display
the output on the emulator screen.

SOURCE CODE:
LABEL SOURCE COMMENTS
.MODEL SMALL ; Initialize data
.STACK 100H segment
.DATA
M1 MSG1 DB ; Display "Enter a
'Enter a string: $' string: "
MSG2 DB ; Read string
M2 0DH,0AH, 'Total input
0s: $' ; Convert
MSG3 DB characters to
M3 0DH,0AH, 'Total binary and count
1s: $' 0s and 1s
BUFFER DB 50 ; Load character
DUP('$') ; Check for Enter
COUNT0 DB 0 (Carriage Return)
COUNT1 DB 0 ; If Enter, stop
processing
.CODE ; 8 bits per
MAIN PROC character
MOV AX, ; Rotate left to
@DATA get MSB
MOV DS, AX

MOV DX,
OFFSET MSG1 ; If carry flag is
MOV AH, 09H set, it's a 1
INT 21H ; Otherwise, it's
a0
MOV SI, ; Repeat for 8
OFFSET BUFFER bits
MOV CX, 50
-
MOV AH, 0AH
INT 21H

MOV SI,
OFFSET BUFFER
PROCESS_CHAR:
MOV AL, [SI]
CMP AL, 0DH
JE DONE ; Move to the
next character
MOV BL, 8 ; Process next
BIT_LOOP: character
ROL AL, 1 ;
JC
COUNT_ONE
INC COUNT0
JMP NEXT_BIT

COUNT_ONE:
INC COUNT1

NEXT_BIT:
DEC BL
JNZ BIT_LOOP
INC SI
JMP
PROCESS_CHAR

DONE:
MOV DX,
OFFSET MSG2
MOV AH, 09H
INT 21H

MOV AL,
COUNT0
AAM
ADD AX,
3030H
MOV DL, AH
MOV AH, 02H
INT 21H
MOV DL, AL
INT 21H

MOV DX,
OFFSET MSG3
MOV AH, 09H
INT 21H Display "Total 0s:
"
MOV AL, ; Display count
COUNT1 of 0s
AAM ; Convert to
ADD AX, ASCII
3030H ; Display "Total
MOV DL, AH 1s: "
MOV AH, 02H ; Display count
INT 21H of 1s
MOV DL, AL
INT 21H

MOV AH, 4CH


INT 21H

MAIN ENDP ; Exit program


END MAIN
OUTPUT:

RESULT :

To perform code to count the number of 1’s and 0’s in the byte has been done successfully
EX NO :8 LARGEST AND SMALLEST NUMBER
27.1.25

AIM:
To write an ALP code to find the largest number and smallest number in a
given array.

TABLE :

8 LABEL CODE COMMENTS


Program Start org 100h Set program start
& Jump address and jump to the
main code.

jmp start

arr db 10 dup(0)
count db 0
largest db 0
smallest db 0

start:
mov dx, offset msg1
mov ah, 9
int 21h

mov ah, 1
int 21h
sub al, '0'
mov count, al
mov cl, al
mov ch, 0
mov si, 0

Display Array mov dx, offset msg2


Input Prompt mov ah, 9
int 21h
input_loop:
mov ah, 1 Loop to read each
element from the
int 21h
keyboard and store it in
sub al, '0' the array.
mov arr[si], al
inc si
loop input_loop

mov si, 0
mov al, arr[si]
mov bl, al
mov cl, count
dec cl
inc si

find_loop:
cmp cl, 0
je done Loop through the array
to update the largest (AL)
and smallest (BL) values.
mov dl, arr[si]
cmp al, dl
jge check_smallest
mov al, dl

check_smallest:
cmp bl, dl
jle next_iter
mov bl, dl

next_iter:
inc si
dec cl
jmp find_loop

done:
mov largest, al
mov smallest, bl

mov dx, offset msg3


mov ah, 9
int 21h
Display the "Largest:"
message and output the
mov dl, largest
largest number
add dl, '0' (converted to ASCII).
mov ah, 2
int 21h

mov dx, offset msg4


mov ah, 9
int 21h

mov dl, smallest


add dl, '0'
mov ah, 2
int 21h Display the "SMALLEST:"
message and output the
SMALLEST number
hlt (converted to ASCII).
Program
Termination
msg1 db 'Enter the
number of elements: $'
Halt the prg.
msg2 db 0Dh, 0Ah, 'Enter
numbers: $'
msg3 db 0Dh, 0Ah,
'Largest: $'
msg4 db 0Dh, 0Ah,
'Smallest: $'
OUTPUT:

RESULT : To find the largest number and smallest number in a given array
has been done sucecssfully
EX NO : 9 MIDDLE VALUE OF AN ARRAY
3.2.25

AIM :
To create an odd sized array and write a code to find the middle value of the
given set of numbers.

TABLE :

9 LABEL CODE COMMENTS


Setup & Data .MODEL SMALL Define memory model,
Declaration stack, array, messages,
.STACK 100H
and variables for size
.DATA and middle value.
ARRAY DB 100
DUP(?)
MSG1 DB 'Enter
the size of the
array (odd
number): $'
MSG2 DB 0DH,
0AH, 'Enter the
numbers: $'
MSG3 DB 0DH,
0AH, 'Middle
value: $'

Program SIZE DB ?
Initialization MIDDLE DB ?
Initialize the data
segment.
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX

LEA DX, MSG1


MOV AH, 09H
INT 21H

MOV AH, 01H


INT 21H
SUB AL, '0'
MOV SIZE, AL

LEA DX, MSG2


MOV AH, 09H
INT 21H Read SIZE number of
characters, convert
each from ASCII, and
MOV CL, SIZE store them in ARRAY
MOV CH, 0
LEA SI, ARRAY
READ_LOOP:
MOV AH, 01H
INT 21H
SUB AL, '0'
MOV [SI], AL
INC SI
LOOP
READ_LOOP

MOV AL, SIZE


SHR AL, 1
MOV AH, 0
LEA SI, ARRAY
ADD SI, AX
Display the "Middle
MOV BL, [SI] value:" message and
output the middle
element (converted to
ASCII).
LEA DX, MSG3
MOV AH, 09H
INT 21H
MOV DL, BL
ADD DL, '0'
MOV AH, 02H
INT 21H
Terminate the program.
Program
Termination
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN

OUTPUT:

RESULT : To create an odd sized array and write a code & to find the middle
value of the given set of numbers has been done successfully.
EX NO : 10 ARRANGING AN ARRAY IN ASCENDING & DESC
5.2.25

AIM:
To write an alp program to sort the given numbers in 1) ascending order and 2)
descending order.

TABLE :
10 LABEL CODE COMMENTS
Setup & Data Define memory model,
Declaration .MODEL SMALL stack, messages, array,
.STACK 100H and variable to store
the number of
elements.
.DATA
msg_size DB
0Dh, 0Ah, 'Enter the
number of elements:
$'
msg_element DB
0Dh, 0Ah, 'Enter
element $'
msg_colon DB ':
$'
msg_sorted_asc
DB 0Dh, 0Ah, 'Sorted
in Ascending Order:
$'
msg_sorted_desc
DB 0Dh, 0Ah, 'Sorted
in Descending Order:
$'
array DW 100
Program DUP(?)
Initialization size DW ? Initialize the data
segment.

.CODE
.STARTUP
MOV AX, @DATA
MOV DS, AX
MOV DX, OFFSET Display prompt, read
msg_size the number of
elements, store it in
MOV AH, 09H
SIZE, and check for valid
INT 21H (positive) size.
CALL READ_NUM
MOV size, AX

CMP AX, 0
JLE
END_PROGRAM

MOV CX, AX
MOV SI, 0
MOV DI, 1

READ_LOOP:
PUSH CX
MOV DX, OFFSET
msg_element
MOV AH, 09H
INT 21H
MOV AX, DI
CALL PRINT_NUM
MOV DX, OFFSET
msg_colon
MOV AH, 09H
INT 21H

CALL READ_NUM
MOV array[SI], AX

ADD SI, 2
INC DI
POP CX
LOOP READ_LOOP

CALL
BUBBLE_SORT_ASC

MOV DX, OFFSET


Display a message and
msg_sorted_asc
then print the sorted
MOV AH, 09H array (in ascending
order).
INT 21H
CALL
PRINT_ARRAY

CALL
BUBBLE_SORT_DESC

MOV DX, OFFSET


Display a message and
msg_sorted_desc
then print the sorted
MOV AH, 09H array (in descending
order).
INT 21H
CALL
PRINT_ARRAY

END_PROGRAM:
MOV AH, 4CH
INT 21H

BUBBLE_SORT_ASC
PROC
PUSH CX
PUSH DX
Sort the array in
PUSH SI ascending order using
bubble sort (nested
PUSH DI
loops to swap adjacent
elements as needed).
MOV CX, size
DEC CX

SORT_OUTER_ASC:
MOV SI, 0
MOV DI, CX

SORT_INNER_ASC:
MOV AX, array[SI]
MOV DX,
array[SI+2]
CMP AX, DX
JLE
NO_SWAP_ASC
MOV array[SI], DX
MOV array[SI+2],
AX

NO_SWAP_ASC:
ADD SI, 2
DEC DI
JNZ
SORT_INNER_ASC

LOOP
SORT_OUTER_ASC

POP DI
POP SI
POP DX
POP CX
RET
BUBBLE_SORT_ASC Sort the array in
ENDP descending order using
bubble sort (nested
loops to swap adjacent
BUBBLE_SORT_DESC elements as neede
PROC
PUSH CX
PUSH DX
PUSH SI
PUSH DI

MOV CX, size


DEC CX

SORT_OUTER_DESC:
MOV SI, 0
MOV DI, CX

SORT_INNER_DESC:
MOV AX, array[SI]
MOV DX,
array[SI+2]
CMP AX, DX
JGE
NO_SWAP_DESC

MOV array[SI], DX
Sort the array in
MOV array[SI+2],
descending order using
AX
bubble sort (nested
loops to swap adjacent
elements as neede
NO_SWAP_DESC:
ADD SI, 2
DEC DI
JNZ
SORT_INNER_DESC
LOOP
SORT_OUTER_DESC

POP DI
POP SI
POP DX
POP CX
RET
BUBBLE_SORT_DESC
ENDP

PRINT_ARRAY PROC
PUSH CX
PUSH DX
PUSH SI

MOV CX, size


MOV SI, 0

PRINT_ARRAY_LOOP:
MOV AX, [array +
SI]
CALL PRINT_NUM
MOV DL, ' '
MOV AH, 02H
INT 21H
ADD SI, 2
LOOP
PRINT_ARRAY_LOOP

Read a multi-digit
POP SI number from the
keyboard, handling
POP DX
signs and converting
POP CX ASCII digits to a numeric
RET value.

PRINT_ARRAY ENDP

READ_NUM PROC
PUSH BX
PUSH CX
PUSH DX

XOR BX, BX
XOR CX, CX

MOV AH, 01H


INT 21H

CMP AL, '-'


JE NEGATIVE
CMP AL, '+'
JE POSITIVE
JMP DIGIT_CHECK
NEGATIVE:
MOV CX, 1
POSITIVE:
MOV AH, 01H
INT 21H

DIGIT_CHECK:
CMP AL, 0DH
JE END_READ
CMP AL, '0'
JB DIGIT_CHECK
CMP AL, '9'
JA DIGIT_CHECK

SUB AL, 30H


MOV AH, 0
PUSH AX
MOV AX, BX
MOV DX, 10
MUL DX
MOV BX, AX
POP AX
ADD BX, AX

MOV AH, 01H


INT 21H
JMP DIGIT_CHECK

END_READ:
CMP CX, 0
JE DONE_READ
NEG BX

DONE_READ:
MOV AX, BX
POP DX
POP CX
POP BX
RET
READ_NUM ENDP Convert a number in AX
to its ASCII digits and
print it, handling
PRINT_NUM PROC negative numbers if
PUSH BX necessary.

PUSH CX
PUSH DX

MOV CX, 0
MOV BX, 10

CMP AX, 0
JGE
POSITIVE_NUM
NEG AX
PUSH AX
MOV DL, '-'
MOV AH, 02H
INT 21H
POP AX

POSITIVE_NUM:
XOR DX, DX
DIV BX
PUSH DX
INC CX
CMP AX, 0
JNE
POSITIVE_NUM

PRINT_NUM_LOOP:
POP DX
ADD DL, 30H
MOV AH, 02H
INT 21H
LOOP
End of the program.
PRINT_NUM_LOOP

POP DX
POP CX
POP BX
RET
ENDP

END

OUTPUT:

RESULT :
Sort the given numbers in 1) ascending order and 2) descending order has
been done successfully.
EX NO : 11 REPLACEMENT OF STRINGS
5.2.25

AIM :
To Input a string from keyboard and replace any two characters of the string
with symbols * and #.

TABLE :
11 LABEL CODE COMMENTS
Setup & Data .MODEL SMALL Define the memory
Declaration model, stack, messages,
.STACK 100H
buffers, replacement
.DATA characters, and
msg_input DB 0Dh, counters.
0Ah, 'Enter a string: $'
msg_char1 DB 0Dh,
0Ah, 'Enter the first
character to replace: $'
msg_char2 DB 0Dh,
0Ah, 'Enter the second
character to replace: $'
msg_result DB 0Dh,
0Ah, 'Modified String: $'
msg_skipped DB
0Dh, 0Ah, 'Skipped
String: $'
msg_star_count DB
0Dh, 0Ah, 'Count of "*":
$'
msg_hash_count DB
0Dh, 0Ah, 'Count of "#":
$'
str DB 50
DUP('$')
skipped_str DB 50
DUP('$')
char1 DB ?
char2 DB ?
star_count DB 0
Program hash_count DB 0
Initialization
.CODE
.STARTUP
MOV AX, @DATA Display input prompt,
read the string into
MOV DS, AX
buffer, and mark the
end of the string with
MOV DX, OFFSET '$'.
msg_input
MOV AH, 09H
INT 21H

MOV DX, OFFSET str


MOV AH, 0AH
MOV BYTE PTR [str], 49
MOV BYTE PTR [str+1],
0
INT 21H

Loop through the


MOV SI, OFFSET str+2 string; if a character
MOV CX, 50 matches char1 or
char2, replace it (with
FIND_END: '*' or '#') and update
CMP BYTE PTR [SI], the corresponding
0DH counter; otherwise,
copy it into the skipped
JE FOUND_ENTER
string.
INC SI
LOOP FIND_END
FOUND_ENTER:
MOV BYTE PTR [SI], '$'

MOV DX, OFFSET


msg_char1
MOV AH, 09H
INT 21H
MOV AH, 01H
INT 21H
Loop through the
MOV char1, AL string; if a character
matches char1 or
char2, replace it (with
MOV DX, OFFSET '*' or '#') and update
msg_char2 the corresponding
MOV AH, 09H counter; otherwise,
copy it into the skipped
INT 21H string.
MOV AH, 01H
INT 21H
MOV char2, AL

MOV BYTE PTR


star_count, 0
MOV BYTE PTR
hash_count, 0

MOV SI, OFFSET str+2


MOV DI, OFFSET
skipped_str
REPLACE_LOOP:
MOV AL, [SI]
CMP AL, '$'
JE DONE_REPLACE

CMP AL, char1


JNE CHECK_SECOND
MOV BYTE PTR [SI], '*'
INC BYTE PTR
star_count
JMP NEXT_CHAR

CHECK_SECOND:
CMP AL, char2
JNE SKIP_CHAR
MOV BYTE PTR [SI], '#'
INC BYTE PTR
hash_count
JMP NEXT_CHAR

SKIP_CHAR:
MOV [DI], AL
INC DI
Display the modified
string and the skipped
NEXT_CHAR: string.
INC SI
JMP REPLACE_LOOP
DONE_REPLACE:
MOV BYTE PTR [DI], '$'

MOV DX, OFFSET


msg_result
MOV AH, 09H
INT 21H
MOV DX, OFFSET str+2
MOV AH, 09H
INT 21H

MOV DX, OFFSET


msg_skipped
MOV AH, 09H
INT 21H
MOV DX, OFFSET
skipped_str
MOV AH, 09H
INT 21H Display messages and
print the counts for '*'
and '#' characters
MOV DX, OFFSET (converted to ASCII)
msg_star_count
MOV AH, 09H
INT 21H
MOV AL, star_count
ADD AL, '0'
MOV DL, AL
MOV AH, 02H
INT 21H

MOV DX, OFFSET


msg_hash_count
MOV AH, 09H
INT 21H
MOV AL, hash_count
ADD AL, '0'
MOV DL, AL
MOV AH, 02H
INT 21H

Terminate the program.


MOV AH, 4CH
INT 21H
END

OUTPUT :

RESULT :
To Input a string from keyboard and replace any two characters of the string
with symbols * and # has been done successfully.

EX NO : 12 TRANSFER OF STRING FROM MEMORY BLOCK


10.2.25

AIM :
To write a logic to transfer a sting from a memory block to a new location.

TABLE :
12 LABEL CODE COMMENTS
Setup & Data .MODEL SMALL Define memory model,
Declaration stack, buffer size,
.STACK 100H
source and destination
.DATA buffers, and messages.
BUFFER_SIZE
EQU 20
SRC DB
BUFFER_SIZE, 0
DB
BUFFER_SIZE
DUP('$')
DST DB
BUFFER_SIZE
DUP('$')

MSG1 DB
"ENTER A
STRING: $"
MSG2 DB
0DH, 0AH,
"COPIED
STRING: $"
Program
Initialization
.CODE
Initialize DS with the
MAIN PROC data segment.
MOV AX,
@DATA
MOV DS, AX

MOV DX,
Display prompt and
OFFSET MSG1
read the string from
MOV AH, 09H the keyboard into the
source buffer
INT 21H

MOV DX,
OFFSET SRC
MOV AH, 0AH
COPY LOOP
INT 21H

MOV BL,
SRC+1
MOV BH, 0
Get the length of the
input (from SRC+1)
and initialize SI (source
LEA SI, SRC+2
pointer) and DI (dest
LEA DI, DST pointer).
MOV CX, BX

Display Copied
COPY_LOOP:
String
MOV AL, [SI]
MOV [DI], AL
INC SI Copy each character
INC DI from SRC (starting at
offset +2) to DST, then
LOOP append a '$'
COPY_LOOP terminator.

MOV BYTE
PTR [DI], '$'
MOV DX,
OFFSET MSG2
MOV AH, 09H
Display the "COPIED
INT 21H STRING:" message and
the copied string from
the DST buffer.
MOV DX,
OFFSET DST
Program
MOV AH, 09H
Termination
INT 21H
Terminate the
program.
MOV AH, 4CH
INT 21H

MAIN ENDP
END MAIN

OUTPUT:

RESULT :
To transfer a sting from a memory block to a new location has been done
successfully.

You might also like