Skip to content

Commit 79995d8

Browse files
committed
X86Assembler: Support printing negative integers
1 parent 26d828e commit 79995d8

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/libasr/codegen/x86_assembler.cpp

Lines changed: 15 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");

0 commit comments

Comments
 (0)