Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

c - Iterating through array, numbers, and manipulting numbers[i] based on conditional in MIPS

I am trying to recreate C code in MIPs and am having trouble. Here is the code I am trying to recreate

   int numbers_len = 10;
   int numbers[10] = {23, -7, 15, -17, 11, -4, 23, -26, 27, 8};


   int num1, num2, temp;
   int j;
     
   printf("Enter an integer:
");

   //read an integer from a user input and store it in num1
   scanf("%d", &num1);

   printf("Enter another integer:
");

   //read an integer from a user input and store it in num2
   scanf("%d", &num2);
     

   //changing the array content 
   for (j = 0; j < numbers_len; j = j+1)
       {
          if (numbers[j] < (num1+num2))
           {
            numbers[j] = numbers[j] + num1 - num2;
           }
       }
     

   printf("Result Array Content:
");
   for (j = 0; j < numbers_len; j = j+1)
     {
          printf("%d
", numbers[j]);
     } 

Here is my code:

.data
    numbers_len: .word 10
    numbers: .word 23, -7, 15, -17, 11, -4, 23, -26, 27, 8
    prompt: .asciiz "Enter an integer
"
    prompt2: .asciiz "Enter another integer
"
    arrayContent: .asciiz "Result Array Content
"
    newLine: .asciiz "
"


.text
.globl main

main:
###############################################################
    li $v0, 4
    la $a0, prompt # prompt of num 1
    syscall

    li $v0, 5
    syscall # scan in num 1
    move $s0, $v0 
###############################################################
###############################################################
    li $v0, 4
    la $a0 prompt2 # prompt of num 2
    syscall

    li $v0, 5
    syscall # scan in num 2
    move $s1, $v0
###############################################################
    lw $s2, numbers_len #s2 = numbers_len = 10
    la $t1, numbers # t1 = base address of numbers
###############################################################
    li $v0, 4
    la $a0, arrayContent
    syscall
###############################################################
fun:
    bge $t0, 10, exit # conditional 
    lw $t2, 0($t1) # t2 = numbers[i]
    addi $t1, $t1, 4 # move down numbers array
    add $t3, $s0, $s1 # t3 = (num1+num2)

    bge $t2, $t3, print # if (numbers[i] < t3 ) go on, else print
    add $t2, $t2, $s0 # numbers[i] += num1
    sub $t2, $t2, $s1 # numbers[i] -= num2
    addi $t0, $t0, 1 # increment i
    j fun #loop
###############################################################
print:
    li $v0, 1
    move $a0, $t2
    syscall # print numbers[i]
    li $v0, 4
    la $a0, newLine
    syscall # print newline 
    j fun # go back to loop
###############################################################
exit:
    jr $ra
        

and here is my output

Enter an integer
15
Enter another integer
-5
Result Array Content
23
15
11
23
27
1702129221
1851859058
1953392928
1919248229
1850015754
544367988
1953459809
544367976
1702129257
175269223
1936019968
544500853
1634890305
1866670201
1852142702
167774836

I am not sure where I am going wrong with this. Maybe it has to do with the way that I increment and jump between the print and fun loops? It seems as if I am double incrementing, but I am not sure how.

question from:https://stackoverflow.com/questions/66060188/iterating-through-array-numbers-and-manipulting-numbersi-based-on-conditiona

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You actually implementing a different C code which is somehting like this instead of the 2 last for loop:

// changing the array content
for (j = 0; j < numbers_len;) {
  int temp = numbers[j];
  if (temp  < (num1 + num2)) {
    temp = temp + num1 - num2;
    j = j + 1;
    continue;
  } else {
    printf("%d
", temp);
  }
}

So there are quite a few problem like incrementing the loop counter conditionally or not really changing the content of the array.

With small changes I ended up with this, which seems to work:

fun:
    bge $t0, 10, exit # conditional 
    lw $t2, 0($t1) # t2 = numbers[i]
    addi $t1, $t1, 4 # move down numbers array
    add $t3, $s0, $s1 # t3 = (num1+num2)

    bge $t2, $t3, print # if (numbers[i] < t3 ) go on, else print
    add $t2, $t2, $s0 # numbers[i] += num1
    sub $t2, $t2, $s1 # numbers[i] -= num2
    sw $t2, -4($t1)   # saving the result to number[i]
###############################################################
print:
    li $v0, 1
    move $a0, $t2
    syscall # print numbers[i]
    li $v0, 4
    la $a0, newLine
    syscall # print newline 
    addi $t0, $t0, 1          # increment i
    j fun                     # loop
###############################################################

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...