heres my C code this was easy:
-Chris g

#include <stdlib.h>
#include <stdio.h>

extern int numodd( int array[], int size ) ;

int main( int argc, char * argv[] )
{
	int numarray[] = { 2, 3, 1025, 3024, 4057, -3, -1025, -3578 } ;
	int size = sizeof(numarray) / sizeof( int ) ;
	int result ;
	
	result = numodd( numarray, size ) ;
	printf( "Number of odd numbers: %d\n", result ) ;
	
	exit( 0 ) ;
	
}

ARM CODE:

.global numodd  
.text

numodd:
stmfd sp!, {v1-v6, lr} @load from memory

mov v1, #0
mov v1, a2 @store
mov v2, #0 
mov v5, #0
circulate:
mov v3, #0      @register set
mov v4, #0	@register set
ldr v3, [a1,v2]   @load 
add v2, v2, #4	 @inc by 4 in v2
sub v1, v1, #1	@ rotate method to v3
ror v4, v3, #1  @changes signs of numbers
cmp v4, #0

bgt circulate
cmp v1, #-1
addgt v5, v5, #1
bgt circulate

mov a1, v5  @move to a1
ldmfd sp!, {v1-v6, pc}
.end

this works but he said its not acceptable? i dont get any other way to do it.


The stmfd and ldmfd instructions are sort of like curly braces in C. They delimit the function. Copy them, and everything outside them from the add2 program.

The two parameters, array and size, are passed in registers a1 and a2, respectively. Remember that an array variable in C is a pointer to (memory address of) the first element.

You can access the first element with the load command. we can auto-increment the pointer (a1) by the size of an integer (4 bytes) at the same time, so it points to the next element next time.

Once you get the first element, you can check whether the last bit is 1 or not. I tried (and failed) to use the barrel shifter to do a logical shift right and then do an adc to increment my count of odd numbers. i guess we can use a bitwise and to "and" it with 00000001. Either way we need to use some form of the add instruction to increment your "variable" holding the number of odd integers so far.

i guess we need to use size, and a conditional branch (with a label of course), to loop through the array. Remember that size is in a2.

At the end, we can put the result, the number of odd integers you've counted, in a1. That way it will be returned to the C program.

if any one could help that would be awesomeee !!!
i typed as much as i could to help !! - Chris G

Dani AI

Generated

Good C test harness, . The ARM attempt works in spirit but is more complex than needed and misuses a few idioms (unnecessary rotates, extra saved registers, and unclear aliases). A clear, idiomatic ARM implementation follows the ARM EABI: r0 = pointer to array, r1 = size, return value in r0. Load each 32-bit element with post-increment, test the low bit, conditionally increment a counter, and decrement the size with flags to drive the loop.

A compact, readable ARM (ARM mode) version:

.global numodd
.text
.type numodd, %function
numodd:
    push {lr}            @ save return address
    mov  r2, #0          @ count = 0
    cmp  r1, #0
    ble  done            @ return 0 if size <= 0

loop:
    ldr  r3, [r0], #4    @ r3 = *ptr; ptr += 4
    tst  r3, #1          @ set flags on LSB
    addne r2, r2, #1     @ count++ if odd
    subs r1, r1, #1      @ size--
    bgt  loop

done:
    mov  r0, r2          @ return value
    pop  {pc}            @ return

Notes and tips:

  • tst is a zero-cost way to check the LSB; it works for negative values too because two's-complement preserves the LSB.
  • If the assembler rejects post-index ldr r3, [r0], #4, replace with ldr r3, [r0] then add r0, r0, #4.
  • Only callee-saved registers (r4-r11) need preservation; avoid pushing more than necessary.
  • Check toolchain differences (ARM vs Thumb, GAS vs ARMASM) and ensure word alignment for ldr.

THANKS to any one that can help its much appreciated

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.