简体   繁体   中英

MIPS - Assembly count goes down but goes into infinite loop

I'm trying to count down from a number that is inputted by the user and want to display all of the integers in between the number and 0. My Output I believe goes down, but then it goes into an infinite loop at 1. It seems to never get to zero.

I just started learning assembly so I'll apologize in advance if this is a bad question.

Thanks

Here is my code:

.globl  main

.data
    msg: .asciiz "Input a number: "
    x: .word 1

    .text
main:

    li  $v0,4       # display the first message
    la  $a0, msg
    syscall 

    li  $v0, 5      # call for an input read, stores in $v0
    syscall

    move    $t0, $v0    # move the input to a temporary register
    lw  $t1, x          # loads x into $t1 registers

# Show Output
doLoop:
    sub $v0, $t0, $t1   # subtracts 1 from given input stores in $v0

    move $s0, $v0

    li $v0, 1       # Prepares to print integer
    move $a0, $v0
    syscall

    bgt $a0, 0, doLoop

    li  $v0,10      # load the "exit" number into register $v0
    syscall 

This seemed to do the trick for me, I keep screwing up where I was putting constants in my registers. Therefore every time I tried to print the register contained a 1.

doLoop: 

sub     $t2, $t0, $t1   # subtracts 1 from given input stores in $v0

li  $v0, 1      
move    $a0, $t2    # Places answer in $a0
la  $v0, 1
syscall

move    $t0, $t2    
bgt $a0, 0, doLoop

li  $v0,10      # load the "exit" number into register $v0
syscall 

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