0% found this document useful (0 votes)
15 views54 pages

Lab MPMC

The document outlines a laboratory report for a Microprocessor and Microcontrollers course, detailing various exercises conducted by a student named Divyadharshini. Each exercise includes the aim, code, and comments explaining the process of performing operations such as addition, subtraction, and character counting using assembly language. The report indicates successful completion of each exercise, demonstrating proficiency in programming with the 8086 microprocessor.

Uploaded by

71762331016
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)
15 views54 pages

Lab MPMC

The document outlines a laboratory report for a Microprocessor and Microcontrollers course, detailing various exercises conducted by a student named Divyadharshini. Each exercise includes the aim, code, and comments explaining the process of performing operations such as addition, subtraction, and character counting using assembly language. The report indicates successful completion of each exercise, demonstrating proficiency in programming with the 8086 microprocessor.

Uploaded by

71762331016
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/ 54

COIMBATORE INSTITUTE OF TECHNOLOGY

MICROPROCESSOR AND MICROCONTROLLERS


LABORATORY

NAME : DIVYADHARSHINI . S
ROLL NO : 2303717672622018
SUBJECT : MPMC LAB
SUBCODE : 40MSS46
DATE : 22.02.25
INDEX

S.NO DATE TOPICS REMARKS


ADDITION OF TWO 8 BIT & 16
1 . 30-12-2024 bit NUMBERS
ADDITION OF TWO ONE DIGIT
2 . 06-01-2025
DECIMAL NUMBERS
ADDITION OF 2 TWO DIGIT
3. 08-01-2025
DECIMAL NUMBERS
SUBTRACTION OF ARRAY OF
4. 13-01-2025
NUMBERS
OCCURENCES OF CHARACTER
5. 20-01-2025
IN THE STRING
6. 22-01-2025 PALINDROME CHECKER

7. 26-01-2025 COUNT OF 0’S AND 1’S


LARGEST AND SMALLEST
8. 27-01-2025
NUMBER
9. 03-02-2025 MIDDLE VALUE OF AN ARRAY
ARRANGING AN ARRAY IN
10. 05-02-2025
ASCENDING & DESC ORDER
11. 05-02-2025 REPLACEMENT OF STRINGS
TRANSFER OF STRING FROM
12. 10-02-2025
MEMORY BLOCK
EX NO : 1 ADDITION OF TWO 8 BIT & 16 bit NUMBERS

30.12.25

AIM :

To write a program to find addi on of two 8 bit and 16 bit numbers

TABLE :

LABEL CODE COMMENTS

Start Address org 100h Define the program’s


star ng address.

Load registers with


Ini aliza on MOV AL, 255
ini al values

MOV BL, 01

perform addi on,


Addi on & ADD AL,BL handle 8-bit overflow,
Overflow and store the RESULT.

MOV DS:[0200H],AL

MOV AL, 00

ADC Opera on Clear AL, use ADC with


ADC AL,AL
the carry to compute a
final value, and store it.
MOV [0301H],AL

Termina on
HLT End program execu on.
LABEL CODE COMMENTS

Start Address org 100h Define the program’s star ng


address.

Load AX with 2234H and BX with


Ini aliza on MOV AX, 2234;
22H.
MOV BX, 22;

Addi on & ADD AX,BX;


Add BX to AX, handle poten al
Overflow
MOV DS:[0200H],AX; overflow in AX, and store the
RESULT.
MOV DX, 0000;
ADC Clear DX, use ADC with the carry
ADC DX,DX;
Opera on to compute a final value, and
MOV [0301H],DX; store it.

Termina on
HLT; End program execu on.

OUTPUT :

RESULT : To perform addi on of two 8 bit and 16 bit numbers has been done successfully.
EX NO : 2 ADDITION OF TWO ONE DIGIT DECIMAL NUMBERS

6.1.25

AIM:

To write an 8086 ALP program to add two one-digit decimal numbers

TABLE :

2 LABEL CODE COMMENTS

Program Start org 100h Set the program’s


star ng address.

Display first prompt, read


First Input lea dx, msg1
a character, convert it
Sequence
mov ah, 9 from ASCII, and store the
first number in BL.
int 21h

Second Input Display second prompt,


mov ah, 1
Sequence read a character, and
int 21h convert it from ASCII.
sub al, 48

mov bl, al

lea dx, msg2

mov ah, 9

int 21h

mov ah, 1
int 21h

