Let’s see how to write a ARM assembly code to find number of odd numbers in an array.
- Load the base address of the array.
- Initialize a counter register to the number of elements in the array.
- Initialize a register(Rd) to zero to store number of odd numbers.
- Load the value from array to a temporary register.
- AND the value in temporary register with 0x01.
- If zero flag is not set, increment to register Rd.
- Else decrement the counter register and jump to step 5.
- Repeat steps 5-6 till counter value becomes zero
Here is an example code to find the number of odd numbers in an array.
Note: initialize the array at the end of the program without fail, before execution.
MOV R0,#01 ; Storing 0x01 for AND operation
LDR R1,=array ; Storing base address of array
MOV R2,#0X09 ; Initializing counter
lbl AND R0,[R1],#4 ; 'AND'ing to check whether number is odd or even
ADDNE R3,R3,#O1 ; R3 register will have count of number of odd numbers.
array DCD 0x11223343, 0x12345672, 0x002917BD3, 0x00000A01, 0x62398746, 0xAB56E3CE, 0x0A761AC7, 0x623ABC46, 0xAB86E3BD, 0x0A762DE8,