Solved

C to assembly conversion help

Posted on 1997-07-29
7
800 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to setup iphone app subscription service 3 120
Best UNIX-compatible free C compiler for Windows or Mac 6 239
chcp 65001 File encoding 66 231
Why is compiler in oracle server ? 9 45
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

867 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