Task

Test whether an integer is even or odd.

There is more than one way to solve this task:

 Use the even and odd predicates, if the language provides them.
 Check the least significant digit. With binary integers, i bitwise-and 1 equals 0 iff i is even, or equals 1 iff i is odd.
 Divide i by 2. The remainder equals 0 iff i is even. The remainder equals +1 or -1 iff i is odd.
 Use modular congruences:
 i ≡ 0 (mod 2) iff i is even.
 i ≡ 1 (mod 2) iff i is odd.


 
        .lf  evenodd6502.lst
        .cr  6502
        .tf  evenodd6502.obj,ap1
;------------------------------------------------------
; Even or Odd for the 6502 by barrym95838 2014.12.10
; Thanks to sbprojects.com for a very nice assembler!
; The target for this assembly is an Apple II with
;   mixed-case output capabilities.  Apple IIs like to
;   work in '+128' ascii, and this version is tailored
;   to that preference.
; Tested and verified on AppleWin 1.20.0.0
;------------------------------------------------------
; Constant Section
;
CharIn   =   $fd0c      ;Specific to the Apple II
CharOut  =   $fded      ;Specific to the Apple II
;------------------------------------------------------
; The main program
;
main    ldy  #sIntro-sbase
        jsr  puts       ;Print Intro
loop    jsr  CharIn     ;Get a char from stdin
        cmp  #$83       ;Ctrl-C?
        beq  done       ;  yes:  end program
        jsr  CharOut    ;Echo char
        ldy  #sOdd-sbase ;Pre-load odd string
        lsr             ;LSB of char to carry flag
        bcs  isodd
        ldy  #sEven-sbase
isodd   jsr  puts       ;Print appropriate response
        beq  loop       ;Always taken
; Output NUL-terminated string @ offset Y
;
puts    lda  sbase,y    ;Get string char
        beq  done       ;Done if NUL
        jsr  CharOut    ;Output the char
        iny             ;Point to next char
        bne  puts       ;Loop up to 255 times
done    rts             ;Return to caller
;------------------------------------------------------
; String Constants (in '+128' ascii, Apple II style)
;
sbase:                  ;String base address
sIntro  .az     -"Hit any key (Ctrl-C to quit):",-#13
sEven   .az     -" is even.",-#13
sOdd    .az     -" is odd.",-#13
;------------------------------------------------------
        .en
 