Hybrid
Hybrid

Reputation: 321

Assembly function is confusing

This is an assembly function. From what I understand this is a function calculating the checksum of the 32-bit value in eax by creating a loop.

My confusion arises as lodsb writes the entire 32-bit value. From my understanding, the loop tries to load a byte after reading a 32-bit value, loading zero into eax prior to the test instruction used for terminating purposes. Please inform me if I'm wrong.

Checksum_Calc:
    lodsb
    test        eax, eax        ;avoids null byte in cmp eax, 0
    jz          CheckFunction
    add         edx, eax
    xor         edx, eax
    inc         edx
    jmp         Checksum_Calc

Upvotes: 2

Views: 170

Answers (2)

xmojmr
xmojmr

Reputation: 8145

Using Wikipedia: x86 instruction listings and Wikipedia: Operators in C and C++ translation of your code fragment into C pseudocode would look like this

eax = 0;
/* some invisible code */
while(true)
{
  al = mem[esi];   /* this modifies only lower 8bits overlayed over the 32bit eax "structure" */
  /* eax = (eax & 0xFFFFFF00) | al; */
  esi = esi + 1;
  if (eax == 0)
  {
    goto CheckFunction;
  }
  edx = edx + eax;
  edx = edx ^ eax;
  edx = edx + 1;
}

In general traslating code into another simpler language or even into a graphical language (like the one invented by authors of "Rapid Quality Systems's Code Rocket Designer", see an example) is the way to go when studying some legacy source code

Upvotes: 0

User.1
User.1

Reputation: 2642

Lodsb is putting the number into the AL register (the lower 8 bits only)

This line...

   Test Eax, Eax        ;Avoid the null byte in Cmp Eax,0

...is looking at all 32 bits

Upvotes: 2

Related Questions