Sonic and Sega Retro Message Board: Conditions in M68K - Sonic and Sega Retro Message Board

Jump to content

Hey there, Guest!  (Log In · Register) Help
  • 2 Pages +
  • 1
  • 2
    Locked
    Locked Forum

Conditions in M68K

#1 User is offline saxman 

Posted 26 May 2008 - 12:06 AM

  • Oldbie
  • Posts: 2626
  • Joined: 08-April 04
  • Gender:Male
  • Location:United States of America
  • Wiki edits:136

"Sample code" said:

Obj01_MdNormal_Checks:
move.b (Ctrl_1_Press_Logical).w,d0
andi.b #$70,d0
bne.s Obj01_MdNormal


What I wanna know is how the conditions work. That BNE (branch if not equal)... what are we comparing, and what are we comparing it to? There's no CMP or TST instructions. I've looked everywhere and can't find an explanation for this.

#2 User is offline Tweaker 

Posted 26 May 2008 - 12:16 AM

  • Posts: 12389
  • Joined: 27-June 04
  • Gender:Male
Certain instructions (and, sub, and some others I can't think of) test the Z-flag when executed, essentially doubling as a tst (which checks for zero). You can do a conditional branch immediately after to save a few cycles. What that's doing is, after the AND instruction, branching if the result is NOT zero (hence the bne). It took me a while to figure out you could do this, too—I believe it was drx who told me about it. If you're optimizing for speed in the strictest sense of the word, then such a thing is important to keep in mind.

On a side note, we have an [asm] tag which will syntax-highlight and preserve tabbing for M68k code, which I think you'll find much better to use than [quote]. :(

#3 User is offline saxman 

Posted 26 May 2008 - 12:25 AM

  • Oldbie
  • Posts: 2626
  • Joined: 08-April 04
  • Gender:Male
  • Location:United States of America
  • Wiki edits:136
Syntax Highlighted Code: ASM
	[color= #00bfff;]cmpi[/color].[color= #00bfff;]b[/color]	[color= #ff0000;]#[/color][color= #ff0000;]$[/color][color= #ff0000;]1E[/color],anim_frame(a0)
[color= #00bfff;]bcs[/color].[color= #00bfff;]s[/color] Obj01_MdNormal

What about that -- it branches if the carry bit is set. I'm confused as to what the carry bit has anything to do with the CMPI instruction. What is this portion actually doing in English?

#4 User is offline FraGag 

Posted 26 May 2008 - 12:35 AM

  • Posts: 659
  • Joined: 09-January 08
  • Gender:Male
  • Location:Québec, Canada
  • Project:an assembler
  • Wiki edits:6
Look at this wiki page: SCHG:68000 Instruction Set. At the bottom, the condition codes are described, with relational operators to help. So according to this, CS means "less than" (unsigned comparison).

#5 User is offline Tweaker 

Posted 26 May 2008 - 12:40 AM

  • Posts: 12389
  • Joined: 27-June 04
  • Gender:Male
I'm blind—I used that page to look up the name of the Z-flag (wasn't sure to refer to bit or flag, so I just said flag =P) and I never noticed the headers for those conditional matchups. I think I'll convert that section into a proper table so it's easier to work out.

#6 User is offline saxman 

Posted 26 May 2008 - 12:43 AM

  • Oldbie
  • Posts: 2626
  • Joined: 08-April 04
  • Gender:Male
  • Location:United States of America
  • Wiki edits:136
You're right, I didn't realize the conditional match-ups there either. But this works.

Okay another question... I know a0 generally points to the object we're currently dealing with, but what address is stored in a1? Reason I'm asking is there's this instance:

Syntax Highlighted Code: ASM
[color= #00bfff;]add[/color].[color= #00bfff;]w[/color]	x_pos(a0),d1
[color= #00bfff;]sub[/color].[color= #00bfff;]w[/color] x_pos(a1),d1


#7 User is offline Tweaker 

Posted 26 May 2008 - 12:54 AM

  • Posts: 12389
  • Joined: 27-June 04
  • Gender:Male
I believe that, when dealing with Sonic, a1 is the address of the object that he's currently standing on. Correct me if I'm wrong, though; it may be Tails instead.

#8 User is offline FraGag 

Posted 26 May 2008 - 12:58 AM

  • Posts: 659
  • Joined: 09-January 08
  • Gender:Male
  • Location:Québec, Canada
  • Project:an assembler
  • Wiki edits:6

View Postsaxman, on May 26 2008, 01:43 AM, said:

You're right, I didn't realize the conditional match-ups there either. But this works.

Okay another question... I know a0 generally points to the object we're currently dealing with, but what address is stored in a1? Reason I'm asking is there's this instance:

Syntax Highlighted Code: ASM
[color= #00bfff;]add[/color].[color= #00bfff;]w[/color]	x_pos(a0),d1
[color= #00bfff;]sub[/color].[color= #00bfff;]w[/color] x_pos(a1),d1

If you want to be 100% sure, backtrack the code until you see a lea something,a1. Inside an object's code, a1 should be explicitly initialized with lea somewhere before being used.

#9 User is offline saxman 

Posted 26 May 2008 - 01:49 AM

  • Oldbie
  • Posts: 2626
  • Joined: 08-April 04
  • Gender:Male
  • Location:United States of America
  • Wiki edits:136
Alrighty thank you all. I'll post in this topic if I run across any additional problems.

#10 User is offline notaz 

Posted 26 May 2008 - 03:36 AM

  • Posts: 32
  • Joined: 05-February 08
  • Wiki edits:2

View PostTweaker, on May 26 2008, 12:16 AM, said:

Certain instructions (and, sub, and some others I can't think of) test the Z-flag when executed, essentially doubling as a tst (which checks for zero).

This is not quite correct, actually those instructions SET the flags, and Bcc both checks them and decides to jump or not to jump..

#11 User is offline saxman 

Posted 26 May 2008 - 11:03 AM

  • Oldbie
  • Posts: 2626
  • Joined: 08-April 04
  • Gender:Male
  • Location:United States of America
  • Wiki edits:136
Here's another question:

Syntax Highlighted Code: ASM
	[color= #00bfff;]andi[/color].[color= #00bfff;]b[/color]	[color= #ff0000;]#[/color][color= #ff0000;]$[/color][color= #ff0000;]C[/color],d0		[color= #adadad; font-style: italic;]; is left/right pressed?[/color]
[color= #00bfff;]bne[/color].[color= #00bfff;]s[/color] Obj01_Traction [color= #adadad; font-style: italic;]; if yes, branch[/color]
[color= #00bfff;]move[/color].[color= #00bfff;]w[/color] inertia(a0),d0
[color= #00bfff;]beq[/color].[color= #00bfff;]s[/color] Obj01_Traction


We're performing AND on the d0 register. Then (I'm assuming) it branches if the d0 register is not zero. Then it moves "inertia" into the d0 register. This is the part that trips me -- it branches if... d0 is zero? Am I guessing right?




EDIT: Also, here's another silly thing I can't figure out:

Syntax Highlighted Code: ASM
	[color= #00bfff;]move[/color].[color= #00bfff;]w[/color]	inertia(a0),d0
[color= #00bfff;]beq[/color].[color= #00bfff;]s[/color] Obj01_Traction
[color= #00bfff;]bmi[/color].[color= #00bfff;]s[/color] Obj01_SettleLeft
 
[color= #adadad; font-style: italic;]; slow down when facing right and not pressing a direction[/color]
[color= #adadad; font-style: italic;]; Obj01_SettleRight:[/color]
[color= #00bfff;]sub[/color].[color= #00bfff;]w[/color] d5,d0
[color= #00bfff;]bcc[/color].[color= #00bfff;]s[/color] +


I get the top portion before the comments, I just included those to ensure everything is in proper context. The half is where I get lost. The BCC will do a branch if the first operand is less than the second operand in the condition. But what operands are we comparing? I see the subtract d5 from d0. I'm assuming d0 is part of the condition, but what is it being compared to... zero?
This post has been edited by saxman: 26 May 2008 - 11:12 AM

#12 User is offline FraGag 

Posted 26 May 2008 - 11:21 AM

  • Posts: 659
  • Joined: 09-January 08
  • Gender:Male
  • Location:Québec, Canada
  • Project:an assembler
  • Wiki edits:6

View Postsaxman, on May 26 2008, 12:03 PM, said:

Here's another question:

Syntax Highlighted Code: ASM
	[color= #00bfff;]andi[/color].[color= #00bfff;]b[/color]	[color= #ff0000;]#[/color][color= #ff0000;]$[/color][color= #ff0000;]C[/color],d0		[color= #adadad; font-style: italic;]; is left/right pressed?[/color]
[color= #00bfff;]bne[/color].[color= #00bfff;]s[/color] Obj01_Traction [color= #adadad; font-style: italic;]; if yes, branch[/color]
[color= #00bfff;]move[/color].[color= #00bfff;]w[/color] inertia(a0),d0
[color= #00bfff;]beq[/color].[color= #00bfff;]s[/color] Obj01_Traction


We're performing AND on the d0 register. Then (I'm assuming) it branches if the d0 register is not zero. Then it moves "inertia" into the d0 register. This is the part that trips me -- it branches if... d0 is zero? Am I guessing right?

Exactly.

View Postsaxman, on May 26 2008, 12:03 PM, said:

EDIT: Also, here's another silly thing I can't figure out:

Syntax Highlighted Code: ASM
	[color= #00bfff;]move[/color].[color= #00bfff;]w[/color]	inertia(a0),d0
[color= #00bfff;]beq[/color].[color= #00bfff;]s[/color] Obj01_Traction
[color= #00bfff;]bmi[/color].[color= #00bfff;]s[/color] Obj01_SettleLeft
 
[color= #adadad; font-style: italic;]; slow down when facing right and not pressing a direction[/color]
[color= #adadad; font-style: italic;]; Obj01_SettleRight:[/color]
[color= #00bfff;]sub[/color].[color= #00bfff;]w[/color] d5,d0
[color= #00bfff;]bcc[/color].[color= #00bfff;]s[/color] +


I get the top portion before the comments, I just included those to ensure everything is in proper context. The half is where I get lost. The BCC will do a branch if the first operand is less than the second operand in the condition. But what operands are we comparing? I see the subtract d5 from d0. I'm assuming d0 is part of the condition, but what is it being compared to... zero?

cmp is almost the same as sub, except cmp doesn't store the result anywhere (it only affects the flags), while sub stores the result in its second argument (here, d0). It will branch if d0 is greater than or equal to zero.


If you're confused by the carry flag, you can also see it as the borrow flag: if the result of sub is negative, the carry/borrow flag will be set.

#13 User is offline saxman 

Posted 26 May 2008 - 11:47 AM

  • Oldbie
  • Posts: 2626
  • Joined: 08-April 04
  • Gender:Male
  • Location:United States of America
  • Wiki edits:136
Ran into another problem:

Syntax Highlighted Code: ASM
	[color= #00bfff;]move[/color].[color= #00bfff;]b[/color]	angle(a0),d0
[color= #00bfff;]add[/color].[color= #00bfff;]b[/color] d1,d0
[color= #00bfff;]move[/color].[color= #00bfff;]w[/color] d0,-(sp)
[color= #00bfff;]bsr[/color].[color= #00bfff;]w[/color] CalcRoomInFront
[color= #00bfff;]move[/color].[color= #00bfff;]w[/color] (sp)+,d0


I know what SP is from x86 assembly, but I'm not sure how I'm supposed to read it here. Here's what I'm able to interpret:

- move "angle" into the d0 register
- add d1 to d0
- push d0 into the stack, then move the stack pointer back one position???
- sub CalcRoomInFront
- pop value from stack into d0, then advance the stack pointer one position???

The + and - is guess work. I don't know what they're there for, and that's the problem.

#14 User is offline FraGag 

Posted 26 May 2008 - 11:52 AM

  • Posts: 659
  • Joined: 09-January 08
  • Gender:Male
  • Location:Québec, Canada
  • Project:an assembler
  • Wiki edits:6

View Postsaxman, on May 26 2008, 12:47 PM, said:

The + and - is guess work. I don't know what they're there for, and that's the problem.

The minus sign before (sp) means to decrement the stack pointer before using it. The plus sign after (sp) means to increment the stack pointer after using it. This is necessary as operations on the stack pointer don't automatically change the stack pointer itself. Predecrementing and postincrementing it is necessary so you won't overwrite previous data.

#15 User is offline Tweaker 

Posted 26 May 2008 - 11:55 AM

  • Posts: 12389
  • Joined: 27-June 04
  • Gender:Male

View Postnotaz, on May 26 2008, 04:36 AM, said:

View PostTweaker, on May 26 2008, 12:16 AM, said:

Certain instructions (and, sub, and some others I can't think of) test the Z-flag when executed, essentially doubling as a tst (which checks for zero).

This is not quite correct, actually those instructions SET the flags, and Bcc both checks them and decides to jump or not to jump..

Hrm. I wasn't sure whether it set the flags or not—from looking at the wiki page, I mean—so I just said "check" to make sure. I mean, it wouldn't set that flag unless the value was zero, right?

Damn it all, this topic is making me look dumb XD

Oh, and saxman, I believe that is simply pushing an address onto the stack for temporary storage, then putting it back into d0 after CalcRoomInFront has been called. It's a quick alternative to storing up to a long of data without taking up a register.

  • 2 Pages +
  • 1
  • 2
    Locked
    Locked Forum

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users