将计算出的整数值添加到我的MIP中的数组中

发布于 2025-01-21 12:45:31 字数 4190 浏览 1 评论 0原文

我尝试将我的数字添加到MIP中的数组中

当我运行程序时, MIPS代码和我用来翻译

import java.util.Scanner;

public class PrimeDivisors {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter n: ");
        int n = input.nextInt();
        int factorial = 1;
        for (int i = 1; i <= n; i++) {
            factorial *= i;
        }
        System.out.println(n + "! = " + factorial);
        int[] primes = findPrimeDivisors(factorial);
        largestPower(primes, n);
    }

    public static void largestPower(int[] primes, int n) {
        System.out.println("Prime divisor\tLargest Power");
        int largestPower = 0;
        for (int i = 0; i < primes.length; i++) {
            if (primes[i] == 0) {
                break;
            }
            int primeNum = primes[i];
            System.out.print("\t" + primeNum);
            int hold = n;
            while (hold > 0) {
                hold /= primeNum;
                largestPower += hold;
            }
            System.out.print("\t\t\t\t" + largestPower + "\n");
            largestPower = 0;
        }
    }

    public static int[] findPrimeDivisors(int factorial) {
        int[] primes = new int[1000000];
        int index = 0;
        System.out.print("Prime Divisors of " + factorial + " = ");

        for (int i = 2; i < factorial; i++) {
            while (factorial % i == 0) {
                primes[index] = i;
                factorial /= i;
            }
            index++;
        }
        if(factorial > 2){
            primes[index-1] = factorial;
        }
        for(int i = 0; i < primes.length; i++){
            if(primes[i+1] == 0){
                System.out.print(primes[i]);
                break;
            } else {
                System.out.print(primes[i] + ", ");
            }
        }
        System.out.println();
        return primes;
    }
}
.data
primeArray: .space 120
prompt: .asciiz "Enter n: "
prompt2: .asciiz "Prime Divisor\tLarges Power"
prompt3: .asciiz "Prime Divisors of "
comma: .asciiz ", "
equals: .asciiz " = "
period: .asciiz ".\n"
exclamation: .asciiz "! = "
tab: .asciiz "\t"
tabs: .asciiz "\t\t\t\t"
newLine: .asciiz "\n"
input_number: .space 32 #Space for user input


.text
.globl main

main:
    #Print the prompt
    li $v0, 4
    la $a0, prompt #Address of prompt
    syscall
    
    #User Input
    li $v0, 5
    syscall

    #Store user input into $t0
    move $t0, $v0
    
    li $t1, 0 #int i = 1
    li $t2, 1 #factorial = 1
    jal calculateFactorial
    
    #print user input
    li $v0, 1
    move $a0, $t0
    syscall
    #print exclamation and equals
    li $v0, 4
    la $a0, exclamation
    syscall
    #print factorial value
    li $v0, 1
    move $a0, $t2
    syscall
    #print period
    li $v0, 4
    la $a0, period
    syscall
    
    jal findPrimeDivisors
    
    #print factorial value
    li $v0, 1
    move $a0, $t2
    syscall
    #exit program
    li $v0, 10
    syscall

findPrimeDivisors:
    #Print prompt3
    li $v0, 4
    la $a0, prompt3
    syscall
    #print factorial value
    li $v0, 1
    move $a0, $t2
    syscall
    #Print equals
    li $v0, 4
    la $a0, equals
    syscall
    
    addi $t3, $zero, 0 #Array index = 0
    li $t1, 2 #Index I=2
    la $s1, primeArray
    jal findPrimes
    
    #exit program
    li $v0, 10
    syscall

#input={$t0} i={$t1}, factorial={$t2}, arrayIndex={$t3}
findPrimes: #outer loop
    bgt $t1, $t2 exitLoop
    sw $ra, ($sp)
    jal innerLoop #innerLoop
    lw $ra, ($sp)
    
    addi $t1, $t1, 2 #increment outer loop by 2
    addi $t3, $t3, 4 #increment array index in outside loop
    
    j findPrimes

