📚 OpenRCE is preserved as a read-only archive. Launched at RECon Montreal in 2005. Registration and posting are disabled.








Flag: Tornado! Hurricane!

 Forums >>  Brainstorms - General  >>  x86 Instruction SBB

Topic created on: April 10, 2009 15:00 CDT by Nadya .

Hello,
I'm trying to re-write one 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?

big thanks in advance

  phn1x     April 10, 2009 18:58.58 CDT
Compiler optimization?

One relatively common compiler optimization that can be handy to quickly recognize relates to conditional assignment (where a variable is conditionally assigned either one value or an alternate value). This optimization typically happens when the ternary operator in C (�?:�) is used, although it can also be used in code like so:

// var = condition ? value1 : value2;
if (condition)
  var = value1;
else
  var = value2;


For example, The sbb instruction can be used to avoid branching in an IF statement, the sbb approach tends to be preferred by the compiler when setting a value to zero or -1 as a further optimization on this theme as it avoids the need to decrement, assuming that the input value was already zero initially and the condition is specified via the carry flag.

ref: http://www.nynaeve.net/?p=178

  Nadya   April 11, 2009 07:29.28 CDT
this is some kind of checksum algorithm, unfortunately my C implementation:

BYTE reg_8_temp = 0;
    
  for (; the_loop_counter>0; the_loop_counter-- )  {
    
    BYTE* b = ( BYTE *)((mem_Ptr + the_loop_counter) - 1);
      
    *b -= (byte_x + reg_8_temp);
    reg_8_temp = ( *b < byte_x) ? 1 : 0;
    
    }

produces slightly different results than the original asm 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

---
anyone can point what's wrong?

  Nadya   April 11, 2009 14:00.16 CDT

  otto     April 12, 2009 05:52.55 CDT
> anyone can point what's wrong?
The logic you use to derive the carry flag is flawed. The value *b you
use in the comparison has been altered by the preceding subtraction
i.e. *b has already been modified. Furthermore, you are not taking the
previous carry into account when evaluating the new carry.

I wrote a quick C implementation but did only cursory testing:
   BYTE dl = byte_x;
   BYTE carry = 0, tempCarry = 0;
   for(; ecx; ecx--){
      BYTE *b = (mem_ptr + ecx - 1);

      //original value: *b
      //value to subtract: dl + carry

      //If the value we're going to subtract is greater than
      //the original value, the carry will be set.
      //We do not set the carry yet as the previous carry is
      //used in the next line.
      tempCarry = ((dl + carry) > *b) ? 1 : 0;

      //Perform the subtraction using the carry from last round
      *b -= (dl + carry);

      //The carry becomes effective
      carry = tempCarry;

   }

There are many alternative ways to implement this, of course.

  Nadya   April 12, 2009 11:41.24 CDT
now I understand, thanks for help otto!

Note: Registration is required to post to the forums.

There are 31,328 total registered users.


Recently Created Topics
[help] Unpacking VMP...
Mar/12
Reverse Engineering ...
Jul/06
let 'IDAPython' impo...
Sep/24
set 'IDAPython' as t...
Sep/24
GuessType return une...
Sep/20
About retrieving the...
Sep/07
How to find specific...
Aug/15
How to get data depe...
Jul/07
Identify RVA data in...
May/06
Question about memor...
Dec/12


Recent Forum Posts
Finding the procedur...
rolEYder
Question about debbu...
rolEYder
Identify RVA data in...
sohlow
let 'IDAPython' impo...
sohlow
How to find specific...
hackgreti
Problem with ollydbg
sh3dow
How can I write olly...
sh3dow
New LoadMAP plugin v...
mefisto...
Intel pin in loaded ...
djnemo
OOP_RE tool available?
Bl4ckm4n


Recent Blog Entries
halsten
Mar/14
Breaking IonCUBE VM

oleavr
Oct/24
Anatomy of a code tracer

hasherezade
Sep/24
IAT Patcher - new tool for ...

oleavr
Aug/27
CryptoShark: code tracer ba...

oleavr
Jun/25
Build a debugger in 5 minutes

More ...


Recent Blog Comments
nieo on:
Mar/22
IAT Patcher - new tool for ...

djnemo on:
Nov/17
Kernel debugger vs user mod...

acel on:
Nov/14
Kernel debugger vs user mod...

pedram on:
Dec/21
frida.github.io: scriptable...

capadleman on:
Jun/19
Using NtCreateThreadEx for ...

More ...


Imagery
SoySauce Blueprint
Jun 6, 2008

[+] expand

View Gallery (11) / Submit