Mahi

22
STRING OPERATION AND INSTRUCTION PREFIX (Move block, reverse string,Sorting, inserting, deleting, length of the string, string comparison) Aim: Write an ALP to perform the following String operations (a) Moving a Block of strings (b) String reversal (c) Comparison of Two Strings (d) Reading a String from Keyboard & Displays the Length (e) To insert a Character before one character in a String (f) To Delete First Character of a String (g) Sorting 16-Bit Unsigned values in Ascending Order (h) Sorting 16-Bit signed values in Ascending Order (i) Sorting 16-Bit Unsigned values in Descending Order (j) Sorting 16-Bit signed values in Descending Order Apparatus: TASM /MASM soft ware Personal computer (a)Moving a Block of strings Algorithm: 1. Store the data in some memory location in data segment 2. Initialize the register which contains the source address in DATA SEGMENT. 3. Initialize the register which contains the destination address in EXTRA SEGMENT. 4. Store the count. 5. Perform the CLD (clear the direction flag).

description

.

Transcript of Mahi

Page 1: Mahi

STRING OPERATION AND INSTRUCTION PREFIX (Move block, reverse

string,Sorting, inserting, deleting, length of the string, string comparison)

Aim: Write an ALP to perform the following String operations

(a) Moving a Block of strings

(b) String reversal

(c) Comparison of Two Strings

(d) Reading a String from Keyboard & Displays the Length

(e) To insert a Character before one character in a String

(f) To Delete First Character of a String

(g) Sorting 16-Bit Unsigned values in Ascending Order

(h) Sorting 16-Bit signed values in Ascending Order

(i) Sorting 16-Bit Unsigned values in Descending Order

(j) Sorting 16-Bit signed values in Descending Order

Apparatus: TASM /MASM soft ware

Personal computer

(a)Moving a Block of strings

Algorithm:

1. Store the data in some memory location in data segment

2. Initialize the register which contains the source address in DATA SEGMENT.

3. Initialize the register which contains the destination address in EXTRA SEGMENT.

4. Store the count.

5. Perform the CLD (clear the direction flag).

6. Perform repeat operation.

7. Use string instruction to move the contents from one location to another location.

8. Stop the program.

Page 2: Mahi

Program:

DATA SEGMENT

SRC DB ‘MICROPROCESSOR’

DB 10 DUP (?)

Electronics and Communication Engineering MP&MC LAB

Sasi Institute of Technology and Engineering Page 43

DST DB 20 DUP (0)

DATA ENDS

CODE SEGMENT

ASSUME CS: CODE, DS: DATA, ES: DATA

START: MOV AX, DATA

MOV DS, AX

MOV ES, AX

LEA SI, SRC

LEA DI, DST

MOV CX, 20

CLD

REP

MOVSB

INT 3H

CODE ENDS

END START

Assembly Language Program after execution:

Electronics and Communication Engineering MP&MC LAB

Sasi Institute of Technology and Engineering Page 44

Page 3: Mahi

Before Execution: SRC DB ‘MICROPROCESSOR’

After Execution: DST DB ‘MICROPROCESSOR’

(b)String reversal

Algorithm:

1. Store the data in some memory location in data segment

2. Initialize a register which contain the source address in DATA SEGMENT.

3. Initialize a register which contain the destination address.

4. Store the count.

Add the count to source register to get the last value.

5. Get the value from source location to one register

6. Store the value from that register to destination location specified by destination register.

7. Decrement source register

8. Increment destination register

9. Decrement count

10. If the count is not zero repeat the steps 5, 6, 7, 8 & 9 operations.

11. Stop the program.

Program:

DATA SEGMENT

ORG 2000H

SRC DB ‘MICROPROCESSOR$’

COUNT EQU ($-SRC)

DEST DB ?

DATA ENDS

CODE SEGMENT

ASSUME CS: CODE,DS:DATA

Page 4: Mahi

START: MOV AX,DATA

MOV DS, AX

MOV CX, COUNT

Electronics and Communication Engineering MP&MC LAB

Sasi Institute of Technology and Engineering Page 45

LEA SI,SRC

LEA DI,DEST

ADD SI,CX

DEC CX

BACK: MOV AL,[SI]

MOV [DI],AL

DEC SI

INC DI

DEC CX

JNZ BACK

INT 3H

CODE ENDS

END START

Assembly Language Program after execution:

Before Execution: ORG 2000H

SRC DB ‘MICROPROCESSOR$’

After Execution: DESTINATION 2010H

DEST DB ‘$ROSSECORPORCIM’

Electronics and Communication Engineering MP&MC LAB

Sasi Institute of Technology and Engineering Page 46

