Skip to content

Commit c0bc3bd

Browse files
committed
asmarm: Fix bug with encoding small negative ints using MVN instruction.
1 parent 83d27b0 commit c0bc3bd

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

py/asmarm.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,9 @@ void asm_arm_mov_reg_i32(asm_arm_t *as, uint rd, int imm) {
282282
// TODO: There are more variants of immediate values
283283
if ((imm & 0xFF) == imm) {
284284
emit_al(as, asm_arm_op_mov_imm(rd, imm));
285-
} else if (imm < 0 && ((-imm) & 0xFF) == -imm) {
286-
emit_al(as, asm_arm_op_mvn_imm(rd, -imm));
285+
} else if (imm < 0 && imm >= -256) {
286+
// mvn is "move not", not "move negative"
287+
emit_al(as, asm_arm_op_mvn_imm(rd, ~imm));
287288
} else {
288289
//Insert immediate into code and jump over it
289290
emit_al(as, 0x59f0000 | (rd << 12)); // ldr rd, [pc]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# comparisons with immediate boundary values
2+
@micropython.viper
3+
def f(a: int):
4+
print(a == -1, a == -255, a == -256, a == -257)
5+
6+
f(-1)
7+
f(-255)
8+
f(-256)
9+
f(-257)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
True False False False
2+
False True False False
3+
False False True False
4+
False False False True

0 commit comments

Comments
 (0)