asm/decimal.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:13 +0200
changeset 62 b7061c0e239f
parent 29 7abf6146898e
child 77 49e0babccb23
permissions -rw-r--r--
unicode.c added and myprintf improved * unicode.c: handle unicode input * myprintf.c: we need the printf family... 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
/**
9
c3fecc82ade6 standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 5
diff changeset
     2
 * test/demos/asm/decimal.c
c3fecc82ade6 standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 5
diff changeset
     3
 * Copyright (C) 2008 Markus Broeker
5
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
#include <stdio.h>
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdlib.h>
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <string.h>
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <limits.h>
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
#define  MAXBITS __WORDSIZE
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
#define  MAXVALUE ULONG_MAX
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
typedef unsigned long int ULONG;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
void usage (char *name)
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
{
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
    printf ("Usage: %s start [end] [base]\n", name);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
}
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
 * Computes the binary representation of a
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
 * decimal value and returns the result
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
 * in proper order and the number of digits
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
 */
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
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
    28
{
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
    char *encoded;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
    char swp;
29
7abf6146898e indent -bad changed to -nbad:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 9
diff changeset
    31
5
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;
29
7abf6146898e indent -bad changed to -nbad:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 9
diff changeset
    65
5
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
    int bits, base, i;
29
7abf6146898e indent -bad changed to -nbad:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 9
diff changeset
    67
5
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
    char *binaer;
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
    base = 2;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    72
    if (argc == 2) {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    73
        start = atol (argv[1]);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
        end = start + 1;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    75
    } else if (argc == 3) {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    76
        start = atol (argv[1]);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    77
        end = atol (argv[2]);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    78
    } else if (argc == 4) {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    79
        start = atol (argv[1]);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    80
        end = atol (argv[2]);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    81
        base = atoi (argv[3]);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    82
    } else {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    83
        usage (argv[0]);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    84
        return EXIT_SUCCESS;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    85
    }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    86
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    87
    if (!((start >= 0) && (end > start) && (base > 1))) {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    88
        usage ("Trottel");
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    89
        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
    90
        return EXIT_SUCCESS;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    91
    }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    92
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    93
    printf ("\tVALUE \t\tDIGITS\t\t Encoded\n");
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    94
    printf ("----------------------------------------------------\n");
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    95
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    96
    for (decimal = start; decimal < end; decimal++) {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    97
        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
    98
            return EXIT_FAILURE;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    99
        }
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
        printf ("%12ld %12d \t\t", decimal, bits);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   102
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   103
        for (i = 0; i < bits; i++) {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   104
            printf ("%X", binaer[i]);
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
        printf ("\n");
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   107
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   108
        if (binaer)
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   109
            free (binaer);
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   110
    }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   111
    return EXIT_SUCCESS;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   112
}