sub al, 48

Add both numbers, then


check if the RESULT
Addi on & Carry add bl, al
exceeds a single digit and
Check
adjust for carry if needed.

mov bh, 0

cmp bl, 9

jle no_carry

sub bl, 10

mov bh, 1

no_carry:
RESULT Display
lea dx, msg3

mov ah, 9

int 21h Display the RESULT


message and OUTPUT
the carry digit followed
mov dl, bh by the sum digit.
add dl, 48

mov ah, 2

int 21h

mov dl, bl

add dl, 48

mov ah, 2

int 21h

hlt
Termina on
Data defini on msg1 db "Enter First Number
(0-9): $"

msg2 db 0dh, 0ah, "Enter


Second Number (0-9): $"

msg3 db 0dh, 0ah, "Your sum End the program


is (Carry + Sum): $" execu on

Define the messages


used for promp ng and
displaying the RESULT.

OUTPUT :

RESULT :

To add two one-digit decimal numbers using 8086 microprocessor has been successfully.
EX NO : 3 ADDITION OF 2 TWO DIGIT DECIMAL NUMBERS

8.1.25

AIM:

To write an 8086 ALP program to add two two-digit decimal numbers.

TABLE:

3 LABEL CODE COMMENTS

Start org 100h Ini alize program start


and data segment

.data

a db 0ah,0dh, "Enter 1st


Number: $"

b db 0ah,0dh, "Enter 2nd


Number: $"

c db 0ah,0dh, "RESULT is:


$"

.code

main:

mov ax,@data

mov ds,ax

First Number Input


Display prompt and read
lea dx,a
the first number digit.
mov ah,09h
int 21h

mov ah,01h

int 21h

Complete First
Number
sub al,30h

mov bh,al

mov ah,01h

int 21h

sub al,30h

mov bl,al ;bh:bl 1st no

Display prompt and read


Second Number Input two digits for second
lea dx,b
number.
mov ah,09h

int 21h

mov ah,01h

int 21h

sub al,30h

mov ch,al

mov ah,01h

int 21h
sub al,30h

mov cl,al ;ch:cl 2nd no

add bl,cl

mov al,bl

mov ah,00h

aaa

mov cl,al

mov bl,ah

Add remaining digits


(carry parts) together.
add bl,bh

add bl,ch
Adjust final sum using
AAA; store RESULT in BX.
mov al,bl

mov ah,00h

aaa

mov bx,ax;mov
bh,ax;mov bl,al
Show the RESULT message
Display RESULT and OUTPUT the three
mov dx,offset c RESULT digits.

mov ah,09h

int 21h

mov dl,bh

add dl,30h
mov ah,02h

int 21h

mov dl,bl

add dl,30h

mov ah,02h

int 21h

mov dl,cl

add dl,30h

mov ah,02h

int 21h

ret

End the program.

OUTPUT:

RESULT :

To add two two-digit decimal numbers using 8086 microprocessor has been successfully.
EX NO : 4 SUBTRACTION OF ARRAY OF NUMBERS

13.1.25

AIM:

To write an 8086 ALP program to subtract N set of decimal numbers

TABLE

4 LABEL CODE COMMENTS

Setup & Data org 100h Ini alize program start, data
Ini aliza on segment, and define data items
(array, RESULT, and messages).
.data

num db 10 dup(0)

RESULT db ?

prompt db 0dh, 0ah,


'Enter total number of
terms in the array: $'

input_prompt db 0dh,
0ah, 'Enter a number: $'

RESULT_msg db 0dh, 0ah,


'RESULT is: $'

.code

start:

mov ax,@data

mov ds, ax
Prompt for lea dx, prompt Display the prompt, read a
Array Size character for total number of
mov ah, 09h
terms, convert it from ASCII,
int 21h store in CL, and check size.

mov ah, 01h

int 21h

sub al, '0'

mov cl, al

cmp cl, 01h

jb end_program

xor si, si Loop to prompt and read each


array element, conver ng ASCII
to digit and storing it in the
input_loop: array.

lea dx, input_prompt

mov ah, 09h

int 21h

mov ah, 01h

int 21h

sub al, '0'

mov num[si], al Ini alize subtrac on with the


first array element, then
inc si
dec cl subtract each subsequent
element in a loop.
jnZ input_loop

mov al, num[0]

mov cx, si

mov si, 1

sub_loop:

