jeudi 27 mai 2021

How do I count the number of occurrences of a particular string in nasm?

I'm trying to write a nasm program where I take string input from the user and count the occurrence of 'a' in the string. I am able to accept and display string but it is not counting the occurrence of character 'a'. this is the code I have written so far: I don't know if my counter variable is incrementing and I don't know where I am going wrong.

%macro accept 2
mov eax,3
mov ebx,0
mov ecx,%1
mov edx,%2
int 80h
%endmacro

%macro display 2
mov eax,4
mov ebx,1
mov ecx,%1
mov edx,%2
int 80h
%endmacro 
section .text
    global _start       
_start:                     

accept str1,50
display msg,len 
display str1,50

dec al
mov byte[cnt],al
;mov ch,[count]
;and ch,0

l1: 
 mov esi,str1
 mov edi,str2
 
again1:
 mov al,[esi]
 cmp al, 'a'
 je l2
 cmp al,'A'
 je l2
 ;jmp again2
 
 
l2: 
 mov [edi],al
 inc esi
 inc edi
 dec cl
 jnz again1

 
;again2: 
 ;inc esi
 ;inc edi
 ;dec cl
 ;jnz again1
 
string:
 mov esi,str2
 
c_string:
 lodsb 
 cmp al,'x'
 je my
 inc ch
 jmp c_string
 
 
my: 
display msg1,len1
mov [count],ch
display count,1
jmp myend
 

 
myend: 
 mov eax,1
 int 80h
 

    

section .data

msg db  'Enter a string',0ah    
len equ $ - msg         
msg1 db 'Number of occurences of "a" is ',0ah
len1 equ $-msg1

section .bss
str1: resb 50
str2: resb 50
cnt resb 50
count resb 1




Aucun commentaire:

Enregistrer un commentaire