📄 assemblyinstruction.java
字号:
else
{
return ( -1 );
}
}
public static boolean isConstantByte( short num )
{
return ( ( num >= 0 ) && ( num <= 0xFF ) );
}
public static boolean isConstantWord( int num )
{
return ( ( num >= 0 ) && ( num <= 0xFFFF ) );
}
/**
* Determines whether or not the specified string is a valid assembly
* mnemonic.
*
* @param str the string to be checked.
* @return <code>true</code> if the string is a valid assembly
* mnemonic; <code>false</code> otherwise.
* @since CPU Simulator 1.1
*/
public static boolean isMnemonic( String str )
{
if ( JMP.equalsIgnoreCase( str ) ||
ADD.equalsIgnoreCase( str ) ||
INC.equalsIgnoreCase( str ) ||
AND.equalsIgnoreCase( str ) ||
END.equalsIgnoreCase( str ) )
{
return true;
}
return false;
}
/**
* Determines whether or not the specified integer is a valid assembly
* address.
*
* @param num the integer to be checked.
* @return <code>true</code> if the integer is a valid assembly
* address; <code>false</code> otherwise.
* @since CPU Simulator 1.1
*/
public static boolean isAddress( int num )
{
return ( ( num >= 0 ) && ( num <= 0xFFFF ) );
}
public static boolean isByte( short num )
{
return ( ( num >= 0 ) && ( num <= 0xFF ) );
}
public static boolean isWord( int num )
{
return ( ( num >= 0 ) && ( num <= 0xFFFF ) );
}
/**
* Determines whether or not the specified string is a valid assembly
* address.
*
* @param str the string to be checked.
* @return <code>true</code> if the string is a valid assembly
* address; <code>false</code> otherwise.
* @since CPU Simulator 1.1
*/
public static boolean isAddress( String str )
{
return ( toAddressInteger( str ) != -1 );
}
/**
* Determines whether or not the specified strings are valid assembly
* operands.
*
* @param str the string to be checked.
* @return <code>true</code> if the string is a valid assembly
* operand; <code>false</code> otherwise.
* @since CPU Simulator 1.1
*/
public static boolean isValidOperands( String mnemonic, String[]
operands )
{
/* if ( mnemonic.equalsIgnoreCase( NOP ) || mnemonic.equalsIgnoreCase(
MVAC ) || mnemonic.equalsIgnoreCase( MOVR ) || mnemonic.
equalsIgnoreCase( ADD ) || mnemonic.equalsIgnoreCase( SUB ) ||
mnemonic.equalsIgnoreCase( INC ) || mnemonic.equalsIgnoreCase(
CLAC ) || mnemonic.equalsIgnoreCase( AND ) || mnemonic.
equalsIgnoreCase( OR ) || mnemonic.equalsIgnoreCase( XOR ) ||
mnemonic.equalsIgnoreCase( NOT ) || mnemonic.equalsIgnoreCase(
END ) )
*/
if ( mnemonic.equalsIgnoreCase( INC )
|| mnemonic.equalsIgnoreCase( END ) )
{
return ( ( operands == null ) || ( operands.length == 0 ) || ( (
operands.length == 1 ) && ( operands[ 0 ].equals( "" ) ) ) );
}
/* else if ( mnemonic.equalsIgnoreCase( LDAC ) || mnemonic.
equalsIgnoreCase( STAC ) || mnemonic.equalsIgnoreCase( JMP ) ||
mnemonic.equalsIgnoreCase( JMPZ ) || mnemonic.equalsIgnoreCase(
JPNZ ) )
*/
else if ( mnemonic.equalsIgnoreCase( ADD ) ||
mnemonic.equalsIgnoreCase( AND ) ||
mnemonic.equalsIgnoreCase( JMP ) )
{
return ( ( operands != null ) && ( operands.length == 1 ) &&
isAddress( operands[ 0 ] ) );
}
return ( false );
}
public static int expectsOperands( String mnemonic )
{
/* if ( mnemonic.equalsIgnoreCase( NOP ) || mnemonic.equalsIgnoreCase(
MVAC ) || mnemonic.equalsIgnoreCase( MOVR ) || mnemonic.
equalsIgnoreCase( ADD ) || mnemonic.equalsIgnoreCase( SUB ) ||
mnemonic.equalsIgnoreCase( INC ) || mnemonic.equalsIgnoreCase(
CLAC ) || mnemonic.equalsIgnoreCase( AND ) || mnemonic.
equalsIgnoreCase( OR ) || mnemonic.equalsIgnoreCase( XOR ) ||
mnemonic.equalsIgnoreCase( NOT ) || mnemonic.equalsIgnoreCase(
END ) )
*/
if ( mnemonic.equalsIgnoreCase( INC )
|| mnemonic.equalsIgnoreCase( END ) )
{
return ( 0 );
}
/* else if ( mnemonic.equalsIgnoreCase( LDAC ) || mnemonic.
equalsIgnoreCase( STAC ) || mnemonic.equalsIgnoreCase( JMP ) ||
mnemonic.equalsIgnoreCase( JMPZ ) || mnemonic.equalsIgnoreCase(
JPNZ ) )
*/
else if ( mnemonic.equalsIgnoreCase( ADD ) ||
mnemonic.equalsIgnoreCase( AND ) ||
mnemonic.equalsIgnoreCase( JMP ) )
{
return ( 1 );
}
return ( -1 );
}
public static short toByteShort( String byteString )
{
short byteShort;
if ( ( byteString != null ) && ( ! byteString.equals( "" ) ) )
{
char baseSpecifier = byteString.charAt( byteString.length() - 1 );
int byteRadix = toRadix( baseSpecifier );
if ( byteRadix != -1 )
{
byteString = byteString.substring( 0, byteString.length() - 1 );
}
else
{
byteRadix = 10;
}
try
{
byteShort = Short.valueOf( byteString, byteRadix ).
shortValue();
}
catch ( NumberFormatException e )
{
return ( -1 );
}
}
else
{
return ( -1 );
}
if ( isByte( byteShort ) )
{
return ( byteShort );
}
else
{
return ( -1 );
}
}
public static int toWordInteger( String wordString )
{
int wordInteger;
if ( ( wordString != null ) && ( ! wordString.equals( "" ) ) )
{
char baseSpecifier = wordString.charAt( wordString.length() - 1 );
int wordRadix = toRadix( baseSpecifier );
if ( wordRadix != -1 )
{
wordString = wordString.substring( 0, wordString.length() -
1 );
}
else
{
wordRadix = 10;
}
try
{
wordInteger = Integer.valueOf( wordString, wordRadix ).
intValue();
}
catch ( NumberFormatException e )
{
return ( -1 );
}
}
else
{
return ( -1 );
}
if ( isWord( wordInteger ) )
{
return ( wordInteger );
}
else
{
return ( -1 );
}
}
public static short[] toWordCode( int word )
{
if ( isWord( word ) )
{
short[] codeByteArray = { ( short ) ( word % 0x100 ), ( short )
( ( word / 0x100 ) % 0x100 ) };
return ( codeByteArray );
}
else
{
return ( null );
}
}
public static String toNumberString( long num, int radix, int length )
{
String str = Long.toString( num, radix );
while ( str.length() < length )
{
str = "0" + str;
}
return ( str.substring( str.length() - length, str.length() ) );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -