I am programming in a RISC-V 32I simulator called CompSim, and I am encountering an issue with the jal instruction in a specific part of my program. The program reads a multi-digit number from the keyboard, processes it, and translates it into register values. It works as expected until a specific point where the program seems to hang.
Here’s the relevant part of the code:
jal x1, beg_input
add x29, x0, x0
l2:
beq x28, x0, l3 # If x28 == 0, terminate the loop
addi x10, x0, 10 # x10 = 10 (base for multiplication)
add x11, x0, x28 # x11 = x28 (exponent of 10)
jal x1, mult # Call multiplication function (x10 = 10^x11)
lw x11, 0(x7) # Load the next digit from the array
jal x1, mult # Multiply the digit by 10^x11
add x29, x29, x10 # Accumulate the result in x29
addi x28, x28, -1 # Decrement the exponent (x28)
addi x7, x7, 4 # Move to the next digit in the array
jal x0, l2 # Repeat the loop
l3:
lw x5, 0(x7)
add x29, x29, x5
add x10, x29, x0
halt
beg_input:
addi x7, x0, 1000 # x7 = starting address of the array (1000)
addi x5, x0, 0x20 # x5 = space (ASCII)
addi x28, x0, 0 # x28 = digit counter (initialized to 0)
input:
lb x6, 1025(x0) # Load a byte from input (address 1025)
beq x6, x5, end # If it's a space, end reading
beq x6, x0, end # If it's null, end reading
addi x6, x6, -48 # Convert ASCII to integer (subtract 48)
sb x6, 0(x7) # Store the digit in the array
addi x7, x7, 4 # Increment the array pointer (4 bytes per digit)
addi x28, x28, 1 # Increment the digit counter
jal x0, input # Repeat the loop
end:
addi x7, x0, 1000 # Reset x7 to the start of the array
addi x28, x28, -1 # Decrement x28 (adjust the counter)
jalr x0, 0(x1) # Return to the caller
mult:
addi sp, sp, -16
sw x1, 12(sp)
sw x7, 8(sp) # aux
sw x6, 4(sp) # i
sw x5, 0(sp) # ac
add x6, x0, x0 # i=0
add x5, x0, x0 # ac=0
l1:
beq x11, x0, exit1 # If b == 0, exit
addi x7, x0, 1 # aux = 1
# aux = b(0)
and x7, x7, x11
srli x11, x11, 1 # b >> 1
# If 0 < aux, increment ac
blt x0, x7, incr
addi x6, x6, 1 # i++
# Repeat the loop
# If b != 0
bne x11, x0, l1
jal x0, exit1
incr:
sll x7, x10, x6 # aux = a << i
# ac += temp1
add x5, x5, x7
addi x6, x6, 1 # i++
jal x0, l1
exit1:
add x10, x5, x0
lw x5, 0(sp) # ac
lw x6, 4(sp) # i
lw x7, 8(sp) # aux
lw x1, 12(sp)
addi sp, sp, 16
jalr x0, 0(x1)
The program works perfectly until it reaches the jal x0, l2 instruction inside the l2 loop. At this point, the simulator shows that the beq x28, x0, l3 instruction is executed, and the next instruction to be executed is addi x10, x0, 10. However, the program does not advance further, even as the clock progresses. It seems to hang at this point.
Observations:
1.The program works correctly for numbers with up to 2 digits if I remove the jal x0, l2 instruction. However, this is not a viable solution for larger numbers.
2.The issue seems to be related to the jal x0, l2 instruction, which is supposed to repeat the loop.
3.The simulator indicates that the beq x28, x0, l3 instruction is executed, but the program does not proceed to the next instruction (addi x10, x0, 10).