Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 28th, 2006, 11:44 PM   #1
eax
Newbie
 
eax's Avatar
 
Join Date: Mar 2006
Posts: 17
Rep Power: 0 eax is on a distinguished road
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.
eax is offline   Reply With Quote
Old Mar 29th, 2006, 2:45 AM   #2
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 312
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
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
andro is offline   Reply With Quote
Old Mar 30th, 2006, 2:46 PM   #3
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>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.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Mar 31st, 2006, 2:08 AM   #4
eax
Newbie
 
eax's Avatar
 
Join Date: Mar 2006
Posts: 17
Rep Power: 0 eax is on a distinguished road
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
eax is offline   Reply With Quote
Old Mar 31st, 2006, 8:16 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Mar 31st, 2006, 1:56 PM   #6
eax
Newbie
 
eax's Avatar
 
Join Date: Mar 2006
Posts: 17
Rep Power: 0 eax is on a distinguished road
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 ?
eax is offline   Reply With Quote
Old Mar 31st, 2006, 2:04 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Mar 31st, 2006, 2:31 PM   #8
eax
Newbie
 
eax's Avatar
 
Join Date: Mar 2006
Posts: 17
Rep Power: 0 eax is on a distinguished road
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
eax is offline   Reply With Quote
Old Mar 31st, 2006, 3:13 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Mar 31st, 2006, 3:23 PM   #10
eax
Newbie
 
eax's Avatar
 
Join Date: Mar 2006
Posts: 17
Rep Power: 0 eax is on a distinguished road
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
eax is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:35 AM.

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