Scanf ARM组装 [英] Scanf ARM Assembly
问题描述
我知道在此处但我真的不明白OP做了什么.我之前使用过x86汇编,为此,您会执行以下操作:
I know there's a question here but I really don't understand what the OP did. I've used x86 assembly before and for that you'd do something like this:
push dword int1
push dword fmtInput
call scanf
add esp, 12
; value is now in int1
我对ARM的猜测是这样的
my guess for ARM is something like this
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
我确定我缺少一些简单的东西,但我真的很迷失.如果ldr和push不起作用,那么还有其他操作码可以使用吗?如果其中之一是正确的,则需要什么输入组合?
I'm sure I'm missing something simple but I'm really very lost. If ldr and push don't work then is there some other opcode to use? If one of those is correct, what combination of inputs does it need?
我还尝试在.data节中定义num: .word 0
并在.data节中使用ldr r1, =num
任何东西似乎都是静态的,还是有另一种方式将它们传递给scanf?
I also tried defining a num: .word 0
in the .data section and using ldr r1, =num
anything in the .data section seems to be static or is there another way to pass them to scanf?
如果有帮助,我正在Qemu的ARMv7处理器上使用gcc.
I'm using gcc on an ARMv7 processor in Qemu if that helps.
-编辑-
这就是我想要做的事情和一些代码.该应用程序打印问候世界,获取输入,添加一个,打印新值.我的代码如下:
Here's what I'm trying to do and some code. The app prints hello world, gets input, adds one, prints the new value. My code looks like this:
.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
输出:您好,世界.
输入:6
输出:数量:3
Outout:您好,世界.
Outout: Hello, world.
输入:d
输出:数量:2
只要我输入数字,最终输出始终为3,而只要输入字符,最终输出始终为2.
the final output is always 3 so long as I input a number and 2 so long as I input characters.
推荐答案
这最终对我有用:
sub sp, sp, #4
@ Call scanf and store value in r4
ldr r0, addrInp
mov r1, sp
bl scanf
ldr r4, [sp]
add sp, sp, #4
在这种情况下,用户输入的值以r4结尾.我不明白为什么Jesters的方式行不通,但每次都会出现段错误.
The value the user inputs ends up in r4 in this case. I don't understand why Jesters way didn't work but it just gave a segfault every time.
这篇关于Scanf ARM组装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!