![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2006
Posts: 17
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Professional Programmer
|
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 |
|
|
|
|
|
#3 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>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 There's not a convenient instruction for that, just shift and count until the value is zero for the simplest solution.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Mar 2006
Posts: 17
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You have no statement to take you out of the loop.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Mar 2006
Posts: 17
Rep Power: 0
![]() |
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 ? |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Mar 2006
Posts: 17
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#10 | |
|
Newbie
Join Date: Mar 2006
Posts: 17
Rep Power: 0
![]() |
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:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|