'else' control structure in assembly.

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
pops
Posts: 91
Joined: Sun Apr 04, 2010 4:28 pm

'else' control structure in assembly.

Post by pops »

What is the most efficient way to write the 'else' program structure in assembly?

The best I've come up with so far is:

Code: Select all

lda #$10    ; if ScrollY <= $10
cmp ScrollY
bcs smallScroll
    ; do code for 'if' = true
jmp endif
smallScroll:
    ; do code for 'if' = false
endif:
User avatar
Movax12
Posts: 529
Joined: Sun Jan 02, 2011 11:50 am

Re: 'else' control structure in assembly.

Post by Movax12 »

That is basically it. Remember you can use a branch instruction rather than a JMP if you know a flag will always be in a known state. Also, if you change the flow of code in your true block, you don't need to have the else. Example:

Code: Select all

lda #$10    ; if ScrollY <= $10
cmp ScrollY
bcs smallScroll
    ; do code for 'if' = true
    rts      ; leave this subroutine
jmp endif  ; not needed 
smallScroll:
    ; do code for 'if' = false
endif:

; rewrite:

lda #$10    ; if ScrollY <= $10
cmp ScrollY
bcs smallScroll
    ; do code for 'if' = true
    rts      ; leave this subroutine
smallScroll:

; else, continue:
Also your code may be wrong, that code will effectively be the same as: If #$10 < Scrolly , or If Scrolly > #$10
doppelganger
Posts: 183
Joined: Tue Apr 05, 2005 7:30 pm

Re: 'else' control structure in assembly.

Post by doppelganger »

Actually it would be if #$10 >= ScrollY. Remember the carry flag is set after a subtraction unless the second number (which in this case is ScrollY) is greater, then it will be clear for borrow.
Be whatever the situation demands.
User avatar
Movax12
Posts: 529
Joined: Sun Jan 02, 2011 11:50 am

Re: Please Help me Understand This Schematic Portion

Post by Movax12 »

I was basing that last statement on OP's comments, especially the 'if'=true part.
pops
Posts: 91
Joined: Sun Apr 04, 2010 4:28 pm

Re: 'else' control structure in assembly.

Post by pops »

doppelganger wrote:Actually it would be if #$10 >= ScrollY. Remember the carry flag is set after a subtraction unless the second number (which in this case is ScrollY) is greater, then it will be clear for borrow.
My bad. I'm still getting used to assembly and the 6502.
User avatar
blargg
Posts: 3717
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Re: 'else' control structure in assembly.

Post by blargg »

Movax12 wrote:Also, if you change the flow of code in your true block, you don't need to have the else.
Your example has the else, it's just that the if and else blocks both end in an rts.

Often the else can be eliminated by doing the work of it before the if, then replacing it if the condition is true. e.g.

Code: Select all

        ldx #10 ; do this work all the time
        lda pos
        cmp #5
        bne skip
        ldx #20 ; overwrite
skip:
User avatar
Movax12
Posts: 529
Joined: Sun Jan 02, 2011 11:50 am

Re: Please Help me Understand This Schematic Portion

Post by Movax12 »

Yes. I find that method interesting. One could code both ldx instructions inside an if-else-endif structure, but it's more efficient to load a default that may be 'wrong'.
User avatar
thefox
Posts: 3139
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Re: 'else' control structure in assembly.

Post by thefox »

pops wrote:
doppelganger wrote:Actually it would be if #$10 >= ScrollY. Remember the carry flag is set after a subtraction unless the second number (which in this case is ScrollY) is greater, then it will be clear for borrow.
My bad. I'm still getting used to assembly and the 6502.
You can use macros to create aliases for opcodes with easier to remember names, such as:

Code: Select all

.macro jl_unsigned dest ; jump if less
    bcc dest
.endmacro

.macro jge_unsigned dest ; jump if greater or equal
    bcs dest
.endmacro
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
tepples
Posts: 22345
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: 'else' control structure in assembly.

Post by tepples »

In 65816 mode, the assembler automatically includes macros that define blt (branch less than) and bge (branch greater or equal) as synonyms for bcc and bcs.
lidnariq
Posts: 10677
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: 'else' control structure in assembly.

Post by lidnariq »

If you're using ca65, use .macpack generic, which provides add, sub, bge, blt, bgt, and ble. I like including .macpack longbranch, too: it provides jeq, jne, jmi, jpl, jcs, jcc, jvs, and jvc.
User avatar
thefox
Posts: 3139
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Re: 'else' control structure in assembly.

Post by thefox »

lidnariq wrote:I like including .macpack longbranch, too: it provides jeq, jne, jmi, jpl, jcs, jcc, jvs, and jvc.
FYI, in at least some version, these macros were buggy (they always generated a long jump).

Looks like bge/blt/bgt/ble macros of the "generic" macpack are only documented in the snapshot documentation, that's probably why I missed them.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
User avatar
Dwedit
Posts: 4470
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: 'else' control structure in assembly.

Post by Dwedit »

pops wrote:What is the most efficient way to write the 'else' program structure in assembly?

The best I've come up with so far is:

Code: Select all

lda #$10    ; if ScrollY <= $10
cmp ScrollY
bcs smallScroll
    ; do code for 'if' = true
jmp endif
smallScroll:
    ; do code for 'if' = false
endif:
This is literally what a compiler does.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
Post Reply