all repos — nand2tetris @ fb4ae27aa022f203ced97d453d450b41687eface

my nand2tetris progress

Add binary output helper for assembler
x1phosura x1phosura@x1phosura.zone
Sat, 27 Aug 2022 23:22:49 -0700
commit

fb4ae27aa022f203ced97d453d450b41687eface

parent

2dd25de3b00bc3e3604e10b13eece7b279a84323

1 files changed, 30 insertions(+), 0 deletions(-)

jump to
A projects/06/bin2text.c

@@ -0,0 +1,30 @@

+#include <stdio.h> +#include <stdint.h> + +void output_binary(uint8_t b) +{ + char i, msb; + + for (i = 0; i < 8; ++i) { + msb = b & 0x80; + if (msb) + putchar('1'); + else + putchar('0'); + b <<= 1; + } +} + +int main(int argc, char *argv[]) +{ + char c, next_line = 0; + + while ((c = getchar()) != EOF) { + output_binary(c); + ++next_line; + if (next_line % 2 == 0) + putchar('\n'); + } + + return 0; +}