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

MMDB_Group#8_lab_report#7 (1)

Uploaded by

Shafla Rehman
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)
5 views

MMDB_Group#8_lab_report#7 (1)

Uploaded by

Shafla Rehman
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/ 21

DEPARTMENT OF COMPUTER &

SOFTWARE ENGINEERING

COLLEGE OF E&ME, NUST,


RAWALPINDI

EC-310 Microprocessor and Microcontroller Based


Design

LAB # 07: Arrays, Strings and Addressing Modes

SUBMITTED TO:
Dr. Sagheer Khan

SUBMITTED BY:
Shafla Rehman; 412780
Areesha Batool ; 413055
Shaheer Mukhtiar ; 432017

CE-44, Syndicate B

Task#1:
Code:
org 100h
.stack 100h
.model small

.data

array db 1,2,3,4,5
array1 db , 0,0,0,0,0

.code
; Direct addressing mode to modify array elements

xor ax, ax
xor bx,bx
xor cx, cx
xor dx,dx
mov cl, 0d
mov bl , 10d
modify:
mov [array] , 10d
mov [array+1] , 20d
mov [array+2] , 30d
mov [array+3] , 40d
mov [array+4] , 50d

;indirect addresssing to read values from array and store


them in another array
mov si , offset array
mov cl, 0
mov di , offset array1
store:
cmp cl , 5d
je end_store
mov dx ,[si]
mov [bx] , dx
mov [di] , dx
inc si
inc di
inc cl
jmp store
end_store:

mov bx, 0
mov dx, 0

adder:
cmp bx, 5d
je terminate
mov al, array[bx]
add dx, ax
inc bx
jmp adder
terminate:
ret

Output:

Task#2:

Code:
org 100h
.stack 100h
.model small
.data
buffer db 50 , 50 dup(1)
buffer1 db 50 , 50 dup(1)
message db 'Enter first string : $ '
message1 db 'Enter second string : $'

CRLF db 0DH , 0AH ,'$'


First_message db 'First precedes second : $ '
Second_message db 'Second precedes first : $'
equal_message db 'Both strings are equal : $'

.code
; Direct addressing mode to modify array elements

xor ax, ax
xor bx,bx
xor cx, cx
xor dx,dx
lea dx, message
mov ah, 09h
int 21h

mov dx ,offset buffer


mov ah, 0ah
int 21h
lea dx, crlf
mov ah, 09h
int 21h
lea dx, message1
mov ah, 09h
int 21h
lea dx, buffer1
mov ah, 0ah
int 21h
mov bl , buffer1[1]
cmp buffer[1] ,bl
jl first
jg second
mov si, 2d
mov di, 2d
again:

mov al , buffer[si]
mov bl, buffer1[di]
cmp al , bl

jl first
jg second
inc si
inc di
cmp al , 0dh
je dollar
jmp again

dollar:
cmp al,bl
je equal_display
jmp again
equal_display:
lea dx, crlf
mov ah, 09h
int 21h
lea dx, equal_message
mov ah, 09h
int 21h
lea dx, crlf
mov ah, 09h
int 21h
mov ah, 09h
mov bh, 00h
mov bl , buffer[1]
mov buffer[bx+2] , '$'
lea dx, buffer[2]
int 21h
mov ah, 09h
mov bh, 00h
mov bl , buffer1[1]
mov buffer1[bx+2] , '$'
lea dx, buffer1[2]
int 21h

jmp terminate

first:
lea dx, crlf
mov ah, 09h
int 21h
lea dx, First_message
mov ah, 09h
int 21h
lea dx, crlf
mov ah, 09h
int 21h
mov ah, 09h
mov bh, 00h
mov bl , buffer[1]
mov buffer[bx+2] , '$'
lea dx, buffer[2]
int 21h
mov ah, 09h
mov bh, 00h
mov bl , buffer1[1]
mov buffer1[bx+2] , '$'
lea dx, buffer1[2]
int 21h
jmp terminate
second:
lea dx, crlf
mov ah, 09h
int 21h
lea dx, Second_message
mov ah, 09h
int 21h
lea dx, crlf
mov ah, 09h
int 21h s
mov ah, 09h
mov bh, 00h
mov bl , buffer1[1]
mov buffer1[bx+2] , '$'
lea dx, buffer1[2]
int 21h
mov ah, 09h
mov bh, 00h
mov bl , buffer[1]
mov buffer[bx+2] , '$'
lea dx, buffer[2]
int 21h

jmp terminate
terminate:
ret

Output:
Task#3:
Take four numbers from user. Calculate their mean, median, mode and
range (Difference between maximum and minimum values).

Code:

org 100h
.model small
.stack 100H
.data

