Does anyone knows how can I count how many digits are there in a binary number? Any ideas???

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.

You would need to convert the binary number to an integer first for that though, wouldn't you?

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.

I'm thinking of a binary number being stored as a string. Nevermind.

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.

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 ?

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:

mov ax, myBinaryValue
mov dx, 0000000000000001b  ; first ROR will make this most significant bit
mov cx, 16  ; 16 bits
 
LOOP:
ror   dx, 1   ; rotate bits to the right by 1
test  dx, ax ; this test will set ZF=0 (not zero) when the a bit is found on
jnz   BITFOUND
dec  cx       ; decrease bit count
jnz   LOOP  ; test again
 
;;;; if program enters here myBinaryValue is zero ;;;;
 
BITFOUND:
 
;;;; if program enters here a bit set on was found ;;;;
;;;; number of digits should be in CX register ;;;

Note: this program was never tested.

;
; Assuming 386 32 bit assembler code
;
   mov ecx,val 
   xor eax,eax  ; Counts the number of '1' bits

again:
   shl ecx,1
   adc eax,0
   or ecx,ecx
   jnz again

done:
;
; EAX has the count of bits in "val"
;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.