Personal.X-Istence.com

Bert JW Regeer (畢傑龍)

StackOverflow

When I was 12 I started to learn how to program, I had just together with my dad, bought my first C++ book. I don't remember the name of the book, it might have been the C++ for Dummies book, either way it came with a CD and on that CD was the DJGPP. That day I spent some time learning the new environment the book had given me, I tried writing simple programs. Just a few days after I had bought the book I wrote a very simple C++ calculator. Nothing fancy, only 32 bit integers and what you wanted to do had to be entered in a very specific way, but I felt empowered. I had learned something, I was able to do this and master the machine.

From there my grand dad introduced me to BASIC, he had written software in BASIC before and figured that would be somewhere else to start and play with, my uncle helped me set up an environment and taught me the basics of BASIC.

10 PRINT HELLO
15 PRINT WORLD
20 GOTO 10

That was the first program I ran, and I remember it mainly because I was wondering why it was scrolling by on the screen so fast. I learned backwards if you will, I started with C++ and then learned BASIC, but I really did not get started until I was in High School, 9th grade I took a programming class taught in QBASIC. While the class was fun, and exciting I wanted to always push the envelope. For one of the programs we were required to draw a house using the QBASIC built in line drawing commands. What I ended up doing was reading the co-ordinates from a file, and drawing the required shapes in the correct places. During this class the teacher knew I was flying through the assignments and he let me play around with compilers and various other programming languages. C++ was once again the programming language of choice, this time I wrote a network based chat client so that I could communicate with my friend on the other side of the room without physically having to get up and talk to him. The teacher had separated us because of too much talking and this was my solution.

From there I also started experimenting with PHP as a web programming language, I wrote various pieces of software and scripts that never took off to accomplish various things, but mostly just to see if I was able to do it, if I could accomplish the goal I had in mind. This code I still have somewhere, on a CD with various other pieces of old code, recently I took the time to go through some of my old code and I have seen how much I have grown, how much I have learned in the various years that I have been programming now. It also reminded me how much time I spent just looking for resources and information, how long it took to get answers to various different questions.

10 years later the Internet has grown, answers are now as easy as using Google with the right keywords, however even then it can take too long to find the answer to something you know someone else has come across before. This is where StackOverflow comes in, all those years that I have been learning to program I wish a site like StackOverflow had existed, it would have allowed me to easily search for answers to questions as well as ask my own, arguably stupid, questions. It is a website by programmers for programmers and is entirely based around the idea that knowledge should be shared. If you start asking Google questions about C# you will most likely find something from StackOverflow on the list of pages it gives you.

StackOverflow has taken over my life, every day I check StackOverflow for new interesting questions, check the answers and if possible I answer the question myself. Finally there is a place where programmers are able to go with all different experience backgrounds and help each other accomplish one single goal, building better software.

Happenings and assembly

School has started again, I am taking six classes. I am now really gearing up to finally leave UAT and go out into the work force. I have this semester, and two more to go and then comes the decision; graduate school or work? Not entirely sure yet which I want to do, but that is what life is all about, trying to figure out what the best would be, and possibly finding out it was a mistake and growing and learning.

There are others things that have been going on lately, I have been spending much time behind a computer screen programming. The intellectual challenges it provides me with are very enjoyable and great way to stay mentally fit. Also as a perfectionist by nature, it makes it really hard to start working on something and then put it down to go to sleep, it has to be perfect. That is probably also the reason why I now have a 300 line patch set instead of 500 lines. Yay for removing redundant code, and re-thinking the problem/solution.

Other things that I have been working on is a challenge that my room mate Victor (Pancho) gave me. It requires a recursion to complete the problem. In itself this does not generally present a problem, as the max recursion level is going to be limited to a fairly reasonable number, however in C/C++ there is still a massive amount of overhead when doing a function call. This includes things like setting up the stack, creating local variables, and then saving certain states. Extra garbage that you want to eliminate especially in tight loops. The problem however cannot be solved using for/while loops, or at least there is no way that I can figure out (if you want to know what it is that I am working on, contact me. Victor is going to make the challenge public later so I don't want to spoil it for everyone). So I have been writing some self-modifying code that unrolls loops on the fly. I will post code sometime in the near future when I have it partially working, not entirely sure yet how I want to proceed right now. More on that later!

Here is a bit of Intel assembly written in AT&T syntax for GCC to grok for Mac OS X/FreeBSD machines that use a stack based syscall infrastructure:

__asm__("pushl  $21;\n"
    "pushl  %0;\n"
    "pushl  $0x1;\n"
    "movl   $4, %%eax;\n"
    "pushl  %%eax;\n"
    "int    $0x80;\n"
    "addl   $16, %%esp;\n"
    :
    : "r" (answer)
    : "%eax");

Note that the length of the string is 21 characters (20 characters, and a newline which I appended before printing), and that answer is the char *. See my previous Intel post to see what the code does, and why. It is exactly the same printing routine, only difference is the assembly syntax. It really is too bad that GCC for Darwin does not grok the .intel_style inline assembly, it would have made things much easier.

You should be able to modify it to accept an integer to push onto the stack, however I have been unable to figure out why the following is not working:

__asm__("pushl  %0;\n"
    "pushl  %1;\n"
    "pushl  $0x1;\n"
    "movl   $4, %%eax;\n"
    "pushl  %%eax;\n"
    "int    $0x80;\n"
    "addl   $16, %%esp;\n"
    :
    : "r" (length), "r" (answer)
    : "%eax");</pre>

Assuming length is an integer and answer is a character pointer. It is kind of frustrating, because there are not many good resources out there on the web that explain inline assembly for GCC. The few that I have been found have been rather vague and have not explained very much. Maybe I am just using the wrong words, and or my Google-Fu is not strong enough.

Speaking of inline assembly, I have heard from some friends that are running on 64-bit systems that if you compile 64-bit binaries for Windows in Visual-C++ 2008 you are not allowed to use inline assembly, they have removed support for it. Does that seem wrong to anyone else? That seems like it would frustrate many developers of tight code that runs many times faster in assembly than using compiled code. Sure compilers have been getting better for ages, however at the same time in assembly the programmer has a lot more control over the CPU and where time is going to be spent than any other way.

Oh, Erlang is my new shiny programming language to learn.