[4.1] SubtractValue Only Runs Once?

TurtleRescueNES

Active member
This is for someone far smarter than I in ASM.

I am decreasing myMoney in a script, just like I used to in 4.0.6.
The odd thing I noticed in 4.1 is that it would only happen once per screen. If I triggered the event a second time, my Money counter would stay the same.

I tried modifying the HUD update script and even moved it around, but nothing worked. Then I thought to replace the SubtractValue macro with the AddMacro.

It worked! Now myMoney adds each and every time the trigger occurs. So what is wrong with SubtractValue that prevents the macro from activating more than once?
 

Kasumi

New member
In a quick check it seems like the SubtractValue value macro is missing an LDX arg3. Here's AddValue:
Code:
LDX arg0
	--:
		LDA arg1,x ;; the variable that you want to push
		STA value,x
		dex
		BPL --
	LDX arg3 ; sets the place to push.  
	LDA arg2
	JSR valueAddLoop ;value_add1 ;; will add what is in accumulator.
Here's SubtractValue:
Code:
LDX arg0
	--:
		LDA arg1,x ;; the variable that you want to push
		STA value,x
		dex
		BPL --
	LDA arg2
	STA temp
	JSR valueSubLoop ;; will add what is in accumulator.
So when the code jsrs to valueSubLoop, X is 255 instead of arg3.

Edit: Oh, hah, I had a really nebulous idea about why that might cause what you described, but it just hit me why this mistake can "almost work" instead of never work.

Inside valueSubLoop, an inx is done, and then a CPX. So that 255 becomes a 0, and then the loop runs like normal. (Assuming arg3 was zero.)
So basically, you subtract from 255 places into the number. If it borrows, it attempts to subtract one from 0 places into the number. So what you were attempting to do was subtract 1 to the zeroes place, it looks the same but only if the subtract from 255 places away borrowed!

So the reason it only works once, is because doing that only borrows once (from whatever that random RAM 255 places away was). (And it's possible that if that random RAM that gets affected never changes, it will actually work if you subtract one ten more times, which would force another borrow.)

Guess I'll email this bug report.
 
Top Bottom