0

This is my code. It is for count 'e' and 'o' in given string and if the count is same it will print 'Equal' and if the count is not same it will print 'Not Equal'. My code do that one but only if i assign value -1 to cx

mov cx, -1

otherwise it is not work correctly

i want to know is 'e' use less than one byte or 'o' use less more one byte

i put my code

_start:
    mov eax,s1 ; move s1 memory address to eax. eax have s1 first letter memory address in start 
    mov dx,0 ; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
checkE:
    cmp byte[eax],'e'
    jne notequalE ; if not equal then go to notequal section
    
    ;if equal do following things
    
    inc dx
    inc eax
    cmp byte[eax],0
    jne checkE
    cmp byte[eax],0
    je reset
    
notequalE:
    inc eax ; increase pointer and get next letter memory address to eax
    cmp byte[eax],0 ; check next letter is empty or not, if empty then string is over
    jne checkE ; we check eax not eqaul to zero if equal then go down else go to check again

reset:
    mov eax,s1 ; move s1 memory address to eax. eax have s1 first letter memory address in start 
    mov cx,-1 ; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
checkO:
    cmp byte[eax],'o'
    jne notequalO ; if not equal then go to notequal section
    
    ;if equal do following things
    
    inc cx
    inc eax
    cmp byte[eax],0
    jne checkO
    cmp byte[eax],0
    je prn
    
    
notequalO:
    inc eax ; increase pointer and get next letter memory address to eax
    cmp byte[eax],0 ; check next letter is empty or not, if empty then string is over
    jne checkO ; we check eax not eqaul to zero if equal then go down else go to check again

prn:
    cmp cx,dx
    je prnEqu
    
    mov eax,4
    mov ebx,1
    mov ecx,Nequal
    mov edx,l1
    int 80h
    jmp exit
    
prnEqu:
    mov eax,4
    mov ebx,1
    mov ecx,Equal
    mov edx,h1
    int 80h
    
exit:
    mov eax,1
    int 80h
    
section .data

s1 db 'eeoo'


Equal db 'Equal'
h1 equ $-Equal
Nequal db 'Not Equal'
l1 equ $-Nequal

    

lines contain xxx... may be problem

  • `s1 db 'eeoo'` doesn't 0-terminate the string, but your loop looks for that. There's an `o` in 'Not Equal` which you're counting before the loop finally gets to a `0` byte. Use a debugger to single-step your code. – Peter Cordes Oct 10 '20 at 11:45
  • BTW, for future readers that want to count characters efficiently, see [How to count character occurrences using SIMD](https://stackoverflow.com/q/54541129) - no need to waste time looping 1 at a time when x86 can easily count 16 at a time. – Peter Cordes Oct 10 '20 at 11:48

0 Answers0