Hey, thanks!

What I actually meant was more "I need to convert some 8080 assembly I already 
have into a smallc function" but this should work, too! 

thanks,
Willard
Sent from Samsung tablet

-------- Original message --------
From Daryl Tester <[email protected]> 
Date: 03/21/2017  12:21 AM  (GMT-07:00) 
To [email protected] 
Subject Re: [M100] Small C teaser 
 
On Tue, 21 Mar 2017 16:39:03 +1030, Daryl Tester wrote:

> On Tue, 21 Mar 2017 00:06:28 -0600, Willard Goosey wrote:

>> But first I desperately need printhex()....

> What I/O (well, O) primitives do you have?  putc()?

Coz assuming it's putchar() (and I forget whether Small-C had the 
trinary
operator, but I'm suspecting it doesn't (and I have a couple of minutes
to kill at work) ...

#include <stdio.h>

void hex4(int c)
{
     /* putchar(c < 10 ? c + '0' : c + '0' + 7); */
     if (c < 10)
         putchar(c + '0');
     else
         putchar(c + '0' + 7);
}

void hex8(int c)
{
     hex4(c >> 4 & 0xf);
     hex4(c & 0xf);
}

void hex16(int c)
{
     hex8(c >> 8 & 0xff);
     hex8(c & 0xff);
}

int
main(int argc, char *argv[])
{
     hex8(0xf); putchar('\n');
     hex16(0x0); putchar('\n');
     hex16(0xff); putchar('\n');
     hex16(0xffff); putchar('\n');
     hex16(0x0123); putchar('\n');
     hex16(0x4567); putchar('\n');
     hex16(0x89ab); putchar('\n');
     hex16(0xcdef); putchar('\n');

     return 0;
}

-- 
Regards,
   Daryl Tester
   Handcrafted Computers Pty. Ltd.

Reply via email to