Untitled
unknown
plain_text
9 months ago
2.7 kB
23
Indexable
main:
# Manually set base addresses
lui $s0, 0x1001 # Load upper 16 bits for A (0x10010000)
lui $s2, 0x1001 # Load upper 16 bits for P (0x10010020)
addi $s2, $s2, 32 # Adjust $s2 to point to P
# Initialize array A in memory
addi $t0, $zero, 10 # A[0] = 10
sw $t0, 0($s0)
addi $t0, $zero, 5 # A[1] = 5
sw $t0, 4($s0)
addi $t0, $zero, -5 # A[2] = -5
sw $t0, 8($s0)
addi $t0, $zero, -2 # A[3] = -2
sw $t0, 12($s0)
addi $t0, $zero, 0 # A[4] = 0
sw $t0, 16($s0)
# Initialize P array to zeros
addi $t0, $zero, 0 # Zero value for initialization
sw $t0, 0($s2)
sw $t0, 4($s2)
sw $t0, 8($s2)
sw $t0, 12($s2)
sw $t0, 16($s2)
# Set n = 5
addi $s1, $zero, 5 # $s1 = n
# Initialize P[0] = 1
addi $t0, $zero, 1 # $t0 = 1
sw $t0, 0($s2) # Store 1 at P[0]
# Initialize loop variables
addi $t1, $zero, 1 # $t1 = j (loop index)
addi $s3, $zero, 1 # $s3 = k (current length of P)
loop:
# Check if j < n
bge $t1, $s1, stop
# Compute A[j] address: offset = j * 4
sll $t3, $t1, 2 # Shift left by 2 (j * 4)
add $t3, $s0, $t3 # Address of A[j]
lw $t2, 0($t3) # $t2 = A[j]
# Call power function
move $a0, $t2 # $a0 = A[j]
move $a1, $t1 # $a1 = j
jal power
move $t4, $v0 # $t4 = result from power
# Call newElement function
move $a0, $s2 # $a0 = base address of P
move $a1, $s3 # $a1 = current size (k)
move $a2, $t4 # $a2 = new element
jal newElement
# Increment j and k
addi $t1, $t1, 1 # j++
addi $s3, $s3, 1 # k++
j loop
stop:
j stop # Infinite loop to stop the program
power:
# Inputs: $a0 = A[j], $a1 = j
# Output: $v0 = A[j]^j
addi $v0, $zero, 1 # Initialize result to 1
beq $a1, $zero, power_end # If j == 0, return 1
power_loop:
mult $v0, $a0 # Multiply $v0 by A[j]
mflo $v0 # Store result in $v0
addi $a1, $a1, -1 # j-- (replacing `sub`)
bne $a1, $zero, power_loop # Repeat until j == 0
power_end:
jr $ra # Return to caller
newElement:
# Inputs: $a0 = base address of P, $a1 = current size (k), $a2 = new element
sll $t0, $a1, 2 # Offset = k * 4
add $t0, $a0, $t0 # Address of P[k]
sw $a2, 0($t0) # Store new element at P[k]
jr $ra # Return to callerEditor is loading...
Leave a Comment