How to count number of occurrences of pattern within a string?

This is program for string operations using near and far procedure.How to find no. of occurrences of substring?(Platform --TASM,OS-windows XP)

  • program part 1- EXTRN CONCAT:FAR EXTRN SUBSTRING:FAR   PUBLIC STR1 PUBLIC STR2 PUBLIC MSG6 PUBLIC MSG7 PUBLIC MSG8 PUBLIC MSG9   MSG MACRO MSG               ;macro of display msg LEA DX,MSG MOV AH,09H INT 21H ENDM   INIT MACRO MOV AX,@DATA                   ;initialize data segment MOV DX,AX ENDM   EXIT MACRO MOV AH,4CH                          ;macro terminate code INT 21H ENDM   ACCEPT MACRO STR MOV AH,0AH                         ;macro accept string LEA DX,STR INT 21H ENDM   DISP MACRO STR MOV AH,09H                        ;macro display string LEA DX,STR INT 21H ENDM   .MODEL SMALL .DATA   MSG1 DB 10,13,""     DB 10,13,"---MENU----"     DB 10,13,"1.CONCAT"     DB 10,13,"2.COMPARE"     DB 10,13,"3.SUBSTRING"     DB 10,13,"4.EXIT"     DB 10,13,"ENTER YOUR CHOICE: $"   MSG2 DB 10,13,"ENTER STRING1: $" MSG3 DB 10,13,"ENTER STRING2: $" MSG4 DB 10,13,"STRINGS ARE EQUAL $" MSG5 DB 10,13,"STRINGS ARE NOT EQUAL $" MSG6 DB 10,13,"CONCAT STRING: $" MSG7 DB 10,13,"SUBSTRINGS ARE FOUND $" MSG8 DB 10,13,"SUSTRINGS ARE NOT FOUND $" MSG9 DB 10,13,"NUMBER OF OCCURRENCES ARE: $"   STR1 DB 25 DUP('$') STR2 DB 25 DUP('$')   SRC DB ? DEST DB ?   .CODE START: INIT                                ;initialize data segment       L1:     MSG MSG1                    ;display msg1     MOV AH,01H                  ;accept 1 digit     INT 21H       CMP AL,31H                 ;compare 1     JE L2                              ;equal then          CMP AL,32H                  ;compare 2     JE L3                                   ;equal then       CMP AL,33H                          ;compare 3     JE L4                                        ;equal then       JMP L5                               ;jump exit L2:     MSG MSG2                     ;display msg2     ACCEPT STR1                ;accept string1     MSG MSG3                    ; display msg3     ACCEPT STR2                 ;accept string2          MSG MSG6                         ;display msg6     CALL CONCAT                    ;call procedure concat     JMP L1   L3:    MSG MSG2                      ;display msg2     ACCEPT STR1                        ;accept string1     MSG MSG3                           ; display msg3     ACCEPT STR2                    ;accept string2       CALL COMPARE              ;call procedure compare     JMP L1   L4:    MSG MSG2                    ;display msg2     ACCEPT STR1                            ;accept string1     MSG MSG3                            ; display msg3     ACCEPT STR2                      ;accept string2       CALL SUBSTRING                  ;call procedure substring     JMP L1   L5:    EXIT                                 ;terminate code   COMPARE PROC NEAR           ;procedure for compare       LEA SI,STR1+2     LEA DI,STR2+2       MOV CL,STR2+1     MOV AL,STR1+1       CMP AL,CL                        ;compare length     JNE L7   L6:    MOV BL,[SI]     MOV DL,[DI]     CMP DL,BL                          ;compare each character of both     JNE L7       INC DI     INC SI     DEC AL          JNZ L6     MSG MSG4                               ;display msg4     JMP L8   L7:    MSG MSG5                       ;display msg5 L8:    RET   ENDP COMPARE                          ;end procedure compare   END START                        ;end code program part 2- EXTRN STR1 EXTRN STR2   EXTRN MSG6 EXTRN MSG7 EXTRN MSG8 EXTRN MSG9   PUBLIC CONCAT PUBLIC SUBSTRING   MSG MACRO MSG LEA DX,MSG MOV AH,09H INT 21H ENDM   .MODEL SMALL .DATA   COUNT DB ? ADD_OFF DW ?   .CODE   START: CONCAT PROC FAR                  ;procedure for concat     LEA SI,STR1+1     LEA DI,STR2+1          MOV CH,[SI]     MOV BH,[DI]     MOV CL,CH     MOV BL,BH       INC SI     INC DI   L9:    INC SI     DEC CH     JNZ L9      L10:    MOV AL,[DI]                            ;concat string     MOV [SI],AL          INC SI     INC DI     DEC BH     JNZ L10          LEA SI,STR1+2                        ;display concat string     ADD BL,CL   L11:    MOV DL,[SI]     MOV AH,02H     INT 21H     INC SI     DEC BL     JNZ L11       RET ENDP CONCAT                             ;end procedure concat     SUBSTRING PROC FAR                    ;procedure for substring       LEA SI,STR1+1     LEA DI,STR2+1       MOV CL,[SI]     MOV CH,[DI]          INC SI     INC DI       MOV COUNT,CH     MOV BL,00H   L12:    MOV AL,[SI]     CMP AL,[DI]                           ;compare string     JE L13       INC SI     DEC CL     JNZ L12       MSG MSG8     JMP L14   L13:    INC SI     INC DI     DEC CL     DEC CH     JNZ L12           INC BL     MSG MSG7     CMP BL,00H       JE L14   L14:    MOV CH,02                   ;display no. of occurrence     MOV CL,04     MSG MSG9   L15:    ROL BL,CL     MOV DL,BL     AND DL,0FH                    ;hex to ascii     CMP DL,09H     JBE L17     ADD DL,07H L17:    ADD DL,30H     MOV AH,02H     INT 21H     DEC CH     JNZ L15     RET ENDP SUBSTRING                      ;end procedure substring   END START

  • Answer:

    As a precondition for answering, please: Provide a link to the documentation of the INT 21h functions that you are using. The documentation should also describe the structure (memory layout) that the INT 21h function expects in its arguments. The description should be complete - all knowledge that is necessary for a programmer to make a function call. (Important.) In particular, make sure you document about the structure of the buffer used by INT 21h, AH = 0Ah. See http://stackoverflow.com/a/13206675/377657 Some early diagnosis. (Note: I may or may not update this if you change your code, so this section may be out-of-date.) It appears that: You did not initialize the structures STR1 and STR2 before calling the ACCEPT macro, specifically before using the INT 21h, AH = 0Ah function. In particular, you did not set the Buffer Capacity and the Current String Length in those structures. (Those are not null-terminated string buffers.) These can be initialized by ACCEPT macro, or by the caller of the ACCEPT macro - it has to be done somewhere.

Ryan Wong at Quora Visit the source

Was this solution helpful to you?

Other answers

Thanks for A2A. "I don't do windows", so I cannot answer this. But I think that you need to define a global main which tlink would use as the first instruction of the program during execution, similar to _start is used by ld on Linux [http://stackoverflow.com/questions/17898989/what-is-global-start-in-assembly-language] A web search and here is your solution: http://edn.embarcadero.com/article/22020 Define a STARTPOINT from where your program will start execution, like main() in C.

Ravi Arora

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.