Google

Dec 29, 2007

Assembly Language Programs on strings

AIM: To write 8086 Assembly Language Program to compare two strings.


Answer:

MODEL SMALL

.STACK 100

.DATA ; Data segment starts

STR1 DB 'FINAL YEAR EEE' ; Define string1 STR1

LEN DW ($-STR1) ; Variable LEN will have length of STR1

D DB 'EQUALS' ; Variable D points to string 'EQUALS'

F DB 'UNEQUALS' ; Variable F points to string 'UNEQUALS'

.EXTRA ; Extra segment starts

STR2 DB 'FINAL YEAR EEE' ; Define string2 STR2



.CODE ; Code segment starts

START:

MOV AX,@DATA

MOV DS,AX ; Initialize Data segment

MOV AX,@EXTRA

MOV ES,AX ; Initialize Extra segment

LEA SI,STR1 ;Lode effective address of STR1 in SI

LEA DI,STR2 ;Lode effective address of STR2 in DI

MOV CX,LEN ; Take length of string STR1 in CX register

CLD ; Clear Direction Flag

REP CMPSB ; Compare two strings till CX = 0 OR ZF ≠ 1

JNE NEXT ; Jump to NEXT if no ZF i.e. ZF = 0

LEA DX,D ; Lode effective address of string D in DX

JMP DISPLAY ; Jump to DICPLAY

NEXT:

LEA DX,F ; Lode effective address of string F in DX

DISPLAY:

MOV AH,09H ; Move 09 H in AH to display the string whose

INT 21H ; Effective address is in DX. DOS interrupt INT 21H

MOV AH,4CH ; Move 43 H in AH to terminate the program using

INT 21H ; Dos interrupt

END START


AIM: To write 8086 Assembly Language Program to add two arrays of hexadecimal
numbers using String Instructions.




Answer:



MODEL SMALL

.STACK 100

.DATA ; Data segment starts

A DB 35H, 36H, 37H, 38H ; Initialize array A

B DB 45H, 46H, 47H, 48H ; Initialize array B



.EXTRA ; Extra segment starts

Z DB ? ; Variable to store result Z



.CODE ; Code segment starts

START:

MOV AX,@DATA ;Initialize data segment

MOV DS,AX

MOV AX,@EXTRA ;Initialize extra segment

MOV ES,AX

MOV BX,0000H ;Initialize BX register to zero

MOV CL,04H ;Initialize count register to no of array elements

MOV DI,OFFSET Z ;Move the offset of Z to DI

UP: MOV AL,A[BX] ;Points to the first no in 'A'

ADD AL,B[BX] ;Adds the nth number in array A with the nth number in

; Array B and stores the result in AL.

INC BX ; Increments the BX register

STOSB ; Stores the result obtained in ES:DI

DEC CL ; Decrement count register

JNZ UP ; Jump to the label up when ZF!=0

INT 03H ; Halt

END START

INPUT: TWO ARRAYS IN DATA SEGMENT

A DB 35H, 36H, 37H, 38H

B DB 45H, 46H, 47H, 48H



OUTPUT: AN ARRAY IN EXTRA SEGMENT

Z DB 7AH, 7CH, 7EH, 80H





AIM: To write 8086 Assembly Language Program to display number of character
present in a string.

Answer:

MODEL SMALL

.STACK 100

.DATA

CR EQU 0DH

LF EQU 0AH

LEN DB 04 DUP(0)

MSG1 DB 'ENTER THE STRING.INPUT=','$'

MSG2 DB CR,LF,'THE LENGTH OF THE STRING=','$'



DISP MACRO MSG ; Micro to display a string

MOV AH,09H

MOV DX,OFFSET MSG

INT 21H

ENDM



.CODE

START:

MOV AX,@DATA

MOV DS,AX

DISP MSG1

MOV CX,00H

READ: MOV AH,01H ; Take the input string at run time

INT 21H

CMP AL,CR ; Detect the end of the string

JE AHEAD

INC CX

JMP READ



AHEAD:DISP MSG2

MOV SI,OFFSET LEN

MOV AX,CX

CALL HEX2ASC

DISP LEN

INT 03H



HEX2ASC PROC NEAR ; Procedure to convert number of character in HEX to ASCII

MOV BX,0001H

MUL BX

AAM

OR AX,3030H

MOV [SI],AH

MOV [SI+1],AL

MOV AL,'$'

MOV [SI+2],AL

INT 03H

HEX2ASC ENDP

END START

INPUT: ENTER THE STRING

.INPUT = Microprocessor lab

OUTPUT: 'THE LENGTH OF THE STRING= 18


You might be also interested in:

:: Interfacing Digital-To-Analog converter to 8086 using 8255

:: MASM 611 SOFTWARE






2 comments:

Unknown said...

it give me 10 no. in my exam

Sahil_Garg said...

Thanks a lot, it really helped :)