Author Topic: vsprintf() doesn't work  (Read 1199 times)

0 Members and 1 Guest are viewing this topic.

Offline compu

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 260
  • Rating: +63/-3
    • View Profile
vsprintf() doesn't work
« on: February 24, 2011, 01:42:29 pm »
Hey, I wanted to make an on-screen console for the nspire.
Normal text output works fine, but i wanted an sprintf()-like function to output text, so I made this function:
Code: [Select]
void c_swrite(struct console *c, char* format, int len, char bgColor, char textColor, ...)
{
char buf[len];
memset(buf,'\0',sizeof(char)*len);
va_list arglist;
va_start(arglist,textColor);
vsprintf(buf,format,arglist);
c_write(c,buf,bgColor,textColor);
va_end(arglist);
}

Always when this function reaches vsprintf, the emulator crashes.

Code: [Select]
Error at PC=102F45C0: Unaligned read_word: 1800e0ed
         Backtrace:
Frame     PrvFrame Self     Return   Start
1800E0B0: 1800E0E8 1800E0B4 1106E9DC 102F4210
1800E0E8: 1109CA20 00000000 1800E934 1106E5A4

I have attached my whole source.
So, what am I doing wrong?
Nspire I/O Discussion|Website - cross-compatible with Prizm!
TI-Nspire Programming Statistics

Offline Goplat

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 289
  • Rating: +81/-0
    • View Profile
Re: vsprintf() doesn't work
« Reply #1 on: February 24, 2011, 02:47:20 pm »
Ndless's definition of va_start is not quite correct, it doesn't take into account that a 1-byte function parameter actually takes up 4 bytes on the stack.

For the time being, try using the gcc intrinsic __builtin_va_start instead.
Numquam te deseram; numquam te deficiam; numquam circa curram et te desolabo
Numquam te plorare faciam; numquam valedicam; numquam mendacium dicam et te vulnerabo

Offline compu

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 260
  • Rating: +63/-3
    • View Profile
Re: vsprintf() doesn't work
« Reply #2 on: February 24, 2011, 02:56:10 pm »
Do you mean I should do it like this:
Code: [Select]
void c_swrite(struct console *c, char* format, int len, char bgColor, char textColor, ...)
{
char buf[len];
memset(buf,'\0',sizeof(char)*len);
__builtin_va_list arglist;
__builtin_va_start(arglist,textColor);
__builtin_vsprintf(buf,format,arglist);
c_write(c,buf,bgColor,textColor);
__builtin_va_end(arglist);
}

I can compile it without any errors, but the emulator still crashes :(
Nspire I/O Discussion|Website - cross-compatible with Prizm!
TI-Nspire Programming Statistics

Offline Goplat

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 289
  • Rating: +81/-0
    • View Profile
Re: vsprintf() doesn't work
« Reply #3 on: February 24, 2011, 03:11:50 pm »
Don't use __builtin_vsprintf, that would end up trying to call an actual function called vsprintf (rather than the syscall). It looks like yagarto's gcc is annoyingly strict about how you can use a __builtin_va_list, but this still works:

   vsprintf(buf,format,*(char **)&arglist);
Numquam te deseram; numquam te deficiam; numquam circa curram et te desolabo
Numquam te plorare faciam; numquam valedicam; numquam mendacium dicam et te vulnerabo

Offline compu

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 260
  • Rating: +63/-3
    • View Profile
Re: vsprintf() doesn't work
« Reply #4 on: February 24, 2011, 03:17:56 pm »
Thanks! :D
This works fine :)
Nspire I/O Discussion|Website - cross-compatible with Prizm!
TI-Nspire Programming Statistics

Offline ExtendeD

  • Coder Of Tomorrow
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 819
  • Rating: +167/-2
    • View Profile
Re: vsprintf() doesn't work
« Reply #5 on: February 25, 2011, 04:44:47 pm »
Thanks, Ndless's va_* definitions are now wrappers to GCC builtin functions.
Ndless.me with the finest TI-Nspire programs