Hi, I am just beginning to learn asm, and I would need some help with inline assembly, it would make me understand asm better. Let's say i have a c function and a pointer to an ushort - ushort *vid.



function
{
ushort *vid;
vid = video+x1+y1*(pitch>>1);
}


video is just an ushort, and pitch is an integer. x1 and y1 are also integers.

Im concerned with making vid equal video+x1+y1*(pitch>>1) in assembly language completely. like this:



ushort *vid;
__asm
{
blah ;
}


Thanks to whoever will translate this simple thing for me.
Posted on 2002-01-24 02:28:03 by gregd
gregd,

I would prefer you doing the first translation attempt. Then we can look at it, eliminate bugs :) and possibly improve it.

japheth
Posted on 2002-01-24 03:08:44 by japheth
sounds good, as embarasing as it looks, i'll send you my attempt... in a few. just hold it i need to recreate it.
Posted on 2002-01-24 03:39:41 by gregd



ushort *video = Lets just say it points to the video buffer's first 2 bytes;

void D_Line(int x1, int y1, int x2, int y2, ushort color)
{
int dx,dy,dx2,dy2,ix,iy,err,i;
ushort *vid;

//vid = video+x1+y1*(pitch>>1);

__asm
{
mov eax, pitch ;i would presume the folowing lines would store
shr eax, 1 ;video+x1+y1*(pitch>>1) in the eax register
imul eax, y1
add eax, x1
add eax, [video] ;not sure if this is the right way( the [] ), but
;isn't video a pointer/address?
mov [vid], eax ;store result in vid
}



(*vid and *video are two different ushorts)

I know it's wrong, it draws 2 lines in different places for some reason, instead of one in one right place. But it's all I can do after 3 days of my assembly studies.
Posted on 2002-01-24 04:07:49 by gregd
gregd,

the brackets doesn't matter (in this case!). In my eyes its a correct translation. Not much to improve here.

japheth
Posted on 2002-01-24 04:41:38 by japheth
If you want to improve the speed, you can create a "pitch lookup table".
But I don't really know how much of a speed improvement this is,
these days. Usually you don't do too many (x,y)->vidptr translations
unless you're dealing with putpixel - which you hopefully aren't :).
Posted on 2002-01-24 07:19:43 by f0dder
There is an error there. "vid" is a USHORT. 16-bit. That means that you can't use the 32-bit "mov" to move to it.
Posted on 2002-01-24 09:24:04 by gliptic
There's no error. "vid" is a *pointer* to u16 data. That means it's
a 32bit variable. As for the u16... that means he's using a 15bpp
or 16bpp video mode.
Posted on 2002-01-24 09:27:49 by f0dder
If video is a ushort*, then you need to scale the integers by multiplying by 2 before adding video.


mov eax, pitch ;i would presume the folowing lines would store
shr eax, 1 ;video+x1+y1*(pitch>>1) in the eax register
imul eax, y1
add eax, x1
[b] shl eax, 1 ;scale2(x1+y1*(pitch>>1)) in the eax register [/b]
add eax, [video] ;not sure if this is the right way( the [] ), but
;isn't video a pointer/address?
mov [vid], eax ;store result in vid
Posted on 2002-01-24 21:58:35 by tank
Yes, that seems to work right. Thanks guys for your help. Can anyone tell me why exactly I should scale a ushort? My assumption would be, a ushort is 16bit/2bytes. eax is 32bit/4bytes. I would assume that the ushort is stored in the lower half of eax. So the shift is used to shift the 2 bytes of ushort into the higher 16bit half of eax because that's the most important part of eax that is read from first? But then again, i'm just a beginner at this.
Posted on 2002-01-25 01:07:54 by gregd
greg, shifting is used as fast power-of-two multiply/divide.
Ushort is 2 bytes, yes. But "ushort *", a pointer to ushort, is 4
bytes,
as that is the size of pointers in 32bit flat mode.

Since pitch is given in bytes, not pixels, and you want a pointer to
video memory (thus, byte offset, not pixel index), you don't really
need the shifting, and can reduce the code to the following:



mov eax, [pitch]
imul eax, [y1]
add eax, [x1]
add eax, [video]
mov [vid], eax
Posted on 2002-01-25 05:42:50 by f0dder
F0dder, Thanks for the info, but I don't think that worked. I use it in a line drawing algorithm and your "short" version doesn't seem to work right.
Posted on 2002-01-25 12:42:02 by gregd
Ah, that's what happens when I post while I'm tired :). Try adding
x1 twice. If that doesn't work... oh well, I should catch some sleep.
Posted on 2002-01-25 13:03:45 by f0dder