I know it has ben asked several times but please at least read it.

I am trying to convert some c source code and I stuck at this line



typedef unsigned short ush; /* USH TYPEDEF WORD */
s->d_buf = overlay + s->lit_bufsize/sizeof(ush);

What I understand from this line is divide lit_bufsize by 2 and add to overlay.Ie if eax=overlay edx=lit_bufsize


shr edx,1
add eax,edx
mov [xxxx],eax

However when I compile this lines with BCC55 I get this weird output


shr edx,1
add edx,edx
add eax,edx
mov [xxxx],eax

I thought maybe it is BCC55 error I compiled with VC and got below result


shr edx,1
lea edx,[2*edx+eax]
mov [xxxx],edx

Could you tell me what I am missing ?Thanks
Posted on 2002-11-24 08:01:17 by LaptoniC
It depends on the type of the overlay variable, if it's an array then you are adding lit_bufsize/2 to a pointer. Adding an integer x to a pointer means, make the pointer point to x elements farther. If each element in the array takes 2 bytes, adding x means adding 2*x bytes, in general sizeof(element) * integer.
This is what happens here too:
shr edx,1

edx = bufsize/2
lea edx,[2*edx+eax]

edx = sizeof(element) * integer + overlay
(the elements are probably words as well, so sizeof(element) is 2, the integer is bufsize/2 (edx) and overlay is in eax. The result will be in edx
 mov [xxxx],edx

Store result.

Thomas
Posted on 2002-11-24 08:19:42 by Thomas
Thanks for the reply.
Posted on 2002-11-24 10:57:16 by LaptoniC