Search code examples
loopsassemblycountmipspic32

Delay Loop in MIPS


I am having trouble writing a delay loop that will return a constant 0x80000. The output should be like Hello, world! 0 Hello, world! 1 Hello, world! 2 ... but when I run my program, the terminal doesn't show anything even though I believe one Hello, world! should be showing up. I tried to figure out what is wrong by debugging the code, but that does not seem to be helping me. Any suggestions on how to fix this?

.ent getDelay
.text
.global getDelay

getDelay:
addi $sp, $sp, -1
sw $ra, 0($sp)
la $a0, helloStr
lw $a1, counter 
jal printf
nop
lw $ra, 0($sp)
addi $sp, $sp, -1
lw $t0, ($a1)
addiu $t0, $t0,1
la $t1, counter
sw $t1, ($a1) 
$v0 = 0x80000
jr $ra

.end getDelay

.data
helloStr: .asciiz "Hello, world %d\n"
counter: .word 100

Solution

    1. You should only ever adjust $sp in multiples of 4 (the word size). You should use addiu $sp, $sp, -4 and addiu $sp, $sp, 4.
    2. You are incrementing $t0 but then storing $t1. You don't need the la $t1, counter and instead of sw $t1, ($a1) you should use sw $t0, ($a1).
    3. $v0 = 0x80000 is not an instruction, you probably want li $v0, 0x80000.
    4. If this function itself is supposed to be some delay, then you need a loop in it.