📄 assemblyinstruction.java
字号:
import java.text.*;
import java.util.*;
/**
*
* Instances of the <code>AssemblyInstruction</code> class represent
* assembly instructions of the CPU. The class contains methods to convert
* instructions between their equivalent mnemonic forms and code forms, and
* to check their validity.
*
* @author Benedicto J. Catalan Jr.
* @version 1.0, 02/13/2000
* @since CPU Simulator 1.1
*/
public
class AssemblyInstruction
{
// String constants for each assembly instruction
// public static final String NOP = new String( "NOP" );
// public static final String LDAC = new String( "LDAC" );
// public static final String STAC = new String( "STAC" );
// public static final String MVAC = new String( "MVAC" );
// public static final String MOVR = new String( "MOVR" );
// public static final String JMPZ = new String( "JMPZ" );
// public static final String JPNZ = new String( "JPNZ" );
// public static final String SUB = new String( "SUB" );
// public static final String CLAC = new String( "CLAC" );
// public static final String OR = new String( "OR" );
// public static final String XOR = new String( "XOR" );
// public static final String NOT = new String( "NOT" );
public static final String AND = new String( "AND" );
public static final String INC = new String( "INC" );
public static final String ADD = new String( "ADD" );
public static final String JMP = new String( "JMP" );
public static final String END = new String( "END" );
// public static final short opcodeNOP = 0x00;
// public static final short opcodeLDAC = 0x01;
// public static final short opcodeSTAC = 0x02;
// public static final short opcodeMVAC = 0x03;
// public static final short opcodeMOVR = 0x04;
// public static final short opcodeOR = 0x0d;
// public static final short opcodeXOR = 0x0e;
// public static final short opcodeNOT = 0x0f;
// public static final short opcodeJMPZ = 0x06;
// public static final short opcodeJPNZ = 0x07;
// public static final short opcodeSUB = 0x09;
// public static final short opcodeCLAC = 0x0b;
public static final short opcodeADD = 0x00;
public static final short opcodeAND = 0x01;
public static final short opcodeJMP = 0x02;
public static final short opcodeINC = 0x03;
public static final short opcodeEND = 0xff;
/**
* Converts a string to its equivalent code byte.
*
* @param mnemonic the assembly mnemonic to be converted
* @return the code byte as a short, or <code>-1</code> if the string
* is not an assembly mnemonic.
* @since CPU Simulator 1.1
*/
public static short toMnemonicCode( String mnemonic )
{
if ( JMP.equalsIgnoreCase( mnemonic ) )
{
return ( opcodeJMP );
}
else if ( ADD.equalsIgnoreCase( mnemonic ) )
{
return ( opcodeADD );
}
else if ( INC.equalsIgnoreCase( mnemonic ) )
{
return ( opcodeINC );
}
else if ( AND.equalsIgnoreCase( mnemonic ) )
{
return ( opcodeAND );
}
else if ( END.equalsIgnoreCase( mnemonic ) )
{
return ( opcodeEND );
}
else
{
// Return -1 if failed to match assembly mnemonic
return ( -1 );
}
}
/**
* Converts a code byte to its equivalent assembly mnemonic.
*
* @param mnemonic the code byte to be converted as a short
* @return the assembly mnemonic, or <code>null</code> if the code
* byte does not represent an assembly mnemonic.
* @since CPU Simulator 1.1
*/
public static String toMnemonic( short code )
{
switch ( code )
{
case opcodeJMP:
return ( JMP );
case opcodeADD:
return ( ADD );
case opcodeINC:
return ( INC );
case opcodeAND:
return ( AND );
case opcodeEND:
return ( END );
default:
return ( null );
}
}
public static short[] toInstructionCode( String[] instruction )
{
if ( ( instruction == null ) || ( instruction.length == 0 ) )
{
return ( null );
}
else
{
if ( ( instruction[ 0 ] != null ) &&
instruction[ 0 ].equalsIgnoreCase( INC )
|| instruction[ 0 ].equalsIgnoreCase( END )
&& ( instruction.length < 2 ) )
{
short[] instructionCode = new short[ 1 ];
instructionCode[ 0 ] = toMnemonicCode( instruction[ 0 ] );
return ( instructionCode );
}
else if ( ( instruction[ 0 ] != null ) && instruction[ 0 ].
equalsIgnoreCase( JMP ) || instruction[ 0 ].
equalsIgnoreCase( ADD ) || instruction[ 0 ].
equalsIgnoreCase( AND ) && ( instruction.length < 3 )
&& isAddress( instruction[ 1 ] ) )
{
short[] instructionCode = new short[ 3 ];
short[] addressCode = toAddressCode( toAddressInteger(
instruction[ 1 ] ) );
instructionCode[ 0 ] = toMnemonicCode( instruction[ 0 ] );
instructionCode[ 1 ] = addressCode[ 0 ];
instructionCode[ 2 ] = addressCode[ 1 ];
return ( instructionCode );
}
return ( null );
}
}
/**
* Converts a string to the equivalent numeric address as an integer.
*
* @param addressString string representation of an assembly
* address
* @return the address as an integer, or <code>-1</code> if the string
* does not represent a valid address
* @exception NumberFormatException if the string does not represent a
* valid numeric address.
* @since CPU Simulator 1.1
*/
public static int toAddressInteger( String addressString )
{
int address;
if ( ( addressString != null ) && ( ! addressString.equals( "" ) ) )
{
char baseSpecifier = addressString.charAt( addressString.length() -
1 );
int addressRadix = toRadix( baseSpecifier );
if ( addressRadix != -1 )
{
addressString = addressString.substring( 0, addressString.
length() - 1 );
}
else
{
addressRadix = 10;
}
try
{
address = Integer.valueOf( addressString, addressRadix ).
intValue();
}
catch ( NumberFormatException e )
{
return ( -1 );
}
}
else
{
return ( -1 );
}
if ( isAddress( address ) )
{
return ( address );
}
else
{
return ( -1 );
}
}
/**
* Converts an integer to the equivalent code bytes.
*
* @param address the assembly address
* @return array of short containing the equivalent code bytes, or
* <code>null</code> if the integer is not a valid address
* @since CPU Simulator 1.1
*/
public static short[] toAddressCode( int address )
{
if ( isAddress( address ) )
{
short[] codeByteArray = { ( short ) ( address % 0x100 ), ( short )
( ( address / 0x100 ) % 0x100 ) };
return ( codeByteArray );
}
else
{
return ( null );
}
}
/**
* Returns radix corresponding to the base specifier character.
*
* @param ch the base specifier character.
* @return radix as an integer, or <code>-1</code> if character is not
* a valid base specifier.
* @since CPU Simulator 1.1
*/
public static int toRadix( char ch )
{
ch = Character.toLowerCase( ch );
if ( ch == 'b' )
{
return ( 2 );
}
else if ( ( ch == 'o' ) || ( ch == 'q' ) )
{
return ( 8 );
}
else if ( ch == 'd' )
{
return ( 10 );
}
else if ( ch == 'h' )
{
return ( 16 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -