@@ -133,6 +133,21 @@ void emit_print_int(X86Assembler &a, const std::string &name)
133
133
X86Reg base = X86Reg::ebp;
134
134
// mov eax, [ebp+8] // argument "i"
135
135
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
136
151
a.asm_xor_r32_r32 (X86Reg::esi, X86Reg::esi);
137
152
138
153
a.add_label (" .loop" );
@@ -183,6 +198,8 @@ void emit_print_int(X86Assembler &a, const std::string &name)
183
198
a.asm_mov_r32_r32 (X86Reg::esp, X86Reg::ebp);
184
199
a.asm_pop_r32 (X86Reg::ebp);
185
200
a.asm_ret ();
201
+
202
+ emit_data_string (a, " string-" , " -" ); // - symbol for printing negative ints
186
203
}
187
204
188
205
} // namespace LFortran
0 commit comments