输入一行字符串,统计单词SUN出现的个数
程序运行:
代码:
datas segmentENG_max_length db 0ffhENG_length db 0ENG db 100h dup(?)eng_len dw 0SUN db 'SUN'sun_len dw $-SUNcount dw 0 input db 'input a line:$'output db 0DH,0AH,'SUN count:$'
datas endsstacks segment stackdb 100h dup(?)stacks endscodes segmentassume cs:codes,ds:datas,ss:stacks
main proc far
start:push dsmov ax,0hpush axmov ax,datas ;初始化dsmov ds,axlea dx,inputmov ah,9int 21hlea dx,ENG_max_lengthmov ah,10 int 21hmov al,ENG_lengthxor ah,ah mov eng_len,ax xor si,sixor di,dimov bp,sun_len ;取查找单词的长度dec bp ;长度减1,指向SUN最后一单元mov cx,eng_len ;取ENG的长度mov dx,0 ;记录查找SUN出现的次数s:mov al,ENG[si]cmp SUN[di],al ;比较SUN[di]与aljnz not_same ;若不相等,跳转cmp di,bp ;判断di是否指向SUN最后一单元jz match ;若是,则进行单词左右匹配inc di ;若不是,di加1jmp next ;跳转至下一次循环match: ;单词左右匹配mov di,si ;取单词的左边索引sub di,sun_len cmp di,0 ;判断左边索引是否小于0jl judge_right ;若小于0,则判断单词右边索引mov bl,ENG[di] and bl,11011111b ;将单词左边的字母转成大写cmp byte ptr bl,'A' ;判断该字母是否小于'A'jb judge_right ;若小于'A',则判断单词右边索引cmp byte ptr bl,'Z' ;判断该字母是否大于'Z'ja judge_right ;若大于'Z',则判断单词右边索引jmp not_same ;否则跳转至不匹配judge_right: ;单词的右匹配mov di,si ;取单词的右匹配索引inc di cmp di,eng_len ;判断右匹配索引是否越界jz same ;若越界,则匹配mov bl,ENG[di] ;右匹配的字母转换成大写and bl,11011111b cmp byte ptr bl,'A' ;判断该字母是否小于'A'jb same ;若小于'A',则匹配cmp byte ptr bl,'Z' ;判断该字母是否大于'Z'ja same ;若大于'Z',则匹配jmp not_same ;否则不匹配same: inc dx ;匹配,则计算器加1mov di,0 ;di索引置0jmp next ;跳转置下一次循环not_same: xor di,di ;di置0next:inc si loop smov count,dxlea dx,outputmov ah,9int 21hmov ax,countcall decimalretmain endp
decimal proc near uses ax bx cx dx mov bx,10 mov cx,0 de:xor dx,dx div bx push dx inc cx cmp ax,0jnz de de1:pop dxadd dl,30hmov ah,2int 21h loop de1retdecimal endp
codes endsend main