#remainder = ${t4}
innerLoop:
    bne $t4, 0, exitLoop #Break if remainder is not equal
    sw $t1, primeArray($t3) #Store value of I into array
    
    div $t2, $t2, $t1 #Factorial = Factorial / I
    mfhi $t4
    j innerLoop
    

#input={$t0} i={$t1}, factorial={$t2}
calculateFactorial:
    beq $t1, $t0, exitLoop
    addi $t1, $t1, 1
    mul $t2, $t1, $t2
    j calculateFactorial
exitLoop:
    jr $ra

任何帮助的代码,以说明为什么我无法访问0索引。

When I run the program I try adding my numbers to my array in mips but the only index in the array that is getting filled is when my index value is = 0, the other numbers are all showing as 0.

Edit: This is my full mips code and the code I used to translate

import java.util.Scanner;

public class PrimeDivisors {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter n: ");
        int n = input.nextInt();
        int factorial = 1;
        for (int i = 1; i <= n; i++) {
            factorial *= i;
        }
        System.out.println(n + "! = " + factorial);
        int[] primes = findPrimeDivisors(factorial);
        largestPower(primes, n);
    }

    public static void largestPower(int[] primes, int n) {
        System.out.println("Prime divisor\tLargest Power");
        int largestPower = 0;
        for (int i = 0; i < primes.length; i++) {
            if (primes[i] == 0) {
                break;
            }
            int primeNum = primes[i];
            System.out.print("\t" + primeNum);
            int hold = n;
            while (hold > 0) {
                hold /= primeNum;
                largestPower += hold;
            }
            System.out.print("\t\t\t\t" + largestPower + "\n");
            largestPower = 0;
        }
    }

    public static int[] findPrimeDivisors(int factorial) {
        int[] primes = new int[1000000];
        int index = 0;
        System.out.print("Prime Divisors of " + factorial + " = ");

        for (int i = 2; i < factorial; i++) {
            while (factorial % i == 0) {
                primes[index] = i;
                factorial /= i;
            }
            index++;
        }
        if(factorial > 2){
            primes[index-1] = factorial;
        }
        for(int i = 0; i < primes.length; i++){
            if(primes[i+1] == 0){
                System.out.print(primes[i]);
                break;
            } else {
                System.out.print(primes[i] + ", ");
            }
        }
        System.out.println();
        return primes;
    }
}
.data
primeArray: .space 120
prompt: .asciiz "Enter n: "
prompt2: .asciiz "Prime Divisor\tLarges Power"
prompt3: .asciiz "Prime Divisors of "
comma: .asciiz ", "
equals: .asciiz " = "
period: .asciiz ".\n"
exclamation: .asciiz "! = "
tab: .asciiz "\t"
tabs: .asciiz "\t\t\t\t"
newLine: .asciiz "\n"
input_number: .space 32 #Space for user input


.text
.globl main

main:
    #Print the prompt
    li $v0, 4
    la $a0, prompt #Address of prompt
    syscall
    
    #User Input
    li $v0, 5
    syscall

    #Store user input into $t0
    move $t0, $v0
    
    li $t1, 0 #int i = 1
    li $t2, 1 #factorial = 1
    jal calculateFactorial
    
    #print user input
    li $v0, 1
    move $a0, $t0
    syscall
    #print exclamation and equals
    li $v0, 4
    la $a0, exclamation
    syscall
    #print factorial value
    li $v0, 1
    move $a0, $t2
    syscall
    #print period
    li $v0, 4
    la $a0, period
    syscall
    
    jal findPrimeDivisors
    
    #print factorial value
    li $v0, 1
    move $a0, $t2
    syscall
    #exit program
    li $v0, 10
    syscall

