arm64: codea/abgabe_aa.0
[uebersetzerbau-ss10.git] / asmb / asmbtest.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 extern unsigned char *asmb(unsigned char *a);
6
7 unsigned char *asmb_ref(unsigned char *s)
8 {
9         unsigned long i;
10         for (i=0; s[i]; i++) {
11                 unsigned char c=s[i];
12                 c += (c>='A' && c<='Z') ? 'a'-'A' : 0;
13                 s[i] = c;
14         }
15         return s;
16 }
17
18 static char ascii(char s) {
19         if(s < 0x20) return '.';
20         if(s > 0x7E) return '.';
21         return s;
22 }
23
24 static void hexdump(void *d, int len) {
25         unsigned char *data;
26         int i, off;
27         data = (unsigned char*)d;
28         for (off=0; off<len; off += 16) {
29                 printf("\t%08x  ",off);
30                 for(i=0; i<16; i++)
31                         if((i+off)>=len) printf("   ");
32                         else printf("%02x ",data[off+i]);
33
34                 printf(" ");
35                 for(i=0; i<16; i++)
36                         if((i+off)>=len) printf(" ");
37                         else printf("%c",ascii(data[off+i]));
38                 printf("\n");
39         }
40 }
41
42 static void fillregisters(void)
43 {
44         __asm__("push %rdx\n\t");
45         __asm__("mov $12345678, %rdx\n\t");
46
47         __asm__("movq %rdx, %xmm0\n\t");
48         __asm__("movq %rdx, %xmm1\n\t");
49         __asm__("movq %rdx, %xmm2\n\t");
50         __asm__("movq %rdx, %xmm3\n\t");
51         __asm__("movq %rdx, %xmm4\n\t");
52         __asm__("movq %rdx, %xmm5\n\t");
53         __asm__("movq %rdx, %xmm6\n\t");
54         __asm__("movq %rdx, %xmm7\n\t");
55         __asm__("movq %rdx, %xmm8\n\t");
56         __asm__("movq %rdx, %xmm9\n\t");
57         __asm__("movq %rdx, %xmm10\n\t");
58         __asm__("movq %rdx, %xmm11\n\t");
59         __asm__("movq %rdx, %xmm12\n\t");
60         __asm__("movq %rdx, %xmm13\n\t");
61         __asm__("movq %rdx, %xmm14\n\t");
62         __asm__("movq %rdx, %xmm15\n\t");
63         __asm__("pop %rdx\n\t");
64 }
65
66
67 #define NUM_TESTCASES 19
68 int main()
69 {
70         char *input[NUM_TESTCASES]={
71                 "AAaaB\0BBUUUUZZZZ",
72                 "AAaaBBB\0",
73                 "AaA\0ABBB",
74                 "A\0ABCDEF",
75                 "foofuuMUHkk", 
76                 "AbC", 
77                 "BLA|MUHMKUH|KA", 
78                 "ASDFNERABHDFKHDFKLGJAHGLKAHGLKASHGEARNAKLVNLVAANLSADJVHASDLGH", 
79                 "asdfABCDEFGHKL544", 
80                 "asdfA\0BCDEFGHKL5", 
81                 "foofuuMUHkk\0AAAA", 
82                 "AbC\0AAAAAAAAAAAA", 
83                 "BLA|MUHMKUH|KAA\0", 
84                 "ASDFASDFasdfasdfaBC\0AAAABBBBCCCC", 
85                 "ASDFASDFasdfasdfaBC0AAAABBBBCCCCmuhKA\0asASDFasdf",
86                 "ASas\0ASas",
87                 "asdfABCDEFGHKL54",
88                 "asdffvdfgerrggre\0",
89                 "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ1234567890allyourbasearebelongtousALLYOURBASEAREBELONGTOUS1337423133711111!!!!elfeinscos(0)sin(M_PI/2)MASTEROFDESTRUCTIONlordoftheuniverseFA!LblogICANhascheezburgericanHASCHEEZBURGERRickROLLrollRICK O RLY? you got 288 IMiss Teen South Carolina"
90         };
91         int len[NUM_TESTCASES+1] = {
92                 16,
93                 8,
94                 8,
95                 8,
96                 11,
97                 3,
98                 14,
99                 61,
100                 17,
101                 16,
102                 16,
103                 16,
104                 16,
105                 32,
106                 48,
107                 9,
108                 16,
109                 17,
110                 296,
111                 31337
112         };
113         int off[NUM_TESTCASES+1] = {
114                 0,
115                 0,
116                 0,
117                 0,
118                 1,
119                 2,
120                 2,
121                 1,
122                 3,
123                 6,
124                 5,
125                 6,
126                 6,
127                 2,
128                 0,
129                 0,
130                 0,
131                 0,
132                 5,
133                 10,
134         };
135         char *output_our, *output_ref;
136         char *input_our,  *input_ref;
137         int right=0, wrong=0, neither=0, i;
138
139         for(i = 0; i < NUM_TESTCASES+1; i++) {
140                 input_our = (char *) calloc (len[i], 16);
141                 input_ref = (char *) calloc (len[i], 16);
142
143                 if(i == NUM_TESTCASES) {
144                         long u;
145                         for(u=0; u < len[i]; u++) {
146                                 input_our[u] = input_ref[u] = 'S';
147                         }
148                         input_our[len[i]] = input_ref[len[i]] = '\0';
149                 } else {
150                         (void) memcpy(input_our, input[i], len[i]+1);
151                         (void) memcpy(input_ref, input[i], len[i]+1);
152                 }
153
154                 fillregisters();
155                 output_our = (char *) asmb((unsigned char *) input_our+off[i]);
156                 output_ref = (char *) asmb_ref((unsigned char *) input_ref+off[i]);
157
158                 if(memcmp(output_our,output_ref, len[i]) != 0) {
159                         if(strncmp(output_our, output_ref, len[i]) == 0) {
160                                 neither++;
161                                 printf("Testfall%02i nach Nullbyte ungleich\n", i);
162                         }
163                         else {
164                                 wrong++;
165                                 printf("Testfall%02i falsch!\n", i);
166                         }
167
168                         if (i < NUM_TESTCASES) {
169                                 /* beim "spezialfall" wuerde das boese enden */
170                                 printf("Input(\"%s\"):\n", input[i]);
171                                 hexdump(input[i], len[i]);
172                         }
173
174                         printf("\nerwartet:\n");
175                         hexdump(output_ref, len[i]);
176
177                         printf("\ntatsaechliches Ergebnis:\n");
178                         hexdump(output_our, len[i]);
179                         printf("\n");
180                 }
181                 else {
182                         right++;
183                         printf("Testfall%02i korrekt\n", i);
184                 }
185                 free(input_our);
186                 free(input_ref);
187         }
188         printf("========\n%2i Testfaelle sind korrekt\n%2i Testfaelle sind nach dem Nullbyte ungleich\n"
189                         "%2i Testfaelle sind falsch\n", right, neither, wrong);
190         return 0;
191 }
192