簡體   English   中英

RISC-V 加載地址未與字邊界對齊

[英]RISC-V Load address not aligned to word boundary

我的編譯器是 RARS。

這是問題所在。 有人告訴我,我需要定義一個內存基地址,並且不允許使用偽指令。 所以,我不能使用la, li, and j

當我嘗試這樣做時,出現此錯誤:

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

Go: execution terminated with errors

我認為有錯誤的第 22 行是: lw t4,0(t3)創建lui指令后。 我需要遍歷它以將值保存到數組中。

我想我是在問如何修復下面的代碼,以獲得lui指令並在沒有錯誤的情況下循環遍歷數組。

  .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

您遇到的問題是您試圖將 lw 與地址 (t1 << 2) + t0 一起使用。 T0 在第一次使用時等於 1(除非它在此期間被修改),這給出了一個未對齊的字地址。 不支持(或至少默認情況下不支持)這會導致您看到的錯誤。

默認情況下禁用可能未對齊的訪問。 規范說明了什么:

為獲得最佳性能,所有加載和存儲的有效地址應該針對每種數據類型自然對齊(即,對於 32 位訪問,在四字節邊界上,對於 16 位訪問,在兩字節邊界上)。 基礎 ISA 支持未對齊的訪問,但這些訪問可能會根據實現而運行得非常緩慢。 此外,自然對齊的加載和存儲可以保證以原子方式執行,而未對齊的加載和存儲可能不會,因此需要額外的同步以確保原子性。

為了避免您面臨的錯誤,有兩種可能性:
您確實需要進行未對齊的訪問,並且必須啟用此功能。
你想要的是讀取第一個單詞,第二個......然后你需要做的是將你從數組中獲得的元素移動(向左移動 2)。

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

可能需要替換為:

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

還:

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)

需要替換為:

main:
    la t0, array
    la s0, positiveSum
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

你當然可以硬編碼這些偏移量,只要你真的這樣做。 您沒有顯示 .text 和 .data 的鏈接器腳本或地址空間需求,以了解它們如何適合您的代碼。 如果這是某種程序來計算有多少正數和多少負數,那么您需要 1) 遍歷數組和 2) 尋址保存計數的位置。

如果您硬編碼不正確,那么代碼可能無法正常運行,因為您似乎看到您沒有提供與該特定問題相關的任何信息。 您有一些片段,但沒有與問題相關的關鍵信息。

在編寫更多代碼之前,您應該重新開始,只循環遍歷數組並查看它是否有效,然后開始處理每個元素,然后記錄分數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM