The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: daniek91 on January 15, 2012, 09:13:58 PM

Title: Problem getting specific bit of variable/register
Post by: daniek91 on January 15, 2012, 09:13:58 PM
Hi all ! :)

I'm working on my school project. I'm stuck at getting specific bit value. I need to test bits of AL.
That's what I've done do far :

mov eax,10101010101010101101010101010101b ;just random value for test
mov ecx,8 ;8 because I want to check 8 bits, so I'll loop it 8 times
mov ebx,0 ;number of starting bit

TEST_BITS:
push ecx

bt eax,ebx ;that should return value of ebx bit in eax to CF
jc one
jnc zero

zero:
invoke WriteConsoleA,hout,OFFSET w_zero,1,rout,0 ; writes 0
jmp skip_one

one:
invoke WriteConsoleA,hout,OFFSET w_one,1,rout,0 ; writes 1

skip_one:
inc ebx
pop ecx
loop TEST_BITS


I thought it would work just fine but it always returns "10000000".

I've been looking for an answer for some time now, but I just couldn't find it.
What I've done wrong ? Please help me..



Best regards,
Daniel
Title: Re: Problem getting specific bit of variable/register
Post by: Gunner on January 15, 2012, 09:35:30 PM
Works fine.  As we don't know what w_zero or rout is it could be that...

Look in \masm32\vkdebug\dbproc

inlcude debug.inc and inlcuelib debug.lib

    mov eax,10101010101010101101010101010101b ;just random value for test
mov ecx,8 ;8 because I want to check 8 bits, so I'll loop it 8 times
mov ebx,0 ;number of starting bit

TEST_BITS:
push ecx

bt eax,ebx ;that should return value of ebx bit in eax to CF
jc one
jnc zero

zero:
PrintText "ZERO"
jmp skip_one

one:
PrintText "One"

skip_one:
inc ebx
pop ecx
dec     ecx
    jnz     TEST_BITS
;loop TEST_BITS
Title: Re: Problem getting specific bit of variable/register
Post by: qWord on January 15, 2012, 09:40:23 PM
The problem is, that API-calls commonly overwrite all register except for EDI,ESI and EBX, which must be preserved by callee.
You must preserve EAX when calling an API function, as you have done it with ECX.
Title: Re: Problem getting specific bit of variable/register
Post by: Gunner on January 15, 2012, 09:41:32 PM
Ah, missed that...  :U
Title: Re: Problem getting specific bit of variable/register
Post by: daniek91 on January 15, 2012, 09:52:21 PM
YES  ! :bg

I've spend 3 days looking for an answer and You've solved it in 3 minutes !
Thank You very, very, very much  :cheekygreen:
Now I'll know where I can find help if anything comes up while working on my project  :U


Best regards,
Daniel