简体   繁体   English

嵌套循环时钟会导致零,而不是MIPS汇编语言中的正确数字

[英]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. 结果,我遇到了0:0:0的无限循环,我试图从0-1:0:59修复它,但是我的代码变得更糟了。 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. 谢谢。

I suggest that first you should write what you want to do in C (or other language you are good at) 我建议您首先应该用C(或您擅长的其他语言)编写想要做的事情

#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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM