简体   繁体   English

RISC-V 汇编语言。 只打印零

[英]RISC-V Assembly language. Only printing zero

The following code is supposed to take two lists and put them into one big list (C[10]) and being a beginner in assembly I am unsure as to how to check if my output is correct.以下代码应该包含两个列表并将它们放入一个大列表(C[10])中,作为汇编的初学者,我不确定如何检查我的 output 是否正确。 I tried implementing a print to check if the left half works but it only prints zero.我尝试实现打印以检查左半部分是否有效,但它只打印零。 ( https://www.kvakil.me/venus/ using this website as an emulator) https://www.kvakil.me/venus/使用这个网站作为模拟器)

    .data
A:
    .word 0,2,3,3,4
B:
    .word 24,22,21,25,23
C:
    .word 1,1,1,1,1,1,1,1,1,1,1
min:
    .word 0
.text
.globl _main   
_main:
    add x8,x8,x0 #x8=i=0
    addi x9,x9,5  #x9=5
    #start of the loop
loop:   
        bge x8,x9,exit
        add x18,x0,x8   #x18=i
        slli x18,x18,2  #x18=i*4
        addi x19,x18,20  #x19 =i*4+ 4*5
        la x20,A #x20=&A
        la x21,B #x21=&B
        la x22,C #x22=&C
        add x20,x18,x20 #x20=&A[i]
        lw x20,0(x20) #x23=A[i]                
        add x21,x19,x21 #x21=&B[i]
        lw x21,0(x21) #x24=B[i]
        add x23,x22,x19 #x23=&C[i+5]
        sw x23,0(x21) #C[i+5]=B[i]
        add x22,x18,x22 #x22=&C[i]
        sw x22,0(x20) #C[i]=A[i] 
        
        
        addi a0,x0,1
        add x20,x0,x20
        ecall

        
        addi x8,x8,1 #i=i+1
        beq x0,x0,loop
                   
exit:

I am guessing the code is supposed to print 0224 however it is only printing 0.我猜代码应该打印 0224 但它只打印 0。

loop:   
    bge x8,x9,exit
    add x18,x0,x8   #x18=i
    slli x18,x18,2  #x18=i*4
    addi x19,x18,20  #x19 =i*4+ 4*5
    la x20,A #x20=&A 
    la x21,B #x21=&B
    la x22,C #x22=&C
    add x20,x18,x20 #x20=&A[i]
    lw x20,0(x20) #x23=A[i]         # comment says x23, but code says x20             
    add x21,x19,x21 #x21=&B[i]
    lw x21,0(x21) #x24=B[i]         # comment says x24, but code says x21
    add x23,x22,x19 #x23=&C[i+5]
    sw x23,0(x21) #C[i+5]=B[i]      # (*B[i]) = &C[i+5] ... I don't think you want this
    add x22,x18,x22 #x22=&C[i]
    sw x22,0(x20) #C[i]=A[i]        # (*A[i]) = &C[i] ... or this
            
    addi a0,x0,1
    add x20,x0,x20
    ecall

    
    addi x8,x8,1 #i=i+1
    beq x0,x0,loop

Do like Jester says, and single step to confirm that what you think is happening is truly so — don't wait to see the final output is right/wrong, check each and every instruction using single step;像 Jester 所说的那样,一步一步来确认你认为正在发生的事情确实如此——不要等着看最终的 output 是对/错,使用单步检查每条指令; Every single instruction is an opportunity for a typo or design issue.每一条指令都是一个错字或设计问题的机会。 if any one thing is wrong a program won't fully work.如果任何一件事是错误的,程序将无法完全运行。

Generally speaking, you are clobbering values by repurposing registers too soon, as it turns out that you still want their older values later on.一般来说,你过早地重新利用寄存器来破坏值,因为事实证明你以后仍然想要它们的旧值。

Other than that, when the comments and the code don't agree, that's something to look into.除此之外,当评论和代码不一致时,这是需要调查的。 I'd also recommend using the friendly register names, like a0 , t0 , s0 instead of the raw numeric ones — probably doesn't matter for this assignment, but it will if you start doing procedure/function calling.我还建议使用友好的寄存器名称,例如a0t0s0 ,而不是原始数字名称——这可能对这个分配无关紧要,但如果你开始进行过程/函数调用,它就会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM