简体   繁体   English

在 RISC-V 上执行函数调用时的非法指令

[英]Illegal instruction when executing function call on RISC-V

Goodmorning,早上好,

I am trying to write a simple program in assembly for RISC-V architecture, in which I have a simple main (_start) that perform a function call that does nothing and return to the caller.我正在尝试为 RISC-V 架构编写一个简单的汇编程序,其中我有一个简单的 main (_start),它执行一个什么都不做并返回给调用者的函数调用。

The code I wrote is the following:我写的代码如下:

.section .text
.global _start

test:
    lw a1, 0(sp)



    # ritorna al chiamante
    jr ra

_start:
    # push sullo stack
    addi sp, sp, -4

    li a1, 5

    # mette valore di ritorno sullo stack
    # salva a1 sullo stack
    sw a1, 0(sp)

    # chiamata a funzione
    jal ra, test

    # pop dallo stack
    addi sp, sp, +4

    li a6, 2

For executing this program I run:为了执行这个程序,我运行:

$ riscv64-unknown-elf-as somma.s -o somma.o
$ riscv64-unknown-elf-ld somma.o -o somma.elf
$ spike pk somma.elf

As output I get:作为输出,我得到:

z  0000000000000000 ra 0000000000010090 sp 000000007f7e9b50 gp 0000000000000000
tp 0000000000000000 t0 0000000000000000 t1 0000000000000000 t2 0000000000000000
s0 0000000000000000 s1 0000000000000000 a0 0000000000000000 a1 0000000000000005
a2 0000000000000000 a3 0000000000000000 a4 0000000000000000 a5 0000000000000000
a6 0000000000000002 a7 0000000000000000 s2 0000000000000000 s3 0000000000000000
s4 0000000000000000 s5 0000000000000000 s6 0000000000000000 s7 0000000000000000
s8 0000000000000000 s9 0000000000000000 sA 0000000000000000 sB 0000000000000000
t3 0000000000000000 t4 0000000000000000 t5 0000000000000000 t6 0000000000000000
pc 0000000000010098 va 0000000000000000 insn       00000000 sr 8000000200046020
An illegal instruction was executed!

In this case we have 2 in the a6 register, so I suppose that the control of the program is correctly returned to the caller, since the last instruction is correctly executed, but I anyway get the error "An illegal instruction was executed!"在这种情况下,我们在 a6 寄存器中有 2 个,所以我认为程序的控制权正确地返回给了调用者,因为最后一条指令被正确执行,但我还是得到了错误“执行了非法指令!” Could you please give me some hints for solving this problem?你能给我一些解决这个问题的提示吗?

Thank you谢谢

This is one of the differences between C and assembly programming: in C the compiler handles the function entry/exit code for you, while in assembly you're meant to handle it yourself.这是 C 和汇编编程之间的区别之一:在 C 中,编译器为您处理函数进入/退出代码,而在汇编中,您应该自己处理它。 In this specific case, your _start function doesn't end in a ret and therefor execution will continue past the end of the function.在这种特定情况下,您的_start函数不会以ret结尾,因此执行将继续超过函数的结尾。 Since that's the end of your binary, the next bytes are probably an illegal instruction (the all-0 instruction is illegal in RISC-V).由于这是二进制文件的结尾,因此接下来的字节可能是非法指令(RISC-V 中的全 0 指令是非法的)。

The _start function is a bit of a special case: while most RISC-V ABIs say it's OK to return from _start , the canonical _start will never return and will instead call exit() directly. _start函数有点特殊:虽然大多数 RISC-V ABI 都说可以从_start返回,但规范的_start永远不会返回,而是直接调用exit() Your best bet for _start is to have it end with li a0, 0; call exit; ret _start最佳选择是以li a0, 0; call exit; ret结尾li a0, 0; call exit; ret li a0, 0; call exit; ret li a0, 0; call exit; ret . li a0, 0; call exit; ret

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

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