Challenge RE #14

Something unexpected this time, I was quite confident with my assembly lines in x86 assembly given that it’s the only assembly language I know. Now the challenge came up with this


  .method public hidebysig static bool  f(char a) cil managed
  {
    // Code size       26 (0x1a)
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  ldc.i4.s   97
    IL_0003:  blt.s      IL_000c

    IL_0005:  ldarg.0
    IL_0006:  ldc.i4.s   122
    IL_0008:  bgt.s      IL_000c

    IL_000a:  ldc.i4.1
    IL_000b:  ret

    IL_000c:  ldarg.0
    IL_000d:  ldc.i4.s   65
    IL_000f:  blt.s      IL_0018

    IL_0011:  ldarg.0
    IL_0012:  ldc.i4.s   90
    IL_0014:  bgt.s      IL_0018

    IL_0016:  ldc.i4.1
    IL_0017:  ret

    IL_0018:  ldc.i4.0
    IL_0019:  ret
  } // end of method some_class::f

Wait what? According to the description we have here

Optimizing csc .NET compiler from MSVS 2015 (/o switch), ildasm output:

As you imagine I wasn’t aware of this at all. Still I have to solve this shit, so we use our powerful tool called Google. Using Google I manage to understand what this code does, so here I go. By the way the documentation from Microsoft it’s just amazing, God!! So detailed…at least in this case. Here it’s a link to the instructions doc in case you will need them:

  1. ldarg.0
  2. ldc.i4.s
  3. blt.s

Analysis

There’s something here to notice first of all, is that we are dealing with a stack machine here. Hence the lines

    IL_0000:  ldarg.0
    IL_0001:  ldc.i4.s   97
    IL_0003:  blt.s      IL_000c

Do the following:

  1. Push argument at index 0 into stack
  2. Move 97, or letter ‘a’ in ASCII code, into stack as an int32
  3. Pop 97 and the argument from the stack and check if argument < 97, jump to location IL_000c.

This is the way arguments are evaluated. Keeping this in mind, we can infer that that this snippet of code, basically check that the character supplied it’s less than 'a' ASCII letter. You can find a list of ASCII codes here.

In the first 6 lines, we check if character supplied it’s between 'a' and 'z', basically if it’s a Lowercase letter in the English alphabet. If that’s the case the program will return 1. Analyzing the next block of code, we can also infer that we perform the same check, but this time with letters 'A' and 'Z'. Basically checking if we have an uppercase letter. If that’s the case we also return 1. Otherwise we return 0.

Formal description

The program check if a supplied character belong to the English Alphabet. Returning 1 for true cases, and 0 otherwise.

Conclusion

Don’t be afraid of new stuffs, just Google.