0

generate jal instruction for riscv gets the unexpected code

I have a riscv assembly code in test_jal.s:

jal a0, 42

And I compile it with

riscv64-linux-gnu-gcc -c -Wl,-Ttext=0x0 -nostdlib -mno-relax -o test_jal test_jal.s

Then I generate the raw riscv binary by

riscv64-linux-gnu-objcopy -O binary test_jal test_jal.bin

I use hexdump -C test_jal.bin to generate its hex code:

6F 05 00 00

Then I use riscv64-linux-gnu-objdump to disasm it. I got

jal     a0,0x0

May I do something wrong?

System: WSL ubuntu 22.04 gcc verison: 9.3.0

1 Answers1

0

Probably the assembler convert 42 to zero since there's no place to jump basically. Try a longer function like:

.global main

.data
    msg: .string "Hello World!"
.text
   main:    
       jal  ra, function
    
       li   a7, 93
       li   a0, 0
       ecall

   function:
      la t0, msg
    
      li a7, 4
      mv a0, t0
      ecall
    
      ret

This is an example done for Rars Emulator