Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# C to assembly conversion help

Posted on 1997-07-29
Medium Priority
811 Views
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:

mov bx,tex
mov ah,[bx]
mov es:[di],ah
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.

0
Question by:Doug8
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 2

LVL 15

Expert Comment

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

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

ID: 1252697
I'll convert it to assembly for you.
0

LVL 1

Expert Comment

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

Author Comment

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

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

LVL 1

Accepted Solution

mosfet earned 1680 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
mov ax,es:[bx]
les bx,DWORD PTR tex
mov ah,es:[bx]
inc dest
mov ah,es:[bx]
mov [dest],ah

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

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
xor eax,eax
mov ax,es:[ebx]
mov cl,g_mip
shr ax,cl
mov edx,u
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
xor eax,eax
mov ax,es:[ebx]
mov cl,g_mip
shr ax,cl
mov edx,v
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
mov ax,es:[bx]
les bx,DWORD PTR tex
mov ah,es:[bx]
inc dest
mov ah,es:[bx]
mov [dest],ah

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

pop cx
loop second_while

// End inline assembly

}
0

## Featured Post

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ouâ€¦
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn hoâ€¦
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 opening and reading files in the C programming language.
###### Suggested Courses
Course of the Month7 days, 8 hours left to enroll