Search code examples
loopsassemblynestedmipsnested-loops

Nested Loop Clock is resulting in zeros, not the correct numbers in the MIPS assembly language


When I was trying to type in the code as exact like so:

.data
    clocksym: .asciiz ":"
    newl: .asciiz "\n"
.text
main:
    li $t0, 0
    li $t1, 0
    li $t2, 0
hour: #The first loop, hour
bgt $t0, 12, exodus #if hours > 12, the program ends
minutes: 
    bgt $t1, 59, hour
        seconds: 
            bgt $t2, 59, minutes

        li $v0, 1
        move $a0, $t0
        syscall

        li $v0, 4
        la $a0, clocksym
        syscall

        li $v0, 1
        move $a0, $t1
        syscall

        li $v0, 4
        la $a0, clocksym
        syscall

        li $v0, 1
        move $a0, $t2
        syscall

        addi $v0, $zero, 4
        la $a0, newl
        syscall

        b seconds
        addu $t1, $t1, 1

exodus

As a result I am getting an infinite loops of 0:0:0 I tried to fix it from 0-1:0:59, but my code has gotten worse with what I put. If anyone could instruct me on how to do this, it would be fine, just no psuedo codes. I just want a demonstration on this. Thank you.


Solution

  • I suggest that first you should write what you want to do in C (or other language you are good at)

    #include <stdio.h>
    
    int main(void) {
      int t0, t1, t2;
      t0 = 0;
      for(;;) { /* hour */
        if (t0 > 12) break;
        t1 = 0;
        for(;;) { /* minutes */
          if (t1 > 59) break;
          t2 = 0;
          for(;;) { /* seconds */
            if(t2 > 59) break;
            printf("%d", t0);
            printf(":");
            printf("%d", t1);
            printf(":");
            printf("%d", t2);
            printf("\n");
            t2 = t2 + 1;
          }
          t1 = t1 + 1;
        }
        t0 = t0 + 1;
      }
      return 0;
    }
    

    then translate it into assembly code.

    .data
        clocksym: .asciiz ":"
        newl: .asciiz "\n"
    .text
    .globl main
    main:
        addiu $t4, $zero, 12 # the limit of hour
        addiu $t5, $zero, 59 # the limit of minute & second
        addu $t0, $zero, $zero
        j hour_begin
        sll $zero, $zero, 0 # nop to avoid addiu after j being executed
    hour:
        addiu $t0, $t0, 1
    hour_begin:
        slt $t3, $t4, $t0
        bne $t3, $zero, exodus
        addu $t1, $zero, $zero
        j minutes_begin
        sll $zero, $zero, 0 # nop to avoid addiu after j being executed
    minutes:
        addiu $t1, $t1, 1
    minutes_begin:
        slt $t3, $t5, $t1
        bne $t3, $zero, hour
        addu $t2, $zero, $zero
        j seconds_begin
        sll $zero, $zero, 0 # nop to avoid addiu after j being executed
    seconds:
        addiu $t2, $t2, 1
    seconds_begin:
        slt $t3, $t5, $t2
        bne $t3, $zero, minutes
        addiu $v0, $zero, 1
        addu $a0, $t0, $zero
        syscall
        addiu $v0, $zero, 4
        la $a0, clocksym
        syscall
        addiu $v0, $zero, 1
        addu $a0, $t1, $zero
        syscall
        addiu $v0, $zero, 4
        la $a0, clocksym
        syscall
        addiu $v0, $zero, 1
        addu $a0, $t2, $zero
        syscall
        addiu $v0, $zero, 4
        la $a0, newl
        syscall
        j seconds
    exodus:
        addiu $v0, $zero, 10
        syscall