Hi Everyone,

I am playing with creating a compiler. So far it has three variable types - string, int, and uint.

Here's the problem - uint and int requires different mnenomics for such things as multiplication and division, and for the various conditional jumps.

Now if I make a statement like:

IF uint_var = int_var THEN

how do I do the compares? If they are both the same, it is easy. But when they are different, do I maybe need to convert the uint to int first? If so, how is that done? And what if the uint is too big to fit in the 32bits?

Same question for multiply and divide, how do I multiply or divide a uint and an int?

Any suggestions would be very welcome.

Posted on 2003-06-07 07:59:00 by bushpilot
Unless stated by the programmer or language documentation, the conversion should preserve all significant bits - in the case above this would mean conversion to signed 64-bit values and then the comparison.

Of course there are work-arounds for specific cases.

mov eax, UINT32
mov edx, INT32
shr eax, 1
sar edx, 1
cmp eax, edx
jne __real_jCC
mov eax, UINT32
mov edx, INT32
cmp eax, edx
j?? ?
This will handle all branch types for UINT/INT compare.

Multiple (left for you to tell me why it works ;)):
	mov	ecx, INT32

mov eax, UINT32
mul ecx
neg ecx
add edx, ecx

; 64-bit signed result in EDX:EAX
I'm lazy and would use the FPU for divide.
Posted on 2003-06-07 09:18:59 by bitRAKE
Thanks bitrake, I'll have to study this a little more, but your solution looks interesting. I'll have to see how it fits with multiplication and division as well.

I am trying to make it so that the registers are not clobbered by whatever the compiler codes. I might just have to preserve them first. The reason for this limitation a "feature" of my compiler that it makes it easy to access the registers directly, and encourages that. If it changes the registers behind the programmers back, that is not so good. Of course, I could just document the behavior, but that is not so cool.

Thinking my question over a little more, first a test could be made of the sign bit of the int. If set, then they are obviously not equal, as the int must be less than the uint.

Thanks for the help. I think I am getting somewhere.

Posted on 2003-06-07 09:52:03 by bushpilot
most languages do not let you directly compare two items of a different type, you usually have to cast one of them to be of the same type as the other. Some languages will do that conversion for you behind the scenes, but if you are writing your own compiler/language then you need to work out how you are going to handle it.
Posted on 2003-06-07 20:19:39 by sluggy