.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.
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, =strOutputNum
bl printf
ldr r0, =strOutputEven
loop:
cmp r1, #101
beq end
cmp r1, #0
bne odd
cmp r1, #1
bne even
odd:
add r1, r1, LSL #1 /* r1 ← r1 + (r1 << 1) */
bl printf
even:
mov r1, r1, ASR #1 /* r1 ← (r1 >> 1) */
b end_loop
end_loop:
add r2, r2, #1 /* r2 ← r2 + 1 */
b loop /* branch to loop */
@ Print the input out as a number.
@ r1 contains the value input to keyboard.
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 a number between 1 and 100: \n"
.balign 4
strOutputNum: .asciz "You entered: %d \n"
.balign 4
strOutputEven: .asciz "The even numbers from 1 to %d are: \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.
.global printf
.global scanf
cannot get even and odd functions to work