![]() |
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. |
Here's some psuedo code...
:
lsbit = 0 |
>find number of the least significant 1 bit set
:
; ebx has the valueThere's not a convenient instruction for that, just shift and count until the value is zero for the simplest solution. |
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 |
You have no statement to take you out of the loop.
|
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 ? |
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.
|
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 |
Been a long time since I wrote this stuff, check the syntax.
:
mov ecx, 32 |
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:
|
| 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