简体   繁体   中英

How do I prevent an infinite loop in my ARM Assembly program?

Update: 4/16/2019 @ 11:46 PM Central. None of my CMP actually work. Pretty much it's jumping to LDR 0 = errorMessage, then re-looping, which is causing the infinite loop. Still trying to work it out.

I am trying to do the following:
1. Ask the user to enter money: Nickle (N), Dime (D), Quarter (Q), or Dollars (B). Repeat until the user has inserted 55 or MORE cents.
2. So what's the problem? Let's say the user enters N, I am trying to compare the INPUT with the ASCII values of (N, D, Q, B) to see if they match up, if so, jump to the approve function, store the value in totalMoney, print out how much money is currently inserted, and call getMoneyLoop again. Repeat until I have reached 55 or more cents.

The program will start fine.

1.) Welcome to my store

2.) Enter Money: Nickle (N), etc

3.) User enters: "N"

However, here I am getting:

Enter money: nickel(N), dime(D), quarters (Q), and one dollar bills (B).
Enter money: nickel(N), dime(D), quarters (Q), and one dollar bills (B).
Enter money: nickel(N), dime(D), quarters (Q), and one dollar bills (B).
Enter money: nickel(N), dime(D), quarters (Q), and one dollar bills (B).
Enter money: nickel(N), dime(D), quarters (Q), and one dollar bills (B).
Enter money: nickel(N), dime(D), quarters (Q), and one dollar bills (B).
Enter money: nickel(N), dime(D), quarters (Q), and one dollar bills (B).

I've been trying crack this puzzle but I can't seem to wrap my head around what is happening.

Thank you, Z

I've tried changing registers. I had this "infinite loop" problem in my previous program, but it was a lot simpler (imo). I know originally when I was getting an infinite loop, it was because I was using r1 and r2 and these are considered "unpreserved" and it can lead to some wonky things, and when I changed it to r5 and r6, it fixed it.

On this program, I just don't know if my logic is correct.

I'm still rather new and learning it, but can anyone lead me in the right direction?

I'm only pasting the specific section I'm having trouble with, and any related data to go along with it.

.data

askUserForMoney: .asciz "Enter money: nickel(N), dime(D), quarters (Q), and one dollar bills (B). \n"

input:  .asciz "%d"

totalCash: .asciz "Total is: %d cents\n" 

errorMessage: .asciz "Invalid Selection. Try Again.\n" 

choice: .word 0 

.text

.global main

main:

getMoney:

    stmfd sp!, {lr, r4}

    mov r4,#0

getMoneyLoop:

 cmp r4, #55
 bge exitGetMoney 

 ldr r0, =askUserForMoney
 bl printf

 ldr r0, =input
 ldr r1, =choice
 bl scanf 

 cmp r1, #78
 addeq r4,r4,#5 
 beq approved 


 cmp r1, #68
 addeq r4,r4,#10
 beq approved


 cmp r1, #81
 addeq r4,r4,#25
 beq approved


 cmp r1, #66
 addeq r4,r4,#100
 beq approved 

 ldr r1, =errorMessage
 bl printf
 b getMoneyLoop

approved:
ldr r0, =totalCash
mov r1, r4
bl printf
b getMoneyLoop


exitGetMoney:
    mov r0,r4
    ldmfd sp!, {lr,r4}
    mov pc, lr

I should not be getting an infinite loop, but I am.

I think following printf statement may cause the problem,

approved: mov r1, r4 bl printf b getMoneyLoop

The output which is set by this printf statement may be treated as input of the subsequent scanf statement.

Another problem may happen that after entering N for the previous input, the buffer keeps N as valid input for the next turn, so the program behaves in such a way that the user set input N every time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM