Skip to content

Commit af804a5

Browse files
committed
Fix .dword replicating the low bits instead of sign-extending
This bug was caused by a few issues. First, when adding .dword I decided to allow existing code to handle the fits within 32 bit case. The old code could not possibly handle that correctly. Second, lengthinbytes = 8 was passed into the Memory system, given that it takes an int as data and there are only 4 bytes in an int it doesn't have defined behaviour for 8 bytes. Third, javas shifting loops after 32 for integers. So in total, I failed to account for a case, passed an undefined input into memory, which resulted in replicated behaviour due to java. I fixed it by just always handling the writes for .dword directly.
1 parent 21f3a45 commit af804a5

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

Diff for: src/rars/assembler/Assembler.java

+16-10
Original file line numberDiff line numberDiff line change
@@ -922,21 +922,27 @@ private void storeInteger(Token token, Directives directive, ErrorList errors) {
922922
int lengthInBytes = DataTypes.getLengthInBytes(directive);
923923
if (TokenTypes.isIntegerTokenType(token.getType())) {
924924
int value;
925-
if (TokenTypes.INTEGER_64 == token.getType()){
926-
long tmp = Binary.stringToLong(token.getValue());
927-
if (directive == Directives.DWORD){
928-
writeToDataSegment((int)tmp, 4, token, errors);
929-
writeToDataSegment((int)(tmp>>32), 4, token, errors);
930-
return;
931-
} else {
932-
value = (int)tmp;
925+
long longvalue;
926+
if (TokenTypes.INTEGER_64 == token.getType()) {
927+
longvalue = Binary.stringToLong(token.getValue());
928+
value = (int)longvalue;
929+
if (directive != Directives.DWORD){
933930
errors.add(new ErrorMessage(ErrorMessage.WARNING, token.getSourceProgram(), token.getSourceLine(),
934-
token.getStartPos(), "value " + Binary.longToHexString(tmp)
931+
token.getStartPos(), "value " + Binary.longToHexString(longvalue)
935932
+ " is out-of-range and truncated to " + Binary.intToHexString(value)));
936933
}
937-
} else {
934+
}else{
938935
value = Binary.stringToInt(token.getValue());
936+
longvalue = value;
939937
}
938+
939+
if (directive == Directives.DWORD){
940+
writeToDataSegment((int)longvalue, 4, token, errors);
941+
writeToDataSegment((int)(longvalue>>32), 4, token, errors);
942+
return;
943+
}
944+
945+
940946
int fullvalue = value;
941947
// DPS 4-Jan-2013. Overriding 6-Jan-2005 KENV changes.
942948
// If value is out of range for the directive, will simply truncate

0 commit comments

Comments
 (0)