D-Bug & Automation Forum
D-Bug & Automation Forum >> Coding >> Sauron's homework! (assignment #1 - bouncy)
http://d-bug.mooo.com/dbugforums/cgi-bin/yabb2/YaBB.pl?num=1304238634

Message started by ggn on 01.05.11 at 08:30:33

Title: Re: Sauron's homework! (assignment #1 - bouncy)
Post by ggn on 07.05.11 at 16:49:35
Well, it took long for someone to reply here :).

Of course it's all very good and fine. There is one small potential bug there, that is if the ball touches 2 borders at once it'll fail to flip both directions. In any case, I'll just add some variations to this to make some other points.

First in line is, how to make it run faster. Now, of course it's a bit moot to speed up such a small routine, but it's mostly to give some ideas.

So here's a faster version:

[code]ball_x            dc.l      160            ; ball x position
ball_y            dc.l      120            ; ball y position
x_direction      dc.l      1            ; vertical direction of ball
y_direction      dc.l      1            ; horizontal direction of ball
topborder      dc.l      0            ; sets top border
bottomborder      dc.l      240            ; sets bottom border
leftborder      dc.l      0            ; sets left border
rightborder      dc.l      320            ; sets right border


start:

     movem.l ball_x(pc),d0-d3      ;load ball_x in d0, ball_y in d1, x_direction in d2 and y_direction in d3

     sub.l      d2,d0            ; moves ball left by one pixel
     add.l      d3,d1            ; moves ball down by one pixel


moveball:

     move.l      d0,ball_x            ; updates ball_x variable
     move.l      d1,ball_y            ; updates ball_y variable

     cmp.l      leftborder,d0      ; compares left border to x position
     bne.s      .check_right      ; branches if not equal to left border
     moveq      #1,d2            ; then starts moving the ball right
     bra.s      .check_top      ; now go check for top/bottom

.check_right:
     cmp.l      rightborder,d0      ; compares right border to x position
     bne.s      .check_top      ; branches if not equal to right border
     moveq      #-1,d2            ; then starts moving the ball left

.check_top:
     cmp.l      topborder,d1      ; compares top border to y position
     bne.s      .check_bottom      ; branches if not equal to top border
     moveq      #1,d3            ; then moves the ball down
     bra.s      .check_done

.check_bottom:
     cmp.l      bottomborder,d1      ; compares bottom border to y position
     beq.s      .move_up      ; branches if equal to bottom border
     moveq      #-1,d3            ; then moves the ball up

.check_done:
     movem.l d0-d3,ball_x
     rts                  ;all done, thank you, drive thru![/code]

A bit different, isn't it? :) Now, let me comment my changes a bit.

  • I changed all move.l statements to moveq - it is one of the fastest cpu instructions on the 68000. It has a small drawback though, that being that you can only use it if you want to load values from -128 to 127, so use with care!
  • Also obvious, I changed all branches to their short sizes (for example beq.s instead of beq.w). Again, these work only if the branched code is less or equal to 128 bytes from the instruction executed. Usually I make all my branches short and let the assembler whine if any passes the limit :)
  • Generally, I tend to use registers as much as possible, as it's much faster than accessing memory all the time. Since we will be using x_direction and y_direction more than once in the code, I load them too and save them after all processing is finished. On the other hand, the border values are only used once, so I let them be.
  • The 68000 has a special instruction for loading multiple registers to and from memory, called movem. You need to care that what you load is sequentially in memory. Fortunately Sauron had all lined up for a movem:)
  • Finally, I restructured the code a bit, so it'll be a bit tighter, and also a bit easier to check, as you have all the relevant code close by and your eyes don't have to search for branches all the time. Dunno, I just think it's nice, it's not a necessity :)

D-Bug & Automation Forum » Powered by YaBB 2.6.0!
YaBB Forum Software © 2000-2024. All Rights Reserved.