Working with negative numbers?

Discussion in 'Discussion and Q&A Archive' started by Pacca, Oct 24, 2014.

Thread Status:
Not open for further replies.
  1. Pacca

    Pacca Having an online identity crisis since 2019 Member

    Joined:
    Jul 5, 2014
    Messages:
    1,175
    Location:
    Limbo
    The genesis seems to use a very odd system to deal with negative numbers, and I can't wrap my head around it. Sometimes it works beautifully, and sometimes it feels broken.

    I'm trying to make code that mess with the players y velocity. when I set the value to certain levels it seems to A. take no noticble effect at all, or b. send the player sky rocketing into the sky, at velocity levels way to low to cause such oddities.

            move.w    300,d0            ; set d0 to 300
            neg.w    d0            ; negate d0 to fix odd issues
            move.w    d0,$12(a0)            ; set y velocity

    this is what I'm currently trying to use, as I again have no idea how this system works...
     
  2. MainMemory

    MainMemory Well-Known Member Member

    Joined:
    Mar 29, 2011
    Messages:
    922
    Well your first problem is that you're moving the word at address 300 ($12C, in the header) to d0, rather than the immediate value #300. The second problem is that speed values are usually handled as hex, because the high byte of the word is the number of pixels per frame, and the low byte is a fractional value (8.8 fixed point format). Additionally, it helps if you understand that the coordinate system in Sonic games (which can be seen when debug is enabled) has the upper left corner as 0000 0000, with values increasing as you go right and down, so adding to your position by setting a positive y_vel moves Sonic downward, and a negative y_vel moves him upwards.

    So if you were trying to move Sonic upwards at 3 pixels per frame (of course gravity will have an effect as well), you could do so by using:

    move.w #-$300,$12(a0)
     
  3. MarkeyJester

    MarkeyJester ♡ ! Member

    Joined:
    Jun 27, 2009
    Messages:
    2,867
    First and most importantly, the move of 300 to d0 is missing a '#' symbol, therefore, it is not an immediate value but rather an address. So, what's being loaded into d0 is whatever is in the ROM at offset 00012C - 00012D.

    Regarding negative numbers, I want you to read this page and then read the Next Part (link at the bottom of that page), this will give you an understanding of how the 68k (or any CPU for that matter) interprets and handles positive/negative polarities.

    Edit: MainMemory beat me to it.
     
    Last edited by a moderator: Oct 24, 2014
  4. Pacca

    Pacca Having an online identity crisis since 2019 Member

    Joined:
    Jul 5, 2014
    Messages:
    1,175
    Location:
    Limbo
    Odd, this code was taken almost directly from one of selbis more polished guides, I'm surprised that it would appear to be so flawed... Thanks for the help.
     
  5. Irixion

    Irixion Well-Known Member Member

    Joined:
    Aug 11, 2007
    Messages:
    670
    Location:
    Ontario, Canada
    There's your problem. In any case you always have to remember address vs value. I'm surprised it even worked at all. Negative offest? Fun stuff!
     
  6. Selbi

    Selbi The Euphonic Mess Member

    Joined:
    Jul 20, 2008
    Messages:
    2,429
    Location:
    Northern Germany
    May I ask which one?
     
    Last edited by a moderator: Oct 24, 2014
  7. Pacca

    Pacca Having an online identity crisis since 2019 Member

    Joined:
    Jul 5, 2014
    Messages:
    1,175
    Location:
    Limbo
    The jumpdash guide. Looking back at it, I think I might have fucked up the syntax somewhere along the lines...
     
Thread Status:
Not open for further replies.