asm/decimal.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 17:57:57 +0100
changeset 5 d752cbe8208e
child 9 c3fecc82ade6
permissions -rw-r--r--
asm and parser demos added committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
 * $Id: decimal.c,v 1.1.1.1 2008-04-28 17:32:52 mbroeker Exp $
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 * $Source: /development/c/asm/decimal.c,v $
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 *
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
 */
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdio.h>
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <stdlib.h>
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <string.h>
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
#include <limits.h>
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
#define  MAXBITS __WORDSIZE
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
#define  MAXVALUE ULONG_MAX
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
typedef unsigned long int ULONG;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
void usage (char *name)
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
{
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
    printf ("Usage: %s start [end] [base]\n", name);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
}
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
/*
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
 * Computes the binary representation of a
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
 * decimal value and returns the result
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
 * in proper order and the number of digits
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
 */
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
char *dec_to_base (ULONG decimal, int base, int *digits)
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
{
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
    char *encoded;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    char swp;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
    int i;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
    i = 0;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
    if ((decimal > MAXVALUE)) {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
        perror ("VALUE TOO BIG");
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
        return NULL;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
    }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
    if ((encoded = malloc ((MAXBITS) * sizeof (char))) == NULL) {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
        perror ("MALLOC ERROR");
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
        return NULL;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
    }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
    while (decimal > base - 1) {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
        encoded[i++] = (decimal % base);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
        decimal /= base;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
    }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
    encoded[i++] = (decimal % base);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
    *digits = i;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
    for (i = 0; i < *digits - 1; i++) {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
        swp = encoded[*digits - i - 1];
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
        encoded[*digits - i - 1] = encoded[i];
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
        encoded[i] = swp;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
    }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
    return encoded;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
}
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
int main (int argc, char **argv)
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
{
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
    ULONG decimal, start, end;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
    int bits, base, i;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
    char *binaer;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
    base = 2;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
    if (argc == 2) {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
        start = atol (argv[1]);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    72
        end = start + 1;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    73
    } else if (argc == 3) {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
        start = atol (argv[1]);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    75
        end = atol (argv[2]);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    76
    } else if (argc == 4) {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    77
        start = atol (argv[1]);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    78
        end = atol (argv[2]);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    79
        base = atoi (argv[3]);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    80
    } else {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    81
        usage (argv[0]);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    82
        return EXIT_SUCCESS;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    83
    }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    84
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    85
    if (!((start >= 0) && (end > start) && (base > 1))) {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    86
        usage ("Trottel");
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    87
        printf ("%ld\t%ld\t%d\n", start, end, base);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    88
        return EXIT_SUCCESS;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    89
    }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    90
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    91
    printf ("\tVALUE \t\tDIGITS\t\t Encoded\n");
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    92
    printf ("----------------------------------------------------\n");
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    93
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    94
    for (decimal = start; decimal < end; decimal++) {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    95
        if ((binaer = dec_to_base (decimal, base, &bits)) == NULL) {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    96
            return EXIT_FAILURE;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    97
        }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    98
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    99
        printf ("%12ld %12d \t\t", decimal, bits);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   100
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   101
        for (i = 0; i < bits; i++) {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   102
            printf ("%X", binaer[i]);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   103
        }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   104
        printf ("\n");
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   105
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   106
        if (binaer)
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   107
            free (binaer);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   108
    }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   109
    return EXIT_SUCCESS;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   110
}