Mercurial > repos > simple16
comparison src/asm.c @ 59:b15187a99d6f default tip
Add a command line option for printing out label addresses on the command line. Useful for debugging purposes.
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 07 Sep 2016 23:15:27 -0700 |
parents | bed2d84eeabe |
children |
comparison
equal
deleted
inserted
replaced
58:bed2d84eeabe | 59:b15187a99d6f |
---|---|
443 fclose(incfile); | 443 fclose(incfile); |
444 *pc += read; | 444 *pc += read; |
445 return 1; | 445 return 1; |
446 } | 446 } |
447 | 447 |
448 uint8_t assemble_file(FILE *input, FILE *output) | 448 uint8_t assemble_file(FILE *input, FILE *output, uint8_t print_labels) |
449 { | 449 { |
450 //fixed size buffers are lame, but so are lines longer than 4K characters | 450 //fixed size buffers are lame, but so are lines longer than 4K characters |
451 //this is good enough for the really simple first version | 451 //this is good enough for the really simple first version |
452 char linebuf[4096]; | 452 char linebuf[4096]; |
453 uint8_t outbuf[MAX_SIZE]; | 453 uint8_t outbuf[MAX_SIZE]; |
654 break; | 654 break; |
655 } | 655 } |
656 } | 656 } |
657 } | 657 } |
658 } | 658 } |
659 if (print_labels) { | |
660 for (int i = 0; i < labels.num_labels; i++) | |
661 { | |
662 printf("%s: %X\n", labels.labels[i].name, labels.labels[i].address); | |
663 } | |
664 } | |
659 if (pc == fwrite(outbuf, 1, pc, output)) { | 665 if (pc == fwrite(outbuf, 1, pc, output)) { |
660 free_labels(&labels); | 666 free_labels(&labels); |
661 return 1; | 667 return 0; |
662 } | 668 } |
663 fputs("Error writing to output file\n", stderr); | 669 fputs("Error writing to output file\n", stderr); |
664 error: | 670 error: |
665 free_labels(&labels); | 671 free_labels(&labels); |
666 return 0; | 672 return 1; |
667 } | 673 } |
668 | 674 |
669 | 675 |
670 int main(int argc, char ** argv) | 676 int main(int argc, char ** argv) |
671 { | 677 { |
681 FILE *outfile = strcmp("-", argv[2]) ? fopen(argv[2], "w") : stdout; | 687 FILE *outfile = strcmp("-", argv[2]) ? fopen(argv[2], "w") : stdout; |
682 if (!outfile) { | 688 if (!outfile) { |
683 fprintf(stderr, "Failed to open %s for writing\n", argv[2]); | 689 fprintf(stderr, "Failed to open %s for writing\n", argv[2]); |
684 return 1; | 690 return 1; |
685 } | 691 } |
686 int ret = assemble_file(infile, outfile); | 692 uint8_t print_labels = argc > 3 && !strcmp("-l", argv[3]); |
693 int ret = assemble_file(infile, outfile, print_labels); | |
687 fclose(infile); | 694 fclose(infile); |
688 fclose(outfile); | 695 fclose(outfile); |
689 return ret; | 696 return ret; |
690 } | 697 } |