Search code examples
assemblyarmscanf

Scanf ARM Assembly


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

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

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?

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?

I'm using gcc on an ARMv7 processor in Qemu if that helps.

--EDIT--

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

Output: Hello, world.

Input: 6

Output: num: 3


Outout: Hello, world.

Input: d

Output: num: 2

the final output is always 3 so long as I input a number and 2 so long as I input characters.


Solution

  • This is what ended up working for me:

    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
    

    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.