cmp si, cx
Store the final subtrac on
jae display_RESULT
RESULT, display the RESULT
sub al, num[si] message, check if nega ve (print
'-' if so), then display the
inc si
absolute RESULT.
jmp sub_loop

display_RESULT:

mov RESULT, al

lea dx, RESULT_msg

mov ah, 09h

int 21h

cmp RESULT, 0

jnl display_posi ve

mov ah, 02h

mov dl, '-'

int 21h
neg RESULT

display_posi ve:

mov al, RESULT

add al, '0'

mov dl, al

mov ah, 02h

int 21h

Exit Program

end_program:

Terminate the program.

mov ah, 4ch

int 21h

OUTPUT:

RESULT:To perform 8086 ALP program to subtract N set of decimal numbers has been done
successfully.
EX NO : 5 OCCURENCES OF CHARACTER IN THE STRING

20.1.25

AIM:

To write a program to find the occurrence(s) of two different characters in a given string.

TABLE:

5 LABEL CODE COMMENTS

Setup & Data .model small Define memory model, stack,


Declara on and declare data items
.stack 100h
(prompts, messages, input
buffer).

.data

prompt db 'Enter a string: $'

charPrompt1 db 0Dh, 0Ah


,'Enter the first character to
search for: $'

charPrompt2 db 0Dh,
0Ah,'Enter the second
character to search for: $'

msg db 'First character


count: $'

msg2 db 'Second character


count: $'

inputStr db 50,?, 50 dup('$')

Program
.code Set up code segment and
Ini aliza on
ini alize DS with the data
main:
segment address.
mov ax, @data
mov ds, ax

mov ah, 09h

lea dx, prompt

int 21h

lea dx, inputStr

mov ah, 0Ah

int 21h

mov ah, 09h

lea dx, charPrompt1

int 21h

mov ah, 01h

int 21h

mov bl, al

mov ah, 09h

lea dx, charPrompt2

int 21h

mov ah, 01h

int 21h

mov bh, al

Count First lea si, inputStr + 2 Set pointer to string; loop


Character through string comparing each
xor cx, cx character with first search char;
count matches.

countFirst:

mov al, [si]

cmp al, 0

je doneCoun ng

cmp al, bl

je incrementFirstCount

inc si

jmp countFirst

incrementFirstCount:

inc cx

inc si

jmp countFirst

doneCoun ng:

mov ah, 09h

lea dx, msg

int 21h

mov ax, cx

call PrintNum
Reset pointer and counter; loop
Count Second lea si, inputStr + 2
through the string to count
Character
xor cx, cx occurrences of the second
search char.

countSecond:

mov al, [si]


cmp al, 0

je doneSecondCoun ng

cmp al, bh

je incrementSecondCount

inc si

jmp countSecond

incrementSecondCount:

inc cx

inc si

jmp countSecond

doneSecondCoun ng:

mov ah, 09h

lea dx, msg2

int 21h

mov ax, cx

call PrintNum

mov ah, 4Ch

int 21h

Exit the program.

Program PrintNum proc


Termina on
add ax, '0'

mov dl, al

mov ah, 02h

int 21h
ret Convert number in AX to ASCII
and display it; then return from
PrintNum endp
the procedure.

end main

OUTPUT :

RESULT :

To perform a program to find the occurrence(s) of two different characters in a given string
has been done successfully.
EX NO : 6 PALINDROME CHECKER

22.1.25

AIM:

To read a string from the keyboard and find whether the input string is a palindrome or not.

TABLE:

6 LABEL CODE COMMENTS

Setup & Data .model small Define memory model, stack,


Declara on and data (prompts, messages,
.stack 100h
input buffer).

.data

prompt db 'Enter a string:


$'

palindrome_msg db
0Dh,0Ah,'The string is a
palindrome.$'

not_palindrome_msg db
0Dh,0Ah, 'The string is not a
palindrome.$'

inputStr db 50,?, 50
dup('$') ; Buffer size (50),
then space for length and
characters

.code Ini alize DS with the data


Program
segment address.
Ini aliza on main:

mov ax, @data

mov ds, ax
mov ah, 09h

lea dx, prompt

int 21h

lea dx, inputStr

mov ah, 0Ah

int 21h

lea si, inputStr + 2

mov cl, inputStr[1]

mov ch, 0

lea di, inputStr + 2

add di, cx

dec di

Palindrome Check check_palindrome: Compare characters from the


