I have an assignment to make a code that will check what is the longest "string" of zeros inside a double word binary number, for example (in a Byte) if I have this number:
10010001
The answer will be 3.

The code I've written works fine until a certain point that the CF keep having zeros although it should be 1.

The loop is not infinite it ends but about half way the CF is not changing, and leads to a incorrect count of zeros in the number.

org 100h

mov cx,32

notZero:
    shr a,1
jc notZero

loop:
jnc firstOne
        isOne:
            shr a,1
            mov counter,0
        jc isOne
        firstOne:
        add counter,1
        mov al,counter
        cmp al,biggest
        jng isSmaller
            mov biggest,al
        isSmaller: 
        shr a,1   
LOOP loop

mov ah,0
int 16h
ret   

a DD 10010001100100011001100111010011b 
biggest DB 0
counter DB 0

Edited 4 Years Ago by iPanda: n/a

Logic is a bit wrong.
No need for the variables biggest or counter. si = counter and di = biggest

This should work:

mov     cx, 32

NextOne:
    shr     a, 1
    jnc     ItsZero
    mov     si, 0
    jmp     Next
    
ItsZero:
    inc     si
    cmp     si, di
    jbe     Next
    mov     di, si
    
Next:
    dec     cx
    jnz     NextOne     

    ; display result in di here

It still doesn't work, about half way the CF only get zeros although it should get 1 so that leads to an invalid count.

My code that I posted works just fine:

10010000000100000000100111010011b (iPanda.asm, 12)
di = 8 (iPanda.asm, 31)

10010001100100011001100111010011b (iPanda.asm, 12)
di = 3 (iPanda.asm, 31)

10000000000000000000000000000001b (iPanda.asm, 12)
di = 30 (iPanda.asm, 31)

10000000000000000000000000000000b (iPanda.asm, 13)
di = 31 (iPanda.asm, 32)

00000000000000000000000000000001b (iPanda.asm, 13)
di = 31 (iPanda.asm, 32)

00000000001000000000000000000001b (iPanda.asm, 13)
di = 20 (iPanda.asm, 32)

10010001100100011001100111010011b (iPanda.asm, 13)
di = 3 (iPanda.asm, 32)

10010001100000011001100111010011b (iPanda.asm, 13)
di = 6 (iPanda.asm, 32)

Shr moves the least significant bit into CF either 1 or 0

How are you using the code and where? Are you adding it right after

notZero:

    shr a,1

jc notZero

because that is NOT needed, the code I posted does not need that.

Maybe you're using the 32\64 bit Assembly?
Because today I were in class and it turns out that because I use the 8086 16 bit compiler it cant check more than 16 bits so after I split the double word number it worked find.

thanks anyway!

And if I knew how to press solved I guess I'd do that.

Right above the editor to enter a reply, you should see something like:

Has this thread been answered?
If this thread has been successfully resolved, please Mark this Thread as Solved so that it can be added to our Knowledge Base and help others. Also, posters who replied appreciate knowing that they were helpful.

This question has already been answered. Start a new discussion instead.