• C

C to assembly conversion help

Hello, I am having trouble converting this function to assembly because i barely know assembly, so i need some help.  Below is the C function(bout 20 lines) and after that is what i have started on(I know its all wrong). So let me explain the variables below:

is_cached - is hit a lot more often
n - is from 0 to 319
tex - is char *
tex_row_table - is int [258]
swim_u & swim_v - is int [256]
g_mip - is int either 0,1,2,3
wid * ht - are int

#include "s.h"
#include "fix.h"
#include "tm.h"

void draw_affine(int n, char *dest, fix u, fix v, fix du, fix dv)
{
   if (is_cached) {
      while (n--) {
         int iu = u >> 16;
         int iv = v >> 16;
         *dest++ = tex[tex_row_table[iv+1] + iu];
         u += du;
         v += dv;
      }
   } else {  
      while (n--) {
         int iu = ((u + (swim_u[(((v<<g_mip) >> 16) & 0xff)] >> g_mip)) >> 16) & wid;
         int iv = ((v + (swim_v[(((u<<g_mip) >> 16) & 0xff)] >> g_mip)) >> 16) & ht;
         *dest++ = tex[tex_row_table[iv+1] + iu];
         u += du;
         v += dv;
      }
   }
}

this is what i have for assembly; (just for some sorta skeleton ;)

      .file "affine.asm"
      .medium
      .486

_global draw_affine

draw_affine:

      mov si,n
      les di,dest
      mov eax,u
      mov ebx,v
      mov ecx,du
      mov edx,dv
      mov ax,tex_row_table
      cmpl is_cached,0
      je scandone

scanloop:
            
      add ax,eax
      add ax,ebx
      adc bx,ax
      mov bx,tex
      mov ah,[bx]
      mov es:[di],ah
      add eax,ecx
      add ebx,edx
      dec si
      jnz scanloop

scandone:

      ret

so im sure a lot, a lot of that is wrong so thats why i need help, i also only attempted the if(is_cached).  This should be enough points for this, and any other questions email me at benco@gisco.net.

Thanx a lot in advance
Doug8Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tommy HuiEngineerCommented:
Which compiler are you using? If you're using Borland C++, you can at least take a look at the code they gerenate for the function by using bcc -S -c foo.cpp on the command line. If you're using Visual C++, you can use cl /c /Fa foo.c on the command line. This will give you at least the compiler's version of the function. Then you can hand optimize or use the compiler's optimization features to see how they do it.
0
Doug8Author Commented:
Im using DJGPP and have already used the gcc -S option and the code is very unreadable because the compiler does many things to protect the stack and so forth and its in AT&T syntax so its even harder for me to read.  The code produced is way too complex for me to optimize.
0
mosfetCommented:
I'll convert it to assembly for you.
0
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

mosfetCommented:
Can you do inline assembly in DJGPP, because it would be easier if you could?
0
Doug8Author Commented:
Yes you can do inline assembly but it is far different from other compilers, if you would do it in inline, im sure i probally can convert it.
0
mosfetCommented:
That would be easier because you wouldn't have to pass as many variables.
0
mosfetCommented:
It would be something like this:

/* All arrays must be declared as pointer
   For some reason it only works this way

 ex.

 char buffer[256];

 would be replaced with

 char *buffer=new char[256];
*/
// I'm assuming "fix" is the equivilent of "long"
// I'm also assuming g_mip is a char, since it won't be bigger //than 256, what's the use of having it as an int?
//since it's a byte, it will fit in cl
// it should be a char anyway, it's not like you're printing the //value
// out.

void draw_affine(int n, char *dest, fix u, fix v, fix du, fix dv)  {

char is_chached;
int iu,iv;

// Begin inline assembly

the_if:

cmp is_chached,0
je the_else

mov cx,n
first_while:
push cx

// iu = u >> 16;
mov eax,u
mov cl,16
shr eax,cl
mov iu,ax

// iv = v >> 16;
mov eax,v
shr eax,cl
mov iv,ax

// *dest++ = tex[tex_row_table[iv+1] + iu];
mov ax,iv
inc ax
shl ax,1 // Same as multiplying by 2
          // Need to multiply by 2 because an int is two bytes
les bx,DWORD PTR tex_row_table
add bx,ax
mov ax,es:[bx]
add ax,iu
les bx,DWORD PTR tex
add bx,ax
mov ah,es:[bx]
inc dest
mov ah,es:[bx]
mov [dest],ah

// u += du;
// v += dv;
mov eax,du
add u,eax
mov eax,dv
add v,eax

pop cx
loop first_while

////////////////////////////////////////////////////////////////////////

the_else:

mov cx,n
second_while:
push cx

xor ebx,ebx
//iu = ( (u + (swim_u[( ( (v<<g_mip) >> 16) & 0xff)] >> g_mip) ) >>16 )
       & wid;
mov cl,16
mov al,g_mip
sub cl,al
mov eax,v
shr eax,cl
and eax,0xff
mov cl,1
shl eax,cl
les bx,DWORD PTR swim_u
add ebx,eax
xor eax,eax
mov ax,es:[ebx]
mov cl,g_mip
shr ax,cl
mov edx,u
add eax,edx
mov cl,16
shr eax,cl
xor edx,edx
mov dx,wid
and eax,edx

//iv = ( (v + (swim_v[( ( (u<<g_mip) >> 16) & 0xff)] >> g_mip) ) >>16 )
       & ht;
mov cl,16
mov al,g_mip
sub cl,al
mov eax,u
shr eax,cl
and eax,0xff
mov cl,1
shl eax,cl
les bx,DWORD PTR swim_v
add ebx,eax
xor eax,eax
mov ax,es:[ebx]
mov cl,g_mip
shr ax,cl
mov edx,v
add eax,edx
mov cl,16
shr eax,cl
xor edx,edx
mov dx,ht
and eax,edx


// *dest++ = tex[tex_row_table[iv+1] + iu];
mov ax,iv
inc ax
shl ax,1 // Same as multiplying by 2
          // Need to multiply by 2 because an int is two bytes
les bx,DWORD PTR tex_row_table
add bx,ax
mov ax,es:[bx]
add ax,iu
les bx,DWORD PTR tex
add bx,ax
mov ah,es:[bx]
inc dest
mov ah,es:[bx]
mov [dest],ah

// u += du;
// v += dv;
mov eax,du
add u,eax
mov eax,dv
add v,eax

pop cx
loop second_while

// End inline assembly

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.