在用Python的unittest时,我得到了如下错误:

在汇编测试里,我的代码运行结果是正确的,但s6的地址也发生了变化 (lw 时加载的不是存进去的值)

我想知道是哪里出了问题

以下为我的代码:


.globl dot

.text
# =======================================================
# FUNCTION: Dot product of 2 int vectors
# Arguments:
#   a0 (int*) is the pointer to the start of v0
#   a1 (int*) is the pointer to the start of v1
#   a2 (int)  is the length of the vectors
#   a3 (int)  is the stride of v0
#   a4 (int)  is the stride of v1
# Returns:
#   a0 (int)  is the dot product of v0 and v1
# Exceptions:
# - If the length of the vector is less than 1,
#   this function terminates the progra,m with error code 57
# - If the stride of either vector is less than 1,
#   this function terminates the progra,m with error code 58
# =======================================================

#a0:size a1:time return a0 
multi:
    
    add t1 a0 x0
    bne a1 zero loop
    mv a0 zero
    j mul_end
    
    loop:
    addi a1 a1 -1
    ble a1 zero mul_end
    add a0 a0 t1
    j loop
    mul_end:
    jalr zero ra 0

#ret a0; arg:a0,a1 find max value 
max:
    bge a0, a1, max_return # if a0 > a1 then max_return
    mv a0 a1

max_return:
    jalr zero ra 0

dot:
    addi sp sp -30
    sw s0 0(sp)
    sw s1 4(sp)
    sw s2 8(sp)
    sw s3 12(sp)
    sw s4 16(sp)
    sw s5 20(sp)
    sw s6 24(sp)
    sw ra, 26(sp)
    # Prologue
    ble a2, zero, error_len # if a2 <= zero then err
    ble a3, zero, error_str
    ble a4, zero, error_str
    jal zero loop_prepare
error_len:
    li a1 57
    call exit2
error_str:
    li a1 58
    call exit2

loop_prepare:
    #s0 - firstAdd
    #s1 - secAdd
    #s2 - firStr
    #s3 - secStr
    #s4 - largestStr
    #s5 - vecInd
    #s6 - sum

    mv s0 a0
    mv s1 a1
    mv s5 a2

    li a0 4
    mv a1 a3
    jal ra, multi
    mv s2 a0
    
    li a0 4
    mv a1 a4
    jal ra, multi
    mv s3 a0

    mv a0 a3
    mv a1 a4
    jal ra, max

    mv s4 a0
    li s6 0


    

loop_start:
    lw a0 0(s0)
    lw a1 0(s1)
    

    jal ra, multi

    add s6 s6 a0

loop_continue:
    sub s5 s5 s4
    ble s5 zero loop_end
    add s0 s0 s2 
    add s1 s1 s3 
    j loop_start

loop_end:
    mv a0 s6

    lw s0 0(sp)
    lw s1 4(sp)
    lw s2 8(sp)
    lw s3 12(sp)
    lw s4 16(sp)
    lw s5 20(sp)
    lw s6 24(sp)
    lw ra, 26(sp)
    addi sp sp 30

    # Epilogue

    jr ra

ps:感觉测试有个小坑

在变量表述中:a2 应该是2个数组的长度

但stride_test里,步长大的到了数组外依旧可以运算,以至于结果为22

