Getting an user input in ARM Assembly language

16,269

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.

Share:
16,269
ThisaruG
Author by

ThisaruG

Human

Updated on June 25, 2022

Comments

  • ThisaruG
    ThisaruG almost 2 years

    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. The output is not matching with the input.

    #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"