MIPS Assembly Language Write a simple program to count the

MIPS Assembly Language

* Write a simple program to count the number of non overlapping repetitions of a character pattern in a character string. For example \" the pattern \"aa\" appears twice in the in the string \"aabbaaa\". Provide a user interface to read the character pattern and the string.

Solution


Function in Python searches for occurence of pattern 11 in \'1011101111\'
def function(string, str_to_search_for):
count = 0
for x in xrange(len(string) - len(str_to_search_for) + 1):
if string[x:x+len(str_to_search_for)] == str_to_search_for:
count += 1
return count


function(\'1011101111\',\'11\')
returns 5

pROGRAM IN x86
COUNTSTR CSECT
USING COUNTSTR,R13 base register
B 72(R15) skip savearea
DC 17F\'0\' savearea
STM R14,R12,12(R13) prolog
ST R13,4(R15) \"
ST R15,8(R13) \"
LR R13,R15 \"
MVC SOMW_VAR,=CL32\'TREE THREE THROUGH\'
MVC LENH,=F\'17\' lh=17
MVC SOM_SHOP,=CL8\'REE\' SOM_SHOP=\'REE\'
MVC LENN,=F\'2\' ln=2
BAL R14,SHOW call show
MVC SOMW_VAR,=CL32\'SUNSUNSHINE \'
MVC LENH,=F\'11\' lh=11
MVC SOM_SHOP,=CL8\'SUN\' SOM_SHOP=\'SUN\'
MVC LENN,=F\'4\' ln=4
BAL R14,SHOW call show
L R13,4(0,R13) epilog
LM R14,R12,12(R13) \"
XR R15,R15 \"
BR R14 exit
HAYSTACK DS CL32 SOMW_VAR
SOM_SHOP DS CL8 SOM_SHOP
LENH DS F length(SOMW_VAR)
LENN DS F length(SOM_SHOP)
*------- ---- show---------------------------------------------------
SHOW ST R14,SAVESHOW save return address
BAL R14,COUNT count(SOMW_VAR,SOM_SHOP)
LR R11,R0 ic=count(SOMW_VAR,SOM_SHOP)
MVC PG(20),SOMW_VAR output SOMW_VAR
MVC PG+20(5),SOM_SHOP output SOM_SHOP
XDECO R11,PG+25 output ic
XPRNT PG,80 print buffer
L R14,SAVESHOW restore return address
BR R14 return to caller
SAVESHOW DS A return address of caller
PG DC CL80\' \' buffer
*------- ---- count--------------------------------------------------
COUNT ST R14,SAVECOUN save return address
SR R7,R7 n=0
LA R6,1 istart=1
L R10,LENH lh
S R10,LENN ln
LA R10,1(R10) lh-ln+1
LOOPI CR R6,R10 do istart=1 to lh-ln+1
BH ELOOPI
LA R8,SOM_SHOP @SOM_SHOP
L R9,LENN ln
LA R4,SOMW_VAR-1 @SOMW_VAR[0]
AR R4,R6 +istart
LR R5,R9 ln
CLCL R4,R8 if substr(SOMW_VAR,istart,ln)=SOM_SHOP
BNE NOTEQ
LA R7,1(R7) n=n+1
A R6,LENN istart=istart+ln
NOTEQ LA R6,1(R6) istart=istart+1
B LOOPI
ELOOPI LR R0,R7 return(n)
L R14,SAVECOUN restore return address
BR R14 return to caller
SAVECOUN DS A return address of caller
* ---- -------------------------------------------------------
YREGS
END COUNTSTR


Output:
TREE THREE THROUGH REE 2
SUNSUNSHINE SUN 2

MIPS Assembly Language * Write a simple program to count the number of non overlapping repetitions of a character pattern in a character string. For example \
MIPS Assembly Language * Write a simple program to count the number of non overlapping repetitions of a character pattern in a character string. For example \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site