Skip to content

Commit df11263

Browse files
authored
Merge pull request #1311 from Shaikh-Ubaid/wasm_x86_print_neg_ints
WASM_X86: Support printing negative integers
2 parents 3064bad + 6767b2b commit df11263

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

integration_tests/print_03.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
def Main0():
22
x: i32
3-
x = (2+3)*5
3+
x = (2+3)* (-5)
4+
y: i32 = 100
5+
z: i32 = 2147483647
6+
w: i32 = -2147483648
47
print(x)
8+
print(y)
9+
print(z)
10+
print(w)
511
print("Hi")
612
print("Hello")
713
print((5-2) * 7)

src/libasr/codegen/x86_assembler.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,21 @@ void emit_print_int(X86Assembler &a, const std::string &name)
133133
X86Reg base = X86Reg::ebp;
134134
// mov eax, [ebp+8] // argument "i"
135135
a.asm_mov_r32_m32(X86Reg::eax, &base, nullptr, 1, 8);
136+
137+
a.asm_mov_r32_r32(X86Reg::ecx, X86Reg::eax); // make a copy in ecx
138+
a.asm_mov_r32_imm32(X86Reg::ebx, 0);
139+
a.asm_cmp_r32_r32(X86Reg::eax, X86Reg::ebx);
140+
a.asm_jge_label(".print_int_"); // if num >= 0 then print it
141+
142+
// print "-" and then negate the integer
143+
emit_print(a, "string-", 1U);
144+
// ecx value changed during print so fetch back
145+
a.asm_mov_r32_m32(X86Reg::ecx, &base, nullptr, 1, 8);
146+
a.asm_neg_r32(X86Reg::ecx);
147+
148+
a.add_label(".print_int_");
149+
150+
a.asm_mov_r32_r32(X86Reg::eax, X86Reg::ecx); // fetch the val in ecx back to eax
136151
a.asm_xor_r32_r32(X86Reg::esi, X86Reg::esi);
137152

138153
a.add_label(".loop");
@@ -183,6 +198,8 @@ void emit_print_int(X86Assembler &a, const std::string &name)
183198
a.asm_mov_r32_r32(X86Reg::esp, X86Reg::ebp);
184199
a.asm_pop_r32(X86Reg::ebp);
185200
a.asm_ret();
201+
202+
emit_data_string(a, "string-", "-"); // - symbol for printing negative ints
186203
}
187204

188205
} // namespace LFortran

0 commit comments

Comments
 (0)