Solved

C to assembly conversion help

Posted on 1997-07-29
7
799 Views
Last Modified: 2012-05-04
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
0
Comment
Question by:Doug8
  • 4
  • 2
7 Comments
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1252695
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
 

Author Comment

by:Doug8
ID: 1252696
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
 
LVL 1

Expert Comment

by:mosfet
ID: 1252697
I'll convert it to assembly for you.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Expert Comment

by:mosfet
ID: 1252698
Can you do inline assembly in DJGPP, because it would be easier if you could?
0
 

Author Comment

by:Doug8
ID: 1252699
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
 
LVL 1

Expert Comment

by:mosfet
ID: 1252700
That would be easier because you wouldn't have to pass as many variables.
0
 
LVL 1

Accepted Solution

by:
mosfet earned 420 total points
ID: 1252701
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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now