Showing posts with label jz. Show all posts
Showing posts with label jz. Show all posts

Friday, January 24, 2020

assembly basics: strcmp ; test eax, eax

push eax (1st string to compare)
push ecx (2nd string to compare)
call strcmp (do the compare using C library ... if same EAX = 0, if different EAX = 1)
test eax, eax (same as 'and eax, eax' ... so if EAX = 0 ZF = 1 ... if EAX = 1 ZF = 0)
jz loc_40124C (so jump if zero jumps if ZF = 1 ... which is when EAX = 0)

------

In simpler terms
- compare the 2 strings
- if same
  - EAX gets set to 0
  - ZF gets set to 1
  - JZ will jump because ZF = 1
- if different
  - EAX gets set to 1
  - ZF gets set to 0
  - JZ will NOT jump because ZF = 0

------

In simplest terms 
  if you see " strcmp ; test ; jz "
    JZ green if the 2 strings are the same (0)
    JZ red if the 2 strings are different (non 0)

  if you see " strcmp ; test ; jnz "
    JNZ green if the 2 strings are different (non 0)
    JNZ red if the 2 strings are the same (0)

  if you see " strlen; test ; jz "
    JZ green if empty string (0)
    JZ red if non-empty string (non 0)

  if you see " strlen ; test ; jnz "
    JNZ green if non-empty string (non 0)
    JNZ red if empty string (0)

  if you see " call; test ; jz "
    JZ green if function call successful (0)
    JZ red if function call failed (non 0)

  if you see " call ; test ; jnz "
    JNZ green if function call failed (non 0)
    JNZ red if function call successful (0)

  if you see " cmp ; test ; jz "
    JZ green if the 2 numbers are the same (0)
    JZ red if the 2 numbers are different (non 0)

  if you see " cmp ; test ; jnz "
    JNZ green if the 2 numbers are different (non 0)
    JNZ red if the 2 numbers are the same (0)


Jump arrows
Green: if condition is satisfied (JZ=0, JNZ=non-0)
Red: if the condition is not satisfied (JZ=non-0, JNZ=0)