提问人:Vincent_Matthew 提问时间:10/16/2023 最后编辑:Nate EldredgeVincent_Matthew 更新时间:10/16/2023 访问量:47
ARM汇编:如何输出存储在寄存器中的值
ARM Assembly : how to output value stored in register
问:
我正在为 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。
答: 暂无答案
评论
```
r3
r6
r6
r6