使用 Scanf Assembly ARM 从用户读取多个值 [英] Reading In Multiple Values from User using Scanf Assembly ARM

查看:17
本文介绍了使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