使用Scanf Assembly ARM从用户读取多个值 [英] Reading In Multiple Values from User using Scanf Assembly ARM
问题描述
我想知道如何使用scanf接收来自用户的多个输入.我正在阅读《 Raspbian初学者》这本书,但没有提到如何实现这一点.这是我的代码.如果我回显R2,我会收到我输入的值,尽管当我回显R1时,我会收到一个随机值.任何帮助将不胜感激.
I was wondering how one would go about taking in multiple inputs from the user using scanf. I am going through the book Raspbian Beginners and it does not mention how to accomplish this. Here is my code. If I echo out R2, I receive the value I entered, though when I echo out R1, I receive a random value. Any help would be appreciated.
.data
string: .asciz "Hours: %d. PayRate: %d"
prompt1: .asciz "Enter pay rate.\n"
prompt2: .asciz "Enter hours.\n"
scantype: .asciz "%d"
hours: .word 0
payrate: .word 0
.text
.global main
main:
push {LR}
ldr r0, addr_prompt1 /*First param of printf */
bl printf /*call printf */
ldr r0, addr_scantype /* First param of scanf*/
ldr r1,addr_hours /*Loading address in memory into register as second param*/
bl scanf
ldr r1,addr_hours
ldr r1,[r1]
ldr r0, addr_prompt2
bl printf
ldr r0, addr_scantype
ldr r1, addr_payrate
bl scanf
ldr r1,addr_payrate
ldr r2,[r1]
mov r0,r1
pop {PC}
mov pc,lr
addr_prompt1: .word prompt1
addr_prompt2: .word prompt2
addr_scantype: .word scantype
addr_hours: .word hours
addr_payrate: .word payrate
.global scanf
.global printf
推荐答案
我复制并修改了您的代码,它可以正常工作.
I copied and modified your code and it works.
该随机数可能是您从r1指向相应的addr _...地址与sp的地址.随机性可能是由于地址空间布局随机化造成的.
The random number is likely an address that you are picking up from pointing r1 to the respective addr_... addresses vs sp. The randomness is likely due to address space layout randomization.
输出在底部.您可能需要调整堆栈以获取更大的值.
The output is at the bottom. You may need to adjust stack for larger values.
.data
string: .asciz "Pay Rate: %d Hours: %d\n"
prompt1: .asciz "Enter pay rate - round up to nearest dollar.\n"
prompt2: .asciz "Enter hours.\n"
scantype: .asciz "%d"
hours: .word 0
payrate: .word 0
.text
addr_prompt1: .word prompt1
addr_prompt2: .word prompt2
addr_scantype: .word scantype
addr_hours: .word hours
addr_payrate: .word payrate
.global main
main:
push {LR}
sub sp, sp, #8
ldr r0, addr_prompt1 /*First param of printf */
bl printf /*call printf */
ldr r0, addr_scantype /* First param of scanf*/
mov r1, sp
bl scanf
ldr r4, [sp]
ldr r0, addr_prompt2
bl printf
ldr r0, addr_scantype
mov r1, sp
bl scanf
ldr r5, [sp]
ldr r0,=string
mov r1, r4
mov r2, r5
bl printf
add sp, sp, #8
pop {PC}
...
root@scw-cb8d4b:~/asm# ./jf
Enter pay rate - round up to nearest dollar.
100
Enter hours.
40
Pay Rate: 100 Hours: 40
这篇关于使用Scanf Assembly ARM从用户读取多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!