diff --git a/integration_tests/print_03.py b/integration_tests/print_03.py index 46f5120a31..c066d1c81d 100644 --- a/integration_tests/print_03.py +++ b/integration_tests/print_03.py @@ -1,7 +1,13 @@ def Main0(): x: i32 - x = (2+3)*5 + x = (2+3)* (-5) + y: i32 = 100 + z: i32 = 2147483647 + w: i32 = -2147483648 print(x) + print(y) + print(z) + print(w) print("Hi") print("Hello") print((5-2) * 7) diff --git a/src/libasr/codegen/x86_assembler.cpp b/src/libasr/codegen/x86_assembler.cpp index 12373385c7..50dd196cdb 100644 --- a/src/libasr/codegen/x86_assembler.cpp +++ b/src/libasr/codegen/x86_assembler.cpp @@ -133,6 +133,21 @@ void emit_print_int(X86Assembler &a, const std::string &name) X86Reg base = X86Reg::ebp; // mov eax, [ebp+8] // argument "i" a.asm_mov_r32_m32(X86Reg::eax, &base, nullptr, 1, 8); + + a.asm_mov_r32_r32(X86Reg::ecx, X86Reg::eax); // make a copy in ecx + a.asm_mov_r32_imm32(X86Reg::ebx, 0); + a.asm_cmp_r32_r32(X86Reg::eax, X86Reg::ebx); + a.asm_jge_label(".print_int_"); // if num >= 0 then print it + + // print "-" and then negate the integer + emit_print(a, "string-", 1U); + // ecx value changed during print so fetch back + a.asm_mov_r32_m32(X86Reg::ecx, &base, nullptr, 1, 8); + a.asm_neg_r32(X86Reg::ecx); + + a.add_label(".print_int_"); + + a.asm_mov_r32_r32(X86Reg::eax, X86Reg::ecx); // fetch the val in ecx back to eax a.asm_xor_r32_r32(X86Reg::esi, X86Reg::esi); a.add_label(".loop"); @@ -183,6 +198,8 @@ void emit_print_int(X86Assembler &a, const std::string &name) a.asm_mov_r32_r32(X86Reg::esp, X86Reg::ebp); a.asm_pop_r32(X86Reg::ebp); a.asm_ret(); + + emit_data_string(a, "string-", "-"); // - symbol for printing negative ints } } // namespace LFortran