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