-5

Here's the C representation of what I'm trying to do in RISC-V assembly:

printf ("x=%d\n", x);

Mr Robot
  • 1
  • 1
  • 1
  • 1
    You try to call the printf function, not to implement it. Can you [edit] your question title accordingly? There are actually two main problems. 1/ passing the arguments according to [risc-v calling conventions](http://riscv.org/wp-content/uploads/2015/01/riscv-calling.pdf) and 2/ linking with your code with the libc https://stackoverflow.com/questions/47088290/riscv-gcc-ld-undefined-reference-to-printf-using-own-script-to-link . Try to solve these problems and ask a more precise question. – Alain Merigot Jun 09 '19 at 16:38
  • In general, questions on this site should show some effort. Questions like this, simply asking how to do something without showing any research or code of your own, are frowned upon. You may want to review the help files. – Tom Zych Jun 09 '19 at 17:05
  • Are you wanting to convert an integer to ASCII decimal and print it *without* calling `printf`? If so, what OS are you running on? You need a system call of some sort to output the eventual string, or store the characters to video RAM yourself... – Peter Cordes Jun 09 '19 at 17:36

2 Answers2

2

https://godbolt.org/ is an interesting site. If you paste in c code, it can be transfered into others, such as RISC-V assembly. The sample c code is available from menie.org/georges/embedded/small_printf_source_code.html. It does work. Good luck.

caot
  • 3,066
  • 35
  • 37
1

Here is a very simple printf (actually only integers and strings and no advanced formatting)

https://godbolt.org/z/sgMVs7

It is not my code - it is tiny ptinf from the atolic studio. But it is a good base to implement something simple but more decent.

0___________
  • 60,014
  • 4
  • 34
  • 74
  • You forgot to enable optimization in your link. This is an assembly question; that steaming pile of crap asm in your link is a terrible suggestion to actually copy into an asm source file. (Obviously easy to fix by adding `-O3` or `-Os`, but that's important). – Peter Cordes Jun 09 '19 at 19:12
  • @petercordes actually anyone can add the needed options. But the question was about the `printf` and this is a very simple one. I use modified version myself – 0___________ Jun 09 '19 at 19:17
  • BTW it is not sane to implement the print In asm. – 0___________ Jun 09 '19 at 19:19
  • Of course anyone *can* add options after opening your previous link. But you're the one that chose to post that link in the first place. Including the choice to include a probably-useless implementation of *actual* `printf`, not just of the `%d` conversion. As you say, it makes much more sense to hard-code the fact that it's a `%d` conversion and just write a `print_integer` function. For whatever unspecified OS the OP is using in this unclear unanswerable question. – Peter Cordes Jun 09 '19 at 19:33
  • 1
    Please don't post link only answers--remember the rules of this site. – fuz Jun 09 '19 at 19:37
  • 1
    @fuz this has to be link only – 0___________ Jun 09 '19 at 20:21
  • 2
    @P__J__ Then make it a comment. – fuz Jun 09 '19 at 20:24
  • @fuz - you can always dv it (but you probably did it already) – 0___________ Jun 09 '19 at 21:07