简体   繁体   中英

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:\n");

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

   printf("Enter another integer:\n");

   //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:\n");
   for (j = 0; j < numbers_len; j = j+1)
     {
          printf("%d\n", 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\n"
    prompt2: .asciiz "Enter another integer\n"
    arrayContent: .asciiz "Result Array Content\n"
    newLine: .asciiz "\n"


.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.

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\n", 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
###############################################################

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM