Stuck on some RISC-V homework, I know where the issue is, but not WHAT the issue is

52 Views Asked by At

I'm working on code for my class and I've been struggling way more than I should, although I think I'm getting close. Whenever I get to the lh command, it terminates. I'm sure (at least I think I’m right) that I’m shifting incorrectly and trying to load an address that doesn't exist. I know the issue is on lh; I am just not sure how to fix it.

We're supposed to code 10 different steps to solve sudoku, and I'm stuck on the second step. This is in 64-bit RISC-V processor. This is my code:

         # get_used(board, group) -> used      #s1:board s2:group s3:group_index s4:elt
get_used:                             #s0: used t0:g_index  a3:9  t1:g_elt_add   t2: b_index
                                      #t3: scaled_b_index  t4: b_elt_add  t5:elt
                addi    sp, sp, -48
                sd      ra, 40(sp)
                sd      s0, 32(sp)
                sd      s1, 24(sp)
                sd      s2, 16(sp)
                sd      s3, 8(sp)
                sd      s4, 0(sp)

                mv      s0, a0
                mv      s1, a1
                li      s2, 0
                li      s3, 0

loop:
                li      t0, 9
                bge     s3, t0, end_loop

                slli    t1, s3, 3
                add     t1, s1, t1
                ld      t2, 0(t1)
                slli    t2, t2, 1
                add     t3, s0, t2
               ** lh      s4, 0(t3)**      #line where it quits

                mv      a0, s4
                jal     ra, count_bits
                mv      t4, a0
                li      t5, 1
                beq     t4, t5, add_to_used
                j       next_iter

add_to_used:
                or      s2, s2, s4
next_iter:
                addi    s3, s3, 1
                j       loop
end_loop:
                mv      a0, s2
                ld      ra, 40(sp)
                ld      s0, 32(sp)
                ld      s1, 24(sp)
                ld      s2, 16(sp)
                ld      s3, 8(sp)
                ld      s4, 0(sp)
                addi    sp, sp, 48
                ret

And here is my first step for reference:

count_bits:
                li      a2, 0
                li      a1, 0
                li      t1,10
1:              bge     a1, t1, 2f
                srl     t0, a0, a1
                andi    t0, t0, 1
                beqz    t0, 3f
                addi    a2, a2, 1
3:              addi    a1, a1, 1
                j       1b
2:              mv      a0, a2
                ret

I've tried a lot at this point and I'm very stuck. I keep running into issue after issue.

1

There are 1 best solutions below

2
Erik Eidt On

There's not enough information to say what is wrong, but it goes to your data structures.  It is somewhat unusual to use a 64-bit item as an index, so maybe look into that.

Write some code in C and test it to make sure it works.  Then duplicate that in assembly.

Study your data to get to know what a good address looks like. (Do in C and also in the assembly versions.)

Write a small assembly test programs to gain proficiency in accessing data in assembly.