My assignment is to take an array of numbers and put it into ARM assembly and perform 2’s complement, and then output it again for display. I was able to do most of the work but the output tells me it is not working right.
C code:
#include <stdio.h>
int * comp( int a[], int size ) ;
void main( int argc, char * argv[] )
{
int array[] = { 1, -1, 252, -252, 0, 3015 } ;
int size = sizeof(array) / sizeof(int) ;
int * result ;
int i ;
result = comp( array, size ) ;
printf( "Original Complement\n" ) ;
for( i = 0 ; i < size ; i++ )
printf( "%d %d\n", array[i], *(result+i) ) ;
}
ARM assembly:
AREA |comp$code|, CODE, READONLY ; tell the assembler stuff
IMPORT malloc ; import malloc to be used
EXPORT comp ; tell the assembler to show this label to the linker
comp ; the label defining the entry point
stmfd sp!, {v1-v6, lr} ; standard entry
str v1, [a1] ; copy a1 over to v1
str v2, [a2] ; copy a1 over to v1
bl malloc ; clears pointer for new array
loop
ldr a4,[v1],#4 ; start going through loop starting at top or array
mvn a4, a4 ; ones complement
add a4,a4,#1 ; make it 2's complement
str a4,[a1], #4 ; move back into the array
subs v2, v2, #1 ; set a flag for the end of the loop
bne loop ; start again for the next value in the array
ldmfd sp!, {v1-v6, pc} ; puts all registers back into the caller
END
output:
Original Complement
0 -442500552
-1 -442500552
252 0
-252 0
0 0
3015 0
can anyone help me figure out why its giving me such a messed up output
That will store the undefined contents of register
v1over the first element of the int-array passed ina1. You can see that the first element in the original array in your output has been overwritten with0.If you mean to remember the original
a1in another register, you probably meantmov v1, a1.Again not what you meant, but with
a2being the small integersizeI’m surprised this attempt to write to low memory doesn’t immediately crash!You’re not passing in the amount of memory you want to
mallochere, it’s getting the int-array address and treating it as a number of bytes. Assuming 32-bitint, you would want tomov a1, a2, asl#2to multiply the int size by 4 bytes.You should probably also check that it hasn’t failed and returned
NULL.The result register
a1will be pointing to the end of its array at this point instead of the start. You’ll want to store the original result ofmallocand return it here.