Search code examples
delaymipssubroutine

MIPS delay subroutine


I recently started using MIPS and am trying to do something simple. Currently, this program prints out Hello World without delay, and I want for it to print out Hello World with a 1 second delay.

.global myprog

.text
.set noreorder
.ent myprog 

myprog:
loop: 
    la      $a0,Serial
    la      $a1,hello
    jal     _ZN5Print7printlnEPKc        
    nop

    jal     mydelay
    nop

    j       loop
    nop

mydelay:
    li      $a2, 1000
    addi    $a2, $a2, -1
    bgez    mydelay     

    jr      $ra

.end myprog 

.data
hello:  .ascii "Hello, world!\0"

Basically running through this, it would print the first Hello World the first time, goes to "mydelay" when it hits that spot, in which mydelay would loop 1000 times (which should offer somewhat of a delay, not entirely sure about this part), and then it should return to the label loop, but currently all it does is print Hello World with no delay.


Solution

    1. You re-initialize $a2 with each iteration, so if your exit condition worked properly, the loop should be infinite.
    2. The above suggests that your conditional branch is not correct; maybe blez?
    3. Even if you do get your delay loop working, 1000 might be too small a count to be noticeable.