Skip to content

WASM_X86: Support printing negative integers #1311

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

Merged
merged 4 commits into from
Nov 19, 2022
Merged
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
8 changes: 7 additions & 1 deletion integration_tests/print_03.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
17 changes: 17 additions & 0 deletions src/libasr/codegen/x86_assembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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