【问题标题】:ARM programming output an array and malloc to clear input array?ARM编程输出一个数组和malloc清除输入数组?
【发布时间】:2011-10-25 21:01:59
【问题描述】:

我的任务是取一个数字数组并把它放入 ARM 汇编并执行 2 的补码,然后再次输出以显示。我能够完成大部分工作,但输出告诉我它工作不正常。

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

谁能帮我弄清楚为什么它给了我如此混乱的输出

【问题讨论】:

    标签: c assembly malloc arm


    【解决方案1】:
    str v1, [a1] ; copy a1 over to v1
    

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

    如果您要记住另一个寄存器中的原始a1,您可能是指mov v1, a1

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

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

    bl malloc ; clears pointer for new array
    

    您没有在此处传递您想要malloc 的内存量,它获取的是 int-array 地址并将其视为多个字节。假设 32 位 int,您可能希望 mov a1, a2, asl#2 将 int 大小乘以 4 个字节。

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

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

    此时结果寄存器a1 将指向其数组的末尾而不是开头。您需要存储 malloc 的原始结果并在此处返回。

    【讨论】:

    • 我对您选择的更改的输出没有任何区别,但我感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 2012-12-29
    相关资源
    最近更新 更多