关于二进制整数的加减法以及其他运算

关于二进制整数以及IEEE754标准下的加减法以及其他运算(即用代码实现ALU)

  1. 用ALU封装类实现整数二进制的加减乘除以及求余数、逻辑以及算数左右移的方法。
package cpu.alu;
import transformer.Transformer;
import util.BinaryIntegers;
import util.IEEE754Float;
import java.util.Arrays;
public class ALU {
    // 模拟寄存器中的进位标志位    
    private String CF = "0";
    // 模拟寄存器中的溢出标志位    
    private String OF = "0";
    private Transformer t =new transformer();
    //add two integer    
    String add(String src, String dest) {
        return adder(src,dest,'0',32);        
       }
    //补全32位然后进行计算加法
    private String adder(String operand1,String operand2,char c,int length){
    	operand1=impleDigits(operand1,length);
    	operand2=impleDigits(operand2,length);
    	String res=carry_adder(operand1,operand2,c,length);
    	OF=""+addOverFlow(operand1,operand2,res);
    	return res;
    }
    //补全32位方法,reverse原因:StringBuffer方法是在后面加上对应的位数
    private String impleDigits(String operand,int length){
    	int len=length-operand.length();
    	char imple =operand.charAt(0);//根据符号位补全
    	StringBuffer res=new StringBUffer(new StringBuffer(operand).reverse());
    	for(int i=0;i<len,i++){
    		res=res.append(imple);}
    	return res.reverse().toString();}
    //进位加法器,根据第一位是否为1来判断是否有溢出
    private String carry_adder(String operand1,String operand2,char c,int length){
    	operand1=impleDigits(operand1,length);
    	operand2=impleDigits(operand2,length);
    	String res="";
    	char carry=c;
   	for(int i=length-1;i>=0;i--){
       		String temp=fullAdder(operand1.charAt(i),operand2.charAt(i),carry);
       		carry=temp.charAt(0);
       		res=temp.charAt(1)+res;
       }
       	CF=""+carry;
       	return res;
       }
    //单位运算
    private String fullAdder(char x,char y,char c){
    	int bit=(x-'0')^(y-'0')^(c-'0');
    	int carry=((x-'0')&(y-'0'))|((y-'0')&(c-'0'))|((x-'0')&(c-'0'));
    	return ""+carry+bit;
    	}
    private String adderOverFlow(String operand1,String operand2,String result){
    	int X=operand1.charAt(0)-'0';
    	int Y=operand2.charAt(0)-'0';
    	int S=result.charAt(0)-'0';
    	return ""+((~X&~Y&S)|(X&Y&~S));
    	}
    //sub two integer    // dest - src    
    String sub(String src, String dest) {
        return adder(src,negation(dest),'1',32);}
    //将对应的二进制取反
    public String negation(String operand){
    	StringBuffer result=new StringBuffer();
    	for(int i=0;i<operand.length();i++){
    		result=operand.charAt(i)=='1'?result.append("0"):result.append("1");}
    	return result.toString();}

    //signed integer mod    
    String imod(String src, String dest) {
    	String temp=dest;
    	if(src.charAt(0)=='0'&&dest.charAt(0)=='0'){
    	  while(isLarger(temp,src)||temp.equals(src)){
    	    temp=sub(src,temp);}
    	if(src.charAt(0)=='0'&&dest.charAt(0)=='1'){
    	  while(isLarger("00000000000000000000000000000000",temp)||temp.equals("00000000000000000000000000000000")){
    	    temp=add(src,temp);}
    	  temp=sub(src,temp);}
    	if(src.charAt(0)=='1'&&dest.charAt(0)=='0'){
    	  while(isLarger(temp,"00000000000000000000000000000000")||temp.equals("00000000000000000000000000000000")){
    	  temp=add(src,temp);}
    	  temp=sub(src,temp);}
    	if(src.charAt(0)=='1'&&dest.charAt(0)=='1'){
    	  while(isLarger(src,temp)||temp.equals(src)){
    	  temp=sub(src,temp);}}
    	  return temp;
    	}
    String and(String src, String dest) {       
    	char[] result =new char[] ;
    	for(int i=0;i<src.length();i++){
    	   char srcItem=src.charAt(i);
    	   char desItem=dest.charAt(i);
    	   result[i]=(char)(srcItem&desItem);
    	      }
    	StringBuilder resultStr =new StringBuilder(32);
    	for(char s:result){
    	  resultStr.append(s);}
    	return resultStr.toString();}
    String or(String src, String dest) {      
    	char[] result=new char[32];
    	for(int i=0;i<src.length;i++){
    	  char srcItem=src.charAt(i);
    	  char destItem=dest.charAt(i);
    	  result[i]=(char)(srcItem|destItem);}
    	StringBuilder resultStr =new StringBuilder(32);
        for(char s:result){
       	  resultStr.append(s);}
       return resultStr.toString();}

    String xor(String src, String dest) {     
    	char[] result = new char[32];
        for (int i = 0; i < src.length(); i++) {
            char srcItem = src.charAt(i);
            char destItem = dest.charAt(i);
            if (srcItem == destItem)
                result[i] = '0';
            else
                result[i] = '1';
        }
        StringBuilder resultStr = new StringBuilder(32);
        for (char s : result) {
            resultStr.append(s);
        }
        return resultStr.toString();
    }  }
    String shl(String src, String dest) {       
      return sal(src, dest);    }
    String shr(String src, String dest) {      
      int shift = Math.abs(Integer.parseInt(transformer.binaryToInt("0" + src.substring(src.length()-5))));
      char[] fill = new char[32];
      Arrays.fill(fill, '0');
      return new String(fill).concat(dest).substring(32 - shift, 64 - shift);  }
    String sal(String src, String dest) {    
    	int shift = Math.abs(Integer.parseInt(transformer.binaryToInt("0" + src.substring(src.length()-5))));
  	char[] fill = new char[32];
  	Arrays.fill(fill, '0');
  	return dest.substring(shift).concat(new String(fill)).substring(0, 32);//concat为合并    }
    String sar(String src, String dest) {       
    	int shift = Math.abs(Integer.parseInt(transformer.binaryToInt("0" + src.substring(src.length()-5))));
        char[] fill = new char[32];
        Arrays.fill(fill, dest.charAt(0));
        return new String(fill).concat(dest).substring(32 - shift, 64 - shift);    }
    String rol(String src, String dest) {        
    	int shift = Math.abs(Integer.parseInt(transformer.binaryToInt("0" + src.substring(src.length()-5))));
        return dest.substring(shift).concat(dest.substring(0, shift));    }
    String ror(String src, String dest) {        
    	int shift = Math.abs(Integer.parseInt(transformer.binaryToInt("0" + src.substring(src.length()-5))));
        return dest.substring(32 - shift).concat(dest.substring(0, 32 - shift));    }
}

你可能感兴趣的:(计算机组成与结构)