Hacker News new | past | comments | ask | show | jobs | submit login

I love how thin the layer above assembly is: without knowing B, is my interpretation correct that this function effectively “inherits” the stack of the calling function? In other words, rather than passing function arguments and let the compiler deal with it, you’re supposed to push the string you want to lcase onto the top of the stack?

Reminds me a lot of writing my own compiler/assembler in university, where it’s expected that all this happens automatically nowadays.




No, that's not correct. It reads the string from standard input. A C translation would look like this:

    main()
    {
        int ch;
        while ((ch = read()) != 4) {
            if (ch > 0100 && ch < 0133)
                ch = ch + 040;
            if (ch == 015) continue;
            if (ch == 014) continue;
            if (ch == 011) {
               ch = 040040;
               write(040040);
               write(040040);
            }
            write(ch);
        }
    }
A more modern C version would look like:

    #include <stdio.h>

    int
    main(void)
    {
        int ch;
        while ((ch = getchar()) != -1) {
            if (ch > 0100 && ch < 0133)
                ch = ch + 040;
            if (ch == 015) continue;
            if (ch == 014) continue;
            // No need to handle tabstop specially
            putchar(ch);
        }
    }


Hmm, don’t think so. The function does not operate on a string, it seems to read a character using read() and write it back, transformed, using write(). Given that the function is named main, it’s probably the top level function anyway (from the programmer’s point of view, often the OS actually calls into a different function that is part of the language runtime, e.g. _start, which in turn calls main eventually, but that is usually hidden from the programmer).


This is the main function ... there is no calling function. Nor is a string on the stack being accessed.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: