I cannot get the character to input and it does not output.

.equ READERROR, 0 @Used to check for scanf read error. 

.global main @ Have to use main because of C library uses. 

main:
 prompt:


@ Ask the user to enter a number.

   ldr r0, =strInputPrompt @ Put the address of my string into the first parameter
   bl  printf              @ Call the C printf to display input prompt. 

@ Ask the user to enter a character.

   ldr r0, =charInputPrompt
   bl printf

get_input:


@ Set up r0 with the address of input pattern.
@ scanf puts the input value at the address stored in r1. We are going
@ to use the address for our declared variable in the data section - intInput. 
@ After the call to scanf the input is at the address pointed to by r1 which 
@ in this case will be intInput. 

   ldr r0, =numInputPattern @ Setup to read in one number.
   ldr r1, =intInput        @ load r1 with the address of where the
                        @ input value will be stored. 
   bl  scanf                @ scan the keyboard.
   cmp r0, #READERROR       @ Check for a read error.
   beq readerror            @ If there was a read error go handle it. 
   ldr r1, =intInput        @ Have to reload r1 because it gets wiped out. 
   ldr r1, [r1]             @ Read the contents of intInput and store in r1 so that
                        @ it can be printed. 
   ldr r0, =charInputPattern @ Setup to read in one number.
   ldr r1, =charInput        @ load r1 with the address of where the
                        @ input value will be stored. 
   bl  scanf                @ scan the keyboard.
   cmp r0, #READERROR       @ Check for a read error.
   beq readerror            @ If there was a read error go handle it. 
   ldr r1, =charInput        @ Have to reload r1 because it gets wiped out. 
   ldr r1, [r1]             @ Read the contents of intInput and store in r1 so that
                        @ it can be printed. 




@ Print the input out as a number.
@ r1 contains the value input to keyboard. 

   ldr r0, =strOutputNum
   bl  printf


   ldr r0, =strOutputChar
   bl  printf
   b   myexit @ leave the code. 


readerror:

@ Got a read error from the scanf routine. Clear out the input buffer then
@ branch back for the user to enter a value. 
@ Since an invalid entry was made we now have to clear out the input buffer by
@ reading with this format %[^\n] which will read the buffer until the user 
@ presses the CR. 

   ldr r0, =strInputPattern
   ldr r1, =strInputError   @ Put address into r1 for read.
   bl scanf                 @ scan the keyboard.
@  Not going to do anything with the input. This just cleans up the input buffer.  
@  The input buffer should now be clear so get another input.

   b prompt


myexit:

@ End of my code. Force the exit and return control to OS

   mov r7, #0x01 @ SVC call to exit
   svc 0         @ Make the system call. 

.data

@ Declare the strings and data needed

.balign 4
strInputPrompt: .asciz "Input the number: \n"

.balign 4
strOutputNum: .asciz "The number value is: %d \n"

.balign 4
charInputPrompt: .asciz "Input the character: \n"

.balign 4
strOutputChar: .asciz "The character is: %c \n" 

@ Format pattern for scanf call.

    .balign 4
    numInputPattern: .asciz "%d"  @ integer format for read. 

    .balign 4
    strInputPattern: .asciz "%[^\n]" @ Used to clear the input buffer for invalid input. 

    .balign 4
    strInputError: .skip 100*4  @ User to clear the input buffer for invalid input. 

    .balign 4
    intInput: .word 0   @ Location used to store the user input.

    .balign 4
    charInputPattern: .asciz "%c"  @ integer format for read. 

    .balign 4
    charInput: .word 0   @ Location used to store the user input 

@ Let the assembler know these are the C library functions. 

.global printf
@  To use printf:
@     r0 - Contains the starting address of the string to be printed. The string
@          must conform to the C coding standards.
@     r1 - If the string contains an output parameter i.e., %d, %c, etc. register
@          r1 must contain the value to be printed. 
@ When the call returns registers: r0, r1, r2, r3 and r12 are changed. 

.global scanf
@  To use scanf:
@      r0 - Contains the address of the input format string used to read the user
@           input value. In this example it is numInputPattern.  
@      r1 - Must contain the address where the input value is going to be stored.
@           In this example memory location intInput declared in the .data section
@           is being used.  
@ When the call returns registers: r0, r1, r2, r3 and r12 are changed.
@ Important Notes about scanf:
@   If the user entered an input that does NOT conform to the input pattern, 
@   then register r0 will contain a 0. If it is a valid format
@   then r0 will contain a 1. The input buffer will NOT be cleared of the invalid
@   input so that needs to be cleared out before attempting anything else.
@


@ End of code and end of file. Leave a blank line after this.

Recommended Answers

All 2 Replies

While I have written in assembly language for a very long time, I'll have to find some common ground to try out your code.

Is there something on the web like Oak or such so I can see what it's doing?
Also, why assembler? I move to C++ or python for my projects today.

Which assembler are you using, and which specific platform (e.g., Raspberry Pi, Apple M1, etc.) is this to run on? While the use of the C library routines makes this generally less important, it is still relevant, as there are differences both in the syntax of specific ARM assemblers, and in how the assembly code interefaces with the library code.

commented: Raspberry PI +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.