ARM编程输出一个数组和malloc来清除输入数组?

时间:2021-07-09 19:23:53

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.

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

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

任何人都可以帮我弄清楚为什么它给我这么搞砸的输出

1 个解决方案

#1


4  

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

That will store the undefined contents of register v1 over the first element of the int-array passed in a1. You can see that the first element in the original array in your output has been overwritten with 0.

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

If you mean to remember the original a1 in another register, you probably meant mov v1, a1.

如果你想记住另一个寄存器中的原始a1,你可能意味着mov v1,a1。

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

Again not what you meant, but with a2 being the small integer size I'm surprised this attempt to write to low memory doesn't immediately crash!

再一次不是你的意思,但是a2是一个小的整数大小我很惊讶这种写入低内存的尝试不会立即崩溃!

bl malloc ; clears pointer for new array

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-bit int, you would want to mov a1, a2, asl#2 to multiply the int size by 4 bytes.

你没有在这里传递你想要malloc的内存量,它获取了int数组地址并将其视为一个字节数。假设32位int,你可能想要移动a1,a2,asl#2来将int大小乘以4个字节。

You should probably also check that it hasn't failed and returned NULL.

您可能还应检查它是否已失败并返回NULL。

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

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 of malloc and return it here.

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

#1


4  

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

That will store the undefined contents of register v1 over the first element of the int-array passed in a1. You can see that the first element in the original array in your output has been overwritten with 0.

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

If you mean to remember the original a1 in another register, you probably meant mov v1, a1.

如果你想记住另一个寄存器中的原始a1,你可能意味着mov v1,a1。

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

Again not what you meant, but with a2 being the small integer size I'm surprised this attempt to write to low memory doesn't immediately crash!

再一次不是你的意思,但是a2是一个小的整数大小我很惊讶这种写入低内存的尝试不会立即崩溃!

bl malloc ; clears pointer for new array

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-bit int, you would want to mov a1, a2, asl#2 to multiply the int size by 4 bytes.

你没有在这里传递你想要malloc的内存量,它获取了int数组地址并将其视为一个字节数。假设32位int,你可能想要移动a1,a2,asl#2来将int大小乘以4个字节。

You should probably also check that it hasn't failed and returned NULL.

您可能还应检查它是否已失败并返回NULL。

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

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 of malloc and return it here.

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