News:

Don't forget to visit the main site! There's lots of helpful docs, patches, and more!

Main Menu

[SM][ASM] First ASM Script Help

Started by xisdense, July 04, 2024, 12:10:17 AM

Previous topic - Next topic

xisdense

I'm an ASM noob, writing my first script, need some help. Basically, I'm trying to make the position of a beetom/nomi attached to Samus different based on whether she is crouched/standing/morphing.

I found the hex address for its Y offset, 143D83 = 04 00.  (143D82 = E9 04 00)

lorom

org $A8BD82 ; where subtract offset for attached beetom happens (143D82)
JSR $F9D0

org $A8F9D0 ; (1479D0)
LDY $0A1F                       ; samus' movement type
CPY #$0003 : BEQ OFFSETSPIN     ; spinning
CPY #$0004 : BEQ OFFSETMRPH     ; morphball
CPY #$0005 : BEQ OFFSETCRCH     ; crouching
CPY #$0008 : BEQ OFFSETMRPH     ; morphball falling
CPY #$000F : BEQ OFFSETCRCH     ; crouching/standing/morphing/unmorphing transition
CPY #$0011 : BEQ OFFSETMRPH     ; springball, on ground
CPY #$0012 : BEQ OFFSETMRPH     ; springball, in air
CPY #$0013 : BEQ OFFSETMRPH     ; springball, falling
BRA OFFSETNORM
OFFSETNORM:
SBC #$000F
RTS
OFFSETSPIN:
SBC #$0FFF
RTS
OFFSETMRPH:
SBC #$00FF
RTS
OFFSETCRCH:
SBC #$0CCC
RTS


None of the branches work, it always goes to OFFSETNORM. (The SBC values are all random currently, just trying to test for changes.) Any help would be greatly appreciated!

caauyjdp

Quote
$0A1F: Samus movement type
$0A20: Samus previous pose
y is in 16 bit mode, so your ldy grabs both those values, ruining the compare
you eiter need to adjust code to load in a, and and with 00ff; temporarily switch to 8bit xy(you'll ruin your value in x tho) or something else

neen

CPY can affect carry flag so you should SEC right before SBC. what's the BRA for?

xisdense

#3
Quote from: caauyjdp on July 04, 2024, 12:57:35 AM
y is in 16 bit mode, so your ldy grabs both those values, ruining the compare
Thanks once again, caauyjdp, for steering me in the right direction.

Here is my solution with the desired effect:
lorom

org $A8BD82 ; subtract offset for attached nomi (jumping right) (143D82)
JSR $F9D0
org $A8BD38 ; subtract offset for attached nomi (jumping left ) (143D38)
JSR $F9D0

org $A8F9D0 ; (1479D0)
TAY                         
SEP #$20                     ; A 8-bit
LDA $0A1F                    ; samus' movement type
CMP #$03 : BEQ OFFSETSPIN    ; spinning
CMP #$04 : BEQ OFFSETMRPH    ; morphball
CMP #$05 : BEQ OFFSETCRCH    ; crouching
CMP #$08 : BEQ OFFSETMRPH    ; morphball falling
CMP #$0F : BEQ OFFSETCRCH    ; crouching/standing/morphing/unmorphing transition
CMP #$11 : BEQ OFFSETMRPH    ; springball, on ground
CMP #$12 : BEQ OFFSETMRPH    ; springball, in air
CMP #$13 : BEQ OFFSETMRPH    ; springball, falling
BRA OFFSETNORM               ; all other types
OFFSETNORM:
REP #$20                     ; A 16-bit
TYA
SBC #$000F
RTS
OFFSETSPIN:
REP #$20
TYA
SBC #$0000
RTS
OFFSETMRPH:
REP #$20
TYA
SBC #$0000
RTS
OFFSETCRCH:
REP #$20
TYA
SBC #$000A
RTS


Not sure if there is an obvious more efficient/clean way to achieve this, but this works!

Quote from: neen on July 04, 2024, 01:11:26 AM
what's the BRA for?
All other possible Samus movement types.

neen

as currently written, the BRA does nothing. it will fall through if all other branches are not taken

xisdense

Quote from: neen on July 04, 2024, 02:01:54 AM
as currently written, the BRA does nothing. it will fall through if all other branches are not taken
Can you explain why please? What's the solution? It appears to be working fine, it sets the offset to that value when in other poses such as standing, running, etc. But it also works when I remove the BRA entirely, I'm assuming simply because there is no RTS and then it goes straight to OFFSETNORM. Are you saying I should just remove the BRA because it's redundant or is there something else I'm missing?

neen

it's not really important. but yes, if all the previous branches are not taken, you end up at OFFSETNORM by default, that's all. it doesn't hurt anything either by being there or not being there. (unless you cared about two bytes)

i think it's more important to rember to SEC before SBC tho. (just like you would CLC before ADC)

xisdense

#7
Quote from: neen on July 04, 2024, 02:34:04 AM
(unless you cared about two bytes)
I gotcha, you're right.

Quote from: neen on July 04, 2024, 02:34:04 AM
i think it's more important to rember to SEC before SBC tho. (just like you would CLC before ADC)
I wouldn't have known to do this, so thanks for letting me know.

$A8/BCF8 AE 54 0E    LDX $0E54  [$A8:0E54]
$A8/BCFB BD B0 0F    LDA $0FB0,x[$A8:0FB0]
$A8/BCFE D0 2E       BNE $2E    [$BD2E]
$A8/BD2E AD F6 0A    LDA $0AF6  [$A8:0AF6]
$A8/BD31 9D 7A 0F    STA $0F7A,x[$A8:0F7A]
$A8/BD34 AD FA 0A    LDA $0AFA  [$A8:0AFA]
$A8/BD37 38          SEC
$A8/BD38 E9 04 00    SBC #$0004
$A8/BD3B 9D 7E 0F    STA $0F7E,x[$A8:0F7E]
$A8/BD3E 20 8C BD    JSR $BD8C  [$A8:BD8C]

$A8/BD41 60          RTS

There is a SEC right before the original SBC I am hijacking. But you're saying I should do it directly before each new branched SBC because of the CMPs.

neen

yes, the CMP can affect the carry flag so it's not guaranteed to still be set by the time you SBC. it's worth having an opcode reference bookmarked/handy much of the time because: which instructions affect which PSR (processor status register) bits is not always obvious/intuitive.