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

State the implementation of a security policy, State the implementation of ...

State the implementation of a security policy The implementation of a security policy should invariably cover all parameters of security such as physical access to the server,

Load sharing facility resource management, The primary aims/details of Load...

The primary aims/details of Load Sharing Facility Resource Management Software(LSFRMS) are good resource utilization by routing the task to the most appropriate system and good uti

How are standard query operators implemented in linq, Standard Query Operat...

Standard Query Operators are executed as extension methods in .NET Framework. These Standard Query Operators can be used to work with any collection of objects that executes the IE

Hazard in pipeline - computer architecture, Hazard in pipeline - computer a...

Hazard in pipeline - computer architecture: A hazard in pipeline .-removing a hazard frequently need that some instructions in the pipeline to be permitted to proceed as othe

Function wallsintact, We also need to know, given a cell, which of its neig...

We also need to know, given a cell, which of its neighbours has all of its walls intact. Write the function wallsintact that accepts the grid and a list of neighbouring cells and r

Define step by step procedure in computer programming, Define Step by Step ...

Define Step by Step Procedure in Computer Programming? The Computer programming is the skill and art of creating a computer program a defined set of instructions in source code

Could tcp allow ip to checksum the data, Could TCP allow IP to checksum the...

Could TCP allow IP to checksum the data? TCP cannot permit IP to checksum data yet IP has its own checksum for its header. IP layer is fundamentally responsible for routing of

What is importance of xml into edi and electronic commerce, What is the imp...

What is the importance of XML into EDI and electronic commerce? XML has been explained as lightweight SGML: XML demonstrates great promise for its inherent capability to per

What is difference between hold time and setup, What is difference between ...

What is difference between hold time and setup? The interviewer was looking for one exact reason, and it’s really a good answer as well..The hint is hold time doesn't depend onto c

C++, Write a program to find the area under the curve y = f(x) between x = ...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

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