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.


 
/* ARM assembly Raspberry PI  or android 32 bits */
/*  program oddEven.s   */ 
 
/* REMARK 1 : this program use routines in a include file 
   see task Include a file language arm assembly 
   for the routine affichageMess conversion10 
   see at end of this program the instruction include */
/* for constantes see task include a file in arm assembly */
/************************************/
/* Constantes                       */
/************************************/
.include "../constantes.inc"
 
/*********************************/
/* Initialized data              */
/*********************************/
.data
sMessResultOdd:        .asciz " @  is odd (impair) \n"
sMessResultEven:       .asciz " @  is even (pair)  \n"
szCarriageReturn:   .asciz "\n"
 
/*********************************/
/* UnInitialized data            */
/*********************************/
.bss
sZoneConv:        .skip 24
/*********************************/
/*  code section                 */
/*********************************/
.text
.global main 
main:                                 @ entry of program 
 
    mov r0,#5
    bl testOddEven
    mov r0,#12
    bl testOddEven
    mov r0,#2021
    bl testOddEven
100:                                  @ standard end of the program 
    mov r0, #0                        @ return code
    mov r7, #EXIT                     @ request to exit program
    svc #0                            @ perform the system call
 
iAdrszCarriageReturn:     .int szCarriageReturn
iAdrsMessResultOdd:       .int sMessResultOdd
iAdrsMessResultEven:      .int sMessResultEven
iAdrsZoneConv:            .int sZoneConv
/***************************************************/
/*     test if number is odd or even               */
/***************************************************/
// r0 contains à number
testOddEven:
    push {r2-r8,lr}             @ save  registers 
    tst r0,#1                   @ test bit 0 to one
    beq 1f                      @ if result are all zéro, go to even
    ldr r1,iAdrsZoneConv        @ else display odd message
    bl conversion10             @ call decimal conversion
    ldr r0,iAdrsMessResultOdd
    ldr r1,iAdrsZoneConv        @ insert value conversion in message
    bl strInsertAtCharInc
    bl affichageMess
    b 100f
1:
    ldr r1,iAdrsZoneConv
    bl conversion10             @ call decimal conversion
    ldr r0,iAdrsMessResultEven
    ldr r1,iAdrsZoneConv        @ insert conversion in message
    bl strInsertAtCharInc
    bl affichageMess
100:
    pop {r2-r8,lr}             @ restaur registers
    bx lr                      @ return
/***************************************************/
/*      ROUTINES INCLUDE                           */
/***************************************************/
.include "../affichage.inc"
 