6
Contributors
8
Replies
9
Views
10 Years
Discussion Span
Last Post by Purple Avenger
0

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.

0

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

0

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.

0

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.

0

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 ?

0

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.

0
;
; 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"
;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.