Fotis’ blog

Blooooooog

Archive for the 'Programming' Category


Thessaloniki calling! (no, not london)

Posted by Fotis on June 26, 2006

It’s been a while since my last post. I’ve been away from home and i didn’t have the time to write a post so i’m back once again with a little proggy!

I’m from thessaloniki, a city in northen Greece. All greeks know about certain ‘mistakes’ we make when we talk and some strange words we use. So i decided to make a thessalonikia (the way we speak in thessaloniki) translator! It’s not complete yet but it’s under development! Since i am going to use regural expressions i decided to write it in perl. So, here it goes!

#!/usr/bin/perl

while() {
  s/thessaloniki/saloniki/g;
  s/thessalonikios/salonikios/g;
  s/thessalonikia/salonikia/g;

  s/ o aderfos/ to karntasi/g;
  s/ tou aderfou/ tou karntasiou/g;
  s/ stou aderfou/ stou karntasiou/g;
  s/ ton aderfo/ to karntasi/g;
  s/ ston aderfo/ sto karntasi/g;
  s/ aderfe/ karntasi/g;
  s/ aderfos/ karntasi/g;

  s/ mou (\w*eis)/ me \1/g;
  s/ mou (\w*ei)/ me \1/g;
  s/ mou (\w*w)/ me \1/g;
  s/ sou (\w*eis)/ se \1/g;
  s/ sou (\w*ei)/ se \1/g;
  s/ sou (\w*w)/ se \1/g;

  if (int(rand(10))) {
   $letter = “”;
  } else {
   $letter = “l”;
  }

  s/la/${letter}la/;

  if (int(rand(20))) {
    $letter = “”;
  } else {
   $letter = “l”;
  }

  s/lo/${letter}lo/;
  s/le/${letter}le/;

  s/\.\s*Sovara\?/Plaka me kaneis?/g;
  s/\.\s*sovara\?/plaka me kaneis?/g;

  s/(\.\s*)Afou (.*)\./\1\u\2 afou./g;
  s/(\.\s*)afou (.*)\./\1\u\2 afou./g;

  print;
}

If you want to add something here leave a comment!

Update: Changed placement of afou and made a funny substitution (plaka me kaneis).

Posted in Programming | 2 Comments »

Inline assembly for your c programs

Posted by Fotis on June 12, 2006

I got a comment at a previous post about the inline assembly i used and what was that ’strange’ syntax of the asm construct with those ‘: :’ so i decided to make a post about this. Inline assembly is to be used only at special cases e.g. using mmx/sse/sse2 instructions to optimize your code or a special instruction like cpuid to get cpu information.
First of all a small introduction. I’m going to use x86 assembly (another architecture some other time) and the gnu c compiler. It uses the at&t syntax (there is support for intel syntax but it sucks). For all of you who are not familiar with at&t syntax here are some things you must know:

  • Registers
    The names of all registers begin with %. So if we want to use the eax register we write %eax.

  • Immediate operants
    All immediate operants begin with $. So 0xdeadbeef becomes $0xdeadbeef.

  • Indirect memory references
    Indirect memory references are done using ( ). So if we want the byte pointed by register esi we use (%esi).

  • Ordering
    The source and destination ordering is:
    instruction source, destination
    It’s the reverse of the intel syntax.

  • Size of operant
    For all instructions that can take operants that have variable sizes you must write this size. The sizes are:

    • b - Byte (1 byte)
    • w - Word (2 bytes)
    • l - Long (4 bytes)

For an example showing the above we’re going to move the value 0xcafebabe to the location pointed by %edi. What we’re going to use is:
movl $0xcafebabe, (%edi)
After this introduction we’re getting into the main part of the post, inline assembly. To put inline assembly into one of your programs you must use the “asm” construct. WARNING! This is NOT ansi c! Each compiler uses it’s own constructs and the same compiler may use different ones for different architectures! What i’m going to show you is only for the gnu compiler and for the x86 architecture. The syntax of the “asm” construct is:
asm( assembly code : output operants (optional) : input operants (optional) : globbered registers);
The output operants and input operants consists of a list of the form:
“constraint” (variable), “constraint” (variable), …
At the output operants, constraint shows what is going to be placed at the variable and at the input operants where is the variable going to be placed.
The constraints can take the following values:

  • r - any register
  • a - eax register
  • b - ebx register
  • c - ecx register
  • d - edx register
  • S - esi register
  • D - edi register
  • f - floating point register
  • t - first floating point register
  • u - second floating point register
  • m - memory operant
  • matching operant

Let’s see some examples now.
First a simple one. We want to put the value of variable kot to the register ebx.

asm(”movl %0, %%ebx\n” : : “m” (kot));


%0 gets substituted with the location of kot and then the value is moved to register ebx.
Now let’s put the value of koko to lala. Both of them are at some locations at memory and we can’t use two memory references at the same instruction. So what we are going to do is put the value of koko to eax and then move eax to to lala.

