/*
 * file: mp30.c
 * main C program that uses assembly functions that the student
 * is to write in the file mp30.asm to encode a string of 
 * up to 8 decimal digits into an integer.
 *
 * The main program in this file does the following,
 * until end of file:
 *    1. Prompts and reads in up to 8 decimal digits into
 *       a string.
 *    2. Packs this string into an integer variable, via an
 *       assembler function, packToInt.
 *    3. Writes out the integer variable 2 ways:
 *       a. in 32 bits (4 bits at a time) via an assembler 
 *          function, bitPrint
 *       b. writes out the integer variable as an unsigned
 *          integer.
 *    4. Unpacks the integer variable to another string via
 *       an assembler function, unPackToStr.
 *    5. Writes out the unpacked string
  * To create executable:
   Linux :  nasm -f elf mp30.asm
            gcc mp30.o mp30.c asm_io.o
 */

#include <stdio.h>
#include <stddef.h>
#include "cdecl.h"

#define MAX_BUF 8

void PRE_CDECL packToInt(char *, int * ) POST_CDECL;
void PRE_CDECL bitPrint(int  ) POST_CDECL;
void PRE_CDECL unPackToStr(int, char * ) POST_CDECL;

int main()
{
    char buff[MAX_BUF + 1]; /* source */
    char dest[MAX_BUF + 1]; /* source */
    char newLine;
    int val;

    printf("\nEnter a string of up to 8 decimal digits: ");
    while ( scanf("%s", buff) != EOF )
    {
       scanf("%c", &newLine);

       printf("\nThe string you typed in is %s\n", buff);
       packToInt( buff, &val);
       printf("\nThe packed version of your string as 32 bits is on the next line, as \n");
       bitPrint(val);
       printf("\n");
       printf("\nThe packed version of your string as an unsigned integer is %u\n", val);

       unPackToStr(val, dest);
       printf("\nThe unpacked string of the packed integer version is %s\n", dest);
       printf("\nEnter a string of up to 8 decimal digits: ");
   }
   printf("\n");
   return 0;
}
