Language…
8 users online: CrazyPencilDawg, DanMario24YT, howardadam1126, iamtheratio, kurtistrydiz, Rhubarb44230, Saela, TheOrangeToad - Guests: 274 - Bots: 312
Users: 64,795 (2,378 active)
Latest user: mathew

ASM misery

Strangely enough, I had posted this block code a while ago when it worked, but now for some reason .. it just crashes the ROM. Here it is:

PHA
LDA $7E1420
CMP #$04
BEQ SetTeleport
BNE Fail

SetTeleport
LDA #$05
STA $7E0071
LDA $7E1420
CMP #$04
BEQ Fail

Fail
PLA
RTS

So yeah, pretty straightforward - it loads the RAM address for the current amount of Yoshi coins, and then branches if it's equal or not equal - but whether or not you have four Yoshi coins, it will crash.

Any tips for why this seemed to mess up?
I'm not a expert when it comes to ASM, so I can't tell what you did wrong. I don't exactly know what PHA & PLA do, but anyway. I'm looking at your code and I guess you want a block that teleport Mario if he collected 4 yoshi coins. I tried to code it myself:


LDA $1420
CMP #$04
BEQ SetTeleport
RTL

SetTeleport

LDA #$05
STA $71
RTL

I tested the code as custom sprite (too lazy to use blocktool)and it works. It should works as a custom block too.
I have a couple of comments on your code. First of all, good job so far. It looks like you took care to ensure your PHA/PLAs were balanced, which is where a lot of beginners make mistakes.

1. If you are dealing with the first bank of RAM ($7E0000-$7E7FFF) you can omit the "7E". If you are dealing with the first page of RAM ($7E0000-$7E00FF) you can omit the leading "7E00"

Examples:

LDA $7E1420 (AF 20 14 7E) can be reduced to
LDA $1420 (AD 20 14)

STA $7E0071 (8F 71 00 7E) can become
STA $0071 (8D 71 00) or better yet
STA $71 (85 71)

2. The marked lines can be removed. "BEQ SetTeleport" is not needed, because if the value is #$04 the branch to "Fail" won't be taken and "SetTeleport" is the next section that will execute anyway. The last 3 lines I marked don't seem to do anything, what were you trying to accomplish with them?

Code
     PHA
     LDA $7E1420
     CMP #$04
     BEQ SetTeleport <---
     BNE Fail

SetTeleport
     LDA #$05
     STA $7E0071
     LDA $7E1420 <---
     CMP #$04 <---
     BEQ Fail <---

Fail 
     PLA
     RTS


3. Overall you seem to be getting the hang of it. This code should work without any problems. It's usually a good idea to say how you are inserting the code (block tool, sprite tool, manually, etc.), whether or not you're using an assembler (and which one), and the raw hex equivalent (if your code is short enough).
Originally posted by mikeyk
1. If you are dealing with the first bank of RAM ($7E0000-$7E7FFF)

actually, it's only work with $7E0000-$7E1FFF, since the rest is occuped by hardware register and enhancement chips memmory/register
I never knew you could omit the 7E - thanks for the info on that :)

Oh, I used xkas to convert my code into a file, then loaded it up in hex, then inserted it through Block Tool. But it looks like you already figured it out, so yeah.

As for the three marked lines under the first branch .. I was trying to recopy the code so after you are teleported, it branches to "Fail" and ends the code. I don't even really know why I did that, it was the only way I could think of for ending it, heh. I never knew I could get rid of BEQ SetTeleport either - I figured if that wasn't there, it would just pass over the BNE and go straight to Fail.

Thanks for all the information everyone.

EDIT: Just tried this out. I altered the code a bit:

PHA
LDA $1420
CMP #$04
BNE Fail
LDA #$05
STA $71
PLA
RTS

Fail
PLA
RTS

When you have gathered 4, it works perfectly. However, if you touch the block before you gather 4 coins, the ROM crashes. I have checked and tried a bunch of different things, but I can't seem to find an error - the PLAs and PHAs are right. Is there anything that immediately sticks out that looks like it could be causing the problem?

Here is the hex:

48 AD 20 14 C9 04 D0 F8 A9 05 85 71 68 60 68
Originally posted by S.N.N.
As for the three marked lines under the first branch .. I was trying to recopy the code so after you are teleported, it branches to "Fail" and ends the code. I don't even really know why I did that, it was the only way I could think of for ending it, heh. I never knew I could get rid of BEQ SetTeleport either - I figured if that wasn't there, it would just pass over the BNE and go straight to Fail.

Thanks for all the information everyone.

EDIT: Just tried this out. I altered the code a bit:

PHA
LDA $1420
CMP #$04
BNE Fail
LDA #$05
STA $71
PLA
RTS

Fail
PLA
RTS


Your code could still be simplified. This is all you should need:

Code
     PHA
     LDA $1420
     CMP #$04
     BNE Fail
     LDA #$05
     STA $71

Fail: 
     PLA
     RTS


Notice I removed the code that was duplicated at the end. Unless there is a branch or a jump, control flows down to the next instruction. After "STA $71", the next instruction will execute which is "PLA".

Originally posted by S.N.N.

When you have gathered 4, it works perfectly. However, if you touch the block before you gather 4 coins, the ROM crashes. I have checked and tried a bunch of different things, but I can't seem to find an error - the PLAs and PHAs are right. Is there anything that immediately sticks out that looks like it could be causing the problem?

Here is the hex:

48 AD 20 14 C9 04 D0 F8 A9 05 85 71 68 60 68


There are two problems looking at the hex. It should end with 60, but I assume that's a typo. The real issue is that the branch instruction is incorrect. It looks like the assembler couldn't find the "fail" label. I think xkas requires labels to end with a colon, so make sure you include on like i did earlier in this post.

The hex for your code should be:
48 AD 20 14 C9 04 D0 06 A9 05 85 71 68 60 68 60
My version would be:
48 AD 20 14 C9 04 D0 04 A9 05 85 71 68 60
Sure enough, it was the colon. I would have never thought of that - I would have just kept playing with the code until I eventually shot myself in frustration.

Thanks again.
Alright, I tried something more difficult. I'm trying to make a block that will act like a regular coin if the ON/OFF switch is ON, but a blue coin if it's set to OFF. I wasn't quite sure how this worked, or which RAM Address the Map16 data was stored at, so I took my best shot at this code:

PHA
LDA $14AF
CMP #$01
BEQ BlockChange
LDA #$2C
STA $03
PLA
RTS

BlockChange:
LDA #$2B
STA $03
PLA
RTS

I'm probably completely off, but it's mainly a test to improve my skill. It doesn't do anything, so I'm guessing I poked fun at the wrong address

Here is the hex:

48 AD AF 14 C9 01 F0 06 A9 2C 85 03 68 60 A9 2B 85 03 68 60

Any information on how to work this would be great, or some advice. Thanks
HEY,LISTEN



Shouldn't there be an 00 before the 03? Unless you can skip that byte without screw-up... (meaning you'd also have to change the 85's to 8D's as well.)



Originally posted by Sparx
Shouldn't there be an 00 before the 03? Unless you can skip that byte without screw-up... (meaning you'd also have to change the 85's to 8D's as well.)

he use direct adressing mode wich only use one byte as an operand, so please don't talk when you don't know what are you talking about