2011-10-25 83 views
2

我的任务是获取一组数字并将其放入ARM程序集并执行2的补码,然后再次输出以供显示。我能够完成大部分工作,但是输出结果告诉我这是行不通的。ARM编程输出数组和malloc来清除输入数组?

C代码:

#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汇编:

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 

输出:

Original Complement 
0   -442500552 
-1  -442500552 
252  0 
-252  0 
0   0 
3015  0 

谁能帮助我弄清楚为什么它给我这么搞砸输出

回答

4
str v1, [a1] ; copy a1 over to v1 

这将在a1中传递的int数组的第一个元素上存储寄存器v1的未定义内容。您可以看到输出中原始数组中的第一个元素已被0覆盖。

如果您的意思是在另一个注册表中记住原始的a1,那么您的意思可能是mov v1, a1

str v2, [a2] ; copy a1 over to v1 

同样不是你的意思,但a2是小整数size我很惊讶这个尝试写入内存不足不会立即崩溃!

bl malloc ; clears pointer for new array 

你不能传递你想malloc这里的内存量,它越来越整型数组地址,并把它当作一个字节数。假设32位int,你希望mov a1, a2, asl#2 int大小乘以4个字节。

您或许还应该检查它是否没有失败并返回NULL

ldmfd sp!, {v1-v6, pc} ; puts all registers back into the caller 

结果寄存器a1将在这一点上,而不是一开始就指向其数组的末尾。您需要储存malloc的原始结果并将其返回。

+0

我没有任何输出做你所选择的变化的差异,但我感谢你的回应。 – NaGeLxZ