assembly help

This is a discussion on assembly help within the C Programming forums, part of the General Programming Boards category; I need to understand, what this code is doing, i dont know assambly coding . what would be the impact, ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Location
    India
    Posts
    17

    assembly help

    I need to understand, what this code is doing, i dont know assambly coding .
    what would be the impact, if i change this logic with corrosponding c code,
    thanks in adv.
    Code:
    WORD lookKbits(BYTE k)
    {
      _asm {
    	mov dl, k
    	mov cl, 16
    	sub cl, dl
    	mov eax, [wordval]
    	shr eax, cl
               }
    }
    
    WORD WORD_hi_lo(BYTE byte_high,BYTE byte_low)
    {
    	_asm {
    		mov ah,byte_high
    			mov al,byte_low
    	}
    }
    
    SWORD get_svalue(BYTE k)
    {
    	_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:
    }
    Last edited by Brw_Abhi; 08-20-2007 at 03:46 AM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Is this a test or assignment of some sort?

    The impact would be slower code unless the compiler was smart enough to optimize everything better than or equal to the current level of optimization.... whatever that may be considered.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Sounds like homework, why don't you write the equivalent in C then generate the assembly (see your compiler manual), then compare it with various optimization levels - an A+ answer.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Quote Originally Posted by zacs7 View Post
    why don't you write the equivalent in C then generate the assembly (see your compiler manual), then compare it with various optimization levels - an A+ answer.
    duh, he doesnt write the C code because he asks what the assembler is doing. Very hard to write code that you dont know what it should do dont you think?

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    I'll translate but some of it does not make sense.

    Code:
    WORD lookKbits(BYTE k)
    {
      _asm {
        mov dl, k        //place k into low byte of DX
        mov cl, 16      //put 16 into low byte of CX
        sub cl, dl        //subtract dl from cl
        mov eax, [wordval]     //place something in eax - perhaps a value at address wordval
        shr eax, cl  //Shift value in eax right cl times (divide by 2^cl)
        }
    }
    
    WORD WORD_hi_lo(BYTE byte_high,BYTE byte_low)
    {
       _asm {
         mov ah,byte_high    //place byte_high into high byte of AX
         mov al,byte_low      //place byte_low into low byte of AX
       }
    }
    
    //SWORD???  Not sure what this is  BYTE, WORD, DWORD, QWORD - never seen SWORD
    SWORD get_svalue(BYTE k)
    {
       _asm {
         xor ecx, ecx           //zero out ecx
         mov cl,k                 //place k into low byte of CX
         mov eax,[wordval]  //place something into eax - perhaps a value at address wordval
         shl eax,cl                //shift eax left cl times  (multiply by 2^cl)
         shr eax, 16            //shift eax right 16   (divide by 2^16)
         dec cl                     //decrement cl
         bt eax,ecx              //???????????? bt ????????????????  Dunno.
         jc end_macro         //conditonal jump (forget what jc is) to label end_macro
    signed_value:             //label
         inc cl                       //increment cl
         mov ebx,[start_neg_pow2]   //place whatever is at address start_neg_pow2 into ebx
         add ax,word ptr [ebx+ecx*2]   //add value at address [ebx+(ecx*2)] to ax
    end_macro:
    }

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    duh, he doesnt write the C code because he asks what the assembler is doing. Very hard to write code that you dont know what it should do dont you think?
    I was answering the second "question", he wants to compare the logic & it's impact (if it were changed to C), going to be a bit hard if he's trying to compare C with assembly - it's like comparing French with English, first you must find a common ground, Latin for example.
    Last edited by zacs7; 08-20-2007 at 06:31 AM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Bubba, et.al.

    bt is "bit test", and it sets the carry flag to the value of the bit indicated by ecx.

    mov eax, [wordval] will fetch the content of wordval (presumably a variable - but could be a function if we wanted to read the code)

    With some thinking, I could probably write something in C.

    As to the performance impact - this is probably part of some code in an old compiler (e.g. Turbo C) - where it would actually make a lot of sense to do this. But in a modern Microsoft / Gnu compiler on a modern processor, it would probably not make much of a difference - but there's really no other way to tell other than to build a C equivalent and test the performance of each implementation. I very much doubt it's much more than 10-20% difference tho'.

    --
    Mats

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    On the rare occasions I write ASM in C, I leave the original C as a comment for situations such as this. Namely, the universe has moved on, and the original reason for the ASM has gone.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Bit test. Yes, thank you. Man I'm a bit rusty.

    Code:
    WORD WORD_hi_lo(BYTE byte_high,BYTE byte_low)
    {
       _asm {
         mov ah,byte_high    //place byte_high into high byte of AX
         mov al,byte_low      //place byte_low into low byte of AX
       }
    }
    You will lose nothing by coding this function in C since this function in assembly is rather assinine in itself.

    As to the other 2 functions I cannot see any major performance losses in converting this to pure C. There are no large loops or nested loops to unroll and most of the code amounts to a simple mov here, add there, and read memory at address [x]. None of these take long in pure C either. Based on what you have posted, this is not a section of code that gains anything by being in assembly rather than C.
    Last edited by VirtualAce; 08-21-2007 at 12:12 AM.

  10. #10
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    326
    Quote Originally Posted by Brw_Abhi View Post
    I need to understand, what this code is doing, i dont know assambly coding .
    what would be the impact, if i change this logic with corrosponding c code,
    thanks in adv.
    Code:
    WORD lookKbits(BYTE k)
    {
      _asm {
    	mov dl, k
    	mov cl, 16
    	sub cl, dl
    	mov eax, [wordval]
    	shr eax, cl
               }
    }
    
    WORD WORD_hi_lo(BYTE byte_high,BYTE byte_low)
    {
    	_asm {
    		mov ah,byte_high
    			mov al,byte_low
    	}
    }
    
    SWORD get_svalue(BYTE k)
    {
    	_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:
    }
    Where is the return value being returned in any of these functions?

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by MacNilly View Post
    Where is the return value being returned in any of these functions?
    EAX register (or parts of it).

    Quote Originally Posted by Bubba View Post
    You will lose nothing by coding this function in C since this function in assembly is rather assinine in itself.
    One return line should suffice. Because of this function, I thought it might be homework or something. The sloppiness of it in general suggests otherwise, though.

  12. #12
    Registered User
    Join Date
    Mar 2007
    Location
    India
    Posts
    17
    Well,
    thanks all for your response..
    Its not a home work , its a part of image decoder code . and i am trying to convert it in C. So i asked for the logic in the assembly code.
    Last edited by Brw_Abhi; 08-21-2007 at 11:57 PM.

  13. #13
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Think I'm kind of rusty on this...

    Maybe a rough idea:

    Code:
    WORD lookKbits(BYTE k)
    {
    	return (wordval >> (16 - k));
    }
    Code:
    WORD WORD_hi_lo(BYTE byte_high,BYTE byte_low)
    {
    	return ((byte_high << 16) | byte_low); /* Endian problems?  Probably not on an intel system. */
    }
    Code:
    SWORD get_svalue(BYTE k)
    {
    	DWORD x = (wordval << k) >> 16;
    	WORD w;
    	if((!(x & (k - 1)))
    	{
    		w = *(WORD *)(start_neg_pow2 + k * 2); /* Maybe w = start_neg_pow2[k*2]; */
    	}
    	return w + (x & 0xFF);
    }
    They're probably all wrong, but the last one especially is probably totally screwed up. I wrote it out twice, but got myself confused in the process.

    Anyway, have fun.

  14. #14
    Registered User
    Join Date
    Mar 2007
    Location
    India
    Posts
    17
    Hi MacGyver,
    you have give code
    Thanks let me try with ur logic .

  15. #15
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    326
    Yes, but those are C functions, I don't see any return statements. (in the OP post)

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learning Assembly
    By mrafcho001 in forum Tech Board
    Replies: 5
    Last Post: 03-12-2006, 05:00 PM
  2. C to assembly interface
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 02-04-2005, 03:51 PM
  3. assembly language...the best tool for game programming?
    By silk.odyssey in forum Game Programming
    Replies: 50
    Last Post: 06-22-2004, 02:11 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. C,C++,Perl,Java
    By brusli in forum C Programming
    Replies: 9
    Last Post: 12-31-2001, 03:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21