Thread: x86 Instruction SBB

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    x86 Instruction SBB

    Hello,
    I'm trying to reverse some application from x86 asm to C but don't know how to translate this code:

    mov ecx, the_loop_counter
    mov esi, mem_Ptr
    mov dl, byte_x
    clc
    @sbb_loop:

    sbb [ecx+esi-1], dl

    loop @sbb_loop

    ----------

    to be specific I don't know how to check carry flag status, can anyone help me out please?
    how to re-write this code into C?

    big thanks in advance

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    sbb does the same as sub, but also subtracts 1 more if carry flag is set. clc clears the carry flag, but I think that somewhere within the loop it may be set in certain situations.

    This is not enough information to actually tell what the loop does.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    thanks, but I'm asking how to re-write this code into C, with sub instruction it would be easy something like:
    Code:
    for   (; the_loop_counter>0; the_loop_counter-- )  {
           
            *( BYTE *)((mem_Ptr + the_loop_counter) - 1) -= byte_x;
    
    }
    but I dont know how to write this "for" loop with sbb instruction, this code is part of a checksum algorithm.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    To rewrite something to C you have to know the exact meaning of everything. There's no way to write the sbb instruction in C. If you know the exact meaning of the entire block of code, then there is probably an equivalent for C, for which this is an optimized compiled result. Not always though...

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Wouldn't this work?
    Code:
    BYTE* b = ( BYTE *)((mem_Ptr + the_loop_counter) - 1);
    *b -= byte_x - (*b < byte_x) ? 1 : 0;

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    If you are after converting a piece of optimized code, if that is the case, within the assembly code back to pre-compiled c code will takes lots of trial and error. For that will require much knowledge of how .asm code is created and what each instruction does. Also this would be very archetecturally dependant as well, but you have already eluded that piece.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    Quote Originally Posted by bithub View Post
    Wouldn't this work?
    Code:
    BYTE* b = ( BYTE *)((mem_Ptr + the_loop_counter) - 1);
    *b -= byte_x - (*b < byte_x) ? 1 : 0;
    thank you very much bithub seems this solve my problem, thanks & respect!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. x86 assembly, mov vs lea
    By cyberfish in forum Tech Board
    Replies: 5
    Last Post: 07-15-2009, 03:12 AM
  2. Simulator
    By MasterAchilles in forum C Programming
    Replies: 10
    Last Post: 11-30-2008, 10:31 PM
  3. Atomic Operations
    By Elysia in forum Windows Programming
    Replies: 27
    Last Post: 03-27-2008, 02:38 AM
  4. Intel x86 instruction set + bytes
    By cboard_member in forum Tech Board
    Replies: 3
    Last Post: 03-31-2006, 02:25 PM
  5. Emulating x86 Instructions
    By SMurf in forum C Programming
    Replies: 1
    Last Post: 12-18-2005, 12:16 PM