JSR Returning to wrong place in code - Printable Version +- FF6 Hacking (https://www.ff6hacking.com/forums) +-- Forum: Discussion Forums (https://www.ff6hacking.com/forums/forum-5.html) +--- Forum: Magitek Research Facility (https://www.ff6hacking.com/forums/forum-9.html) +--- Thread: JSR Returning to wrong place in code (/thread-3370.html) Pages:
1
2
|
JSR Returning to wrong place in code - GrayShadows - 11-30-2016 I live! And have resurrected my old project... and have encountered a problem. For some reason, my JSRs are returning to a different place in the code than they started. Has anyone run across this? The places it's returning to aren't even JSRs, just random parts in the code. I'm not even sure where to start fixing this. RE: JSR Returning to wrong place in code - madsiur - 11-30-2016 It's usually a problem with the stack. This happens when you push inside a function but don't pull before the RTS/RTL. Same thing if you push then do a JSR then pull or another thing, pushing twice and only pulling once. But this is really common mistake for me and for a lot of ASM newbies, maybe you're talking about something else... Edit: Or are you saying your JSR goes to another offset than the opcode params??? RE: JSR Returning to wrong place in code - GrayShadows - 11-30-2016 No, you're right that I was talking about the RTS. I wondered about pushing/pulling being an issue, but I'm not seeing any mismatches in the code anywhere. I'll post the code snippet tomorrow, when I can get my laptop to some wifi. XD I'm sure I'm just missing something obvious because I've been staring at the code too long. RE: JSR Returning to wrong place in code - B-Run - 12-01-2016 Man, I did a whole stream on this subject. But the I had to stop/start the stream a few times changing resolution cause my internet was dumb... otherwise I'd have it on youtube. Basically, when you JSR it pushes your current location to the stack so that the RTS knows where to come back to. RTS takes the last 2 bytes on the stack (pushed from your JSR) as a return address then adds 1 to get to the next byte of code. This is why as a general rule, you don't f*** with the stack. however, if you know where you are in the stack its all usable. Keep an eye out for maybe a push or pull inside a conditional that may be skipped due to a branch. That's the place I most often screw up the stack. RE: JSR Returning to wrong place in code - GrayShadows - 12-01-2016 Okay, agh, I haven't done much with the stack in previous hacks (and also I haven't been doing much assembly for, uh... since whenever I was last around, at the very latest, so I'm out of practice). I did find a PLA nested inside a conditional that was being skipped! So I swapped that out with a LDA $01,S and moved the PLA to the end of the code patch, and... now it's RTSing to a DIFFERENT wrong position. I can't win for losing. Here's my code -- the start of the function is from vanilla, and my modified/new code starts at C2/5587. Code: C2/5551: 08 PHP Code: !Terra = #$00 ; character IDs; I'm moving Celes and Relm up from their original positions It's jumping correctly to NotSummon (actually 'Lore' ability, but I'm co-opting Lore for summoning, rather than using the existing Esper function, in my hack) and then back; it's the RTS at the end of CharNextSpell that's causing problems. From what B-Run is saying, I suspect I'm missing something SOMEWHERE with the stack, but I'm not experienced enough with it to tell where. XD Am I misunderstanding how to use LDA $01,S? From the way it's used in the base code, it looks like it was reading the last value of A that had been pushed to the stack, without actually pulling it off, but I'm definitely open to being wrong about that. RE: JSR Returning to wrong place in code - madsiur - 12-01-2016 From a first quick look you're doing a PHA for "save spell ID". The NotSummon branch is ok (one PHX, one PLX). However the IsSummon has zero push two pull (PLA, PLP) at the end and NotSummon eventually end there too to at CharNextSpell. What you do in fact is restoring the flags according to the stack value preceding your initial PHA. Is the PLP really neccesary? If you really need to restore the flags or clear them do something like the following: This clear all flags Code: LDA #$00 Edit: Adding a PHP at beginning of your custom code should work too. RE: JSR Returning to wrong place in code - GrayShadows - 12-01-2016 The PLP is actually in reference to a PHP at the very start of the original codeblock (C2/5551), before the part I started changing, and the PLA is from a previously PHA under BuildCharLists. Some of this is derived from vanilla code -- they used PHA/LDA $01,S to keep track of the spell ID while leaving it on the stack, and then using PLA instead for the last time it was being used. Initially, I had a PLA under IsSummon where it's now a LDA $01,S, right before that JSR $5723, but realised that it was being skipped if it was branching after NotSummon. I'm clearly doing SOMETHING wrong with the stack, but all of my PH*/PL* are paired properly, as far as I can tell. One thing I need to look at, actually, is LDA $01,S in the middle of NotSummon, although it's (probably) not causing the immediate problem -- would that be pulling the right value from the stack? It seems to be, from watching the accumulator while the stepping the code, but if the JSR pushes the address to the stack, would that interfere? XD Clearly I have much to learn about how the stack works. RE: JSR Returning to wrong place in code - madsiur - 12-01-2016 LDA $01,S in AddToList pulls the first byte from the stack ($0001FF). Since we come from JSR NotSummon I think the spell index is shifted in $0001FD and $0001FE-$0001FF is occupied by the address. I think the correct thing to do here would be LDA $03,S but I'm not 100% sure as I never use this type of LDA. However even if you load from the stack a LDA does not alter the stack like a push/pull does. I'm not sure we are solving the root problem here... :/ RE: JSR Returning to wrong place in code - GrayShadows - 12-01-2016 Yeah, it's definitely a side issue, but it does indicate (along with a little further reading) that I had some fundamental misunderstandings about how the stack works. I'll go over my code again with that in mind and see if I can track down the problem now. ETA: I figured it out! I need to pull both inside IsSummon AND right after I return from NotSummon, before I branch, rather than at CharNextSpell. I realised that the address it was RTSing to was actually the IDs for the two Lore/Summon spells that were learned at the point I was testing this, because it wasn't pulling the value I'd pushed from A during that loop. Things aren't actually working yet, but I've fixed the immediate problem. I'm at the actual menu-building code part now and I've barely started to scratch the surface of that. WISH ME LUCK. RE: JSR Returning to wrong place in code - madsiur - 12-03-2016 (12-01-2016, 03:06 PM)GrayShadows Wrote: Things aren't actually working yet, but I've fixed the immediate problem. I'm at the actual menu-building code part now and I've barely started to scratch the surface of that. WISH ME LUCK. I'm glad you fixed this! I've never done any serious battle menu coding but B-Run has a good understanding of this I think with his work on CoV. |