获得一个用户输入ARM汇编语言 [英] Getting an user input in ARM Assembly language

查看:294
本文介绍了获得一个用户输入ARM汇编语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的ARM汇编语言。我有一个项目。我的问题是如何从用户臂组件(在QEMU仿真器)的输入?只是尝试这样做,但看起来像它不会工作:/

  #Scanf    。文本
    。全球主要主要:
    子SP,SP,#4
    STR LR,[SP,#0]#提示符为输入
    LDR R0,=提示
    BL的printf#Scanf
    LDR R0,=格式
    子SP,SP,#4
    MOV R1,SP
    BL scanf函数
    LDR R2,[SP,#0]
    加SP,SP,#4#打印消息
    MOV R1,R2
    BL的printf    LDR LR,[SP,#0]
    加SP,SP,#4
    MOV PC,LR    。数据格式:
    .asciz您的号码为%d \\ n提示:
    .asciz输入一个数字\\ n


解决方案

您还没有定义什么样的它不会工作的意思,但我会假设,显示的号码由最后的printf 不匹配你输入的是什么。

这是你传递您的号码为%d \\ n事实上作为格式字符串 scanf函数是一个问题,因为它包含了一堆非格式说明字符。从文档


  

非空格字符,除了格式指示符(%):的任何字符,是不是无论是空白字符(空白,换行或制表符)或格式说明(其中以%开始的一部分字符)导致函数从流中读取下一个字符,把它比作这个非空白字符,如果匹配,则丢弃并在函数格式的下一个字符继续。如果字符不匹配,函数调用失败,返回和离开流的后续字符未读。


你应该做的是声明仅包含格式说明另一个字符串,即是这样的:

 的InputFormat:.asciz%D

和该字符串传递给 scanf函数而不是格式

I'm new to ARM Assembly language. I have a project. The problem i have is how to get an input from user in arm assembly (in QEMU emulator) ? Just tried this, but looks like it won't work :/

#Scanf

    .text
    .global main

main:
    sub sp, sp, #4
    str lr, [sp, #0]

# Prompt For An Input
    ldr r0, =prompt
    bl  printf

#Scanf
    ldr r0, =format
    sub sp, sp, #4
    mov r1, sp
    bl  scanf
    ldr r2, [sp, #0]
    add sp, sp, #4

# Printing The Message
    mov r1, r2
    bl  printf

    ldr lr, [sp, #0]
    add sp, sp, #4
    mov pc, lr

    .data

format:
    .asciz "Your Number Is %d \n"

prompt:
    .asciz "Enter A Number\n"

解决方案

You haven't defined what "it won't work" means, but I'm going to assume that the number displayed by the last printf doesn't match what you inputted.

The fact that you're passing "Your Number Is %d \n" as the format string to scanf is a problem, since it contains a bunch of non-format specifier characters. To quote from the documentation:

Non-whitespace character, except format specifier (%): Any character that is not either a whitespace character (blank, newline or tab) or part of a format specifier (which begin with a % character) causes the function to read the next character from the stream, compare it to this non-whitespace character and if it matches, it is discarded and the function continues with the next character of format. If the character does not match, the function fails, returning and leaving subsequent characters of the stream unread.

What you should be doing is declare another string that contains only a format specifier, i.e. something like this:

inputformat: .asciz "%d"

And pass that string to scanf instead of format.

这篇关于获得一个用户输入ARM汇编语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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