2018-02-24 03:19:29 +00:00
|
|
|
#include "defs.h"
|
|
|
|
#include "io.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-02-24 05:15:33 +00:00
|
|
|
static constexpr size_t nbuflen = 11;
|
|
|
|
|
2018-02-24 03:19:29 +00:00
|
|
|
void
|
|
|
|
write_num(IO &interface, KF_INT n)
|
|
|
|
{
|
|
|
|
|
2018-02-24 05:15:33 +00:00
|
|
|
// TODO(kyle): make the size of the buffer depend on the size of
|
2018-02-24 03:19:29 +00:00
|
|
|
// KF_INT.
|
2018-02-24 05:15:33 +00:00
|
|
|
char buf[nbuflen];
|
|
|
|
uint8_t i = nbuflen;
|
2018-02-24 03:19:29 +00:00
|
|
|
memset(buf, 0, i);
|
2018-02-24 05:15:33 +00:00
|
|
|
bool neg = n < 0;
|
|
|
|
|
2018-02-24 03:19:29 +00:00
|
|
|
if (n < 0) {
|
|
|
|
interface.wrch('-');
|
2018-02-24 05:15:33 +00:00
|
|
|
n = ~n;
|
2018-02-24 03:19:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (n != 0) {
|
|
|
|
char ch = (n % 10) + '0';
|
2018-02-24 05:15:33 +00:00
|
|
|
if (neg && (i == nbuflen)) ch++;
|
|
|
|
buf[i-1] = ch;
|
|
|
|
i--;
|
2018-02-24 03:19:29 +00:00
|
|
|
n /= 10;
|
|
|
|
}
|
|
|
|
|
2018-02-24 05:15:33 +00:00
|
|
|
uint8_t buflen = nbuflen - i % nbuflen;
|
|
|
|
interface.wrbuf(buf+i, buflen);
|
2018-02-24 03:19:29 +00:00
|
|
|
}
|