Page 5: Mahi

(c)Comparison of Two Strings

Algorithm:

1. Store one string in data segment and one string in extra segment

2. Initialize the register which contains the source address in DATA SEGMENT.

3. Initialize the register which contains the destination address in EXTRA SEGMENT.

4. Store the count.

5. Perform the CLD (clear the direction flag).

6. Compare the two string contents one by one up to zero flag bit equals to 1

7. If they are not equal give output as (M2 DB “STRINGS R NOT EQUAL$” other wise M1

DB “STRINGS R EQUAL$”)

8. Stop the program.

Program:

PRINTSTRING MACRO MSG

MOV AH, 09H

MOV DX,OFFSET MSG

INT 21H

ENDM

DATA SEGMENT

STR1 DB ‘MICROPROCESSORS’

LEN EQU ($-STR1)

STR2 DB ‘MICROPROCSSOR’

MSG1 DB “STRINGS R EQUAL$”

MSG2 DB “STRINGS R NOT EQUAL$”

DATA ENDS

CODE SEGMENT

Page 6: Mahi

ASSUME CS:CODE,DS:DATA,ES:DATA

START: MOV AX,DATA

MOV DS,AX

MOV ES,AX

LEA SI,STR1

Electronics and Communication Engineering MP&MC LAB

Sasi Institute of Technology and Engineering Page 47

LEA DI,STR2

MOV CX,LEN

CLD

REPE

CMPSB

JNE FAIL

PRINTSTRING MSG1

INT 3H

FAIL: PRINTSTRING MSG2

INT 3H

CODE ENDS

END START

Assembly Language Program after execution:

Before Execution: STR1 DB ‘MICROPROCESSORS’

STR2 DB ‘MICROPROCSSOR’

After Execution: MSG2 DB “STRINGS R NOT EQUAL$”

Electronics and Communication Engineering MP&MC LAB

Sasi Institute of Technology and Engineering Page 48

Page 7: Mahi

(d)Reading a String from Keyboard & Displays the Length

Algorithm:

1. Create a Macro to display message.

2. Store the message1& 2 indicate entering the string, length of the string.

3. Initialize data segment to display message1to give suggestion to enter the data from

keyboard.

4. Initialize the count register.

5. Reading a key from keyboard use DOS command.

6. Compare content in the accumulator with ENTER (enter key). If zero flag is set display

message 2 otherwise increment count register then go to step 5.

7. Initialize the offset register to load the length of the string.

8. Use DOS command to display the length of the string.

9. Stop the program

Program:

DISP MACRO MSG

MOV AH,09H

MOV DX,OFFSET MSG

INT 21H

ENDM

DATA SEGMENT

ENTER EQU 0DH

LF EQU 0AH

LEN DB 04 DUP(0)

MSG1DB 10,13, 'ENTER THE STRING INPUT: ', '$'

MSG2 DB CR,LF, 'THE LENTH OF THE STRING INPUT: ', '$'

Page 8: Mahi

DATA ENDS

CODE SEGMENT

Electronics and Communication Engineering MP&MC LAB

Sasi Institute of Technology and Engineering Page 49

ASSUME CS:CODE; DS:DATA

START:MOV AX,DATA

MOV DS,AX

DISP MSG1

MOV CX,0000H ;Count For Length Of Before Execution String

RDKEY:MOV AH,01H

INT 21H

CMP AL,CR ;If Typed Key Is Not ‘CR’ (Enter Key)

JE AHEAD

INC CX ; Increment Count

JMP RDKEY ;Read A Key Until CR Was Pressed

AHEAD:DISP MSG2

MOV SI,OFFSET LEN

MOV AX,CX

CALL HEX2ASCI ; Call a Near Procedure

DISP LEN

MOV AX,4C00H

INT 21H

This procedure converts HEXADECIMAL number to ASCII number

HEX2ASCI PROC NEAR

MOV BX,0001H

Page 9: Mahi

MOV DX,0000H

DIV BX

ADD DL, '0'

ADD AL,'0'

Electronics and Communication Engineering MP&MC LAB

Sasi Institute of Technology and Engineering Page 50

MOV [SI],AL

MOV [SI+1],DL

MOV AL,'$'

MOV [SI+2],AL

RET

HEX2ASCI ENDP

CODE ENDS

END START

Assembly Language Program after execution:

Before Execution: ENTER STRING: SITEECEDEPT

After Execution: LENGTH OF THE STRING IS :0B

Electronics and Communication Engineering MP&MC LAB

Sasi Institute of Technology and Engineering Page 51

(e)Insert a Character before end of String

Algorithm:

1. Initialize the data segment with the string and data which is to be inserted

