ARM汇编:如何输出存储在寄存器中的值

ARM Assembly : how to output value stored in register

提问人:Vincent_Matthew 提问时间:10/16/2023 最后编辑:Nate EldredgeVincent_Matthew 更新时间:10/16/2023 访问量:47

问:

我正在为 raspberry pi 4 32 位编写 ARM 代码。我需要让用户输入两个有符号的 int 值,然后使用这些值来计算运算 add、mul 和 orr 并输出到屏幕。当我运行程序时,每次操作的结果都是 0。分配是显示所选的用户输入以及这些输入的操作结果。

附件是程序的输出。

在此处输入图像描述

这是我的代码:

/* -- two_int_data_op.s -- */



.section .data
/* Prompt message */
prompt: .asciz "Please enter two signed integers: "

/* Response message */
response: .asciz "You entered %d and %d from the keyboard, now some operations on those values!\n"
sum_message: .asciz "Sum of %d and %d is %d\n " /*print out the sum of chosen values*/
product_message: .asciz "Product of %d and %d is %d\n " /*print out the product of chosen values*/
logical_and_message: .asciz "Logical AND of %d and %d is %d \n " /*print out the logical AND of chosen values*/
logical_or_message: .asciz "Logical OR of %d and %d is %d \n " /*print out the logical OR chosen values*/


/* Format pattern for scanf */
pattern: .asciz "%d %d"


/* Where scanf will store the number read */
value_read1: .word 0
value_read2: .word 0

.section .text
.global main
main:
        push {lr}               /* save our return address */

        // use r4, r5 as registers holding pointers to value_read1, and value_read2
        ldr r4, =value_read1
        ldr r5, =value_read2

        ldr r0, =prompt         /* r0 contains pointer to prompt message */
        bl printf               /* call printf to output our prompt */

        ldr r0, =pattern        /* r0 contains pointer to format string for our scan pattern */
        mov r1, r4              /* r1 contains pointer to variable label where our first number is stored */
        mov r2, r5              /* r2 contains pointer to variable label where our second number is stored */
        bl scanf                /* call to scanf */

next:
        ldr r0, =response       /* r0 contains pointer to response message */
        mov r1, r4              /* r4 contains pointer to value_read1 */
        ldr r1, [r1]            /* r1 contains value dereferenced from r1 in previous instruction */
        mov r2, r5
        ldr r2, [r2]
        bl printf               /* call printf to output our response */

        mov r1, r4              /* r4 contains pointer to value_read1 */
        ldr r1, [r1]            /* r1 contains value dereferenced from r1 in previous instruction */
        mov r2, r5
        ldr r2, [r2]
        add r6, r1, r2          /* calculate the sum of the two integers, store result r6 */
        ldr r0, =sum_message    /* r0 contains pointer to sum  message */
        bl printf               /* call printf to output our response */

        mov r1, r4              /* r4 contains pointer to value_read1*/
        ldr r1, [r1]            /* r1 contains value dereferenced from r1 in previous instruction */
        mov r2, r5
        ldr r2, [r2]
        mul r6, r1, r2          /* calculate the product of  the two integers, store result r6 */
        ldr r0, =product_message /* r0 contains pointer to product message*/
        bl printf                       /* call printf to output our response */

        mov r1, r4                      /* r4 contains pointer to value_read1*/
        ldr r1, [r1]                    /* r1 contains value dereferenced from r1 in previous instruction */
        mov r2, r5
        ldr r2, [r2]
        and r6, r1, r2                  /* bitwise and r1 and r2, store result in r6*/
        ldr r0, =logical_and_message    /* r0 contains pointer to logical and mesage*/
        bl printf

        mov r1, r4                      /* r4 cntains pointer value_read1 */
        ldr r1, [r1]                    /* r1 contains value dereferenced from r1 in previous instruction */
        mov r2, r5
        ldr r2, [r2]
        orr r6, r1, r2                  /* bitwise or r1, r2, sotre result in r6 */
        ldr r0, =logical_or_message     /* ro contains pointer to logical or message */
        bl printf

        mov r0, #0              /* exit code 0 = program terminated normally */
        pop {pc}                /* exit our main function */

我期望 r6 输出操作的计算值。相反,所有 4 个操作都会导致 0。

汇编 逻辑运算符 调用约定

评论

0赞 Nate Eldredge 10/16/2023
欢迎访问本站。为了将来参考,您可以在开头和结尾使用自己的行来格式化代码块。有关格式设置的详细信息,请参阅 stackoverflow.com/help/formatting。此外,对于程序的输出,请将其复制/粘贴为文本,而不是使用屏幕截图。为什么我不应该上传代码/数据/错误的图像?```
0赞 Nate Eldredge 10/16/2023
您是否尝试过在调试器中单步执行代码?这应该可以帮助您准确确定其行为异常的位置。当您这样做时,请编辑您的帖子,提供有关您发现的内容的更多详细信息,这将有助于缩小您的问题范围并使其更容易回答。
0赞 Nate Eldredge 10/16/2023
传递给函数的第四个参数是 ,而不是 。我不确定你从哪里会得到这个想法.(如果超过四个,则它们在堆栈上传递; 从不用于参数/参数传递。有关调用约定的权威说明,请参阅 github.com/ARM-software/abi-aa/blob/main/aapcs32/...。另一个注意事项是,您必须保留 r4-r11 的内容,因此您需要将 r4 和 r5 添加到函数顶部和底部的推送/弹出中。r3r6r6r6

答: 暂无答案