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

Example of processor arrangements, Q. Example of processor arrangements? ...

Q. Example of processor arrangements? !HPF$ PROCESSORS P (10) This initiates a group of 10 abstract processors assigning them combined name P. !HPF$ PROCESSORS Q (4, 4)

Two services that are used to deal with communication, Explain about the tw...

Explain about the two services that are used to deal with communication. Message Service: Used by the application servers to change short internal messages, all system commu

What is a customer-to-business transaction, What is a customer-to-business ...

What is a customer-to-business transaction? C2B (customer-to-business): The most significant activity into e-commerce isn’t selling. That is buying. Rather often which do

Describe the working of components of i-way infrastructure, Describe the wo...

Describe the working of components of I-way Infrastructure. Working of I-way Infrastructure components: a. Consumer access equipment shows a critical category, it is the abs

SGML, Write short note of SGML

Write short note of SGML

Milestones, Here are some common development milestones that  you should ai...

Here are some common development milestones that  you should aim for: 1. Send a packet over. Send an acknowledgement back. 2. Have checksum algorithm executed 3. Ability t

Types of buses, different types of buses with diagram

different types of buses with diagram

Example calculation of entropy, Example Calculation: If we see an exam...

Example Calculation: If we see an example we are working with a set of examples like S = {s 1 ,s 2 ,s 3 ,s 4 } categorised with a binary categorisation of positives and negati

Determine the equivalent hexadecimal form of decimal number, Solve the equa...

Solve the equation 65.535 10 = X 16 Ans. In order to get X, convert the Decimal number 65.535 in its equal Hexadecimal form. So first taking 65 the integer part to convert in i

What is arithmetic and logic unit, What is Arithmetic and Logic Unit Ar...

What is Arithmetic and Logic Unit Arithmetic and Logic Unit: The ALU is the 'core' of any processor. It implements all arithmetic operations (addition, multiplication, subtract

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