Bug in 16 to 32 bit integer conversion

General FreeBASIC programming questions.
Post Reply
azbluevw
Posts: 16
Joined: Dec 31, 2011 21:54

Bug in 16 to 32 bit integer conversion

Post by azbluevw »

When multiplying a 16 bit signed integer by zero and saving it into a 32 bit signed integer, the compiler (under Qbasic compatibility mode) generates invalid assembly.

Consider the following code:
DEFINT A-Z '16 bit integers in QB mode
DIM B AS LONG '32 BIT INTEGER
B = A * 3
B = A * 0

For the first multiply, the assembly generated is essentially:

mov ax, a
imul ax, 3
movsx eax, ax
mov b, eax

For the second multiply, the compiler realizes that a variable times zero is zero, and so it attempts to skip the multiply by loading a sign-extended 16 bit 0 into the 32bit register:

movsx eax, 0
mov b, eax

But the sign extend opcode does not allow for immediate values (ie, constants), causing the assembler to generate an error:
"Error: suffix or operands invalid for 'movsx' "
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Bug in 16 to 32 bit integer conversion

Post by Richard »

You have done some excellent work tracking that one down. Can you please post it as a bug report on sourceforge so it won't get lost or forgotten in this forum.
I think the link is http://sourceforge.net/tracker/?group_i ... tid=693196
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Bug in 16 to 32 bit integer conversion

Post by counting_pine »

Wow, not bad for a first post :)
Here's a version in the standard dialect that shows the same effect:

Code: Select all

dim a as short, b as long
b = a * cshort(3)
b = a * cshort(0)
EDIT: similar effects for And cshort(0) / Or cshort(-1) (unsurprisingly).
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Bug in 16 to 32 bit integer conversion

Post by counting_pine »

azbluevw
Posts: 16
Joined: Dec 31, 2011 21:54

Re: Bug in 16 to 32 bit integer conversion

Post by azbluevw »

D'oh! Sorry guys, I didn't see the correct place to post bugs until after I posted this; I felt kinda stupid :P

Thank you, Counting_pine, for correctly reporting it for me.
Post Reply