So, my goal is to print (on the standard output) a number using a Nios II system on a FPGA Cyclone II Altera.


1) I created the following Nios II system with the Qsys tool (Quartus II):


enter image description here

2) I wrote the following C code:

#include <stdio.h>
int main(){
    char a[13]="Hello world!";
    printf("%s\n", a);

3) I downloaded the system on the FPGA and I compiled and loaded my code using Altera Monitor Program, I ran the code and it works, my code prints "Hello world" on the Monitor Program terminal. enter image description here

4) So, I tried to print a number and it fails :(

  • The code:

    #include <stdio.h>
    int main(){
        int a=3;
        printf("%i\n", a);
  • The error:

    enter image description here

NOTES: I tried to change %i with %d %u

Why it doesn't work?


1 Answer 1


Based on the verification error and looking at your Qsys system, there isn't enough RAM.

In Qsys, your RAM goes from 0x4000 to 0x7FFF. However when the JTAG programmer tries to upload the Nios firmware, it tries to verify 0x4000 all the way up to 0xE693. That means the size of your compiled code is a full 0x6694 (26260) bytes larger than the RAM.

By using %i,d or u, you are forcing the compiler to include the full routines for integer to string conversion, which in turn requires division and modulus code. This will use a fair amount of space. Much more so than %s which requires little code.

  • \$\begingroup\$ Thanks for the exhaustive answer and your time, tomorrow i'll try to increase the amount of ram. \$\endgroup\$ Oct 10, 2015 at 1:25
  • 1
    \$\begingroup\$ @MadDogTunnen just a thought, what settings have you got in your BSP? There is are settings like selecting enable_small_c_library, deselecting enable_c_plus_plus, selecting enable_reduced_device_drivers, deselecting enable_exit and enable_clean_exit. These will all reduce the code footprint and take a fair amount off the size. They are mostly there for when the Nios processor is being used to run things like an RTOS on Linux. For small embedded stuff, they just waste space. \$\endgroup\$ Oct 10, 2015 at 1:31
  • \$\begingroup\$ I confirm the problem was the low memory, I try to run the code using an external sram (placed on Altera board, 256K x 16 bits) and it works! I did not use BSP, anyway there is the Use_small_C_library setting, I checked it but it doesn't work. (I mean with on_chip_memory). \$\endgroup\$ Oct 10, 2015 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.