MIPS汇编while循环 [英] MIPS Assembly while loop

查看:555
本文介绍了MIPS汇编while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习MIPS,在做一些练习时,我遇到了一个练习,即首先用Java编写斐波那契序列,然后将其转换为MIPS Assembly.我只能使用beqbneslt 我的Java代码如下:

I am currently learning MIPS and while doing some exercises I came across one which is to write the Fibonacci sequence first in Java then convert it to MIPS Assembly. I only can use beq, bne and slt My Java code is the following:

    int n = 50; F_MAX  v
    int t1 = 0;
    int t2 = 1;
    int sum = 0;
    while(t1 <= n)
    {
      System.out.print(t1 + " ")
      sum = t1 + t2
      t1 = t2
      t2 = sum;
     }

意思是,如果n = 50,则应打印50之前的所有数字(0; 1; 1; 2; 3; 5; 8; 13; 21; 34)

Meaning that if n = 50 if should print all the numbers before 50 (0; 1; 1; 2; 3; 5; 8; 13; 21; 34)

我的MIPS汇编代码为:

My MIPS Assembly code is:

      la $s0, F_MAX
      lw $s0, 0($s0)         #$s0 = int n = F_MAX (50);



  addi  $t1, $zero, 0     # $t1 = int t1 = 0;
  addi  $t2, $zero, 1     # $t2 = int t2 = 1;
  addi  $t3, $zero, 0     # $t3 = int sum = 0


  while:

        beq $t1, $s0, Exit  #if t1 == 50 exit the program

        addi $v0, $zero, 1      # syscall code to print integer
        add $a0, $zero, $t1     # t1 to be printed
        syscall                 # print t1

        add $t3, $t1, $t2
        addi $t1, $t2, 0
        addi $t2, $t3, 0

        addi  $v0, $zero, 4     # syscall code to print a string
        la  $a0, COMMA
        syscall                 # print a comma (and a space)


        j while

Exit:
li $v0, 10
syscall

但是由于某种原因,它使我产生溢出并打印所有可能的正数,但我不知道为什么.

But for some reason, it gives me overflow and prints all the possible positive numbers and I cannot figure out why.

推荐答案

所以这是更新的代码: 由于从未满足过t1 == 50的条件,因此alwasys是错误的,因此我必须使用sltbne指令.

So here is the updated code: As the condition t1== 50 never was met it alwasys was false I had to use slt and bne instructions.

     la $s0, F_MAX
     lw $s0, 0($s0)                    #$s0 = int n = F_MAX (50);



     addi  $t1, $zero, 0               # $t1 = int t1 = 0;
     addi  $t2, $zero, 1               # $t2 = int t2 = 1;
     addi  $t3, $zero, 0               # $t3 = int sum = 0


           while:



           addi $v0, $zero, 1           # syscall code to print integer
           add $a0, $zero, $t1          # t1 to be printed
           syscall                      # print t1

           add $t3, $t1, $t2
           addi $t1, $t2, 0
           addi $t2, $t3, 0

           addi  $v0, $zero, 4          # syscall code to print a string
           la  $a0, COMMA
           syscall                      # print a comma (and a space)

           slt $t4, $t1, $s0            #if t1 < 50 $t4 = 1
           bne $t4, $zero, while        #if $t4 != $zero goto while

           slt $t4, $s0, $t1            #if 50 < $t1 $t4 = 1
           bne $t4, $zero, Exit         #if $t4 != $zero goto Exit

  Exit:
  li $v0, 10
  syscall

这篇关于MIPS汇编while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