Let’s see how to write a ARM assembly code to find number of negative numbers in an array.
ALGORITHM
- Start
- Load the base address of array.
- Initialize a counter register to number of elements in array.
- Initialize a register(Rd) to zero to store number of negative numbers.
- Load the value from array to a temporary register.
- Compare the value with zero
- If zero flag is set, increment the register Rd.
- Else decrement the counter register and jump to step 5.
- Repeat steps 5-6 till counter value becomes zero
- Stop
Here is a example code to find number of negative numbers in an array.
Note: initialize the array at the end of program without fail, before execution.
CODE
AREA program,CODE,READONLY
ENTRY
LDR R1,=array ; Storing base address of array
MOV R2,#0X09 ; Initializing counter
lbl AND R0,[R1],#4
CMP R0,#00 ; Comparing with zero
ADDMI R3,R3,#01 ; R3 register will have count of number of negative numbers.
SUBS R2,R2,#01
BPL lbl
array DCD 0x11223343, -0x12345672, 0x002917BD3, -0x00000A01, -0x62398746, 0xAB56E3CE, -0x0A761AC7, 0x623ABC46, -0xAB86E3BD, 0x0A762DE8,
END
1 thought on “ARM Assembly code to find number of negative numbers in an array”