아래에 MIPS 코드를 게시했습니다. p가있을 때 내가 줄 추가 한MIPS 입력 된 숫자 아래의 모든 소수를 찾는 어셈블리 코드
for (int i=2;i<n;i++){
p = 0;
for (int j=2;j<i;j++){
if (i % j == 0)
p = 1;
}
if (p = 0) System.out.println(i);
}
I가에를 근거로하고 자바 코드 ...
자바 코드 ... "BEQ의 $의 T3, 1, L4는"L4로 건너 뜁니다 시간을 절약하려면 1로 설정하십시오. 그러나이 코드 줄을 추가하면 프로그램에서 아무 것도 출력하지 않습니다. 이 줄을 추가하기 전에 모든 정수를 2 ~ n으로 출력합니다.
MIPS 코드 ...
# The number is read through the keyboard
.text
.globl main
main:
# Display message to user for a number
li $v0, 4
la $a0, prompt1
syscall
# read keyboard into $v0 (number x is upper bound number to find primes)
li $v0, 5
syscall
# move the number from $v0 to $t0
move $t0, $v0 # $t0 = n
# store 2 in $t1 and $t2
li $t1, 2 # i
li $t2, 2 # j
L3: # for (int i=2; i<n; i++)
# store 0 in $t3
li $t3, 0 # p = 0;
L2: # for (int j=2; j<i; j++)
# do div of two numbers
div $t2, $t1
# store the remainder in $t4
mfhi $t4
# branch if remainder is not 0 to L1
bne $t4, 0, L1 # if (i % j == 0)
# set $t3 as 1
li $t3, 1 # p = 1
# if p=1 break to next i
beq $t3, 1, L4
L1: # if (i % j == 0)
# add 1 to t2
addi $t2, $t2, 1 # j++
# repeat code while j < i
ble $t2, $t1, L2
# print integer function call 1
# put the answer into $a0
li $v0, 1
move $a0, $t1
syscall # System.out.println(i)
#print comma
li $v0, 4
la $a0, comma
syscall
L4:
# add 1 to t1
addi $t1, $t1, 1 # i++
# repeat code while i < n
ble $t1, $t0, L3 # for (int i=2; i<n; i++)
.data
prompt1:
.asciiz "Enter a number "
comma:
.asciiz ","
나는 나의 조립 논리가 for 루프의 J < 난 부분 차지하지 않기 때문에 오류가 발생 생각합니다. 나는 올바른 길을 가고 있는가?
고맙습니다. 나는 무조건적인 가지를 살펴볼 것이다. 그것은 그것이 무엇이든 상관없이 뛰어날 것인가? @ 호슈아 리 정확하게 –
. 실제로 두 가지 방법이 있습니다 : 무조건 부 분 즉 "b L4"와 무조건 부 점프 즉 "J L4". 코딩 측면에서 볼 때 그들은 동일하지만 두드러진 것들로 인해 서로 다른 점이 있습니다. 차이점에 대한 자세한 내용은 다음을 참조하십시오. http://stackoverflow.com/questions/10981593/what-is-the-difference-between-unconditional-branch-and-unconditional-jump-inst – theKunz