关于二进制整数以及IEEE754标准下的加减法以及其他运算(即用代码实现ALU)
- 用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();
String add(String src, String dest) {
return adder(src,dest,'0',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;
}
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();}
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));
}
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();}
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);
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)); }
}