Soaps
Soaps

Reputation: 23

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.

Upvotes: 2

Views: 3697

Answers (2)

Jeff
Jeff

Reputation: 7674

This seems like a really bad approach for invoking a reliable delay. Always look for a syscall to see if there's something that already does it for you.

If you're using MARS, this will delay by 1000 milliseconds:

li $v0, 32
li $a0, 1000
syscall

Upvotes: 1

Scott Hunter
Scott Hunter

Reputation: 49803

  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.

Upvotes: 0

Related Questions