2. Initialize the index register with the string address

3. Load the value from string in to the accumulator

4. Compare content in the accumulator with the '$'. If zero flag is set read the character in to

Page 10: Mahi

the base register otherwise increment the index register and then go to step 3.

5. Load the data which is to be inserted into the base register from memory location

6. Increment index register

7. Load the base register content to the memory location

8. Stop the program.

Program:

DATA SEGMENT

CHAR DB 'F'; CHARACTER TO BE INSERTED

STRNG DB 'ABCDE','$'

DATA ENDS

CODE SEGMENT

ASSUME CS:CODE,DS:DATA

START: MOV AX,DATA

MOV DS,AX

LEA SI,STRNG ;point to string

NXT:MOV AL,[SI] ;check for end of the string

CMPAL,'$'

JE INSERT ;if string is at the end insert character

INC SI

JMP NXT

INSERT: MOV BL,CHAR

MOV [SI],BL

INC SI

Electronics and Communication Engineering MP&MC LAB

Sasi Institute of Technology and Engineering Page 52

Page 11: Mahi

MOV BL,'$'

MOV [SI],BL REINSERT

MOV AH,4CH

INT 21H

CODE ENDS

END START

Assembly Language Program after execution:

Before Execution: Before Executing Program Strng is ABCDE$

After Execution: After Executing String is ABCDEF$

(f)Delete First Character of given String

Algorithm:

1. Initialize the data segment with the string N

2. Load the count register with N

3. Decrement the count register to indicate the length of the string

4. Initialize the index register to load the string

Electronics and Communication Engineering MP&MC LAB

Sasi Institute of Technology and Engineering Page 53

5.Load the accumulator with the content in the memory location specified by the incremented

index register

6. Increment index register and decrement count. If the count is not zero go to step 5.

7. Stop the program.

Program:

DATA SEGMENT

STRNG DB 'ABCD',24H

SLEN EQU ( $ - STRNG) ;LENGTH OF THE STRING

Page 12: Mahi

DATA ENDS

CODE SEGMENT

ASSUME CS:CODE,DS:DATA

START:MOV AX,DATA

MOV DS,AX

MOV CX,SLEN

DEC CX

MOV SI,OFFSET STRNG

NXT:MOV AL,[SI+1]

MOV [SI],AL

INC SI

DEC CX

JNZ NXT

MOV AH,4CH

INT 21H

CODE ENDS

END START

Electronics and Communication Engineering MP&MC LAB

Sasi Institute of Technology and Engineering Page 54

Assembly Language Program after execution:

Before Execution: ABCD

After Execution: BCD

(g)Sorting16-Bit unsigned values in Ascending order

Algorithm:

1. Start the program.

Page 13: Mahi

2. Load count to CL register & copy the value into CH

3. Initialize source index register (SI)

4. Get the content from the specified memory location to accumulator

4. Compare the content of accumulator with content in the incremented SI

5. If the carry flag is SET then go for the next value step 7

6. Exchange the contents of accumulator and incremented SI

7. Increment SI by two

8. Decrement the count by one

9. Repeat the same procedure until the count equal to zero.

10. Stop the program.

Program:

DATA SEGMENT

NUM DW 0034H, 0056H, 0015H, 0038H, 0093H

Electronics and Communication Engineering MP&MC LAB

Sasi Institute of Technology and Engineering Page 55

DATA ENDS

CODE SEGMENT

ASSUME CS: CODE, DS: DATA

START: MOV AX, DATA

MOV DS, AX

MOV CL,05H

LOOP1: MOV CH,CL

MOV SI, OFFSET NUM

LOOP2: MOV AX,[SI]

CMP AX,2[SI]

Page 14: Mahi

JC LOOP3

XCHG AX,2[SI]

XCHG AX,[SI]

LOOP3:INC SI

INC SI

DEC CH

JNZ LOOP2

DEC CL

JNZ LOOP1

MOV AX, 4C00H

INT 21H

CODE ENDS

END START

Electronics and Communication Engineering MP&MC LAB

Sasi Institute of Technology and Engineering Page 56

Assembly Language Program after execution:

Before Execution:

0000H - 0034H 0002H - 0056H 0004H- 0015H 0006H- 0038H

0008H- 0093H

After Execution:

0000H -0015H 0002H-0034H 0004H - 0038H 0006H - 0056H

0008H - 0093H

(h)Sorting 16-Bit signed values in ascending order

Algorithm:

1. Start the program.

Page 15: Mahi

2. Move count to CL register & copy the value into CH

3. Initialize source index register (SI)

4. Compare the content of accumulator with content in the incremented SI

