Page 1 of 1

ASM: Bitwise operations

Posted: Wed Jun 25, 2008 8:25 am
by inflater
Hi,
sorry as I'm asking this too late, but could you verify me please?

I'm writing a pmode floppy disk driver. If a "bad hardware" error is encountered, the bits 6-7 of the status byte (now loaded to AL let's say) should be "11" in binary, like this:

11000000

What should I do to detect them if they are 1?
I tried this:

Code: Select all

test al,0C0h
jnz BadHardwareError
The driver reads properly from the floppy and puts the data in the buffer (a miracle, lol. :shock:).
But when I set the floppy speed to "reserved" (1 mbps) which shouldn't be used, on a real machine it displays "bad HW" message. That same applies if I'll remove out the floppy before reading :shock:, which should display another error, but it'll show "bad HW" again. ("bad HW" should the driver show only if a floppy drive is broken I think). If everything is okay, the error won't show. /EDIT: At least on emulators. On the real machine it did show.

My snippet of code looks like this:

Code: Select all

...

	mov al,[FDD_StatusByte0]
	
	test al,0C0h
	jnz FDD_ErrorBadHW

	test al,80h
	jnz FDD_ErrorBadCommand

	test al,40h
	jnz FDD_ErrorCantDone

	test al,20h
	jnz FDD_ErrorSectorError
...
The "bad HW" should check if the floppy's status byte is 11000000, "bad command" for
10000000, etc. And if I would put the "bad command" error handler before the Bad HW handler, it would show "Bad command error" right instead of "Bad HW error", so I'm making a mistake somewhere.

Could somebody post me some "templates" in assembly on how to e.g. check if bytes 2-3 are 1, or 0, or if only a single bit is 0, etc?

Re: ASM: Bitwise operations

Posted: Wed Jun 25, 2008 8:44 am
by RedEagle
test x, y means:
Check, if the y-th bit is set in x

you have to use the and-instruction:

Code: Select all

and eax, 0xC0
jnz error1

and eax 0x80
jnz error2

;...
check a single bit

Code: Select all

test eax, *position of the bit (0..31)*
check for more bits

Code: Select all

and eax, *mask*

Re: ASM: Bitwise operations

Posted: Wed Jun 25, 2008 8:46 am
by kmtdk
hi
well have you tried with "bt"
"bt ax,0xc0"
this shuld work too ..
ยจ

kmt

Re: ASM: Bitwise operations

Posted: Wed Jun 25, 2008 8:50 am
by inflater
RedEagle wrote:test x, y means:
Check, if the y-th bit is set in x
Where did you get that? In "Intel Hex Opcodes And Mnemonics" I found for TEST opcode this:
Performs a logical AND of the two operands updating the flags register without saving the result. And I don't want to alter the FDD return byte...

But thanks for your response, I'll try it.

@kmtdk: Thanks for posting. I'll try the both methods and I'll post up my results.

Regards
inflater

Re: ASM: Bitwise operations

