hidetatz
hidetatz

Reputation: 255

Unclear output by riscv objdump -d

Now I am trying to understand the RISC-V ISA but I have an unclear point about the machine code and assembly.

I have written a C code like this:

int main() {
    return 42;
}

Then, I produced the .s file by this command:

$ /opt/riscv/bin/riscv64-unknown-linux-gnu-gcc -S 42.c

The output was:

        .file   "42.c"
        .option nopic
        .text
        .align  1
        .globl  main
        .type   main, @function
main:
        addi    sp,sp,-16
        sd      s0,8(sp)
        addi    s0,sp,16
        li      a5,42
        mv      a0,a5
        ld      s0,8(sp)
        addi    sp,sp,16
        jr      ra
        .size   main, .-main
        .ident  "GCC: (g5964b5cd727) 11.1.0"
        .section        .note.GNU-stack,"",@progbits

Now, I run following command to produce an elf.

$ /opt/riscv/bin/riscv64-unknown-linux-gnu-gcc -nostdlib -o 42 42.s

So, a binary file is produced. I tried to read that by objdump like this:

$ /opt/riscv/bin/riscv64-unknown-linux-gnu-objdump -d 42

So the output was like this:


42:     file format elf64-littleriscv


Disassembly of section .text:

00000000000100b0 <main>:
   100b0:       1141                    addi    sp,sp,-16
   100b2:       e422                    sd      s0,8(sp)
   100b4:       0800                    addi    s0,sp,16
   100b6:       02a00793                li      a5,42
   100ba:       853e                    mv      a0,a5
   100bc:       6422                    ld      s0,8(sp)
   100be:       0141                    addi    sp,sp,16
   100c0:       8082                    ret

What I don't understand is the meaning of the machine code in objdump output. For example, the first instruction addi is translated into .....0010011 according to this page, (while this is not an official spec). However, the dumped hex is 1141. 1141 can only represent 2 bytes, but the instruction should be 32-bit, 4bytes.

I guess I am missing some points, but how should I read the output of objdump for riscv?

Upvotes: 1

Views: 1280

Answers (1)

zero-one
zero-one

Reputation: 51

You can tell objdump to show compressed (16-bit) instructions by using -M no-aliases in this way

riscv64-unknown-elf-objdump -d -M no-aliases

In that case, instructions starting with c. are compressed ones.

Unfortunately that will also disable some other aliases, making the asm less nice to read if you're used to them. You can just look at the number of bytes (2 vs. 4) in the hexdump to see if it's a compressed instruction or not.

Upvotes: 5

Related Questions