Loop beginning (SI) and end (DI); if
mov al, [si]
mismatch, jump to
mov bl, [di] not_palindrome; otherwise
con nue.
cmp al, bl

jne not_palindrome

inc si

dec di

cmp si, di
jge is_palindrome

jmp check_palindrome

is_palindrome: If all characters match, display


the palindrome message.

mov ah, 09h

lea dx, palindrome_msg

int 21h

jmp exit

If a mismatch is found, display


not_palindrome:
the non-palindrome message.

mov ah, 09h

lea dx,
not_palindrome_msg

int 21h

exit:
Exit Program
Terminate the program.
mov ah, 4Ch

int 21h

end main

OUTPUT:

RESULT : To find whether the input string is a palindrome or not has been done successfully.
EX NO : 7 COUNT OF 0’S AND 1’S

26.1.25

AIM:

To write a code to count the number of 1’s and 0’s in the byte.

TABLE :

7 LABEL CODE COMMENTS

Data .DATA Declare messages, counters, and


Declara on & input buffer.
inputMsg DB 'Enter a byte
Setup
(0-99): $'

onesMsg DB 0DH, 0AH,


'Number of 1s: $'

zerosMsg DB 0DH, 0AH,


'Number of 0s: $'

byteInput DB ?

onesCount DB 0

zerosCount DB 0

input DB 4, ?, 4 DUP('$')

.CODE
Program
Ini alize the data segment.
Ini aliza on MAIN PROC

MOV AX, @DATA

MOV DS, AX
LEA DX, inputMsg

MOV AH, 09H

INT 21H

LEA DX, input

MOV AH, 0AH

INT 21H

MOV onesCount, 0

MOV zerosCount, 0

LEA SI, input + 1

MOV CL, [SI]

MOV CH, 0

LEA SI, input + 2

DIGIT_LOOP: For each digit, load it into AL and


prepare to process its 8 bits.
MOV AL, [SI]

INC SI

MOV DX, 8

MOV BL, AL

COUNT_LOOP:

SHR BL, 1
JC INCREMENT_ONES

INC zerosCount

JMP NEXT_BIT

INCREMENT_ONES:

INC onesCount

NEXT_BIT:

DEC DX

JNZ COUNT_LOOP

LOOP DIGIT_LOOP

LEA DX, onesMsg Display the message for ones


count and OUTPUT the ones
MOV AH, 09H
count value (converted to ASCII).
INT 21H

MOV DL, onesCount

ADD DL, '0'

MOV AH, 02H

INT 21H

LEA DX, zerosMsg


Display the message for zeros
MOV AH, 09H count and OUTPUT the zeros
count value (converted to ASCII).
INT 21H

MOV DL, zerosCount

ADD DL, '0'

MOV AH, 02H

INT 21H
Exit Program MOV AH, 4CH Terminate the program.

INT 21H

MAIN ENDP

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 address


Jump 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: Loop to read each element


from the keyboard and store
mov ah, 1
it in the array.
int 21h

sub al, '0'

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 Loop through the array to
update the largest (AL) and
je done
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
Display the "Largest:"
int 21h message and OUTPUT the
largest number (converted to
ASCII).
mov dl, largest
add dl, '0'

mov ah, 2

int 21h

mov dx, offset msg4

mov ah, 9

int 21h

mov dl, smallest

add dl, '0' Display the "SMALLEST:"


message and OUTPUT the
mov ah, 2
SMALLEST number
int 21h (converted to ASCII).

Program hlt
Termina on
Halt the prg.
msg1 db 'Enter the number of
elements: $'

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
sucesfully
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,


Declara on stack, array, messages, and
.STACK 100H
variables for size and
.DATA 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: $'

SIZE DB ?

MIDDLE DB ?

Program
Ini aliza on .CODE
Ini alize the data segment.
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
MOV CL, SIZE
from ASCII, and store them
MOV CH, 0 in ARRAY

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

MOV BL, [SI]

Display the "Middle value:"


message and OUTPUT the
LEA DX, MSG3
middle element (converted
MOV AH, 09H to ASCII).

INT 21H

MOV DL, BL

ADD DL, '0'

MOV AH, 02H

INT 21H

MOV AH, 4CH Terminate the program.


INT 21H

Program MAIN ENDP


Termina on
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 ORDER

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,


Declara on .MODEL SMALL stack, messages, array, and
variable to store the
.STACK 100H
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
DUP(?)

