Cant scanf and printf in risk-v rars

631 views Asked by At

Hello i am a beginer risc-v learner and i want to do a scanf of an int and print it on console upon searching a lot on the internet i couldnt find how to proper do it, i try using compiler explorer https://godbolt.org/ i want to use this c code

#include<stdio.h>    
int main(){    
int number;    
printf("enter a number:");    
scanf("%d",&number);    
printf("number is:%d ",number);    
return 0;  
}    

and its giving me this risc-v code

main:                                   # @main
        addi    sp, sp, -32
        sw      ra, 28(sp)                      # 4-byte Folded Spill
        sw      s0, 24(sp)                      # 4-byte Folded Spill
        addi    s0, sp, 32
        mv      a0, zero
        sw      a0, -20(s0)                     # 4-byte Folded Spill
        sw      a0, -12(s0)
        lui     a0, %hi(.L.str)
        addi    a0, a0, %lo(.L.str)
        call    printf
        lui     a0, %hi(.L.str.1)
        addi    a0, a0, %lo(.L.str.1)
        addi    a1, s0, -16
        call    scanf
        lw      a1, -16(s0)
        lui     a0, %hi(.L.str.2)
        addi    a0, a0, %lo(.L.str.2)
        call    printf
        lw      a0, -20(s0)                     # 4-byte Folded Reload
        lw      s0, 24(sp)                      # 4-byte Folded Reload
        lw      ra, 28(sp)                      # 4-byte Folded Reload
        addi    sp, sp, 32
        ret
.L.str:
        .asciz  "enter a number:"

.L.str.1:
        .asciz  "%d"

.L.str.2:
        .asciz  "number is:%d "

but the part call printf call scanf doesnt work and i cant figure how to do such a esy work any tips?

1

There are 1 answers

1
OSdev On

linker will give undefined reference error because directly you not compiled it in C instead took assembly code from gcc and gave it to linker.

if you directly compile with c you will get reference to printf and scanf and for risk v you can tell c to give static binary instead of dynamic

If i not understood what you want to know than plz explain