RISC-V Load address not aligned to word boundary

0

My compiler is RARS.

Here is the problem. I was told that I need to define a memory base address, and the use of pseudoinstructions is not allowed. So, I cannot use la, li, and j

When I tried to do it, I got this error:

Error in /Users/SumOfArray.asm line 22: Runtime exception at 0x00400018: Load address not aligned to word boundary 0x00000001

Go: execution terminated with errors

Line 22 that I think have the mistake is this one: lw t4,0(t3) After creating lui instruction. I will need to loop through it to save values into an array.

I guess I am asking how can I fix my code below, to have a lui instruction and loop through the array with no errors.

  .data
array:      .word 1,2,3,4,5,6,7,8,9,10,11,12,-1,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11,-12,-5, 0 
positiveSum:    .word 0
negativeSum:    .word 0

   .text

main:
    lui s0, 0x10010
        lw t0,0(s0)      #load array to register t0 
        lw s1,0(s0)      #load the positive sum to s1, and negative sum to s2           
        lw s2,0(s0)

loop:                   #store values in an array using loop
    slli t3,t1,2
    add t3,t3,t0        #add the base address and the above result
    lw t4,0(t3)         #load the word from above address into t4
    beqz t4,exit        #exit loop when reaching 0

assembly
riscv
asked on Stack Overflow Nov 9, 2020 by Bella • edited Dec 31, 2020 by Jonas

2 Answers

1

The problem you are having is that you are trying to use lw with the address (t1 << 2) + t0. T0 is equal to 1 on first use (unless it has been modified in the meantime) which gives an unaligned word address. Not being supported (or at least not by default) this gives the error you saw.

May be misaligned access are disabled by default. There what the specification tell:

For best performance, the effective address for all loads and stores should be naturally aligned for each data type (i.e., on a four-byte boundary for 32-bit accesses, and a two-byte boundary for16-bit accesses). The base ISA supports misaligned accesses, but these might run extremely slowly depending on the implementation. Furthermore, naturally aligned loads and stores are guaranteed to execute atomically, whereas misaligned loads and stores might not, and hence require additional synchronization to ensure atomicity.

To avoid the error you are facing there is two possibilities:
You really need to do misaligned access and you have to enable this feature.
What you want is read the first word , second ... then what you need to do is to shift the elements you get from the array ( by 2 to the left).

lw s1,0(s0)      #load the positive sum to s1, and negative sum to s2           
lw s2,0(s0)

need to be replaced probably by :

lw t4,0(t3)     
sw s1, 0(s0)    
sw s2, 4(s0)    

also:

lui s0, 0x10010
    lw t0,0(s0)   #load array to register t0 
    lw s1,0(s0)   #load the positive sum to s1, and negative sum to s2           
    lw s2,0(s0)

needs to be replaced by:

main:
    la t0, array
    la s0, positiveSum
answered on Stack Overflow Nov 9, 2020 by yflelion • edited Nov 10, 2020 by yflelion
0
unsigned int hello;
unsigned int world;
void fun ( void )
{
    hello++;
    world++;
}

-Ttext=0x1000 -Tdata=0x20000300 

Disassembly of section .text:

00001000 <fun>:
    1000:   20000637            lui x12,0x20000
    1004:   200006b7            lui x13,0x20000
    1008:   30462703            lw  x14,772(x12) # 20000304 <hello>
    100c:   3006a783            lw  x15,768(x13) # 20000300 <__DATA_BEGIN__>
    1010:   0705                    addi    x14,x14,1
    1012:   0785                    addi    x15,x15,1
    1014:   30e62223            sw  x14,772(x12)
    1018:   30f6a023            sw  x15,768(x13)
    101c:   8082                    ret

Disassembly of section .sbss:

20000300 <world>:
20000300:   0000                    unimp
    ...

20000304 <hello>:
20000304:   0000                    unimp
    ...

fun:
    lui a2,%hi(hello)
    lui a3,%hi(world)
    lw  a4,%lo(hello)(a2)
    lw  a5,%lo(world)(a3)
    addi    a4,a4,1
    addi    a5,a5,1
    sw  a4,%lo(hello)(a2)
    sw  a5,%lo(world)(a3)
    ret

you can of course hard code those offsets so long as you actually do it. You didnt show your linker script or address space desires for .text and .data to see how those fit into your code. If this is some sort of program to count up how many numbers are positive and how many negative then you need to 1) loop through the array and 2) address the locations where the counts are held.

If you hardcode incorrectly then the code may not run right as you appear to be seeing you have not provided any information tied to that specific problem. You have some fragments but not the key information related to the problem.

Before writing any more code you should start over and only loop through the array and see that that works, then start processing each element, and then keeping score.

answered on Stack Overflow Nov 9, 2020 by old_timer

User contributions licensed under CC BY-SA 3.0