16bit arithmetic

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
Sonny_Jim
Posts: 18
Joined: Sat Jan 13, 2018 7:45 am

16bit arithmetic

Post by Sonny_Jim »

So I've been working on a decompile of Super Spike V'Ball and found that it seems to use a lot of 16 bit numbers, I'm guessing this is due to it being a port of a 16bit arcade game. I'm kinda sorta making sense of it, but I'm getting a bit unglued by the following:

Code: Select all

calc_var, push XY regs to the stack
 07:E791: 8A       TXA
 07:E792: 48       PHA
 07:E793: 98       TYA
 07:E794: 48       PHA
clear result
>07:E795: A9 00    LDA #$00
 07:E797: 85 33    STA $33 tmp_var2result_lo = #$3B
 07:E799: 85 34    STA $34 tmp_var2result_hi = #$95
loop:
end sub if tmp_var0 = 0
 07:E79B: A5 30    LDA $30 tmp_var0_hi = #$00
 07:E79D: 05 2F    ORA $2F tmp_var0_lo = #$08
 07:E79F: F0 21    BEQ $E7C2 end_sub_carry_clear
tmp_var0 / 2
 07:E7A1: 46 30    LSR $30 tmp_var0_hi = #$00
 07:E7A3: 66 2F    ROR $2F tmp_var0_lo = #$08
 07:E7A5: 90 0F    BCC $E7B6 
 07:E7A7: 18       CLC
var2 += var1
 07:E7A8: A5 33    LDA $33 tmp_var2result_lo = #$3B
 07:E7AA: 65 31    ADC $31 tmp_var1_lo = #$10
 07:E7AC: 85 33    STA $33 tmp_var2result_lo = #$3B
 07:E7AE: A5 34    LDA $34 tmp_var2result_hi = #$95
 07:E7B0: 65 32    ADC $32 tmp_var1_hi = #$00
 07:E7B2: 85 34    STA $34 tmp_var2result_hi = #$95
 07:E7B4: B0 06    BCS $E7BC end_sub_carry_set
tmp_var1 * 2
 07:E7B6: 06 31    ASL $31 tmp_var1_lo = #$10
 07:E7B8: 26 32    ROL $32 tmp_var1_hi = #$00
 07:E7BA: 90 DF    BCC $E79B loop
end_sub_carry_set:
 07:E7BC: 68       PLA
 07:E7BD: A8       TAY
 07:E7BE: 68       PLA
 07:E7BF: AA       TAX
 07:E7C0: 38       SEC
 07:E7C1: 60       RTS-------------------------
end_sub_carry_clear:
 07:E7C2: 68       PLA
 07:E7C3: A8       TAY
 07:E7C4: 68       PLA
 07:E7C5: AA       TAX
 07:E7C6: 18       CLC
 07:E7C7: 60       RTS-------------------------
So I get most of it in sections but as a whole I'm not really sure what it does. It seemed to me that it's var2 = var1 * (var0 / 2), but then that doesn't seem right. So two questions:

1. I'm not sure what the point of "E7A5 BCC $E7B6" is. It's looking at the lowest bit of the lowest byte and branching if it's one? So something to do with odd/even numbers
2. Because of the two branches (there's another at E7BA) I'm not really able to understand the code as whole. Could someone please help me understand it?

Thanks.
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: 16bit arithmetic

Post by lidnariq »

Sonny_Jim
Posts: 18
Joined: Sat Jan 13, 2018 7:45 am

Re: 16bit arithmetic

Post by Sonny_Jim »

That's a great help, I've give it a read. Thanks
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: 16bit arithmetic

Post by tokumaru »

As for 8-bit games using lots of 16-bit numbers, that has nothing to do with being ports of 16-bit games. There are lots of things in 8-bit games that do not fit in 8 bits... Most games that do any kind of scrolling, for example, will need 16-bit coordinates to track the positions of game objects, due to levels being wider/taller than 256 pixels. 8-bit CPUs were designed with 16-bit (and beyond) arithmetic in mind, otherwise we wouldn't have a carry flag or instructions like ADC and SBC, and console manufacturers didn't pick these CPUs because they expected games to only use 8-bit math, they picked them because they were cheaper.
Sonny_Jim
Posts: 18
Joined: Sat Jan 13, 2018 7:45 am

Re: 16bit arithmetic

Post by Sonny_Jim »

Makes perfect sense, the only other game I've looked at (Bomber King) is very simplistic with it's calculations in comparison.
User avatar
Controllerhead
Posts: 314
Joined: Tue Nov 13, 2018 4:58 am
Location: $4016
Contact:

Re: 16bit arithmetic

Post by Controllerhead »

Using 16-bit coordinates, or anything more than 8-bit, can really smooth out motion, like say for a volleyball. Mario's X movement in SMB1 is an example, it uses 12 bits in the original game, so there are 16 "sub-pixel positions" between pixels.
Image
Post Reply