MIPS:接受用户输入值并使用循环计算指数的程序

问题描述

所以我写出了整个程序,我的循环用于计算指数在自己的程序中工作,但是当我逐步执行该程序时,它们并没有按照我希望的方式多次运行并输出程序给出的全为0。

这是代码: 编辑:我已经将代码更新为我目前拥有的代码,最后添加了除法功能。目前,我正在为前三个循环集获取适当的值。这意味着我得到了f的正确值,但是g的第二个分量d ^ w没有吐出正确的值。

 ##designed and written by Aidan Rosser-Jones
.data #data needed
    A : .word 0
    B : .word 0
    C : .word 0
    D : .word 0 ## A-D are the base values that the user inputs
    x : .word 8
    y : .word 5
    z : .word 7
    w : .word 5 ## x-w are the exponents with hardcoded student ID numbers

    userPrompt : .asciiz "Please enter four integers seperated by an enter key." ## prompting the user
    F          : .asciiz "f(decimal) : " ## result for F in base 10
    binaryF    : .asciiz "f(binary)  : " ## result for F in base 2
    G          : .asciiz "g(decimal) : " ## result for G in base 10
    binaryG    : .asciiz "g(binary)  : " ## result for G in base 2
    quotientH  : .asciiz "h quotient : " ## quotient result for H
    remainderH : .asciiz "h remainder : " # remainder result for H
    skipLine   : .asciiz "\n"            ## for printing a new line
    
