Hi, I have two questions regarding binary numbers. 1) Does anyone have or know of a routine for converting a dword to ASCII in binary format. Iím aware of the dwtohex procedure in the masm32 lib but I would prefer a binary representation for debugging my program 2) Does anyone know of a quick and efficient method for returning the number of set bits in a dword. e.g. if I input 010011001101b (I know this isnít 32 bit) the return would be 6 Thanks
Posted on 2001-03-21 09:43:00 by Zadkiel
Part 2:

mov eax, TestValue
xor ecx, ecx

.WHILE eax != 0
  shl eax, 1
  adc ecx, 0
.ENDW
ecx will hold the number of bits! That should do it! Mirno This message was edited by Mirno, on 3/21/2001 11:24:36 AM
Posted on 2001-03-21 10:23:00 by Mirno
Heres a nice site to look at for this sort of thing, for your second question look at the 'population count' gem... Assembler Gems umbongo
Posted on 2001-03-21 10:30:00 by umbongo
And as an exercise, here is my first attempt to to the first question, it works, but I think I might optimize it a little.... Anyone fancy a little competition? Umbongo


	mov	ecx,00000000Fh		; value to convert
	mov	eax,31			; start at the top
	mov	ebx,0			; start at the front of the string
lp:
	bt	ecx,eax			; test the bit
	jc	its_a_one		; jmp if it was a '1'
	mov	szResult,'0'	; store it as 0
	inc	ebx			; move the string pointer along
	dec	eax			; decrease the bit to test
	cmp	eax,0FFFFFFFFh		; did we do the last one?
	je	finished		; yep, leave
	jmp	lp			; no loop around

its_a_one:
	mov	szResult,'1'	; store it as 1
	inc	ebx			; move the string pointer along
	dec	eax			; decrease the bit to test
	cmp	eax,0FFFFFFFFh		; did we do the last one?
	je	finished		; yep, leave
	jmp	lp			; no loop around

finished:
Posted on 2001-03-21 10:59:00 by umbongo
My answer to Q1:

push esi
mov eax, myVal
mov esi, lpBuffer
mov ecx, 31

.WHILE ecx > 0
  xor edx, edx
  shl eax, 1
  adc dl, 48
  mov , dl
  dec ecx
  inc esi
.ENDW
mov BYTE PTR , "b"
inc esi
mov BYTE PTR , 0
pop esi
Mirno (forgot to dec ecx :mad: DOH!) This message was edited by Mirno, on 3/21/2001 12:26:52 PM
Posted on 2001-03-21 11:24:00 by Mirno
I sent fastest version to Q1 few days ago. The Svin.
Posted on 2001-03-21 13:24:00 by The Svin
TANSTATFC
Posted on 2001-03-21 14:30:00 by umbongo
Thanks Everyone, Mirno, your routine worked a charm for outputting the binary number. I had to change the line: mov ecx, 31 to mov ecx, 32 as it was missing the last digit otherwise. As for counting the set bits I used the routine on x86 gems page as it is just so incredably fast. Thanks again Everyone. This message was edited by Zadkiel, on 3/21/2001 4:43:48 PM
Posted on 2001-03-21 15:04:00 by Zadkiel
Mirno, Your code isn't working, it's not testing the last bit (bit 0), you have to change thee while from :-

.WHILE	ecx > 0

to

.WHILE	ecx != -1

That said I'm timing it at 180 cycles, I have mine down to 171 now :) when I get it right down, I'll post it here. Sven, I can't find you binary converter - can you post it again? umbongo
Posted on 2001-03-21 15:44:00 by umbongo
Now, here's entertaining, I spend an hour optimizing my code and came up with Mirno's code difference registers, and not using .while funny world isn't it? Umbongo
Posted on 2001-03-21 15:57:00 by umbongo
Two last arg - booleans space - false out: '1111000011110000' space - true out: '1111 0000 1111 0000' sz - false - make ASCII (for insert cases) sz - true - make ASCIIZ So there can be 4 combinations. The algo is quite simple: B2Asiids proc uses ecx eax edx edi ebx (Hiro I use codestar\codeend t but nothing happend, sorry) num:DWORD,dest:DWORD,space:DWORD,sz:DWORD .data ALIGN 4 tbl dd '0000','1000','0100','1100' dd '0010','1010','0110','1110' dd '0001','1001','0101','1101' dd '0011','1011','0111','1111' .code mov ebx,space mov ecx,num mov edi,dest rol ecx,8 xor edx,edx mov al,cl mov dl,cl and eax,0Fh shr dl,4 mov eax,tbl mov edx,tbl mov dword ptr ,eax mov dword ptr ,edx mov byte ptr ,20h rol ecx,8 xor edx,edx mov al,cl mov dl,cl and eax,0Fh shr dl,4 mov eax,tbl mov edx,tbl mov dword ptr ,eax mov dword ptr ,edx mov byte ptr ,20h rol ecx,8 add ebx,ebx xor edx,edx mov al,cl mov dl,cl and eax,0Fh shr dl,4 mov eax,tbl mov edx,tbl mov dword ptr ,eax mov dword ptr ,edx mov byte ptr ,20h rol ecx,8 add ebx,ebx xor edx,edx dec ebx mov al,cl adc ebx,0 mov dl,cl and eax,0Fh mov ecx,sz shr dl,4 mov eax,tbl lea ecx, mov edx,tbl mov dword ptr ,edx mov byte ptr ,0 mov dword ptr ,eax ret B2Asiids endp
Posted on 2001-03-21 16:11:00 by The Svin
No branching, nice! I was thinking of a lookup table to do a quicker version, now all I need is a few ram chips to store 4,294,967,295 strings of 32 bytes each.... hmmm can anyone lend me a few quid? :) Umbongo
Posted on 2001-03-21 16:28:00 by umbongo