this post was submitted on 27 Oct 2023
8 points (83.3% liked)

Single Board Computers

1233 readers
5 users here now

A community for the discussion of all single board computers. Raspberry Pi is ok, but there are so many other boards now that get looked over that deserve attention.

Post news, questions, your setups, guides, anything that has to do with SBCs. Server wide rules apply

founded 1 year ago
MODERATORS
 

Technically both are RISC based but probably they are way different in implementation. I'm thinking about buying a RISC-V SBC, and I just want to be sure about compatiblity with running ARM based software.

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 10 points 1 year ago (1 children)

No. This is like asking if a 6502 microprocessor can run a 6809's code because they're both 8-bit micros.

The family of instruction sets under the RISC-V banner is completely different from the family of instruction sets under the ARM banner. I'll compile this program for RISC-V and ARM and show you the compiler outputs for an example:

#include 
int main(int argc, char** argv)
{
    printf("Hello, world!");
}

First ARM (a fairly generic ARM32 processor core's output):

.LC0:
        .ascii  "Hello, world!\000"
main:
        push    {r7, lr}
        sub     sp, sp, #8
        add     r7, sp, #0
        str     r0, [r7, #4]
        str     r1, [r7]
        movw    r0, #:lower16:.LC0
        movt    r0, #:upper16:.LC0
        bl      printf
        movs    r3, #0
        mov     r0, r3
        adds    r7, r7, #8
        mov     sp, r7
        pop     {r7, pc}

Now RISC-V (again a fairly convention 32-bit RISC-V core):

.LC0:
        .string "Hello, world!"
main:
        addi    sp,sp,-32
        sw      ra,28(sp)
        sw      s0,24(sp)
        addi    s0,sp,32
        sw      a0,-20(s0)
        sw      a1,-24(s0)
        lui     a5,%hi(.LC0)
        addi    a0,a5,%lo(.LC0)
        call    printf
        li      a5,0
        mv      a0,a5
        lw      ra,28(sp)
        lw      s0,24(sp)
        addi    sp,sp,32
        jr      ra

Comparing the two you're going to note several very major differences. One of the biggest and easiest to spot, however, is how ARM pushes r7 and lr onto the stack at the beginning, but pops r7 and pc at the end. This is because lr contains the "return address" (it's a bit more complicated than that, but close enough for jazz) on entry and pc is the "program counter" which is where the processor will get its next instruction from. By popping what was the lr value into pc you've effectively done a transfer of control to the return address without an explicit branch.

RISC-V, conversely, uses ra (their equivalent of lr) pushed onto the stack (note how each value is pushed manually, not part of a combined instruction), then popped back from the stack and an explicit jr branch instruction is used to set the next instruction to be executed. So even ignoring the different names of instructions (which could just be different naming conventions for the same thing), the very way the two systems operate is very different.

So while ARM and RISC-V are both RISC processors, they are not even similar to each other in operations beyond both following the RISC philosophy of instruction set design.

[–] [email protected] 5 points 1 year ago (1 children)

Thank you for the detailed answer. I feel I learned something today.

[–] [email protected] 3 points 1 year ago* (last edited 1 year ago)

No troubles.

If you really want to know what assembler looks like and how it relates to higher-level languages when compiled, you can't really beat Godbolt. It's amazing what you can learn from there, and changing options to include maximum optimization, for example, can show you just how much work compilers do for you under the covers these days.