ARM编程输出数组并malloc清除输入数组?

发布于 2024-12-12 04:44:01 字数 1429 浏览 0 评论 0原文

我的任务是获取一个数字数组并将其放入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

装纯掩盖桑 2024-12-19 04:44:01
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 数组地址并将其视为字节数。假设 32 位 int,您需要 mov a1, a2, asl#2 将 int 大小乘以 4 个字节。

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

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

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

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.

If you mean to remember the original a1 in another register, you probably meant 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!

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.

You should probably also check that it hasn't failed and returned 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文