this is a snippet of a coding project I'm working on that's having a few issues I'm struggling to understand. The goal of this project is to learn how to use Push/Pop to transfer data between the main function and a subroutine. Everything seems to be working alright, but the code exits prematurely after the print statement in 'and_op' right before moving onto 'end_Prompt'. Any ideas on why this is failing would be very helpful!
Here's the code snippet (missing all functions tied to ORR/EOR/BIC although they are written similarly to AND)
.equ READERROR, 0 @Used to check for scanf read error.
.global main
main: @Must use this label where to start executing the code.
@*******************
welcome:
@*******************
ldr r0, =welcomePrompt @print first prompt of script
bl printf
@*******************
get_hex1:
@*******************
@Get user input for Hexadecimal 1
ldr r0, =opp1InputPrompt @print prompt for second input
bl printf
ldr r0, =hexInputPattern @ Setup to read in one number.
ldr r1, =hexInput1 @ 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, =hexInput1 @ Have to reload r1 because it gets wiped out.
ldr r4, [r1] @ Read the contents of intInput and store in r1 so that
@ it can be printed.
cmp r0, #1 @Check that hex number entered is correct
beq get_hex2
b readerror @branch to read error if the input is not correct
@*******************
get_hex2:
@*******************
@Get user input for Hexadecimal 2
ldr r0, =opp2InputPrompt @print prompt for second input
bl printf
ldr r0, =hexInputPattern @ Setup to read in one number.
ldr r1, =hexInput2 @ 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, =hexInput2 @ Have to reload r1 because it gets wiped out.
ldr r5, [r1] @ Read the contents of intInput and store in r1 so that
@ it can be printed.
cmp r0, #1 @Check that hex number entered is correct
beq get_operation
b readerror @branch to read error if the input is not correct
@*******************
get_operation:
@*******************
@Get user input for Hexadecimal 2
ldr r0, =opInputPrompt @print prompt for second input
bl printf
ldr r0, =opInputPattern @ Setup to read in one number.
ldr r1, =operation @ 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, =operation @ 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.
cmp r1, #1 @ Branch to appropriate operation
beq and_op
@cmp r1, #2 @ Branch to appropriate operation
@beq orr_op
@cmp r1, #3 @ Branch to appropriate operation
@beq eor_op
@cmp r1, #4 @ Branch to appropriate operation
@beq bic_op
b readerror @branch to read error if the input is not correct
@***********
and_op:
@***********
@complete AND operation
@r4 = opperand1
@r5 = opperand2
push {r4, r5} @push user input
bl _AND @call subroutine
pop {r6} @restore final value
ldr r0, =andOutput @set string to be printed
mov r1, r4 @temp storage of opp1 for print
mov r2, r5 @temp storage of opp2 for print
mov r3, r6 @temp storage of output for print
bl printf
b end_Prompt @branch to end prompt
@***********
end_Prompt:
@***********
@Prompt user to complete another calculation
ldr r0, =endPrompt @print end prompt
bl printf
ldr r0, =charInputPattern @ Setup to read in one number.
ldr r1, =cont @ 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, =cont @ 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.
cmp r1, #'y' @Branch to start if the user says yes
beq welcome
b myexit @Branch to exit if the user says no
@*******************
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.
@***********
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.
ldr r0, =readerErrorOutput @Prompts user for a valid input
bl printf @ Call the C printf to display input prompt.
b get_hex1
@***********
_AND:
@***********
@AND subroutine
pop {r10, r11} @restore user input to the following registers (r10 = r4 & r11 = r5)
AND r0, r10, r11 @preform operation
push {r0} @store final value
bx lr @branch back
.data
@ Declare the strings and data needed
.balign 4
welcomePrompt: .asciz "Welcome!\nTo properly use this program please follow the instructions... \n"
.balign 4
opp1InputPrompt: .asciz "\nPlease enter a 32 bit (8 digit) hexadecimal digit: "
.balign 4
opp2InputPrompt: .asciz "Please enter a second 32 bit (8 digit) hexadecimal digit: "
.balign 4
hexInputPrompt: .asciz "Please enter a second 8 digit (32 bit) hexadecimal digit: "
.balign 4
opInputPrompt: .asciz "\nPlease choose one of the following options:\n 1: AND \n 2: OR \n 3: XOR \n 4: BIC \n\n"
.balign 4
readerErrorOutput: .asciz "Your Input was invalid, please try again...\n\n"
.balign 4
andOutput: .asciz "\n%x AND %x = %x\n"
.balign 4
orrOutput: .asciz "\n%x OR %x = %x\n"
.balign 4
eorOutput: .asciz "\n%x XOR %x = %x\n"
.balign 4
bicOutput: .asciz "\n%x BIC %x = %x\n"
.balign 4
endPrompt: .asciz "\nWould you like to perform another calculation (y/n)? "
@ Format pattern for scanf call.
.balign 4
hexInputPattern: .asciz "%x" @ hex format for read.
.balign 4
opInputPattern: .asciz "%d" @ integer format for read.
.balign 4
charInputPattern: .asciz "%c" @ char 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
hexInput1: .word 0 @ Location used to store the user input.
.balign 4
hexInput2: .word 0 @ Location used to store the user input.
.balign 4
operation: .word 0 @ Location used to store the user input.
.balign 4
cont: .word 0 @ Location used to store the user input.
.global printf
.global scanf
@ End of code and end of file. Leave a blank line after this.
The code spits out the following:
Welcome!
To properly use this program please follow the instructions...
Please enter a 32 bit (8 digit) hexadecimal digit: F0F0F0F0
Please enter a second 32 bit (8 digit) hexadecimal digit: AFAFAFAF
Please choose one of the following options:
1: AND
2: OR
3: XOR
4: BIC
1
f0f0f0f0 AND afafafaf = a0a0a0a0
1