实际上应该为7

    能调整一下自己这段代码的运行逻辑吗?阅读起来绕来绕去的…

      MingLLuo .globl main

      .text
      # =======================================================
      # FUNCTION: main product of 2 int vectors
      # Arguments:
      #   a0 (int*) is the pointer to the start of v0
      #   a1 (int*) is the pointer to the start of v1
      #   a2 (int)  is the length of the vectors
      #   a3 (int)  is the stride of v0
      #   a4 (int)  is the stride of v1
      # Returns:
      #   a0 (int)  is the main product of v0 and v1
      # Exceptions:
      # - If the length of the vector is less than 1,
      #   this function terminates the progra,m with error code 57
      # - If the stride of either vector is less than 1,
      #   this function terminates the progra,m with error code 58
      # =======================================================
      
      #a0:size a1:time return a0 
      
      main:
          
          addi sp sp -28
          sw s0 0(sp)
          sw s1 4(sp)
          sw s2 8(sp)
          sw s3 12(sp)
          sw s4 16(sp)
          sw s5 20(sp)
          sw ra, 24(sp)
          # Prologue
          ble a2, zero, error_len # if a2 <= zero then err
          ble a3, zero, error_str
          ble a4, zero, error_str
          jal zero loop_prepare
      error_len:
          li a1 57
          # call exit2
      error_str:
          li a1 58
          # call exit2
      
      loop_prepare:
          #s0 - firstAdd
          #s1 - secAdd
          #s2 - firStr
          #s3 - secStr
          #s4 - vecInd
          #s5 - sum
      
          mv s0 a0
          mv s1 a1
          mv s4 a2
      
          li a0 4
          mv a1 a3
          jal ra, multi
          mv s2 a0
          
          li a0 4
          mv a1 a4
          jal ra, multi
          mv s3 a0
      
          li s5 0
      
      loop_start:
          lw a0 0(s0)
          lw a1 0(s1)
          
      
          jal ra, multi
      
          add s5 s5 a0
      
      loop_continue:
          addi s4 s4 -1
          ble s4 zero loop_end
          add s0 s0 s2 
          add s1 s1 s3 
          j loop_start
      
      loop_end:
          mv a0 s5
      
          lw s0 0(sp)
          lw s1 4(sp)
          lw s2 8(sp)
          lw s3 12(sp)
          lw s4 16(sp)
          lw s5 20(sp)
          lw ra, 24(sp)
          addi sp sp 28
      
          # Epilogue
      
          jr ra
      
      multi:
          add t1 a0 x0
          bne a1 zero loop
          mv a0 zero
          j mul_end
          
          loop:
          addi a1 a1 -1
          ble a1 zero mul_end
          add a0 a0 t1
          j loop
          mul_end:
          jalr zero ra 0

      YouerSu
      实际上是内存分配时ra配了半个字,串地址了;代码比较复杂的缘故是因为我以为a2里存的是数组长度,并通过strider判断是否到顶;在作业描述里说明了是stride后的数组长度(属于我没认真读题了

      不过我都删除了怎么还发了出来啊.....

        YouerSu 不过我都删除了怎么还发了出来啊.....

        噢噢,因为这里是你第一次发帖,还在审核状态,然后我通过了(可能顺手恢复了删除状态的帖子)。

        论坛的删除默认是软删除,已删除的帖子会根据情况定期清理,才是物理删除。

        说起来,感觉记录探索的过程,也是很有意义滴,不用有太多负担 😄

        MingLLuo
        这是用上了mul扩展的

        dot:
            # Prologue
            ble a2, zero, error_len # if a2 <= zero then err
            ble a3, zero, error_str
            ble a4, zero, error_str
            jal zero loop_prepare
        error_len:
            li a1 57
            call exit2
        error_str:
            li a1 58
            call exit2
            
        loop_prepare:
            # t0 = step_address v0
            # t1 = step_address v1
            # t2 = value
            li t0 4
            mul t1, t0 a4
            mul t0 t0 a3
            li t2 0
        loop_start:
            lw a3 0(a0)
            lw a4 0(a1)
            mul a3 a3 a4
            add t2 t2 a3
            add a0 a0 t0
            add a1 a1 t1
            addi a2 a2 -1
            bgt a2 zero loop_start
        loop_end:
            # Epilogue
            mv a0 t2
            ret
        9 个月 后

        你好,请问在终端中输入python3 ok -q python_basics -u出现了问题:
        KeyError:‘BREAK_LOOP’
        试过了lab00和lab01都是出现同样的问题,然后我的python版本是3.9.7
        我应该如何解决这个问题呢

        © 2018-2025 0xFFFF