C代码转MIPS代码时数组更新失效问题求助
Fixing Your MIPS Array Loop Implementation
Let's break down the issues in your current code and fix them step by step—right now your program isn't updating the array correctly because of several key mistakes.
Key Issues in Your Current Code
- Incorrect Array Indexing: You're using fixed offsets (
$zeroand4) instead of calculating offsets based on your loop counteri($s4). This means every iteration only operates onA[0]andA[1], never progressing through the array as intended. - Wrong Multiplication Logic: In the
ifbranch, you're repeatedly adding$t5(which holdsdiff) to itself—this doesn't compute5*A[i]at all. You need to use the actual value ofA[i](stored in$t3) for the multiplication. - Broken Else Branch: The code here is incomplete and incorrect. You aren't calculating
-5*A[i]properly, nor are you storing it to the correctA[i+1]location.
Corrected MIPS Code
Here's the fixed version, with comments explaining each critical change:
### MIPS PROJECT PART 1: ARRAY USING FOR LOOPS .data # allocate 16 bytes memory for 4 integer array A: .space 16 .text # Store array values in registers addi $s0,$zero,2 addi $s1,$zero,4 addi $s2,$zero,6 addi $s3,$zero,8 # Index = $t0 addi $t0,$zero,0 # Store the first index and then store others by increasing $t0 by 4 bytes sw $s0,A($t0) addi $t0,$t0,4 sw $s1,A($t0) addi $t0,$t0,4 sw $s2,A($t0) addi $t0,$t0,4 sw $s3,A($t0) main: li $s4, 0 # counter i = $s4 (starts at 0) li $t2, 3 # loop runs while i < 3 loop: beq $s4, $t2, end # exit loop when i == 3 # Calculate byte offsets for A[i] and A[i+1] sll $t1, $s4, 2 # $t1 = i * 4 (each int is 4 bytes; shift left 2 = multiply by 4) add $t6, $t1, 4 # $t6 = (i+1)*4, offset for A[i+1] # Load values from array lw $t3, A($t1) # $t3 = A[i] lw $t4, A($t6) # $t4 = A[i+1] # Compute diff = A[i+1] - A[i] sub $t5, $t4, $t3 # $t5 = diff # Check if diff > 0: jump to Else if diff is <= 0 blez $t5, Else # blez checks if $t5 is less than or equal to 0 # If diff > 0: A[i] = 5*A[i] (using repeated addition since mult is forbidden) add $t7, $t3, $t3 # $t7 = 2*A[i] add $t7, $t7, $t3 # $t7 = 3*A[i] add $t7, $t7, $t3 # $t7 = 4*A[i] add $t7, $t7, $t3 # $t7 = 5*A[i] sw $t7, A($t1) # Store updated value back to A[i] j LoopEnd # Skip the else branch Else: # Else: A[i+1] = -5*A[i] add $t7, $t3, $t3 # Compute 5*A[i] via repeated addition add $t7, $t7, $t3 add $t7, $t7, $t3 add $t7, $t7, $t3 sub $t7, $zero, $t7 # Negate to get -5*A[i] sw $t7, A($t6) # Store updated value back to A[i+1] LoopEnd: addi $s4, $s4, 1 # increment loop counter i j loop # jump back to loop start end: li $v0,10 syscall
Explanations of Key Fixes
- Dynamic Index Calculation: We use
sll $t1, $s4, 2to compute the byte offset forA[i](since each integer takes 4 bytes, shifting left by 2 bits is equivalent to multiplying by 4). Adding 4 to this offset gives us the position ofA[i+1]. - Correct Multiplication: Instead of using
diff, we use the actual value ofA[i]($t3) to compute 5x via repeated addition. For-5*A[i], we first calculate 5x then negate the result withsub $t7, $zero, $t7. - Proper Branching: After handling the
ifcase, we jump toLoopEndto avoid executing the else code. The else branch now correctly stores the computed value toA[i+1]. - Clean Loop Flow: The loop counter is incremented at the end of each iteration, ensuring we progress through all valid
ivalues (0, 1, 2).
This code will now correctly iterate through the array, compute diff for each adjacent pair, and update the array elements exactly as your original C code specifies.
内容的提问来源于stack exchange,提问作者Habil Ganbarli
相关产品推荐
相关产品推荐