5. If it is less (accumulator content is small one) go for getting next content step 7 otherwise

go for step 6

Electronics and Communication Engineering MP&MC LAB

Sasi Institute of Technology and Engineering Page 57

6. Exchange the contents of accumulator and incremented SI other wise

7. Increment SI by two

8. Decrement the count by one

9. Repeat the same procedure until the count equal to zero.

10. Stop the program

Program:

DATA SEGMENT

NUM DW 9034H, 0056H, 0015H, 8038H, 0093H

DATA ENDS

CODE SEGMENT

ASSUME CS: CODE, DS: DATA

START: MOV AX, DATA

MOV DS, AX

MOV CL,05H

LOOP1: MOV CH,CL

MOV SI, OFFSET NUM

LOOP2: MOV AX,[SI]

CMP AX,2[SI]

Page 16: Mahi

JL LOOP3

XCHG AX,2[SI]

XCHG AX,[SI]

LOOP3:INC SI

INC SI

DEC CH

JNZ LOOP2

DEC CL

JNZ LOOP1

MOV AX, 4C00H

INT 21H

Electronics and Communication Engineering MP&MC LAB

Sasi Institute of Technology and Engineering Page 58

CODE ENDS

END START

Assembly Language Program after execution:

Before Execution:

0000H - 0056H, 0002H - 0015H, 0004H- 9034H 0006H- 8038H

0008H- 0093H

After Execution:

0000H - 8038H 0002H - 9034H 0004H- 0015H, 0006H - 0056H,

0008H - 0093H,

(i)Sorting 16-Bit Unsigned values in descending order

Algorithm:

1. Start the program.

Page 17: Mahi

2. Move count to CL register & copy the value into CH

3. Initialize source index register (SI)

4. Compare the content of accumulator with content specified by SI

5. If the carry flag is not SET go for the next content step 7

6. Exchange the contents of accumulator and incremented SI

Electronics and Communication Engineering MP&MC LAB

Sasi Institute of Technology and Engineering Page 59

7. Increment SI by two

8. Decrement the count by one

9. Repeat the same procedure until the count equal to zero.

10. Stop the program

Program:

DATA SEGMENT

NUM DW 0034H,0056H,0015H,0038H,0093H,

CNT EQU 04H

DATA ENDS

CODE SEGMENT

ASSUME CS:CODE,DS:DATA,ES:DATA

START: MOV AX,DATA

MOV DS,AX

MOV CX,CNT

LOOP1:MOV CH,CL

MOV SI,NUM

LOOP2:MOV AX,[SI]

CMP AX,2[SI]

Page 18: Mahi

JNC LOOP3

XCHG AX,2[SI]

XCHG AX,[SI]

LOOP3:INC SI

INC SI

DEC CH

JNZ LOOP2

DEC CL

JNZ LOOP1

INT 03

CODE ENDS

Electronics and Communication Engineering MP&MC LAB

Sasi Institute of Technology and Engineering Page 60

END START

Assembly Language Program after execution:

Before Execution:

0000H - 0034H 0002H - 0056H 0004H- 0015H 0006H- 0038H

0008H- 0093H

After Execution:

0000H - 0093H 0002H- 0056H 0004H - 0038H 0006H - 0034H

0008H - 0015H

(j)Sorting 16-Bit Signed values in Descending Order

Algorithm:

1. Start the program.

2. Move count to CL register & copy the value into CH

Page 19: Mahi

3. Initialize source index register (SI)

4. Compare the content of accumulator with content in the incremented SI

5. If the carry flag is not SET go for getting the next content step 7

Electronics and Communication Engineering MP&MC LAB

Sasi Institute of Technology and Engineering Page 61

6. Exchange the contents of accumulator and incremented SI

7. Increment SI by two

8. Decrement the count by one

9. Repeat the same procedure until the count equal to zero.

10. Stop the program

Program:

DATA SEGMENT

NUM DW 9034H, 0056H, 0015H, 8038H, 0093H

DATA ENDS

CODE SEGMENT

ASSUME CS: CODE, DS: DATA

START:MOV AX, DATA

MOV DS, AX

MOV CL,05H

LOOP1:MOV CH,CL

MOV SI, OFFSET NUM

LOOP2:MOV AX,[SI]

CMP AX,2[SI]

JG LOOP3

XCHG AX,2[SI]

Page 20: Mahi

XCHG AX,[SI]

LOOP3:INC SI

INC SI

DEC CH

JNZ LOOP2

DEC CL

JNZ LOOP1

MOV AX, 4C00H

INT 21H

Electronics and Communication Engineering MP&MC LAB

Sasi Institute of Technology and Engineering Page 62

CODE ENDS

END START