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

时间:2019-04-17 02:23:47

标签: loops input arm

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.

1 个答案:

答案 0 :(得分:0)

我认为以下printf语句可能会引起问题,

已批准: mov r1,r4 bl printf b getMoneyLoop

此printf语句设置的输出可以视为后续输入的输入 scanf语句。

另一个问题可能是,在为前一个输入输入N之后,缓冲区将N保留为下一轮的有效输入,因此程序的行为是用户每次都要设置输入N。

相关问题