码迷,mamicode.com
首页 > 其他好文 > 详细

指令系统的性能决定了计算机的基本功能

时间:2018-07-29 22:22:14      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:md5   art   核心   ota   有一个   tor   crc   数据   十六进制   

指令系统的性能决定了计算机的基本功能,它的设计直接关系到计算机的硬件结构和用户的需要。一个完善的指令系统应满足如下四方面的要求:

完备性

指用汇编语言编写各种程序时,指令系统直接提供的指令足够使用,而不必用软件来实现。完备性要求指令系统丰富、功能齐全、使用方便。

有效性

是指利用该指令系统所编写的程序能够高效率地运行。高效率主要表现在程序占据存储空间小、执行速度快。

规整性

包括指令系统的对称性、匀齐性、指令格式和数据格式的一致性。对称性是指:在指令系统中所有的寄存器和存储器单元都可同等对待,所有的指令都可使用各种寻址方式;匀齐性是指:一种操作性质的指令可以支持各种数据类型;指令格式和数据格式的一致性是指:指令长度和数据长度有一定的关系,以方便处理和存取。

兼容性

至少要能做到“向上兼容”,即低档机上运行的软件可以在高档机上运行。

 

  1 package Com.TableText;
  2 
  3 import java.io.File;  
  4 import java.io.FileInputStream;  
  5 import java.io.FileNotFoundException;  
  6 import java.io.IOException;  
  7 import java.math.BigInteger;  
  8 import java.security.MessageDigest;  
  9 import java.util.HashMap;  
 10 import java.util.Map;  
 11 import java.util.zip.CRC32;  
 12 import java.util.zip.CheckedInputStream;  
 13   
 14 /************************************************* 
 15  * md5 类实现了RSA Data Security, Inc.在提交给IETF 的RFC1321中的MD5 message-digest 算法。 
 16  *************************************************/  
 17 public class TableText_05 {  
 18     /* 
 19      * 下面这些S11-S44实际上是一个44的矩阵,在原始的C实现中是用#define 实现的, 这里把它们实现成为static 
 20      * final是表示了只读,切能在同一个进程空间内的多个 Instance间共享 
 21      */  
 22     private static final int S11 = 7;  
 23     private static final int S12 = 12;  
 24     private static final int S13 = 17;  
 25     private static final int S14 = 22;  
 26     private static final int S21 = 5;  
 27     private static final int S22 = 9;  
 28     private static final int S23 = 14;  
 29     private static final int S24 = 20;  
 30     private static final int S31 = 4;  
 31     private static final int S32 = 11;  
 32     private static final int S33 = 16;  
 33     private static final int S34 = 23;  
 34     private static final int S41 = 6;  
 35     private static final int S42 = 10;  
 36     private static final int S43 = 15;  
 37     private static final int S44 = 21;  
 38     private static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  
 39             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  
 40             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  
 41             0, 0, 0, 0, 0, 0, 0 };  
 42     /* 
 43      * 下面的三个成员是MD5计算过程中用到的3个核心数据,在原始的C实现中 被定义到MD5_CTX结构中 
 44      */  
 45     private long[] state = new long[4]; // state (ABCD)  
 46     private long[] count = new long[2]; // number of bits, modulo 2^64 (lsb  
 47                                         // first)  
 48     private byte[] buffer = new byte[64]; // input buffer  
 49     /* 
 50      * digestHexStr是MD5的唯一一个公共成员,是最新一次计算结果的 16进制ASCII表示. 
 51      */  
 52     private String digestHexStr;  
 53     /* 
 54      * digest,是最新一次计算结果的2进制内部表示,表示128bit的MD5值. 
 55      */  
 56     private byte[] digest = new byte[16];  
 57   
 58     /** 
 59      * getMD5ofStr是类MD5最主要的公共方法,入口参数是你想要进行MD5变换的字符串 
 60      * 返回的是变换完的结果,这个结果是从公共成员digestHexStr取得的. 
 61      */  
 62     public String getMD5ofStr(String inbuf) {  
 63         md5Init();  
 64         md5Update(inbuf.getBytes(), inbuf.length());  
 65         md5Final();  
 66         digestHexStr = "";  
 67         for (int i = 0; i < 16; i++) {  
 68             digestHexStr += byteHEX(digest[i]);  
 69         }  
 70         return digestHexStr;  
 71     }  
 72   
 73     // 这是MD5这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数  
 74     public TableText_05() {  
 75         md5Init();  
 76         return;  
 77     }  
 78   
 79     /* md5Init是一个初始化函数,初始化核心变量,装入标准的幻数 */  
 80     private void md5Init() {  
 81         count[0] = 0L;  
 82         count[1] = 0L;  
 83         // /* Load magic initialization constants.  
 84         state[0] = 0x67452301L;  
 85         state[1] = 0xefcdab89L;  
 86         state[2] = 0x98badcfeL;  
 87         state[3] = 0x10325476L;  
 88         return;  
 89     }  
 90   
 91     /*  
 92      * F, G, H ,I 是4个基本的MD5函数,在原始的MD5的C实现中,由于它们是  
 93      * 简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们 实现成了private方法,名字保持了原来C中的。  
 94      */  
 95     private long F(long x, long y, long z) {  
 96         return (x & y) | ((~x) & z);  
 97     }  
 98   
 99     private long G(long x, long y, long z) {  
100         return (x & z) | (y & (~z));  
101     }  
102   
103     private long H(long x, long y, long z) {  
104         return x ^ y ^ z;  
105     }  
106   
107     private long I(long x, long y, long z) {  
108         return y ^ (x | (~z));  
109     }  
110   
111     /* 
112      * Rotation is separate from addition to prevent recomputation. 
113      */  
114     private long FF(long a, long b, long c, long d, long x, long s, long ac) {  
115         a += F(b, c, d) + x + ac;  
116         a = ((int) a << s) | ((int) a >>> (32 - s));  
117         a += b;  
118         return a;  
119     }  
120   
121     private long GG(long a, long b, long c, long d, long x, long s, long ac) {  
122         a += G(b, c, d) + x + ac;  
123         a = ((int) a << s) | ((int) a >>> (32 - s));  
124         a += b;  
125         return a;  
126     }  
127   
128     private long HH(long a, long b, long c, long d, long x, long s, long ac) {  
129         a += H(b, c, d) + x + ac;  
130         a = ((int) a << s) | ((int) a >>> (32 - s));  
131         a += b;  
132         return a;  
133     }  
134   
135     private long II(long a, long b, long c, long d, long x, long s, long ac) {  
136         a += I(b, c, d) + x + ac;  
137         a = ((int) a << s) | ((int) a >>> (32 - s));  
138         a += b;  
139         return a;  
140     }  
141   
142     /* 
143      * md5Update是MD5的主计算过程,inbuf是要变换的字节串,inputlen是长度,这个 
144      * 函数由getMD5ofStr调用,调用之前需要调用md5init,因此把它设计成private的 
145      */  
146     private void md5Update(byte[] inbuf, int inputLen) {  
147         int i, index, partLen;  
148         byte[] block = new byte[64];  
149         index = (int) (count[0] >>> 3) & 0x3F;  
150         // /* Update number of bits */  
151         if ((count[0] += (inputLen << 3)) < (inputLen << 3))  
152             count[1]++;  
153         count[1] += (inputLen >>> 29);  
154         partLen = 64 - index;  
155         // Transform as many times as possible.  
156         if (inputLen >= partLen) {  
157             md5Memcpy(buffer, inbuf, index, 0, partLen);  
158             md5Transform(buffer);  
159             for (i = partLen; i + 63 < inputLen; i += 64) {  
160                 md5Memcpy(block, inbuf, 0, i, 64);  
161                 md5Transform(block);  
162             }  
163             index = 0;  
164         } else  
165             i = 0;  
166         // /* Buffer remaining input */  
167         md5Memcpy(buffer, inbuf, index, i, inputLen - i);  
168     }  
169   
170     /* 
171      * md5Final整理和填写输出结果 
172      */  
173     private void md5Final() {  
174         byte[] bits = new byte[8];  
175         int index, padLen;  
176         // /* Save number of bits */  
177         Encode(bits, count, 8);  
178         // /* Pad out to 56 mod 64.  
179         index = (int) (count[0] >>> 3) & 0x3f;  
180         padLen = (index < 56) ? (56 - index) : (120 - index);  
181         md5Update(PADDING, padLen);  
182         // /* Append length (before padding) */  
183         md5Update(bits, 8);  
184         // /* Store state in digest */  
185         Encode(digest, state, 16);  
186     }  
187   
188     /* 
189      * md5Memcpy是一个内部使用的byte数组的块拷贝函数,从input的inpos开始把len长度的 
190      * 字节拷贝到output的outpos位置开始 
191      */  
192     private void md5Memcpy(byte[] output, byte[] input, int outpos, int inpos,  
193             int len) {  
194         int i;  
195         for (i = 0; i < len; i++)  
196             output[outpos + i] = input[inpos + i];  
197     }  
198   
199     /* 
200      * md5Transform是MD5核心变换程序,有md5Update调用,block是分块的原始字节 
201      */  
202     private void md5Transform(byte block[]) {  
203         long a = state[0], b = state[1], c = state[2], d = state[3];  
204         long[] x = new long[16];  
205         Decode(x, block, 64);  
206   
207         /* Round 1 */  
208         a = FF(a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */  
209         d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */  
210         c = FF(c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */  
211         b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */  
212         a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */  
213         d = FF(d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */  
214         c = FF(c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */  
215         b = FF(b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */  
216         a = FF(a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */  
217         d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */  
218         c = FF(c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */  
219         b = FF(b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */  
220         a = FF(a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */  
221         d = FF(d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */  
222         c = FF(c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */  
223         b = FF(b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */  
224         /* Round 2 */  
225         a = GG(a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */  
226         d = GG(d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */  
227         c = GG(c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */  
228         b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */  
229         a = GG(a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */  
230         d = GG(d, a, b, c, x[10], S22, 0x2441453L); /* 22 */  
231         c = GG(c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */  
232         b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */  
233         a = GG(a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */  
234         d = GG(d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */  
235         c = GG(c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */  
236         b = GG(b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */  
237         a = GG(a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */  
238         d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */  
239         c = GG(c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */  
240         b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */  
241         /* Round 3 */  
242         a = HH(a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */  
243         d = HH(d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */  
244         c = HH(c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */  
245         b = HH(b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */  
246         a = HH(a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */  
247         d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */  
248         c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */  
249         b = HH(b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */  
250         a = HH(a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */  
251         d = HH(d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */  
252         c = HH(c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */  
253         b = HH(b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */  
254         a = HH(a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */  
255         d = HH(d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */  
256         c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */  
257         b = HH(b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */  
258         /* Round 4 */  
259         a = II(a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */  
260         d = II(d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */  
261         c = II(c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */  
262         b = II(b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */  
263         a = II(a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */  
264         d = II(d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */  
265         c = II(c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */  
266         b = II(b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */  
267         a = II(a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */  
268         d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */  
269         c = II(c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */  
270         b = II(b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */  
271         a = II(a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */  
272         d = II(d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */  
273         c = II(c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */  
274         b = II(b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */  
275         state[0] += a;  
276         state[1] += b;  
277         state[2] += c;  
278         state[3] += d;  
279     }  
280   
281     /* 
282      * Encode把long数组按顺序拆成byte数组,因为java的long类型是64bit的, 只拆低32bit,以适应原始C实现的用途 
283      */  
284     private void Encode(byte[] output, long[] input, int len) {  
285         int i, j;  
286         for (i = 0, j = 0; j < len; i++, j += 4) {  
287             output[j] = (byte) (input[i] & 0xffL);  
288             output[j + 1] = (byte) ((input[i] >>> 8) & 0xffL);  
289             output[j + 2] = (byte) ((input[i] >>> 16) & 0xffL);  
290             output[j + 3] = (byte) ((input[i] >>> 24) & 0xffL);  
291         }  
292     }  
293   
294     /* 
295      * Decode把byte数组按顺序合成成long数组,因为java的long类型是64bit的, 
296      * 只合成低32bit,高32bit清零,以适应原始C实现的用途 
297      */  
298     private void Decode(long[] output, byte[] input, int len) {  
299         int i, j;  
300         for (i = 0, j = 0; j < len; i++, j += 4)  
301             output[i] = b2iu(input[j]) | (b2iu(input[j + 1]) << 8)  
302                     | (b2iu(input[j + 2]) << 16) | (b2iu(input[j + 3]) << 24);  
303         return;  
304     }  
305   
306     /** 
307      * b2iu是我写的一个把byte按照不考虑正负号的原则的"升位"程序,因为java没有unsigned运算 
308      */  
309     public static long b2iu(byte b) {  
310         return b < 0 ? b & 0x7F + 128 : b;  
311     }  
312   
313     /** 
314      * byteHEX(),用来把一个byte类型的数转换成十六进制的ASCII表示, 
315      * 因为java中的byte的toString无法实现这一点,我们又没有C语言中的 sprintf(outbuf,"%02X",ib) 
316      */  
317     public static String byteHEX(byte ib) {  
318         char[] Digit = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘A‘,  
319                 ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘ };  
320         char[] ob = new char[2];  
321         ob[0] = Digit[(ib >>> 4) & 0X0F];  
322         ob[1] = Digit[ib & 0X0F];  
323         String s = new String(ob);  
324         return s;  
325     }  
326       
327       
328     //-----------------------------------------------------------  
329   
330     /** 
331      * 获取单个文件的MD5值! 
332      * @param file 
333      * @return 
334      */  
335     public static String getFileMD5(File file) {  
336         if (!file.isFile()) {  
337             return null;  
338         }  
339         MessageDigest digest = null;  
340         FileInputStream in = null;  
341         byte buffer[] = new byte[1024];  
342         int len;  
343         try {  
344             digest = MessageDigest.getInstance("MD5");  
345             in = new FileInputStream(file);  
346             while ((len = in.read(buffer, 0, 1024)) != -1) {  
347                 digest.update(buffer, 0, len);  
348             }  
349             in.close();  
350         } catch (Exception e) {  
351             e.printStackTrace();  
352             return null;  
353         }  
354         BigInteger bigInt = new BigInteger(1, digest.digest());  
355         return bigInt.toString(16).toUpperCase();  
356     }  
357         
358     /** 
359      * 获取单个文件的SHA1值! 
360      *  
361      * @param file 
362      * @return 
363      */  
364     public static String getFileSHA1(File file) {  
365         if (!file.isFile()) {  
366             return null;  
367         }  
368         MessageDigest digest = null;  
369         FileInputStream in = null;  
370         byte buffer[] = new byte[1024];  
371         int len;  
372         try {  
373             digest = MessageDigest.getInstance("SHA1");  
374             in = new FileInputStream(file);  
375             while ((len = in.read(buffer, 0, 1024)) != -1) {  
376                 digest.update(buffer, 0, len);  
377             }  
378             in.close();  
379         } catch (Exception e) {  
380             e.printStackTrace();  
381             return null;  
382         }  
383         BigInteger bigInt = new BigInteger(1, digest.digest());  
384         return bigInt.toString(16).toUpperCase();  
385     }  
386       
387     /** 
388      * 获取单个文件的CRC32值! 
389      * @param file 
390      * @return 
391      * @throws Exception 
392      */  
393     public static String getFileCRC32(File file) {  
394         FileInputStream fileinputstream;  
395         try {  
396             fileinputstream = new FileInputStream(file);  
397             CRC32 crc32 = new CRC32();  
398             for (CheckedInputStream checkedinputstream = new CheckedInputStream(  
399                     fileinputstream, crc32); checkedinputstream.read() != -1;) {  
400             }  
401             return Long.toHexString(crc32.getValue());  
402         } catch (FileNotFoundException e) {  
403             e.printStackTrace();  
404         } catch (IOException e) {  
405             e.printStackTrace();  
406         }  
407         return null;  
408     }  
409   
410       
411     /** 
412      * 获取文件夹中文件的MD5值 
413      *  
414      * @param file 
415      * @param listChild 
416      *            ;true递归子目录中的文件 
417      * @return 
418      */  
419     public static Map<String, String> getDirMD5(File file, boolean listChild) {  
420         if (!file.isDirectory()) {  
421             return null;  
422         }  
423       
424         Map<String, String> map = new HashMap<String, String>();  
425         String md5;  
426         File files[] = file.listFiles();  
427         for (int i = 0; i < files.length; i++) {  
428             File f = files[i];  
429             if (f.isDirectory() && listChild) {  
430                 map.putAll(getDirMD5(f, listChild));  
431             } else {  
432                 md5 = getFileMD5(f);  
433                 if (md5 != null) {  
434                     map.put(f.getPath(), md5);  
435                 }  
436             }  
437         }  
438         return map;  
439     }  
440       
441     /** 
442      * 获取文件夹中文件的SHA1值 
443      *  
444      * @param file 
445      * @param listChild 
446      *            ;true递归子目录中的文件 
447      * @return 
448      */  
449     public static Map<String, String> getDirSHA1(File file, boolean listChild) {  
450         if (!file.isDirectory()) {  
451             return null;  
452         }  
453        
454         Map<String, String> map = new HashMap<String, String>();  
455         String sha1;  
456         File files[] = file.listFiles();  
457         for (int i = 0; i < files.length; i++) {  
458             File f = files[i];  
459             if (f.isDirectory() && listChild) {  
460                 map.putAll(getDirSHA1(f, listChild));  
461             } else {  
462                 sha1 = getFileSHA1(f);  
463                 if (sha1 != null) {  
464                     map.put(f.getPath(), sha1);  
465                 }  
466             }  
467         }  
468         return map;  
469     }  
470   
471     /** 
472      * 获取文件夹中文件的CRC32值 
473      *  
474      * @param file 
475      * @param listChild 
476      *            ;true递归子目录中的文件 
477      * @return 
478      */  
479     public static Map<String, String> getDirCRC32(File file, boolean listChild) {  
480         if (!file.isDirectory()) {  
481             return null;  
482         }  
483       
484         Map<String, String> map = new HashMap<String, String>();  
485         String crc32;  
486         File files[] = file.listFiles();  
487         for (int i = 0; i < files.length; i++) {  
488             File f = files[i];  
489             if (f.isDirectory() && listChild) {  
490                 map.putAll(getDirCRC32(f, listChild));  
491             } else {  
492                 crc32 = getFileCRC32(f);  
493                 if (crc32 != null) {  
494                     map.put(f.getPath(), crc32);  
495                 }  
496             }  
497         }  
498         return map;  
499     }  
500 }  

 

指令系统的性能决定了计算机的基本功能

标签:md5   art   核心   ota   有一个   tor   crc   数据   十六进制   

原文地址:https://www.cnblogs.com/borter/p/9387709.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!