Posted: Wed Jun 25, 2008 8:59 am
by devel
Hi,
I think you should compare two operands by another way. `test' instruction does logical bitwise
so if you get an error 0x1100000 or 0x10000000 the ZF flag is non-zero in both cases because
also (0x11000000 & 0x10000000) != 0.

Regards,
devel

Re: ASM: Bitwise operations

Posted: Wed Jun 25, 2008 9:11 am
by Korona
Never use BT or jumps to compute such a simple thing. That's incredibly inefficient.
and eax, 0xC0
cmp eax, 0xC0
jnz error1 <- typo: this line should be: jz error1

EDIT: Use test when you only have to check whether one or more bits are set.
Use and + cmp to check whether multiple bits are set at the same time.

Re: ASM: Bitwise operations

Posted: Wed Jun 25, 2008 10:35 am
by inflater
Well, I think the problem is somewhere else.

AThelp with the "mnemonics manual" shows that the TEST opcode does just a ordinary logical AND but without updating EAX but the flags. If I would perform the AND EAX and stuff, I would need to push eax pop eax etc.

The TEST opcode as I debugged can be used for multiple bits at the same time.
Let's say our "data byte" returned was 00111111 (lol). If I would place it in AL and TEST it with 0C0h:

00111111 TEST 1100000 = 0. So it's not the "bad HW" exception.
If our returned data byte was "successful operation", AL would be 0 and TESTing with 0C0h results in 0.

If the floppy disk controller was rabid and it would show "bad command error" (10000000) along with e.g. "drive error" (00010000), the result would be nonzero. But hey - is that even possible? :shock: In Bochs after successful read, the first data byte is 0. But IF I'll try to remove the floppy out, the data byte is still 0 :shock: - I think it's a bug in Bochs, because when a no disk is in drive, Windows fires a exception and Bochs clarifies the situation as "successful" with "0 bytes read", according to bochsout.txt: read() on floppy image returns 0

On the other side, in the first floppy status byte there seems to be bits 0-2 defined as a "current drive" and "current head". That may be the culprit. I'll try to zero them out.

//EDIT 2: Right before the function readsector was called, I paused Bochs with the function getch() in my OS, removed the disk out and signalled in Bochs that the floppy was no longer inserted. After a attempt to read from floppy, Windows didn't show any exception, and in EAX there was 0x90 = 10010000 in binary: bad command + drive error. Good grief, it seems I've predicted it. :lol:

**** these multi error codes! :evil:

Re: ASM: Bitwise operations

Posted: Wed Jun 25, 2008 10:46 am
by Korona
If I would perform the AND EAX and stuff, I would need to push eax pop eax etc.
If you have to check whether two or more bits are set in a register (at the same time) i.e. if((register & 0xC0) == C0) you have to use the AND instruction. (and trash the other bits) The x86 has no (fast) instruction that can perform this operation in one operation.

Re: ASM: Bitwise operations

Posted: Wed Jun 25, 2008 11:07 am
by inflater
Okay, I've tried the AND EAX thing and it had the SAME effect (!) like the TEST opcode. It should be "bad command" error, but it was Bad HW again. :?

A solution seems to be as this: firstly, I need to check if the Drive error bit is set. Then I can diagnose if it's Bad HW, Bad command or cant done error.

But what seems to be confusing: is "Drive error" bit set when a "Not ready" bit is set too? #-o I think it shouldn't, because the 7 and 6 bits are "status after end of operation" :

If 7. and 6. bit are set both (11), it's Bad HW.
If only 7. bit is set (from the 7-6 group, not from the whole byte), it's Bad command.
If only 6. bit is set, it's "Cant done".
If both 7 and 6 bit are zero (not set), the "end of operation" means that it was okay.

The 5th bit is "head standing in progress" or something like that :D, 4th "drive error", 3rd "not ready" and 2nd, 1st and 0. are zeroed by me.

OMFG. Confusing as hell. Is the drive error bit set when the 3rd (outside of the 7-6 group) is set too? God damn. The engineers of the first IBM PC should have more sense when designing these floppy status bytes. :roll:

Re: ASM: Bitwise operations

Posted: Wed Jun 25, 2008 11:12 am
by Korona
and eax, 0xC0
cmp eax, 0xC0
jz error (there is a typo in my previous post in this line)

has definitely not the same effect as

test eax, 0xC0
jnz error

The first snippet does if((value & 0xC0) == 0xC0) and the second one does if((value & 0xC0) != 0).

Re: ASM: Bitwise operations

Posted: Wed Jun 25, 2008 11:38 am
by inflater
Now I tried the fixed 'AND EAX' routine and replaced the all three test eax's from the 7-6 group and throws this:

Bochs, floppy inserted: okay, no message
Bochs, floppy out: Bad command error (in the 7-6 bits group)

MS VPC 2007, floppy inserted: Error reading sector (head "standing", out of the 7-6 group)
MS VPC 2007, floppy out: Can't done operation (7-6 group)

Pentium II, floppy inserted: Can't done operation (7-6 group)
Pentium II, floppy out: machine freeze (?!)

Well, that's it. I'm going to pause the work for *at least* 2 weeks. After that, I'll try to construct a function that'll read the fdd's volume label, since in Bochs it seems to load fine (on VPC and PII I didn't tried). It's phucked allrite.