Log in

View Full Version : How do I make Bit Test set the Carry Flag?


5aLIVE
October 15th, 2009, 08:42
I am trying to establish the format of a simple text file.

I've been analysing a subroutine that returns a pointer to a substring.

I can get it to handle the first substring properly.

The routine of interest to me is called twice. Each call returns a pointer to a substring.

On the second call to the routine, it returns a null pointer.
I know what the second substring should look like, I just can't seem to get the routine to return the pointer.

I believe this is the bit of code that could hold the answer.

005472A6 AND EAX,0FF <-EAX contains a character read from the text file
005472AB BT DWORD PTR DS:[EDX],EAX <-This pointer holds the value 00000000
005472AE JNB SHORT 005472B3 <-I *think* I need the carry flag to be set so as not to jump and start parsing the second string.

What character (if any) will set the carry flag? I have tried editing the text file with a hex editor and inserting 00 and FF so far, this didn't work.

I would have thought EAX = 00h would have set the CF to 1?

So I need to format the text like this substring_1?substring_2.

I just need to find the "separating" character "?" which will set the CF for the second string to be parsed. I confirmed this to work as expected by setting the CF to 1 by hand at the right time in the loop.


Thanks,
5aLIVE.

bilbo
October 15th, 2009, 15:04
The best explanation is found in Art of Assembly (6.6.4.2):
Quote:

If the first operand is a memory location, the bt instruction tests the bit at the given offset in memory, regardless the value of the index. For example, if bx contains 65 then

bt TestMe, bx

will copy bit one of location TestMe+8 into the carry flag. Once again, the size of the operand does not matter. For all intents and purposes, the memory operand is a byte and you can test any bit after that byte with an appropriate index. The actual bit bt tests is at bit position index mod 8 and at memory offset effective address + index/8.


In your case, the array starting at memory address EDX must hold 32 bytes (256 characters / 8 bits per byte). The bits, in this array, whose value is ONE represent with their position in the array the characters that will set the Carry Flag.

Best regards, bilbo

5aLIVE
October 15th, 2009, 16:42
Thankyou Bilbo, I managed to get it working now. I though I would show my result in case it helps someone.

Here is the 32 element character array with a single bit at the 8th index position (counting from 0) :

006B3D34 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00
006B3D44 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

We can see that we need to access the 8th index at bit position zero.
The calculation 64 mod 8 = 0 gives us the result we need.

So by setting the "separator" character in the text file to the '@' symbol (40h) gets the program to parse a second string and return its pointer as expected.

Kind regards,
5aLIVE.

Kayaker
October 15th, 2009, 23:32
I never did quite figure out how to use this, but some might be interested in playing with this old bobble:

BitTest Strings by The Svin

It's a utility to create bitstrings to be used as part of switchcase along with bt opcode.

http://www.apihooks.com/EliCZ/import/bt.zip

5aLIVE
October 16th, 2009, 02:35
Hi Kayaker, I can't say I understand how to read a created bitstring with this tool either. At first I thought it places the modulo of the character you wish to switch to in the relevant bit position you need to test. However, the results don't seem consistent. I can't be using it properly and the help file doesn't offer much.

Can anyone explain how to use this little trinket?

Your post does answer one question I had which was how would this code look as a high level routine? I've never seen a switch case structure like this before. But then I'm not an accomplished programmer so that might explain why.