Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert simple assembly into C

Status
Not open for further replies.

fayya

Programmer
Jul 20, 2005
3
CA
Hi,

I am working on a project written in C that involves assembly codes. I need some help on converting an assembly function to complete C code, any help or pointer is welcome, thanks!

This is the asm code that I want to convert to C:

SWORD get_svalue(BYTE k)
// k>0 always
// Takes k bits out of the BIT stream (wordval), and makes them a signed value
{
_asm {
xor ecx, ecx
mov cl,k
mov eax,[wordval]
shl eax,cl
shr eax, 16
dec cl
bt eax,ecx
jc end_macro
signed_value:inc cl
mov ebx,[start_neg_pow2]
add ax,word ptr [ebx+ecx*2]
end_macro:
}
}
 
The registers obviously need replacing with variables
================================
>>= and <<= in place of shr and shl
==================================

mov ebx, [start_neg_pow2]
add ax, word ptr [ebx + ecx * 2 ]

replacing start_neg_pow2 with p, the above becomes something like:

add regax, *( *p + regcx * 2 );
 
I think you'd get better code if you look at what this snippet does, and do it in C.
It will be more readable, and more future-proof.
 
Intel x86 assembly language is as future proof as could be wished for. A program written in 1981 for the very first PC could, in theory, be run on the very latest PC. Provided you installed MS-DOS first of course, or alternatively (like me) have it already installed on a spare partition for old time's sake.
 
Absolutely. I didn't mean that at all. Fayya's project is based in C, not intel x86 assembly. What I meant was that different compilers have considerable flexibility in the definition of C about how their various number formats are allowed to be handled behind the scenes.

If you embed assembler code into C with an intention of returning a particular number format, it is great until someone changes the compiler, and the new compiler expects a different format. Hence my suggestion that if you're writing C, and need to fiddle with formats, let the compiler do as much as possible, in standard C.
 
Hello!
This works pretty well for the code

SWORD get_svalue(BYTE k)
// k>0 always
// Takes k bits out of the BIT stream (wordval), and makes them a signed value
{
/* Original asm code.
_asm {
xor ecx, ecx
mov cl,k
mov eax,[wordval]
shl eax,cl
shr eax, 16
dec cl
bt eax,ecx
jc end_macro
signed_value:inc cl
mov ebx,[start_neg_pow2]
add ax,word ptr [ebx+ecx*2]
end_macro:
}*/
/* Substitute with this */
unsigned long wordtemp;
signed short int ret;

wordtemp = wordval<<k;
wordtemp>>=16;

k--;

if((wordtemp>>k)&0x00000001)
return wordtemp;

else{
k++;
ret = wordtemp+neg_pow2[k];
return ret;
}

}

Njoy!

Nacker
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top