findPrimeDivisors:
    #Print prompt3
    li $v0, 4
    la $a0, prompt3
    syscall
    #print factorial value
    li $v0, 1
    move $a0, $t2
    syscall
    #Print equals
    li $v0, 4
    la $a0, equals
    syscall
    
    addi $t3, $zero, 0 #Array index = 0
    li $t1, 2 #Index I=2
    la $s1, primeArray
    jal findPrimes
    
    #exit program
    li $v0, 10
    syscall

#input={$t0} i={$t1}, factorial={$t2}, arrayIndex={$t3}
findPrimes: #outer loop
    bgt $t1, $t2 exitLoop
    sw $ra, ($sp)
    jal innerLoop #innerLoop
    lw $ra, ($sp)
    
    addi $t1, $t1, 2 #increment outer loop by 2
    addi $t3, $t3, 4 #increment array index in outside loop
    
    j findPrimes

#remainder = ${t4}
innerLoop:
    bne $t4, 0, exitLoop #Break if remainder is not equal
    sw $t1, primeArray($t3) #Store value of I into array
    
    div $t2, $t2, $t1 #Factorial = Factorial / I
    mfhi $t4
    j innerLoop
    

#input={$t0} i={$t1}, factorial={$t2}
calculateFactorial:
    beq $t1, $t0, exitLoop
    addi $t1, $t1, 1
    mul $t2, $t1, $t2
    j calculateFactorial
exitLoop:
    jr $ra

Any help for why I am not able to access any index past 0 would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

极度宠爱 2025-01-28 12:45:31

您从不初始化$ t4。&nbsp;因此,第一次bne $ t4,0,exitloop掉落,因为$ t4是由模拟器初始化为零的。&nbsp;此后,它是“初始化”到其故意的最后一个值,因为这是内部环路退出的唯一途径。

该内部循环也不会修改$ t3,因此,如果它提出了多个值,则将它们全部存储在同一位置。

从根本上讲,您犯了一个错误,可能是在翻译为汇编到集会期间偏离了高级语言代码。这始终是一个易于努力的错误。&nbsp;如果您想进行更改(例如要改进或优化),则首先在高级或伪代码中进行此操作,并对其进行测试以确保其有效。&nbsp; nbsp;然后忠实地将伪代码转录为组装。

在组件中,您引入了一个附加功能,并在这些新功能中移动了伪代码循环的一部分,同时将同一环路的其他部分留在外面。&nbsp;这不是基于逻辑等价的转换。&nbsp;因此,没有理由期望装配代码与伪/Java代码相同。&nbsp;虽然您可能认为引入较小的功能进行工作的部分是逻辑上的转换,但您已经将主循环的工作划分为新功能(S)及其呼叫者。


您有许多违反惯例的呼叫违规行为,但是如果否则有效,这可能对您来说并不是问题。

You never initialize $t4.  So, the first time the bne $t4, 0, exitLoop falls through, because $t4 is initialized to zero by the simulator.  Thereafter, it is "initialized" to the last value it took on which was, on purpose, a non-zero value, since that is the only way that the inner loop exits.

That inner loop also does not modify $t3, so if it ever comes up with multiple values, it will store them all to the same place.

Fundamentally, you have made the mistake of having presumably working high-level language code, but chosen to deviate from that during the translation to assembly.  This is always an error prone endeavor.  If you want to make changes (e.g. to improve or optimize) then do that in your high-level or pseudo code first, and test it to make sure it works.  Then transcribe faithfully the pseudo code to assembly.

In the assembly you have introduced an additional function, and moved parts of the pseudo code's loop inside those new function(s), while leaving other parts of the same loop outside.  This is not a transformation based on logical equivalence.  So, there's no reason to expect the assembly code to work the same as the pseudo/Java code.  While you might think introducing smaller functions to do parts of work is a logical transformation, you have split the work of the main loop incorrectly over the new function(s) and its caller.


You have a number of calling convention violations, but if it otherwise works, that may not be of issue to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文