Ad
  • Custom User Avatar

    thank you for this incredibly thorough explaination

  • Custom User Avatar

    Codewars works by receiving code as a piece of text from your web browser; saving it to a file ;executing it on their server with the appropriate compiler/interpreter; and returning the output to your browser. It follows that you can get information on the system that runs your code by calling the appropriate commands.

    It's easy to establish that the server runs on Linux because of the low-level nature of C (for example the __linux__ macro is defined); web servers also typically run on Ubuntu, and the x86_64 architecture has become almost ubiquitous for desktop computers. But you can make sure of that by running something like this on Codewars (i wrote it in C because it is the only language you trained, but you could do the equivalent in almost all the other languages on Codewars):

    void print_os (void)
    {
    #ifdef __linux__
        char buffer[256];
        FILE * fp = popen("lsb_release -c -d", "r"); // lsb_release prints the OS info
        while (fgets(buffer, sizeof buffer, fp) != NULL) 
            printf("%s", buffer);
        fclose(fp);
        fp = popen("lscpu", "r"); // lscpu prints the architecture info
        while (fgets(buffer, sizeof buffer, fp) != NULL) 
            printf("%s", buffer);
        fclose(fp);
    #endif // you could add more test macros and code for Windows, etc.
    }
    

    Use this function on Codewars (just call it in the function you're supposed to write for the kata) and you will see:

    Description:	Ubuntu 18.04.6 LTS
    Codename:	bionic
    Architecture:        x86_64
    CPU op-mode(s):      32-bit, 64-bit
    Byte Order:          Little Endian
    CPU(s):              4
    

    So we know the endianness, OS, and architecture.


    how are you associatting these termss with the code ?

    C is a compiled language and C compilers generate lower-level assembly code for a specific architecture. We've established that Codewars runs on Linux x86_64, so we only need to search which calling convention is used: the System V ABI. A calling convention is a way to precisely describe how functions are to be called in assembly and how they return. It specifies which arguments are to be put in which registers by the caller, what the callee has to restore before returning control, etc. Without a calling convention, the different parts of a program could not talk to each other and it would be impossible to write reliable code.

    is an IEEE754 double number

    IEEE 754 is a techincal standard/specification for floating-point numbers. It's so widely adopted that it's become almost synonymous with them. Almost all programming languages use it for their float and/or double datatypes.

    what made you think it was little endian was it the assembly code ?

    we've established that Codewars runs on a Little-Endian machine, so the bytes that represent the float value are going to be hardcoded in little endian order in the executable files (on disk) as well, as they will be directly loaded into memory (RAM) in the same order.

    linker cares about is that there is something called multiply associated with a callable piece of code

    You should experiment with compiling and building C programs that are split between several files to get a better understanding of the process. The peculiarity of the Codewars setup is that it uses no header (.h) files, unlike most large C programs. The command basically goes like cc user_code.c tests.c. Due to the absence of header files there is no mechanism to ensure that types are compatible between the two files. All tests.c knows (from a declaration) is that there is a function int multiply(int, int) that it can call, but nothing prevents you from writing e.g. double multiply(int a, float b, short c) instead in user_code.c (which is the file that you edit when you train on a kata), and it will still compile and run (but will behave strangely); you can see it for yourself.

    can you name the resource materials, concepts that I would need to learn in order to have knowledge like this.

    I have known (not in great detail) about these things for some time now and it would be hard for me to give advice to a beginner on the best way to learn in 2025, especially within the current AI boom. Many Wikipedia articles give in depth descriptions of subjects such as calling conventions; cppreference.com is a good reference on C/C++ specifically, godbolt.org is good to see what your code compiles to in different languages/environments/compilers, I often use tsnippet.trust-in-soft.com to find bugs in my C programs. But none of these tools are aimed at beginners.

  • Custom User Avatar

    Could you please let me all the concepts that you have gone through ?
    How have you managed to link all the terminologies together like:

    • linker cares about is that there is something called multiply associated with a callable piece of code
    • is an IEEE754 double number
    • (little-endian, hexadecimal notation) what made you think it was little endian was it the assembly code ?
    • System V AMD64 ABI that Linux uses how are you associatting these termss with the code ?

    can you name the resource materials, concepts that I would need to learn in order to have knowledge like this.

  • Custom User Avatar
  • Custom User Avatar
  • Custom User Avatar

    Hi, I replied on email 😉

  • Custom User Avatar

    What you did in those two last lines of code is crazy. I am still trying to wrap my head around it.

  • Custom User Avatar

    i learn a lot standard library from solution

  • Custom User Avatar

    didn't knew that you can call asm in c

  • Custom User Avatar

    holy shit thats quite clever

  • Custom User Avatar
  • Custom User Avatar

    Just use namespace std, it will save you a lot of time.

  • Custom User Avatar
  • Custom User Avatar

    That's an overkill, but I salute you for this solution.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Loading more items...