将java代码转换为mips
这段Java代码代表了寻找素数的eratosthenes方法将java代码转换为mips,java,mips,sieve-of-eratosthenes,Java,Mips,Sieve Of Eratosthenes,这段Java代码代表了寻找素数的eratosthenes方法 int n = 10; // initially assume all integers are prime boolean[] isPrime = new boolean[n + 1]; for (int i = 2; i <= n; i++) { isPrime[i] = true; } // mark non-primes <= N using Sieve of Eratosthenes for (int
int n = 10;
// initially assume all integers are prime
boolean[] isPrime = new boolean[n + 1];
for (int i = 2; i <= n; i++) {
isPrime[i] = true;
}
// mark non-primes <= N using Sieve of Eratosthenes
for (int i = 2; i*i <= n; i++) {
// if i is prime, then mark multiples of i as nonprime
// suffices to consider mutiples i, i+1, ..., N/i
if (isPrime[i]) {
for (int j = i; i*j <= n; j++) {
isPrime[i*j] = false;
}
}
}
int n=10;
//最初假设所有整数都是素数
布尔值[]isPrime=新布尔值[n+1];
对于(int i=2;i),在访问数组[i*j]后,您缺少跳回innerLoop:
。您的s2
初始化在哪里?固定为$s1而不是$s2a0
。那么您的问题是什么?您已经使用lw
指令访问数组元素i,因为您的a0
指向数组基。这次只需使用s1
而不是t1
。我必须再次更改偏移量吗?ass如果你的i*j在s1
中,只需发出以下命令:mult$s1,$s1,4
,然后添加$s1,$s1,$a0
然后lw,0($s1)
.data
array: .word 0:1000000 # array of 0s where 0 = "prime", 1 = "not prime"
.globl main
main:
addi $t0, $t0, 2 # i = 2
outerLoop:
ble $s0, 10, Exit # if (i x i <= array.length)
mult $s0, $t0, $t0 # $s0 = i x i
addi $t0, $t0, 1 # i++
sll $t1, $t0, 2 # i x 4
add $t1, $a0, $t1 # &array[i]
lw $t1, 0($t1) # array[i]
bne $t1, $0, Exit # if array[i] is prime
innerLoop:
ble $s1, 10, Exit # if (i x j <= array.length)
move $t0, $t2 # j = i
mult $s1, $t0, $t2 # $s1 = i x j
# array[i x j]
j outerLoop
Exit: ...
.text