size DW ?
Program .CODE
Ini aliza on
.STARTUP Ini alize the data segment.

MOV AX, @DATA

MOV DS, AX

MOV DX, OFFSET


msg_size

MOV AH, 09H


Display prompt, read the
INT 21H number of elements, store
it in SIZE, and check for
CALL READ_NUM
valid (posi ve) size.
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


msg_sorted_asc

MOV AH, 09H Display a message and then


print the sorted array (in
INT 21H
ascending order).
CALL PRINT_ARRAY

CALL
BUBBLE_SORT_DESC

MOV DX, OFFSET


msg_sorted_desc

MOV AH, 09H Display a message and then


print the sorted array (in
INT 21H
descending order).
CALL PRINT_ARRAY

END_PROGRAM:

MOV AH, 4CH

INT 21H

BUBBLE_SORT_ASC
PROC

PUSH CX

PUSH DX

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

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
ENDP

BUBBLE_SORT_DESC
PROC Sort the array in descending
order using bubble sort
PUSH CX
(nested loops to swap
PUSH DX adjacent elements as neede

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

MOV array[SI+2], AX

NO_SWAP_DESC:

ADD SI, 2

DEC DI Sort the array in descending


order using bubble sort
JNZ
(nested loops to swap
SORT_INNER_DESC
adjacent elements as neede

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

POP SI

POP DX

POP CX

RET

PRINT_ARRAY ENDP
Read a mul -digit number
from the keyboard,
handling signs and
READ_NUM PROC
conver ng ASCII digits to a
PUSH BX numeric value.

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
PRINT_NUM PROC

PUSH BX

PUSH CX Convert a number in AX to


its ASCII digits and print it,
PUSH DX
handling nega ve numbers
if necessary.

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
PRINT_NUM_LOOP

POP DX

POP CX

POP BX
End of the program.
RET

ENDP

END

OUTPUT:

RESULT :

To perform an alp program to 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 model,


Declara on stack, messages, buffers,
.STACK 100H
replacement characters,
.DATA and counters.

msg_input DB 0Dh, 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

hash_count DB 0

.CODE
Program
Ini aliza on .STARTUP

MOV AX, @DATA

MOV DS, AX
Display input prompt, read
the string into buffer, and
mark the end of the string
MOV DX, OFFSET
with '$'.
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

MOV SI, OFFSET str+2


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

FOUND_ENTER:

MOV BYTE PTR [SI], '$'

MOV DX, OFFSET


msg_char1

MOV AH, 09H

INT 21H

MOV AH, 01H

INT 21H

MOV char1, AL Loop through the string; if a


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

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 string.
NEXT_CHAR:

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 Display messages and print


the counts for '*' and '#'
INT 21H
characters (converted to
ASCII)

MOV DX, OFFSET


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 s ng from a memory block to a new loca on.

TABLE :

12 LABEL CODE COMMENTS

Setup & Data .MODEL SMALL Define memory model,


Declara on stack, buffer size, source
.STACK 100H
and des na on buffers,
.DATA 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: $"

.CODE
Program MAIN PROC
Ini aliza on
MOV AX, @DATA Ini alize DS with the data
segment.
MOV DS, AX

MOV DX, OFFSET


MSG1

MOV AH, 09H

INT 21H Display prompt and read


the string from the
keyboard into the source
MOV DX, OFFSET buffer
SRC

MOV AH, 0AH

INT 21H

COPY LOOP
MOV BL, SRC+1

MOV BH, 0

LEA SI, SRC+2

LEA DI, DST


Get the length of the input
MOV CX, BX (from SRC+1) and ini alize
SI (source pointer) and DI
(dest pointer).
COPY_LOOP:

MOV AL, [SI]

MOV [DI], AL
Display Copied
String INC SI

INC DI

LOOP
Copy each character from
COPY_LOOP
SRC (star ng at offset +2)
to DST, then append a '$'
terminator.
MOV BYTE PTR
[DI], '$'

MOV DX, OFFSET


MSG2

MOV AH, 09H

INT 21H

Display the "COPIED


MOV DX, OFFSET STRING:" message and the
DST copied string from the DST
buffer.
MOV AH, 09H

INT 21H

Program
Termina on
MOV AH, 4CH
Terminate the program.
INT 21H

MAIN ENDP

END MAIN

OUTPUT:

RESULT :

To transfer a s ng from a memory block to a new loca on has been done successfully.

You might also like