Scanf ARM 组件

dlk*_*ulp 0 assembly arm scanf

我知道这里有一个问题但我真的不明白 OP 做了什么。我之前使用过 x86 程序集,为此你会做这样的事情:

push dword int1
push dword fmtInput
call scanf
add esp, 12
; value is now in int1
Run Code Online (Sandbox Code Playgroud)

我对 ARM 的猜测是这样的

ldr r0, fmtInput
push r1 @ says this is too complex, tried `ldr r1` but that also failed saying that ldr needed more inputs
bl scanf
@ I'm assuming the value is now in r1
Run Code Online (Sandbox Code Playgroud)

我确定我错过了一些简单的东西,但我真的很迷茫。如果 ldr 和 push 不起作用,那么还有其他一些操作码可以使用吗?如果其中之一是正确的,它需要什么样的输入组合?

我还尝试num: .word 0在 .data 部分定义 a并使用 .data 部分中的ldr r1, =num任何内容似乎是静态的还是有另一种方法将它们传递给 scanf?

如果有帮助,我将在 Qemu 的 ARMv7 处理器上使用 gcc。

- 编辑 -

这是我正在尝试做的事情和一些代码。该应用程序打印 hello world,获取输入,添加一个,打印新值。我的代码如下所示:

.text
.align 4
.global main
main:
    @ keep stack 8-byte aligned
    push {ip, lr}

    @ print message
    ldr r0, =message
    bl printf

    @ scanf for number
    ldr r0, =fmtInt
    ldr r1, =num
    bl scanf

    @ add 2 to input and store in r3
    @ldr r1, =num
    mov r2, #2
    add r3, r2, r1

    @ print new value
    ldr r0, =textOut
    mov r1, r3
    bl printf

    @ return 0
    mov r0, #0

    @ reverse align
    pop {ip, pc}

@ vars and stuff
.data
message:    .asciz "Hello, world.\n"
fmtInt:     .string "%d"
textOut:    .asciz "num: %d\n"
num:        .word 1
Run Code Online (Sandbox Code Playgroud)

输出:你好,世界。

输入:6

输出:数量:3


输出:你好,世界。

输入:d

输出:数量:2

只要我输入一个数字,最终输出总是 3,只要我输入字符,就总是 2。

Jes*_*ter 5

ldr should by all means work. This certainly compiles:

ldr r0, =fmtInput
ldr r1, =num
bl scanf

.data
num: .word 0
fmtInput: .string "%d"
Run Code Online (Sandbox Code Playgroud)