Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Assembly (http://www.programmingforums.org/forum20.html)
-   -   least significant 1 bit (http://www.programmingforums.org/showthread.php?t=9096)

eax Mar 28th, 2006 11:44 PM

least significant 1 bit
 
please give me any clue for:


How i can write a code in assembly language (80*86 ) to find number of the least significant 1 bit set and total number of 1 bits set.

andro Mar 29th, 2006 2:45 AM

Here's some psuedo code...

:

lsbit = 0
pattern = 0b00000001
count = 0

for (loopcount = 1; loopcount <= 8; loopcount++)
        num = num && pattern
        num = num - pattern

        if ZeroFlag is set
                count = count + 1
                if lsbit == 0
                        lsbit = loopcount

        pattern = pattern << 1


Narue Mar 30th, 2006 2:46 PM

>find number of the least significant 1 bit set
:

; ebx has the value
mov ecx,0 ; Only needed if ebx won't be zero
bsf ecx,ebx
; ecx has the bit position of the LSB

>total number of 1 bits set
There's not a convenient instruction for that, just shift and count until the value is zero for the simplest solution.

eax Mar 31st, 2006 2:08 AM

I wrote this code(pasted below) and collecting total number of 1 bits in 00fffff0h
but when i run my program it goes in infinite loop.

I am not able to figure out why its going in infinite loop.
help me with this.


mov ecx,32
mov eax,00fffff0h


check: shl eax,1
jc setcount
loop check



setcount: inc count
loop check

DaWei Mar 31st, 2006 8:16 AM

You have no statement to take you out of the loop.

eax Mar 31st, 2006 1:56 PM

i used "loop" inctruction which automatically decrement value in ecx
and when ecx =0 it fall out of the loop

And since i want to process all 32 bits integer (which is input for program) , i am using loop instruction for both labels : check and setcount.

Does using loop instruction for both labels can cause some problem ?

DaWei Mar 31st, 2006 2:04 PM

Suppose ecx goes to zero at the upper loop statement. Then you fall to "inc count", do that, then execute the last loop statement, which decrements ecx to a non-zero value, so off you go, around the mulberry bush again.

eax Mar 31st, 2006 2:31 PM

yes, i think i got your point.

But when i change my code (pasted below) it still goes into infinite loop


mov ebx,31
mov eax,00fffff0h


dojob: cmp ebx,0
jnz doshift

doshift: shl eax,1
dec ebx
jc setcount
jmp dojob



setcount: inc count
jmp dojob

DaWei Mar 31st, 2006 3:13 PM

Been a long time since I wrote this stuff, check the syntax.
:

mov ecx, 32
mov eax, theValueToCheck

check:
shl eax, 1
jnc dontCountTheThang
inc count

dontCountTheThang:
loop check


eax Mar 31st, 2006 3:23 PM

yeh !! it is working perfectly fine. Thanks a lot for the code.

BUT , in the mean time i wrote a test code to see if instruction "jc" is working :



mov eax,00fffff0h

dojob: shl eax,1
jc print_value_after_shift
jnc exit



print_value_after_shift: dtoa no,eax
output lable

and when i run the program it was jumping to label "print_value_after_shift"
even when cf <> 1 , i.e i got the output as 33554400 .

This is really surprising for me now,


DOES IT MEAN instruction "jc" IS NOT WORKING ??











Quote:

Originally Posted by DaWei
Been a long time since I wrote this stuff, check the syntax.
:

mov ecx, 32
mov eax, theValueToCheck

check:
shl eax, 1
jnc dontCountTheThang
inc count

dontCountTheThang:
loop check




All times are GMT -5. The time now is 10:09 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC