Write my own printf() in c without variadic args -


i'm writing own os using bcc , i'd write printf scratch, without variadic arg macros. there reason can't that? have start, can't first argument after string.

void print_f(char *fmt, ...) {     char *str = fmt;     uint16_t  *arg = (uint16_t *)&fmt + 1;     uint32_t  *long_arg;      while (*fmt != '\0') {         if (*fmt == '%') {             switch(*++fmt) {                 case 'c':                     putchar((uint8_t)*arg);                     arg++;                     break;                 default:                     print_f("malformed printf");                     break;             }         } else {             putchar(*fmt);         }          fmt++;     }     return; } 

i think suppose increment pointer 1 address space , go there, returns garbage.

incrementing address of first argument access subsequent variadic arguments may work on older architectures, not work in general, , not on on x86-64, assuming that target:

some otherwise portable c programs depend on argument passing scheme, implicitly assuming arguments passed on stack, , arguments appear in increasing order on stack. programs make these assumptions never have been portable, have worked on many implementations. however, not work on amd64 architecture because arguments passed in registers. portable c programs must use header file <stdarg.h> in order handle variable argument lists.

amd64 abi, 3.5.7 variable argument lists

if interested in reimplementing <stdarg.h> on x86-64, there sufficient details including va_arg algorithm in above reference.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -