Assembly; How to check if score / cash is greater or equal to X?

timefor

New member
This is probably a simple one but for the life of me I can't find an example of this.

I'm trying to build a store type system and I need to check if the player's cash (myCash) which supports upto $999, is greater than or equal to XXX for the item. I can subtract the value easy enough. I've found examples when the value is a single digit. But how do I check higher value variables so I don't end up subtracting and looping the value around when it goes negative?

IE, If player has $10 or more, and the item costs $10 then, continue with transaction, otherwise branch and do something else.

I know this code isn't right, it's just what I've been poking around at for an hour now. Could someone point me in a direction please?

Code:
LDA myCash
CMP #$0A
BNE +noCash

SubtractValue #$03, myCash, #$01, #$01 ;;subtract $10
UpdateHudElement #$05 ;;myCash var

+noCash:
 

dale_coop

Moderator
Staff member
Depends of how many bytes your variable is (= how many digits your variable in the hud)
Because you need to check individually each byte.

Let's say your myCash shows "345" in your HUD and your player wants to buy a item that would cost "123"

So, you will have to first compare the "3" against the "1"... then if greater or equal, the "4" against the "2"... and lastly, if greater or equal, the "5" against the "3"... in order to know if you have enough cash.

The code would look like:
Code:
;; the item price is "123":

LDA myCash+2    ;; the hundreds digit of myCash
CMP #1   ;; the "1" in 123
BEQ +keepChecking
BCS +definitelyEnoughMoney
    JMP +notEnoughCash
+keepChecking:

LDA myCash+1    ;; the tens digit of myCash
CMP #2   ;; the "2" in 123
BEQ +keepChecking
BCS +definitelyEnoughMoney
    JMP +notEnoughCash
+keepChecking:

LDA myCash    ;; the units digit of myCash
CMP #3   ;; the "2" in 123
BCS +definitelyEnoughMoney
    JMP +notEnoughCash

+definitelyEnoughMoney:
  ;; enough money, now can do what we need to buy that item
 
  ;; subtracting 123 :
  SubtractValue #$03, myCash, #$01, #$02   ;;subtract #100
  SubtractValue #$03, myCash, #$02, #$01   ;;subtract #20
  SubtractValue #$03, myCash, #$03, #$00.  ;;subtract #3

  ;; now update the HUD :
  UpdateHudElement #$05 ;;myCash var


+notEnoughCash:


For a "10" item, it would be something like:
Code:
;; the item price is "010":

LDA myCash+2    ;; the hundreds digit of myCash
CMP #0   ;; the "0" in "010"
BEQ +keepChecking
BCS +definitelyEnoughMoney
    JMP +notEnoughCash
+keepChecking:

LDA myCash+1    ;; the tens digit of myCash
CMP #1   ;; the "1" in "010"
BEQ +keepChecking
BCS +definitelyEnoughMoney
    JMP +notEnoughCash
+keepChecking:

LDA myCash    ;; the units digit of myCash
CMP #0   ;; the "0" in "010"
BCS +definitelyEnoughMoney
    JMP +notEnoughCash

+definitelyEnoughMoney:
  ;; enough money, now can do what we need to buy that item
 
  ;; subtracting 10 :
  ;; here I just subtract the tenth digit (no need to subtract the hundreds or the units)
  SubtractValue #$03, myCash, #$01, #$01   ;;subtract #10

  ;; now update the HUD :
  UpdateHudElement #$05 ;;myCash var

+notEnoughCash:


Small note:
in fact, every "CMP #0" lines are not needed (you could comment out those lines) because comparing a variable to 0 is already done when then you LDA the var. so you could directly write:
LDA myCash+2
BEQ +keepChecking
(without the "CMP #0" line)

Small note 2:
I haven't tested the code, I just wanted to share the idea (checking every byte / digit of the hud variable)... you'll have to figure it yourself for the case of your project.
 
Last edited:

timefor

New member
Thank you @dale_coop ! This was incredibly helpful and I've used the same logic for other places where have I have multi digit variables in the game too. Really appreciate your example code
 
Top Bottom