The worst abuse of the C preprocessor (IOCCC winner, 1986)

The worst abuse of the C preprocessor (IOCCC winner, 1986)

  • obfuscation?is the deliberate act of creating machine code that is difficult for humans to understand. This article is about to explore one of these kinds of codes, in this time looking for printing in morse.

Morse code is an old method to express words, sentences, or letters using only three symbols.

  • Short signals.
  • Long signals.
  • Pauses.

We could use for example sound, light, or electronic signals. Or in where is mostly used in write scenario. Using the characters: “.” “-” “ ”.

No hay texto alternativo para esta imagen

Example of use: When we use an "SOS" signal.

  • "...---..."

C Program by Jim Hague (1986)

Jim Hague (born in the UK) programmed a C program that translates ASCII code which is inserted in standard input to Morse code.

Jim Hague won the IOCCC contest in 1986. In this one, they wanted to find who can create the most confusing and unorganized code for something basic, and in this situation, the code used is mostly from the preprocess part using the precompiled.

  • "hague.c" - In this time is running in a Replit so it will be called "main.c"

No hay texto alternativo para esta imagen

In this situation is basically impossible to understand what the code does. And because of that, we are gonna "decode" step by step to understand what is this code about.

Like was told before this code uses basically the preprocess, so we are gonna use the corresponding command to compile the .c file till getting an executable file linked.

$gcc main.c -o h        
No hay texto alternativo para esta imagen

This will cause a lot of warnings, creating a new file called h, which when we execute, we can actually write the text that we want to "translate" to morse. Just like is shown below.

No hay texto alternativo para esta imagen
No hay texto alternativo para esta imagen

We have to move left (DAH or Dash) or right (DIT or Dot) to "decode" the message. So, with this, we are able to translate any input into morse code.

Transforming the code

$gcc -E main.c -o hague.pr        

In order to Make it, we are gonna use the first part in the compile process, which is called the preprocessing, and what this does is mainly remove comments and expand macros. So we are gonna be able to expand the #define preprocessor directive.

No hay texto alternativo para esta imagen

Like we can see in the above image we were able to get readable text.

Text transformed

The code uses a string full of letters and symbols, and this is the base to relate with the morse symbols.

No hay texto alternativo para esta imagen

In the above image, we sort a little bit the code for best understanding, now we can continue with the de codification as follow.

  • _DIT -> DOT.
  • DAH_ -> DASH.
  • DIT_ -> DOTS.
  • _DIT_ -> DOTSDOTS.

Functions names

  • _DAH -> TRANSFORM_TO_MORSE.
  • _DIT -> PRINT_MORSE.

No hay texto alternativo para esta imagen

At this point, we finally got a readable file that we can analyze.

What we can see is that basically, we have a .c file with the main function and another 2, that are TRANSFORM TO MORSE and PRINT_MORSE.

PRINT_MORSE is like a _putchar function and is used to print characters.

TRANSFORM_TO_MORSE purpose Is to transform every character into morse code, and when DOTS_ > 3 move DOTS_ one byte to the right side if not prints zero.

The main() function uses 3 loops.

  • The inner loop: checks if the input contains characters and changes lower case characters into upper case.
  • The middle loop: uses the TRANSFORM_TO_MORSE function when the input has characters that are part of _DAH_[] array.
  • The external loop prints a new line when the input finishes.

With this, we were able to look into the possibilities of using macros and also to explore the problems that can be generated if the programmer abuses them. Then we conclude the objective of this article.

I hope the information was useful for the lector and that you can understand a little bit more about this interesting topic.

要查看或添加评论,请登录

Juan Sebastian Gonzalez的更多文章

  • Recursion

    Recursion

    What is it? Essentially is a technique that split a situation or a problem into shorter pieces of itself. Take a look…

  • Mutable, Immutable... everything is an object! O.o

    Mutable, Immutable... everything is an object! O.o

    In Python, everything is an object, which means every entity has some metadata (called attributes) and associated…

  • Differences between static and dynamic libraries

    Differences between static and dynamic libraries

    A library is a powerful tool in a programmer's arsenal, consisting of pre-compiled functions that can be easily reused…

  • How integers are stored in memory using two’s complement

    How integers are stored in memory using two’s complement

    A computer is an amalgamation of hardware and software, where digital memories store information in bits (short for…

  • C static libraries

    C static libraries

    This article is about exploring in C what are the static libraries, why should we use them, how they work, how can be…

  • Soft Link Vs. Hard link

    Soft Link Vs. Hard link

    Understanding the difference between a hard link and a symbolic link requires knowledge of how computers store and…

  • Compilation Process using in programs like C.

    Compilation Process using in programs like C.

    In programming, there are two types of languages, which are interpreted and compilated. The code of compiled language…

社区洞察

其他会员也浏览了