numbers DB 4 DUP(0)
mean DB 0
median DB 0
mode DB 0
range DB 0
msg DB 'Enter 4 numbers: $'
msg1 DB 0Dh, 0Ah, 'Mean: $'
msg2 DB 0Dh, 0Ah, 'Median: $'
msg3 DB 0Dh, 0Ah, 'Mode: $'
msg4 DB 0Dh, 0Ah, 'Range: $'
newline DB 0Dh, 0Ah, '$'
.code

main:

MOV AX, @data


MOV DS, AX
MOV DX, OFFSET msg
MOV AH, 09h
INT 21h

MOV CX, 4
LEA DI, numbers

input:

CALL read
MOV [DI], AL
INC DI

LOOP input

; Calculate mean

XOR AX, AX
MOV CX, 4
LEA DI, numbers
sum:

ADD AL, [DI]


INC DI
LOOP sum
MOV BL, 4
MOV AH, 0
DIV BL
MOV mean, AL

CALL sort

; median

MOV AL, numbers[1]


ADD AL, numbers[2]
MOV BL, 2
MOV AH, 0
DIV BL
MOV median, AL

; mode
CALL CalcMode
; range
MOV CX, 4
LEA DI, numbers
MOV AL, [DI]
MOV BL, AL

range1:

INC DI
MOV AL, [DI]
CMP AL, BL
JG new_max
CMP AL, [numbers]
JL new_min
JMP next_num_range

new_max:
MOV BL, AL
JMP next_num_range

new_min:

MOV [numbers], AL

next_num_range:

LOOP range1
; Calculate the range
SUB BL, [numbers]
MOV range, BL

MOV DX, OFFSET msg1


MOV AH, 09h
INT 21h
MOV AL, mean
CALL print

MOV DX, OFFSET msg2


MOV AH, 09h
INT 21h
MOV AL, median
CALL print

MOV DX, OFFSET msg3


MOV AH, 09h
INT 21h
MOV AL, mode
CALL print

MOV DX, OFFSET msg4


MOV AH, 09h
INT 21h
MOV AL, range
CALL print
MOV AX, 4C00h
INT 21h

read PROC
MOV AH, 01h
INT 21h
SUB AL, '0'
RET

read ENDP

print PROC
ADD AL, '0'
MOV DL, AL
MOV AH, 02h
INT 21h
RET
print ENDP

sort PROC
LEA DI, numbers
MOV CX, 4

outer_loop:
MOV BX, 0
MOV DI, OFFSET numbers
MOV AX, [DI]
MOV DX, [DI + 1]
CMP AX, DX
JBE no_swap
MOV [DI], DX
MOV [DI + 1], AX
INC BX

no_swap:
ADD DI, 1
LOOP outer_loop
CMP BX, 0
JNZ outer_loop
RET
sort ENDP

CalcMode PROC
LEA DI, numbers
MOV CX, 4
XOR BX, BX
XOR DX, DX
MOV AL, [DI]
MOV AH, 0

next_number_outer:
MOV BL, [DI]
MOV CL, 1
MOV SI, 0
count:
CMP SI, 4
JAE check
CMP BL, numbers[SI]
JNE not_a_match
INC CL
not_a_match:
INC SI
JMP count
check:

CMP CL, AH
JBE next
MOV AH, CL
MOV AL, BL
next:
INC DI
DEC CX
JNZ next_number_outer
MOV mode, AL
RET
CalcMode ENDP

END main

Output:
Task#4:
Create a program to test whether 2 phrases entered by the user are
Anagrams or not.

Code:

ORG 100h

.MODEL SMALL
.STACK 100H

.DATA
PROMPT1 DB 'Enter first string: $'
PROMPT2 DB 'Enter second string: $'
STR1 DB 20, 20 DUP(0)
STR2 DB 20, 20 DUP(0)
RESULT3 DB 'Both strings are anagrams.$', 0
NOT_ANAGRAM DB 'Strings are not anagrams.$', 0
.CODE
START:
MOV AH, 09h
LEA DX, PROMPT1
INT 21h

LEA DX, STR1


MOV AH, 0Ah
INT 21h

MOV AH, 09h


LEA DX, PROMPT2
INT 21h

LEA DX, STR2


MOV AH, 0Ah
INT 21h

LEA SI, STR1 + 1

UPPER_LOOP:
MOV AL, [SI]
CMP AL, 13
JE STRINGS_IDENTICAL

LEA DI, STR2 + 1


MOV CX, 20
INNER_LOOP:
MOV BL, [DI]
CMP BL, AL
JE MATCH_FOUND

INC DI
LOOP INNER_LOOP

JMP NOT_ANAGRAM_FOUND

MATCH_FOUND:
MOV [DI], 0
INC SI
JMP UPPER_LOOP

STRINGS_IDENTICAL:
MOV AH, 09h
LEA DX, RESULT3
INT 21h
JMP EXIT_PROGRAM

NOT_ANAGRAM_FOUND:
MOV AH, 09h
LEA DX, NOT_ANAGRAM
INT 21h
EXIT_PROGRAM:
MOV AX, 4C00h
INT 21h
Output:

You might also like