author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sat, 13 Dec 2008 17:58:06 +0100 | |
changeset 13 | 2640a7fd96ca |
parent 9 | c3fecc82ade6 |
child 29 | 7abf6146898e |
permissions | -rw-r--r-- |
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; |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
31 |
int i; |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
32 |
|
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
33 |
i = 0; |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
34 |
|
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
35 |
if ((decimal > MAXVALUE)) { |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
36 |
perror ("VALUE TOO BIG"); |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
37 |
return NULL; |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
38 |
} |
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 |
if ((encoded = malloc ((MAXBITS) * sizeof (char))) == NULL) { |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
41 |
perror ("MALLOC ERROR"); |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
42 |
return NULL; |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
43 |
} |
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 |
while (decimal > base - 1) { |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
46 |
encoded[i++] = (decimal % base); |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
47 |
decimal /= base; |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
48 |
} |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
49 |
encoded[i++] = (decimal % base); |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
50 |
*digits = i; |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
51 |
|
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
52 |
for (i = 0; i < *digits - 1; i++) { |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
53 |
swp = encoded[*digits - i - 1]; |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
54 |
encoded[*digits - i - 1] = encoded[i]; |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
55 |
encoded[i] = swp; |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
56 |
} |
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 |
return encoded; |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
59 |
} |
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 |
int main (int argc, char **argv) |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
62 |
{ |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
63 |
ULONG decimal, start, end; |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
64 |
int bits, base, i; |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
65 |
char *binaer; |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
66 |
|
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
67 |
base = 2; |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
68 |
|
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
69 |
if (argc == 2) { |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
70 |
start = atol (argv[1]); |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
71 |
end = start + 1; |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
72 |
} else if (argc == 3) { |
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 = atol (argv[2]); |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
75 |
} else if (argc == 4) { |
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 |
base = atoi (argv[3]); |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
79 |
} else { |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
80 |
usage (argv[0]); |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
81 |
return EXIT_SUCCESS; |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
82 |
} |
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 |
if (!((start >= 0) && (end > start) && (base > 1))) { |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
85 |
usage ("Trottel"); |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
86 |
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
|
87 |
return EXIT_SUCCESS; |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
88 |
} |
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 |
printf ("\tVALUE \t\tDIGITS\t\t Encoded\n"); |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
91 |
printf ("----------------------------------------------------\n"); |
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 |
for (decimal = start; decimal < end; decimal++) { |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
94 |
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
|
95 |
return EXIT_FAILURE; |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
96 |
} |
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 |
printf ("%12ld %12d \t\t", decimal, bits); |
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 |
for (i = 0; i < bits; i++) { |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
101 |
printf ("%X", binaer[i]); |
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 |
printf ("\n"); |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
104 |
|
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
105 |
if (binaer) |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
106 |
free (binaer); |
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 |
return EXIT_SUCCESS; |
d752cbe8208e
asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
109 |
} |