ARM编程输出数组并malloc清除输入数组?
我的任务是获取一个数字数组并将其放入 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
任何人都可以帮我弄清楚为什么它给我这样一个混乱的输出
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这会将寄存器
v1
的未定义内容存储在a1
中传递的 int 数组的第一个元素上。您可以看到输出中原始数组的第一个元素已被0
覆盖。如果您想记住另一个寄存器中的原始
a1
,您可能意味着mov v1, a1
。再次不是你的意思,但由于
a2
是小整数size
我很惊讶这种写入低内存的尝试并没有立即崩溃!您没有在此处传递要
malloc
的内存量,而是获取 int 数组地址并将其视为字节数。假设 32 位int
,您需要mov a1, a2, asl#2
将 int 大小乘以 4 个字节。您可能还应该检查它是否没有失败并返回
NULL
。结果寄存器
a1
此时将指向其数组的末尾而不是开头。您需要存储malloc
的原始结果并在此处返回它。That will store the undefined contents of register
v1
over 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
a1
in another register, you probably meantmov v1, a1
.Again not what you meant, but with
a2
being the small integersize
I'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
malloc
here, 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#2
to multiply the int size by 4 bytes.You should probably also check that it hasn't failed and returned
NULL
.The result register
a1
will be pointing to the end of its array at this point instead of the start. You'll want to store the original result ofmalloc
and return it here.