Jump to content



0

Smartbranching question


7 replies to this topic

#1 Primordial Ooze OFFLINE  

Primordial Ooze

    Dragonstomper

  • 504 posts
  • Quacker Blaster Lead Programmer
  • Location:United States of America

Posted Thu Mar 20, 2008 1:44 PM

According to the bataribasic online reference turning on smartbranching will reduce the code size of goto statements at the cost of speed and making the assembly code harder to read. I would like to know if it also effects the gosub command as both are very similar and peform similar actions?

Sincerely,

Open Source Pong

#2 batari OFFLINE  

batari

    )66]U('=I;B$*

  • 6,236 posts
  • begin 644 contest

Posted Thu Mar 20, 2008 2:24 PM

View PostOpen Source Pong, on Thu Mar 20, 2008 2:44 PM, said:

According to the bataribasic online reference turning on smartbranching will reduce the code size of goto statements at the cost of speed and making the assembly code harder to read. I would like to know if it also effects the gosub command as both are very similar and peform similar actions?

Sincerely,

Open Source Pong
It does not affect goto by itself, and does not affect gosub at all.

With smartbranching off, if you try to use a statement like "if x=4 then 20" it may or may not work, depending how far away line 20 is. Using "if x=4 then goto 20" will always work, but at the cost of some space. "then 20" always uses 6 bytes, but "then goto 20" always uses 9 bytes. It doesn't sound like much but it does add up.

With smartbranching on, you may always use "if x=4 then 20" and bB will insert conditional directives in the assembly code to automatically determine if it should use "then" or "then goto" internally, and will use the 6-byte version if it can, but in doing so, the assembly code will be much more obfuscated.

To compare, "if x=4 then 20" with smartbranching off compiles to:
		 LDA x
		CMP #4
		beq .20
But with smartbranching on, it compiles to:
		LDA x
		CMP #4
  if ((* - .20) < 127) && ((* - .20) > -128)
		BEQ .20
  else
		bne .1skip20
		jmp .20
.1skip20
 endif
If you don't even intend to look at the assembly code, you can leave smartbranching on and never worry about where to use "then goto."

#3 Primordial Ooze OFFLINE  

Primordial Ooze

    Dragonstomper

  • 504 posts
  • Quacker Blaster Lead Programmer
  • Location:United States of America

Posted Thu Mar 20, 2008 3:41 PM

i save 3 bytes per "if goto" statement but, i lose x ticks per "fix" with smartbranching turned on right?

Sincerely,

Open Source Pong

#4 batari OFFLINE  

batari

    )66]U('=I;B$*

  • 6,236 posts
  • begin 644 contest

Posted Thu Mar 20, 2008 3:53 PM

View PostOpen Source Pong, on Thu Mar 20, 2008 4:41 PM, said:

i save 3 bytes per "if goto" statement but, i lose x ticks per "fix" with smartbranching turned on right?

Sincerely,

Open Source Pong
Not sure what you mean by "ticks." If you never want to look at the assembly, with smartbranching on, you won't lose anything, and can only save ROM space.

But, you don't always save 3 bytes. That only happens if the target label in an if-then statement is somewhat near. If it's far away, you will use just as much space as an if-then goto statement.

#5 SeaGtGruff OFFLINE  

SeaGtGruff

    River Patroller

  • 4,543 posts
  • Location:Georgia, USA

Posted Thu Mar 20, 2008 4:06 PM

View Postbatari, on Thu Mar 20, 2008 5:53 PM, said:

View PostOpen Source Pong, on Thu Mar 20, 2008 4:41 PM, said:

i save 3 bytes per "if goto" statement but, i lose x ticks per "fix" with smartbranching turned on right?
Not sure what you mean by "ticks."
He means cycles-- ticks of the CPU clock. ;)

You always save with smartbranching-- you save on cycles as well as bytes. That is to say, you never *lose* bytes or cycles with smartbranching turned on.

You know, it might make sense to have smartbranching turned on by default, and then make the user specifically turn it off if they don't want to use it. I know you were thinking the compiler directives would make the code more difficult to read, but (1) there are a lot of compiler directives in the code anyway, (2) most bB users don't ever look at the compiled assembly code anyway, and (3) most users would probably prefer that smatrbranching be turned on automatically so they don't have to worry about trying to remember to turn it on.

Michael

#6 Primordial Ooze OFFLINE  

Primordial Ooze

    Dragonstomper

  • 504 posts
  • Quacker Blaster Lead Programmer
  • Location:United States of America

Posted Thu Mar 20, 2008 4:15 PM

By ticks i mean the amount of processing time the action takes. I believe the max ticks for the atari 2600 is 2700 ticks, anything higher then that and the game will jitter, shake and roll. Also SeaGtGruff answered my question about smartbranching not affecting game speed.

Sincerely,

Open Source Pong

Edited by Open Source Pong, Thu Mar 20, 2008 4:22 PM.


#7 batari OFFLINE  

batari

    )66]U('=I;B$*

  • 6,236 posts
  • begin 644 contest

Posted Thu Mar 20, 2008 4:27 PM

View PostOpen Source Pong, on Thu Mar 20, 2008 5:15 PM, said:

By ticks i mean the amount of processing time the action takes. I believe the max ticks for the atari 2600 is 2700 ticks, anything higher then that and the game will jitter, shake and roll. Also SeaGtGruff answered my question about smartbranching not affecting game speed.

Sincerely,

Open Source Pong
Aha. As SeaGtGruff said, smartbranching will not use any extra clock cycles either. The longer code you see consists of an assembler directive to select one set of code or another. Not all of that code actually gets compiled into the final binary.

#8 SeaGtGruff OFFLINE  

SeaGtGruff

    River Patroller

  • 4,543 posts
  • Location:Georgia, USA

Posted Thu Mar 20, 2008 4:30 PM

View PostOpen Source Pong, on Thu Mar 20, 2008 6:15 PM, said:

By ticks i mean the amount of processing time the action takes. I believe the max ticks for the atari 2600 is 2700 ticks, anything higher then that and the game will jitter, shake and roll. Please let me know if turning on smartbranching will decrease my games speed due to the additional assembly code required by smartbranching.

Sincerely,

Open Source Pong
Smartbranching doesn't require additional assembly code. What it does is add *conditional* code that's used only by the assembler, while the assembler is converting the assembly op-codes into binary machine code. So the "additional" code never makes it into the final binary code, although you can see it in the assembly listing.

For example:

   IFCONST do_it_this_way
		 LDA #1
		 STA some_RAM_location
   ELSE
		 LDA #2
		 STA some_RAM_location
   ENDIF
If you use the variable name "do_it_this_way" somewhere in your program, then DASM will compile your code with the first version-- otherwise, it will compile your code with the second version. Either way, you only get one version, not both. The "extra code" doesn't make its way into the final binary, but it does affect what the final binary will have in it.

Michael




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users