.text ##switching back to code section
    
    .globl main
        main:
            #first we must prompt the user for the four integers
            la $a0,userPrompt #loading the prompt from data section into $a0
            li $v0,4          #syscall for printing a string
            syscall            # this actually does the printing
            
            #Now we gotta read the first integer
            li $v0,5
            syscall
            
            lw $s4,A          #loading the address of variable a into $t0
            move $s4,$v0      #copying the user input to $t0
            
            #time to read the second integer (basically second verse,same as the first for the next 3)
            li $v0,5
            syscall
            
            lw $s5,B
            move $s5,$v0
            
            #3rd integer
            li $v0,5
            syscall
            
            lw $s6,C
            move $s6,$v0
            
            #4th integer
            li $v0,5
            syscall
            
            lw $s7,D
            move $s7,$v0
            
            #time to do some calculations
            #calculating a^x
            move $t1,$s4           #loading address of A into $t1
            ##lw $t1,($t1)       #loading the value of A into $t1
            
            la $t2,x           #loading address of x into $t2
            lw $t2,($t2)       #loading the value of x into $t2
            
            #loop prep
            move $t5,$t1       #answer = base
            move $t6,$t1       #increment = base
            li $t3,1           # i = 1
            li $t4,1           # j = 1
            
        For0:   
                ble $t2,$t3,Exit0 #for(i =1; i<x; i++)
                addi $t3,1    #i++
                li $t4,1           #resesting j to 1 so nested loop works properly
                move $t6,$t5
                j For1
                
        For1:  
                ble $t1,$t4,For0  #for(j=1; j < A; j++)
                add $t5,$t5,$t6   #answer+=increment              
                addi $t4,1    #j++
                j For1
                
        Exit0:
                move $s0,$t5       #storing a^x in $s0
                
                #calculating b^y
            move $t1,$s5           #loading address of B into $t1
            ##lw $t1,($t1)       #loading the value of B into $t1
            
            la $t2,y           #loading address of y into $t2
            lw $t2,($t2)       #loading the value of y into $t2
            
            #loop prep
            move $t5,1           # j = 1
            
        For2:   
                ble $t2,Exit1 #for(i =1; i<y; i++)
                addi $t3,$t5
                j For3
                
        For3:  
                ble $t1,For2  #for(j=1; j < B; j++)
                add $t5,$t6   #answer+=increment
                addi $t4,1    #j++
                j For3
                
        Exit1:
                move $s1,$t5       #storing b^y in $s0
                
                #calculating c^z
                move $t1,$s6           #loading address of C into $t1
            ##lw $t1,($t1)       #loading the value of C into $t1
            
            la $t2,z           #loading address of z into $t2
            lw $t2,($t2)       #loading the value of z into $t2
            
            #loop prep
            move $t5,1           # j = 1
            
        For4:   
                ble $t2,Exit2 #for(i =1; i<z; i++)
                addi $t3,$t5
                j For5
                
        For5:  
                ble $t1,For4  #for(j=1; j < c; j++)
                add $t5,1    #j++
                j For5
                
        Exit2:
                move $s2,$t5       #storing c^z in $s0
                
                #calculating d^w
                move $t1,$s6           #loading address of D into $t1
            ##lw $t1,($t1)       #loading the value of D into $t1
            
            la $t2,w           #loading address of w into $t2
            lw $t2,($t2)       #loading the value of w into $t2
            
            #loop prep
            move $t5,1           # j = 1
            
        For6:   
                ble $t2,Exit3 #for(i =1; i<w; i++)
                addi $t3,$t5
                j For7
                
        For7:  
                ble $t1,For6  #for(j=1; j < D; j++)
                add $t5,1    #j++
                j For7
                
        Exit3:
                move $s3,$t5       #storing d^w in $s3
                
                ##THIS IS FOR TESTING PURPOSES ONLY
                li $v0,1
                move $a0,$s3
                syscall
                ##CONCLUDING TEST CODE
                
                #calculating and storing f = a^x + b^y
                add $s0,$s0,$s1   #f = $s0
                
                #calculating and storing g = c^z + d^w
                add $s2,$s2,$s3   #g = $s2
                
                #Now lets print out all of the data,starting with a fresh line to avoid confusion!
                la $a0,skipLine
                li $v0,4
                syscall             #skips a line
                
                #printing f
                la $a0,F
                li $v0,4
                syscall
                
                li $v0,$s0
                syscall             #print f in decimal 
                
                #printing new line for organization
                la $a0,4
                syscall             #skips a line
                
                la $a0,binaryF
                li $v0,35
                move $a0,$s0
                syscall             #this should print the binary f
                
                #printing new line for organization
                la $a0,4
                syscall             #skips a line
                
                #lets print g!
                la $a0,G
                li $v0,$s2
                syscall             #this should print decimal g
                
                #printing new line for organization
                la $a0,4
                syscall             #skips a line
                
                #lets print g but binary this time!
                la $a0,binaryG
                li $v0,$s2
                syscall
                
                #Now we must calculate h = f/g
                #division prep
                # f = $s0
                # g = $s2
                add $t0,$zero,$s0 #copies the value of f into $t0
                add $t1,$s1 #copies the value of g into $t1
                add $t2,$zero #make $t2 the counter starting at 0
          
    div_loop:
            sle $t3,$t2,$t1 #checks if equal
            beq $t3,end_divis
            sub $t0,$t0,$t1
            addi $t2,1 
            j div_loop
    end_divis:
            move $s6,$t2 # stores quotient in $s6
            move $s7,$t0 # stores remainder in $s7
            
            #Now its time to print the values for h
            #printing new line for organization
                la $a0,quotientH
                li $v0,4
                syscall       # prints quotientH string
                
                li $v0,$s6
                syscall   #print integer value for quotient H
                
                #printing new line for organization
                la $a0,remainderH
                li $v0,4
                syscall    #print remainder H string
                
                li $v0,$s7
                syscall   #print integer value for remainder H
                
                #terminate the program
                li $v0,10
                syscall

如果有些评论不对,我深表歉意。我复制并粘贴了循环,然后更改了计算的存储位置和循环名称(出于跳转目的)以实现所需的功能

当前我的输出如下:

请输入四个用Enter键分隔的整数。2 3 1个 2 1(这是d ^ w值的测试打印) f(十进制):499 f(二进制):00000000000000000000000111110011 g(十进制):2 g(二进制):00000000000000000000000000000010 商:244 h余数:-58793 -程序运行完毕-

如前所述,问题与我的循环有关,但是我一直盯着它看了一段时间,无法弄清楚是什么原因。由于我不到一周的学习MIPS的经验,任何帮助将不胜感激。

作为参考,这些循环是根据我编写的以下Java代码建模的:

/*
 * Written by Aidan Rosser-Jones
 */
public class exponentLoop {
    public static final int BASE = 5;
    public static final int POWER = 6;
    public static void main(String[] args) {
        int answer = BASE;
        int increment = BASE;
        for(int i = 1; i < POWER; i++)
        {
            for(int j = 1; j < BASE; j++)
            {
                answer += increment;
            }
            increment = answer;
        }
        System.out.println(answer);

    }

}

**对于这个项目,我们不允许使用任何种类的乘法指令或函数,这就是为什么代码以这种方式格式化

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...