[comp.sys.amiga.tech] 68000 DBcc command

mph@rover.UUCP (Mark Huth) (07/16/88)

In article <7332@cup.portal.com> Julian@cup.portal.com writes:
> [ a description of a "problem" with DBEQ instruction ]
>    Obviously, something was wrong. Then I noticed a difference - the Z flag
>had been set by a previous operand earlier in the program. I ran through
>the code section several times, checking the looping mechanism (DBEQ) with
>the Z flag set and cleared. Strangely, the DBEQ command would only decrement
>d3 whenever the Z flag was cleared. According to the Motorola 68000 reference
>manual, the condition codes play no role in any DBxx command. What is going on?

You're welcome.

This operation is exactly how the DBcc instructions are supposed to
work.

DBEQ first checks the condition code flag Z and if set, goes to the
next instruction.  Else, the counter register is decremented.  If -1,
then the next instruction is executed else the instruction that is the
target of the branch is executed.

See pg 89 of 4th edition or pg 115 of 3rd edition of the 68000 manual.

This instruction allows conditional termination of simple counted for loops -
such as a break condition within the loop.

Mark Huth

phils@tekigm2.TEK.COM (Philip E Staub) (07/16/88)

In article <7332@cup.portal.com> Julian@cup.portal.com writes:
>
...
>d3 whenever the Z flag was cleared. According to the Motorola 68000 reference
>manual, the condition codes play no role in any DBxx command. What is going on?
....
>                   Many thanks in advance,
>                           Julian L Brown
>                           sun!portal!cup.portal.com!julian

Wrongo!

The condition codes most definitely play a role in the operation of all of
the DBxx commands.

To quote (without permission) from the description of the DBcc series of 
commands from the 68000 programmers reference manual:

	"The instruction first tests the condition to determine if the
	termination condition for the loop has been met, and if so, no
	^^^^^^^^^^^^^^^^^^^^^
	operation is performed. If the termination condition is not true,
	the low order 16 bits of the counter data register are decremented
	by one. If the result is -1, the counter is exhausted and execution
	continues at the next instruction. if the result is not equal to -1,
	execution continues at the location indicated by the current value 
	of PC plus the sign-extended 16-bit displacement."

The "termination condition" is based solely upon the contents of the
condition codes, so that a DBEQ instruction will test the zero flag, and if
it is set, the termination condition is met, therefore no operation is
performed. I suspect that you may have really wanted a DBF instruction.
(Many assemblers also will accept DBRA). Thus the condition for this
instruction is never met ("false" is never TRUE), and terminal count is the
only terminator of the instruction.

Hope this helps.

Phil
-- 
------------------------------------------------------------------------------
Phil Staub        
Tektronix, Inc., Vancouver, Washington 98668
phils@tekigm2.MEN.TEK.COM

scott@applix.UUCP (Scott Evernden) (07/16/88)

In article <7332@cup.portal.com> Julian@cup.portal.com writes:
> ...  Strangely, the DBEQ command would only decrement
>d3 whenever the Z flag was cleared. According to the Motorola 68000 reference
>manual, the condition codes play no role in any DBxx command. What is going on?

DBxx does this:
	1. It checks the condition codes to see if a termination condition
	   has been met.  If so, it does nothing, and execution continues
	   with the next instruction.
	2. Next, it decrements a register, and compares it to -1.  If equal,
	   it does nothing, and execution continues.  Otherwise, the branch
	   is taken.

DBEQ d3, label 
can be read as "Until Z is clear, decrement d3, and branch if it isn't -1."
	  
My guess is that you really want to use:
		DBF d3,loop
	or:
		until FALSE is TRUE, decrement d3, and if it's not -1,
		then branch to 'loop'.

-scott

dillon@CORY.BERKELEY.EDU (Matt Dillon) (07/16/88)

:had been set by a previous operand earlier in the program. I ran through
:the code section several times, checking the looping mechanism (DBEQ) with
:the Z flag set and cleared. Strangely, the DBEQ command would only decrement
:d3 whenever the Z flag was cleared. According to the Motorola 68000 reference
:manual, the condition codes play no role in any DBxx command. What is going on?
:   What I want to know is: Is this problem caused by my debugger and the
:looping problem exists somewhere in MY code  OR   is this an undocumented
:feature for the DBxx series of commands?

	Specifically, motorola says this:

	if specified-condition-is-false
	    Dn.W = Dn.W - 1;
	    if (Dn.W != -1) then loop, else break out of the loop
	else
	    break out of the loop.

	I.E. the (word sized) data register is only decremented when the
	condition is FALSE.  When the condition is TRUE, the loop is
	broken out of WITHOUT decrementing the data register.  The loop
	is also broken out of when the data register reaches -1 (word
	sized).

Quoted without permission:

	"The instruction first tests the condition to determine if the
	termination condition for the loop has been met, and if so, NO
	OPERATION is performed.  If the termination condition is not true,
	the low order 16 bits of the counter data register are DECREMENTED
	by one.  If the result is -1, the counter is exhausted and 
	execution contiues with the next instruction" (i.e. no branch)
	"If the result is not equal to -1, execution continues at the
	location indicated by the current value of the PC plus the
	sign-extended 16 bit displacement." (i.e. branch).

					-Matt

Julian@cup.portal.com (07/17/88)

Phil,
   As several others have shown me, you are right. I simply misunderstood
what the manual was saying. I assumed that the condition used in the DBcc
series of commands was refering to the internal check on the counter being
decremented. Obviously, if I assumed that, the loop would perform wildly, and
it did - to my suprise and amazement :-).
   Anyway, thank you all for the help!

                     Julian L Brown
                     sun!portal!cup.portal.com!julian

pl@tut.fi (Pertti Lehtinen) (07/19/88)

From article <7332@cup.portal.com>, by Julian@cup.portal.com:
> the Z flag set and cleared. Strangely, the DBEQ command would only decrement
> d3 whenever the Z flag was cleared. According to the Motorola 68000 reference
> manual, the condition codes play no role in any DBxx command. What is going on?

	As read from manual:

		If condition false then (Dn - 1 => Dn;
		If Dn != -1 then PC+d => PC

	Condition is termination condition which is tested first
	if it doesn't terminate loop, counter is decremented and tested
	this operation does not alter flags.

	For pure counter controlled loop use dBRA, because
	condition has nothing to do with counting, it is 
	alternate exit condition based on flags set by previous
	instructions.

	As we can see, decrement is done only when condition is false.
	( This case Z is cleared ), but in that case we don't branch
	either.  Odd thing is that only low 16-bit are used as counter.

	So C strncpy could be something like

		LEA	str1, A1
		LEA	str2, A2
		MOVE.W	max,D0
	Loop:
		MOVE.B	(A2)+,(A1)+	; sets/clears zero flag (I hope)
		DBEQ	D0,Loop		; loop until count or NULL moved

	I hope this helps.



-- 
pl@tut.fi			! All opinions expressed above
Pertti Lehtinen			! are preliminary and in subject
N 61 26' E 23 50'		! to change without any further notice.