943,973 Members | Top Members by Rank

Ad:
  • Assembly Discussion Thread
  • Unsolved
  • Views: 5335
  • Assembly RSS
Dec 14th, 2006
0

counting digits

Expand Post »
Does anyone knows how can I count how many digits are there in a binary number? Any ideas???
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kiks is offline Offline
2 posts
since Dec 2006
Dec 14th, 2006
0

Re: counting digits

create a loop that repeatedly divides the value by 10 until its value becomes 0. increment a counter on each loop iteration. see the description for the idiv instruction if you do not know what it does.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 14th, 2006
0

Re: counting digits

You would need to convert the binary number to an integer first for that though, wouldn't you?
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Dec 14th, 2006
0

Re: counting digits

Click to Expand / Collapse  Quote originally posted by cscgal ...
You would need to convert the binary number to an integer first for that though, wouldn't you?
binary numbers are either signed or unsigned integers. No conversion necessary.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 14th, 2006
0

Re: counting digits

I'm thinking of a binary number being stored as a string. Nevermind.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Dec 17th, 2006
0

Re: counting digits

Click to Expand / Collapse  Quote originally posted by kiks ...
Does anyone knows how can I count how many digits are there in a binary number? Any ideas???
From what I understand, you want to count the number of digits in binary form, ex. 10001 (decimal 17) has 5 digits. You simply test the most significant towards least significant bits untill you reach a bit that is set on or 1. Total bits minus that count is the number of digits, example would be a 16bit register and a 0000000000010001 binary will give you 11 as your count (as theres 11 zeros). So 16-11 = 5. If you want the number of digits in base 10 then refer to the previous posts.
Ryu
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ryu is offline Offline
24 posts
since Jul 2006
Jan 4th, 2007
0

Re: counting digits

Click to Expand / Collapse  Quote originally posted by Ryu ...
From what I understand, you want to count the number of digits in binary form, ex. 10001 (decimal 17) has 5 digits. You simply test the most significant towards least significant bits untill you reach a bit that is set on or 1. Total bits minus that count is the number of digits, example would be a 16bit register and a 0000000000010001 binary will give you 11 as your count (as theres 11 zeros). So 16-11 = 5. If you want the number of digits in base 10 then refer to the previous posts.
How do i test the most/last significant bits of register?
In a general, is there list of all assembly functions available somewhere on web, i haven't found one ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vov4ik is offline Offline
16 posts
since Jul 2006
Jan 7th, 2007
0

Re: counting digits

Click to Expand / Collapse  Quote originally posted by vov4ik ...
How do i test the most/last significant bits of register?
In a general, is there list of all assembly functions available somewhere on web, i haven't found one ?
You use the TEST instruction, I'm assuming that you are working with IA32 architecture.

Example:

Assembly Syntax (Toggle Plain Text)
  1. mov ax, myBinaryValue
  2. mov dx, 0000000000000001b ; first ROR will make this most significant bit
  3. mov cx, 16 ; 16 bits
  4.  
  5. LOOP:
  6. ror dx, 1 ; rotate bits to the right by 1
  7. test dx, ax ; this test will set ZF=0 (not zero) when the a bit is found on
  8. jnz BITFOUND
  9. dec cx ; decrease bit count
  10. jnz LOOP ; test again
  11.  
  12. ;;;; if program enters here myBinaryValue is zero ;;;;
  13.  
  14. BITFOUND:
  15.  
  16. ;;;; if program enters here a bit set on was found ;;;;
  17. ;;;; number of digits should be in CX register ;;;

Note: this program was never tested.
Last edited by Ryu; Jan 7th, 2007 at 12:49 pm.
Ryu
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ryu is offline Offline
24 posts
since Jul 2006
Jan 8th, 2007
0

Re: counting digits

Assembly Syntax (Toggle Plain Text)
  1. ;
  2. ; Assuming 386 32 bit assembler code
  3. ;
  4. mov ecx,val
  5. xor eax,eax ; Counts the number of '1' bits
  6.  
  7. again:
  8. shl ecx,1
  9. adc eax,0
  10. or ecx,ecx
  11. jnz again
  12.  
  13. done:
  14. ;
  15. ; EAX has the count of bits in "val"
  16. ;
Reputation Points: 31
Solved Threads: 0
Light Poster
Purple Avenger is offline Offline
49 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Assembly Forum Timeline: Re: Word Association Game
Next Thread in Assembly Forum Timeline: how to show PC clock in the screen using TASM





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC