Using the student_input.s ARM code provided in the Files --> ARM Programs on Can
ID: 3603432 • Letter: U
Question
Using the student_input.s ARM code provided in the Files --> ARM Programs on Canvas and discussed in class expand the code to do all the following:
1. Prompt the user for an input number (integer). [Already in program]
2. Print the input number back to the screen. [Already in program]
3. Prompt the user for an input character. [New function]. Use "%s" instead of "%c" for the input. This will keep from reading the CR from the previous input.
4. Print the input character back to the screen. [New function]. Use "%c" for the output.
5. Return control back to the operating system. [Already in program]
student_input.s File as follows:
@ Filename: student_input.s
@ Author:
@ Purpose: show how to read user inputs from KB.
@ Use these command to assemble, link and run this program
@ 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.
Explanation / Answer
.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.
@ 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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.