Question
How would I add the ability for the user to input a character into the program and it is printed along with the numbers like
How would I add the ability for the user to input a character into the program and it is printed along with the numbers like demonstrated below?
This is in ARM on a raspberry pi 3
@ Filename: student_input.s
@ as -o student_input.o student_input.s
@ gcc -o student_input student_input.o
@ ./student_input ;echo $? @ Have to use C compiler to link because of C library uses.
.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 print to display input prompt.
get_input:
@ Set up r0 with the address of input pattern
ldr r0, =numInputPattern @ Setup to read in one number.
@ scanf puts the input value at the address stored in r1. We are going
@ to use the stack (sp) for that address. But first we have to push the
@ stack to get a new pointer to an unused location. After that call scanf.
@ The value read from the input is stored at the address pointed to by
@ sp. Load the value into r1 and pop the stack to what is was before.
sub sp, sp, #4 @ Update stack pointer to new loc. mov r1, sp @ Put address into r1 for read.
bl scanf @ scan the keyboard.
ldr r1, [sp, #0] @ The character is on the stack. add sp, sp, #4 @ Reset the stack.
@ print the input out as a number.
printinputasNum:
@ r1 is already loaded with the number to print. ldr r0, =strOutputNum
bl printf
@End of my code. Force the exit and return control to OS
myexit:
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: "
.balign 4
strOutputNum: .asciz "The number value is: %d "
@ Format pattern for scanf call. .balign 4
numInputPattern: .asciz "%d"
@ Let the assembler know these are the C library functions.
.global printf
.global scanf
@end of code.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started