简体   繁体   中英

how to repeat a mips program using user input 'y'?

i have this program where the user inputs an integer and the program simply just prints it. another function of this program is that it is supposed to ask if the user wants to repeat the program and input another number.

so the user should input 'y' if they want to continue and 'n' if not.

.data
    repeat: .asciiz     "\n Repeat [y/n]? "
    store: .byte ' '

.text
.globl main
main:                   # Main function 
    
    li $v0, 5
    syscall
    
    move $a0, $v0
    li $v0, 1
    syscall
    
    li $v0, 4
    la $a0, repeat
    syscall
    
    li $v0, 12
    syscall
    
    la $s0, store
    sb $v0, store
    syscall
    
    beq $v0, 'y', main
    beq $v0, 'Y', main

    li $v0, 10
    syscall

using this source code, the program can read and print the integer that i input,

but the problem here is that whenever I input 'y' or 'n' and enter, the program prompts that there's an error.

is the problem on the beq instructions? or is it on the part where it reads the character?

i'm just new to programming MIPS and there are still parts that confuse me.

can anyone point out where i'm doing it wrong?

.data
    str1: .asciiz       "\nEnter an integer value: "
    str2: .asciiz       "You entered: "
    repeat: .asciiz     "\nRepeat [y/n]? "
    store: .byte ' '    # This will hold a single byte
.text

.globl main

main:                   # Main function 
    la $a0, str1            # Load address of 'str1' to $a0
    li $v0, 4           # Print 'str1' string
    syscall
    
    li $v0, 5           # Reads the integer input
    syscall
    
    move $s0, $v0           # Moves the value of $v0 to $s0
    
    la $a0, str2            # Load address of 'str2' to $a0
    li $v0, 4           # Prints the 'str2' string
    syscall
    
    move $a0, $s0           # Copy the integer value
    li $v0, 1           # Prints the integer input
    syscall             
    
    la $a0, repeat          # Load address of repeat to $a0
    li $v0, 4           # Print 'repeat' string
    syscall
    
    li $v0, 12          # Reads the char input
    syscall
    
    la $s0, store           # Load address of 'store' to $s0    
    sb $v0, store           # Store byte
        
    beq $v0, 'y', main      # If user input is 'y', then program is branched to the 'main' function

    li $v0, 10
    syscall             # System call to exit program

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