How to use ASM6 conditions (IF)?

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

Post Reply
User avatar
MartsINY
Posts: 66
Joined: Sun Jun 11, 2017 5:39 pm

How to use ASM6 conditions (IF)?

Post by MartsINY »

Simple,

IF are simple to use (from README):

IF j>0

However I have something more complex:

IF (OBJ_Dir_Bits,X AND #$01 == #$00)

How should I write this
User avatar
nesrocks
Posts: 563
Joined: Thu Aug 13, 2015 4:40 pm
Location: Rio de Janeiro - Brazil
Contact:

Re: How to use ASM6 conditions (IF)?

Post by nesrocks »

How about

Code: Select all

LDA OBJ_Dir_Bits,X
AND #$01
BEQ resultiszerolabel
notzero:
...
resultiszerolabel:
...
edit: actually your code didn't make much sense, if 1 == 0? Why not give a more concrete example?
If you just want a generic code to check if two conditions are true I guess you don't have much choice, do one and then do the other.
https://twitter.com/bitinkstudios <- Follow me on twitter! Thanks!
https://www.patreon.com/bitinkstudios <- Support me on Patreon!
User avatar
MartsINY
Posts: 66
Joined: Sun Jun 11, 2017 5:39 pm

Re: How to use ASM6 conditions (IF)?

Post by MartsINY »

nesrocks wrote:How about

Code: Select all

LDA OBJ_Dir_Bits,X
AND #$01
BEQ resultiszerolabel
notzero:
...
resultiszerolabel:
...
edit: actually your code didn't make much sense, if 1 == 0? Why not give a more concrete example?
If you just want a generic code to check if two conditions are true I guess you don't have much choice, do one and then do the other.
Thanks for your answer.
However I know full well how to do the condition with BEQ and such. However, ASM6 allow the use of IF, which is easier to read. I would prefer to use them.

About the condition I do not understand why it doesn't make sense? I want to ensure BIT 0 if to 0. It's a flag. So let's say my variable has the value FF
then AND #$01 return #$01. I then want to branch according to the value.

On the opposite, if its value is #$FE, AND #$01 will return #$00.
User avatar
nesrocks
Posts: 563
Joined: Thu Aug 13, 2015 4:40 pm
Location: Rio de Janeiro - Brazil
Contact:

Re: How to use ASM6 conditions (IF)?

Post by nesrocks »

Then shouldn't it be...

Code: Select all

IF (OBJ_Dir_Bits,X AND #$01) == #$00
Better yet:

Code: Select all

IF !(OBJ_Dir_Bits,X AND #$01)
I'm not sure about the order in which asm6 processes the operators, but it just may be that it would do the == comparator before everything else.
https://twitter.com/bitinkstudios <- Follow me on twitter! Thanks!
https://www.patreon.com/bitinkstudios <- Support me on Patreon!
User avatar
MartsINY
Posts: 66
Joined: Sun Jun 11, 2017 5:39 pm

Re: How to use ASM6 conditions (IF)?

Post by MartsINY »

nesrocks wrote:Then shouldn't it be...

Code: Select all

IF (OBJ_Dir_Bits,X AND #$01) == #$00
Better yet:

Code: Select all

IF !(OBJ_Dir_Bits,X AND #$01)
I'm not sure about the order in which asm6 processes the operators, but it just may be that it would do the == comparator before everything else.

Thanks, you are right about the way I wrote it. However it seems that syntax is not accepted by ASM6, and the Readme is far to be detailed about this...
User avatar
nesrocks
Posts: 563
Joined: Thu Aug 13, 2015 4:40 pm
Location: Rio de Janeiro - Brazil
Contact:

Re: How to use ASM6 conditions (IF)?

Post by nesrocks »

Maybe it's the addressing mode. Can you see if this works?

IF !(OBJ_Dir_Bits AND #$01)

If this works I don't know, I guess you're gonna have to store it in A and then do the IF using A instead.
https://twitter.com/bitinkstudios <- Follow me on twitter! Thanks!
https://www.patreon.com/bitinkstudios <- Support me on Patreon!
User avatar
MartsINY
Posts: 66
Joined: Sun Jun 11, 2017 5:39 pm

Re: How to use ASM6 conditions (IF)?

Post by MartsINY »

nesrocks wrote:Maybe it's the addressing mode. Can you see if this works?

IF !(OBJ_Dir_Bits AND #$01)

If this works I don't know, I guess you're gonna have to store it in A and then do the IF using A instead.
unfortunately this doesn't work either... incorrect expression
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How to use ASM6 conditions (IF)?

Post by tokumaru »

Assembler conditionals are for assembly-time decisions, not run-time decisions. You can't check the values of game variables or do any game logic with them.
User avatar
MartsINY
Posts: 66
Joined: Sun Jun 11, 2017 5:39 pm

Re: How to use ASM6 conditions (IF)?

Post by MartsINY »

tokumaru wrote:Assembler conditionals are for assembly-time decisions, not run-time decisions. You can't check the values of game variables or do any game logic with them.
Ah so it's not a way to convert a IF to conditions with BEQ, etc?
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How to use ASM6 conditions (IF)?

Post by tokumaru »

Nope. It's for testing conditions that affect how the program is assembled. You can test variable addresses, the value of the PC, symbols used for settings, things like that.

You can for example define a PAL/NTSC symbol that you check whenever the program needs to assemble something different for each of these versions, so you can generate either a PAL or NTSC ROM just by changing the value of this symbol.

What you want to do (make conditional branches more readable) can kinda be accomplished with macros. ASM6's macro system isn't the most advanced, but you should be able to do something like this:

Code: Select all

  lda OBJ_Dir_Bits,X
  and #$01
  IFEQ
    ;"if equal" code here
  ELSE
    ;"if not equal" code here
  ENDIF
Using these macros:

Code: Select all

.macro IFEQ
  bne +Else
.endm

.macro ELSE
  jmp +End
  +Else:
.endm

.macro ENDIF
  +End:
.endm
There are probably better "IF" implementations out there, but I'm not sure how far you can get with ASM6.
User avatar
MartsINY
Posts: 66
Joined: Sun Jun 11, 2017 5:39 pm

Re: How to use ASM6 conditions (IF)?

Post by MartsINY »

tokumaru wrote:Nope. It's for testing conditions that affect how the program is assembled. You can test variable addresses, the value of the PC, symbols used for settings, things like that.

You can for example define a PAL/NTSC symbol that you check whenever the program needs to assemble something different for each of these versions, so you can generate either a PAL or NTSC ROM just by changing the value of this symbol.

What you want to do (make conditional branches more readable) can kinda be accomplished with macros. ASM6's macro system isn't the most advanced, but you should be able to do something like this:

Code: Select all

  lda OBJ_Dir_Bits,X
  and #$01
  IFEQ
    ;"if equal" code here
  ELSE
    ;"if not equal" code here
  ENDIF
Using these macros:

Code: Select all

.macro IFEQ
  bne +Else
.endm

.macro ELSE
  jmp +End
  +Else:
.endm

.macro ENDIF
  +End:
.endm
There are probably better "IF" implementations out there, but I'm not sure how far you can get with ASM6.

thanks a lot for your answer!! Since you mentionned it's not the best for macro, is there a better program than ASM6? This is the first I used, so far I like it but if there are better I'd be interested to learn them.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How to use ASM6 conditions (IF)?

Post by tokumaru »

CA65's macro system if extremely powerful. People have implemented entirely new instruction sets using CA65 macros. IIRC, tepples reimplemented 6502 from scratch (as a proof of concept) and blargg did GBZ80, on a 6502 assembler... that's impressive!

ASM6 is great for getting a quick ROM ready, and is very easy to pick up and use, but it doesn't offer much to help manage larger projects. Not that you can't use it for larger projects, but you'll have to handle banking, pointers, memory allocation and stuff more actively, instead of using built-in functionality or your own macros to manage some of this stuff automatically. That being said, CA65 has a much steeper learning curve.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How to use ASM6 conditions (IF)?

Post by tokumaru »

You might want to check this thread out: viewtopic.php?f=2&t=9272
User avatar
MartsINY
Posts: 66
Joined: Sun Jun 11, 2017 5:39 pm

Re: How to use ASM6 conditions (IF)?

Post by MartsINY »

thanks, I will check it!!
Post Reply