Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binary literal #190

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/rars/util/Binary.java
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,18 @@ public static Integer stringToIntFast(String s){
return null;
}
}
}else if(s.length() > 2+i && s.charAt(i) == '0' && (s.charAt(i+1) == 'b' || s.charAt(i+1) == 'B')) { // Binary case
if(s.length() > 32+2+i) return null; // This must overflow or contain invalid characters
i += 2;
for(; i < s.length(); i++) {
char c = s.charAt(i);
result *= 2;
if ('0' <= c && c <= '1') {
result += c - '0';
} else {
return null;
}
}
}else if (first == '0'){ // Octal case
if(s.length() > 12+i) return null; // This must overflow or contain invalid characters
for(; i < s.length(); i++){
Expand Down
14 changes: 14 additions & 0 deletions test/literal.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#stdout:168
.text
main:
li a0, 42
addi a0, a0, '*'
addi a0, a0, 0x2a
addi a0, a0, 0b101010
li a7, 1 # print integer
ecall

success:
li a0, 42
li a7, 93
ecall