asm(”movl %%eax, %0\n” : “=m” (lala) : “a” (koko));


Since we use the “a” constraint before putting the assembly instructions the compiler first moves the value of koko to eax. Of course this could be done using two instructions.

asm(”movl %1, %%eax\n movl %%eax, %0\n” : “=m” (lala) : “m” (koko) : “%eax” );


You should note that since eax changes value inside the instructions we put it in the list of globbered registers.
Some times we need to use the same register for both input and output. So what we do is use matching constraints. In the following example we are going to increase the value of kot by one.

asm(”incl %0\n” : “=a” (kot) : “0″ (kot));


Some times the compiler while optimizing our code changes the location of the instructions. As an example the compiler could move something we put inside a loop and put it before or after the loop. To instruct the compiler to leave the code to the place we put it we use the volatile keyword. So the above example becomes

asm volatile (”incl %0\n” : “=a” (kot) : “0″ (kot));


Well, this was a simple introduction. I hope you find it useful. Check the GCC-Inline-Assembly-HOWTO for more info.

Posted in Programming | 2 Comments »

Playing with the return address

Posted by Fotis on June 7, 2006

Try this

#include <stdio.h>
#include <stdlib.h>

void lala(void)
{
printf(”Hello world!\n”);
exit(0);
}

int main(void)
{
asm(”movl %%eax, 4(%%ebp)” : : “a” (lala));
}

Isn’t it nice?
What we’re doing here is very simple, just overwritting the return address from main() and changing it to the address of lala(). main() then returns to lala() which calls printf. Nice, isn’t it? Btw, this only works for the x86 platform!

Posted in Programming | 4 Comments »

Music and programming

Posted by Fotis on June 5, 2006

When i program i always listen to some music. What i have noticed is that there is a relationship between productivity, the type of program (big project or just a simple script), the language i write it and the type of music.
I listen a lot to greek music. My favorite artist is Nikos Makropoulos (’Ypopto to aisthima sou’, ‘Katastasi ektaktou anagkias’ and ‘Ypo to miden’ are some of his best songs) but that doesn’t mean that i don’t listen to other greek quality singers like Panos Kiamos and Nikos Vertis. All of them are the so called ‘dog’ singers (greek expression).
Depending on the mood i also listen to house, reggae, rock and some times even electrofunk. What i can’t listen to are some types of metal like death metal and goth.
So, let’s denote productivity as P, the type of program as T with 5 meaning a big project and 1 meaning a simple script, the language as L and type of music as M.
L has the following values:
5: C
3: Assembly (btw, at&t syntax rules, intel sucks :) )
2: Java, Perl, Shellscript
1: Others (C++, …)
M has the values:
4: Greek music
2: Other
And the magic equation is

P = (T + L) * M

Some times i feel that the values of the variables can change depending on some other factors (e.g. weather, programming for fun and pleasure or for university, …) and especially M. Maybe some time i’ll find a new equation with more variables that gives a better result…

Posted in Programming | No Comments »

C editing with vim

Posted by Fotis on June 3, 2006

I believe that the best editor for a programmer is vim. I have used it for quite some time and i found that all those features can make your life really easy. Some settings that may help you are the following.
First some general stuff:

set nocp
set number
set ruler


My coding style is softtabs and each tab is 2 spaces. So for some help for the indentation i use the following:

set cindent
set expandtab
set shiftwidth=2
set tabstop=2


Another very good feature of vim is syntax (for highlighting, folding, etc). The settings i use are:

syntax enable
” I hate spaces at the end of a line so i want to highlight them.
syntax match Error “\s\+$”

For the folding i always use the syntax method. I also made some mappings that help me with fold open/close:

set foldmethod=syntax
syntax sync fromstart
” Never fold at the beginning
set foldlevelstart=99
map zo
map zc
map zR
map zM


These are my general settings.
Apart from these i use some plugins. A programmer must have the taglist plugin. It is a source code browser for your programs. It creates a window with all functions, variables, macros, etc that you have at your program and you can jump anywhere you want anytime. To use it you must also have the exuberant ctags utility, but you’re a programmer, you should have it! A mapping that i use is

map :Tlist


that allows me to open the browser at anytime by just pressing F11.
Another plugin that i have installed is the c reference plugin. It has a complete c reference and you can find info about any command and any function at the standard c library by just using some keystrokes. To be honest i never needed it but you may find it usefull :)
Finally i would suggest you to get vim7, the latest vim version. It has an autocomplete feature (try typing pri and press Ctrl-p !), it’s syntax for c is much better and it has many many other improvements.
I think that’s all. I wrote this while watching an episode from a known tv series where jac^H^H^Hgod saves earth for the fifth time in five days (more to come about this in a new post). If i find something else or if there is a bug i’ll post again about vim :)

Update: As dado1945 pointed out ctrl-p was in vim 6 too, only omni-complete was added. Sorry, i didn’t know this :) But the menu was added in vim 7!

Posted in Programming | 4 Comments »