Write a program for comparing two strings, Computer Engineering

Assignment Help:

Q. Write a program for comparing two strings?

Let's write a program for comparing two strings. Determine the following piece of code that has been written in C to compare two strings. Let's presume that 'str1' and 'str2' are two strings which are initialised by some values and 'ind' is index for these character strings:

for (ind = 0; ( (ind <9) and (str1[ind] = = str2[ind]) ), ind + +)

Intermediate code in assembly language produced by a non-optimising compiler for the above piece may appear like:

MOV IND, 00; ind: = 0

L3:                               CMP IND, 08; Ind < 9

                                    JG L1   ; not so; skip

                                    LEA AX, STR1; offset of str1 in AX register

                                    MOV BX, IND; it uses a register for indexing into 

                                                                        ; The array

LEA CX, STR2; str2 in CX

                                    MOV DL, BYTE PTR CX [BX]

                                    CMP DL, BYTE PTR AX [BX]; str1 [Ind] = str2 [Ind]

                                    JNE L1    ; no, skip

                                    MOV IND, BX 

                                    ADD IND, 01

L2:                               JMP L3    ; loop back

L1:

 

What we find in above code: a large code which could have been improved further if 8086 string instructions would have been used.

; Program: Matching two strings of similar length stored in memory locations.

; REGISTERS: Uses CS, DS, ES, AX, DX, CX, SI, DI

DATA SEGMENT

            PASSWORD              DB                  'FAILSAFE'              ; source string

            DESTSTR                   DB                  'FEELSAFE'              ; destination string

            MESSAGE                 DB                  'String are equal $'

DATA ENDS

CODE SEGMENT

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

                                    MOV AX, DATA

                                    MOV DS, AX   ; Initialise data segment register

                                    MOV ES, AX   ; Initialise extra segment register

; As destination string is considered to be in extra segment. Please note that ES is also 

; initialised to the same segment as of DS.

LEA SI, PASSWORD; Load source pointer

                                    LEA DI, DESTSTR; Load destination pointer

                                    MOV CX, 08   ; Load counter with string length

                                    CLD   ; Clear direction flag so that comparison is

                                                ; done in forward direction.

EPE CMPSB; Compare the two string byte by byte

                                    JNE NOTEQUAL; If not equal, jump to NOTEQUAL

                                    MOV AH, 09; else display message 

                                    MOV DX, OFFSET MESSAGE;

                                    INT 21h; display the message

NOTEQUAL:             MOV AX, 4C00h; interrupt function to halt

                                    INT 21h

CODE ENDS

END


Related Discussions:- Write a program for comparing two strings

Define syntax of mpi_scan function, Q. Define syntax of MPI_Scan  function...

Q. Define syntax of MPI_Scan  function? MPI_Scan (Sendaddr,, Receiveaddr , count, datatype, op, comm): It merges the partial values in p final results that are received in

How can we design radio button, Q. How can we design Radio Button? Radi...

Q. How can we design Radio Button? Radio buttons are used when only one out of group of options is to be chosen. In the illustration code we have put a line break after every b

Illustrate the advantages of encapsulation, Advantages of Encapsulation ...

Advantages of Encapsulation You can also delay the resolution of the details until after the design.  You can keep your code modular.

What is meant by opening a data file, What is meant by opening a data file ...

What is meant by opening a data file A data file is a file that can be read from or written to. Data files are particularly useful when large amounts of data are involved. Each

Describing Random Algorithm, Suppose you''re given n numbers and asked to f...

Suppose you''re given n numbers and asked to find a number that is greater than or equal to the median a) What is the lower bound for the worst case complexity of this problem?

Sql requirements in the sap development system, Which interface converts SQ...

Which interface converts SQL requirements in the SAP development system to those of the database? Database Interface converts SQL in the SAP.

Rational performance of an agent - artificial intelligence, Rational perfor...

Rational performance of an agent - artificial intelligence: To summarize, an agent takes input from its violence and affects that environment. The rational performance of an a

How linq is beneficial than stored procedures, There are couple of benefit ...

There are couple of benefit of LINQ over stored procedures.   1. Debugging - It is really very difficult to debug the Stored procedure but as LINQ is part of .NET, you can us

How to set table properties in dreamweaver, Q. How to Set Table Properties ...

Q. How to Set Table Properties in dreamweaver? Now you will use table Property inspector to clear the extra space in navigation buttons table and to add a background color in t

Explain interactive reporting, What is interactive reporting? A classic...

What is interactive reporting? A classical non-interactive report having of one program that makes a single list.  Instead of one extensive and detailed list, with interactive

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd