Answer Set Programming: with dlv and ASPIDE

- go to
- download dlv.x86-64-linux-elf-static.bin
- go to
- chose ".jar packages without installer" to download
- # mv dlv.x86-64-linux-elf-static.bin dlv
- # chmod a+rx dlv
- extract, it gives a folder ASPIDE
- go to ASPIDE
- # java -jar startAspide.jar
- internet connection is required here.
- it asks to install latest aspide, chose to install.
- it asks to install aspide add-ons. allow to install it.
- after installation, it asks for registration, register with your name and email.
- it sends a authentication code to mailbox and asks to input authentication code.
- provide authentication code and aspide starts...
- it asks for a project workspace path and then asks for path to dlv, provide them.
- now IDE of aspide opens... close it.
- now onwards aspide can be opened by # java -jar startAspide.jar
however, it needs internet to check updates etc. before starting IDE.
better to use # java -jar application/aspide/aspide.jar
- now IDE opens...
- create a new project
- create a new file : "DLV File with Arithmetic Expressions"
- write an asp program in the file and save it.
- to run the asp program, click Execute->Run.
- to make a query, click Execute->Query.
- query dialog opens, tick the checkbox "Epistemic Mode".
- type the query and suffix it with a question mark, then click "Execute".
- you may need to click "Run Configurations" and set path of dlv executable, if it asks: executable not found.
- enjoy.

Answer Set Programming: with clingo

- goto
- download clingo-4.4.0-x86_64-linux.tar.gz
- # tar -xvzf clingo-4.4.0-x86_64-linux.tar.gz
- goto clingo-4.4.0-x86_64-linux folder
- execute an asp program as # ./clingo -n 0 family.lp
- it prints all answer sets; enjoy.

Some low level debugging tools in linux.

nm : just lists symbols from obj file
-n sort symbols numerically by address
-D shows dynamic symbols only

Symbol type:
If lowercase, the symbol is local;
if uppercase, the symbol is global (external).
A - symbol's value is absolute, will not be changed by further linking.
B - symbol is in the uninitialized data section (known as BSS).
C - symbol is common. Common symbols are uninitialized data.
D - symbol is in the initialized data section.
N - symbol is a debugging symbol.
R - symbol is in a read only data section.
T - symbol is in the text(code) section.
U - symbol is undefined.

objdump : shows details of obj files
-d disassembly
-S mix source code when disassembles
-x display section/program headers
-r display relocation entries
-w more than 80 character wide display

readelf : displays information about contents of ELF format files
-a Equivalent to: -h -l -S -s -r -d -V -A -I
-e Equivalent to: -h -l -S

-h Display the ELF file header
-l Display the program headers
-S Display the section headers

-s Display the symbol table
--dyn-syms Display the dynamic symbol table

-r Display the relocations (if present)
-d Display the dynamic section (if present)

-W more than 80 character wide display

good examples:
# gcc -c -o test test.c
# readelf -esr test > a.txt

# gcc -g -c -o test test.c
# objdump -drS test > b.txt

PHP code cleaning tools.

PHP LOC statistics:
1. wget --no-check-certificate
2. /opt/lampp/bin/php phploc.phar --exclude="lib" --exclude="Resources" /mnt/data/MyWebProject/ > loc.txt

PHP Mess Detector:
1. Download
2. /opt/lampp/bin/php phpmd.phar /mnt/data/MyWebProject/ text codesize --exclude lib,Resources --reportfile code_complexity.txt
[output format: text; available mess detectors: cleancode, codesize, controversial, design, naming, unusedcode]

PHP Code Sniffer (Enforces a coding standard):
1. Download
2. /opt/lampp/bin/php PHP_CodeSniffer-2.0.0RC1/scripts/phpcs --report-file=code_sniff.txt --standard=Generic --extensions=php --ignore=*/lib/*,*/Resources/* /mnt/data/MyWebProject/

PHP Dead Code Detector:
1. wget --no-check-certificate
2. /opt/lampp/bin/php phpdcd.phar --recursive --exclude="lib" --exclude="Resources" /mnt/data/MyWebProject/ > dead_code.txt

PHP Copy Paste Detector
1. wget --no-check-certificate
2. /opt/lampp/bin/php phpcpd.phar --exclude="lib" --exclude="Resources" /mnt/data/MyWebProject/ > copy_paste.txt

While searching files with find, exclude some directories

find . -type f -name '*.php' ! -path "./lib/*" ! -path "./Resources/*" -exec vim -s /root/vimscript.scr "{}" \;

To print colored text in linux terminal

echo "$(tput setaf 1) Hello $(tput setaf 2) World! $(tput sgr0)"
echo -e "\033[31m Hello \033[32m World! \033[0m"

Option -e means "enable interpretation of backslash escapes"

How to indent php+html+javascript code using Vim on Linux

1. install full vim with plugins.
put following vim commands in /root/.vimrc

:set nocompatible
:filetype indent on
:syntax on
:set tabstop=4
:set shiftwidth=4
:set expandtab

2. create a vim script file (lets say vimscript.scr) in /root
put the following vim commands in vimscript.scr file

" this is a vim script comment
" remove \r in case file is from windows world

" set filetype as javascript and indent it
:set ft=javascript

" set filetype as html and indent it
:set ft=html

" set filetype as php and indent it
:set ft=php

" convert multiple blank lines to single blank line

" after indenting save file
" script end

3. to indent all php files in a dir, go to that dir and run
#find . -type f -name '*.php' -exec vim -s /root/vimscript.scr "{}" \;

Building geany from source

[To compile geany]
Get source tar file from
# cd geany-1.24.1
# ./configure
# make

[To compile geany plugin]
Get source tar file from
Make sure geany 1.24 is compiled and installed.
# cd geany-plugins-1.24
# ./configure
[This sometimes gives error, "configure: error: Package requirements (geany >= 1.24) were not met".
Test if installation is proper,
#PKG_CONFIG_PATH=/usr/local/lib/pkgconfig pkg-config --modversion geany
May be path is not having /usr/local/lib/pkgconfig, then do,
# PKG_CONFIG_PATH=/usr/local/lib/pkgconfig ./configure]
# make

To create puppy pet/sfs package from source code


# cd xyz-<ver>
# ./configure
# make

# new2dir make install
# cd ..

[to create a pet]
# dir2pet xyz-<ver>-<arch>

[to create a sfs]
# mksquashfs xyz-<ver>-<arch> xyz-<ver>-<arch>.sfs -noappend

Simplest way to install virtualbox in puppy linux.

VirtualBox has a package for generic Linux distributions, like

You can find this link at under name "All distributions".

Simply choose one .run file (32bit or 64bit).
After downloading it, simply run it from terminal.
You need to have devx and kernel-sources packages already loaded into your puppy linux.

VirtualBox gets installed in /opt.

That's it.

To adjust brightness of laptop screen, without help of keyboard. (2)

Some times acpi can't adjust the brightness.

Next thing we need to try is, pass the following parameter to Linux kernel from grub configuration file.


Now if you are lucky, you can control brightness using the same old technique.

Current brightness is provided by file /sys/class/backlight/intel_backlight/actual_brightness.
Collect current brightness.
Max brightness is provided by file /sys/class/backlight/intel_backlight/max_brightness.
Now write a number, from 0 to MAX, to the file /sys/class/backlight/intel_backlight/brightness using echo.

Thanks to

To adjust brightness of laptop screen, without help of keyboard.

Thanks to my puppy Linux, which provides nice little scripts "brightness-up" and "brightness-down" to do the job.

Once we peep into it, we see what is going on under the hood.

To adjust brightness of laptop screen, we do this:

Current brightness is provided by file /sys/class/backlight/acpi_video0/actual_brightness.
Collect current brightness.
Max brightness is provided by file /sys/class/backlight/acpi_video0/max_brightness.
Now write a number, from 0 to MAX, to the file /sys/class/backlight/acpi_video0/brightness using echo.

So simple!!

Compile (with gcc) and link (with ld) to create your executable (without C library).

int add(int a, int b);

#include "x.h"
extern int extra;
int main()
int a = 9;
int b = 8;
int c;
c = add(9, 8) + extra;

return 0;

#include "x.h"
int extra = 9;
int add(int a, int b)
return a+b;

.globl _start

call main
movl $1, %eax
xorl %ebx, %ebx
int $0x80

Now do
~/# gcc -c -nostdlib a.c
~/# gcc -c -nostdlib b.c
~/# gcc -c -nostdlib start.S
~/# ld -o test a.o b.o start.o

Your executable test is ready!!


Good example showing how to create a Position Independent Executable (PIE).

We can create a Position Independent Executable (PIE) using -fPIE compiler option and -pie linker option of gcc.

A nice example collected from net.

#include <stdio.h>

static void foo() {}
static int bar = 5;

int main(void)
int baz = 5;
printf("function: %p, library function: %p, data: %p, stack: %p\n", foo, &printf, &bar, &baz);
return 0;

Now do

$ gcc -o aslr aslr.c; for i in $(seq 1 10); do ./aslr; done
$ gcc -fPIE -o aslr aslr.c; for i in $(seq 1 10); do ./aslr; done
$ gcc -fPIE -pie -o aslr aslr.c; for i in $(seq 1 10); do ./aslr; done

and see what you get!!


When 'clear' command doesn't clean the whole terminal window.

We can use 'reset' command to clean the whole terminal window.

C statement execution time in microseconds

#include <sys/time.h>

struct timeval tv1, tv2;

gettimeofday(&tv1, NULL);
//put c statements here
gettimeofday(&tv2, NULL);

printf ("Total time = %lld microseconds\n", ((long long)tv2.tv_sec*(long long)1000000+(long long)tv2.tv_usec) - ((long long)tv1.tv_sec*(long long)1000000+(long long)tv1.tv_usec));

Sort directories based on human readable size

# du --max-depth=3 . | sort -n | cut -f 2 | while read file_name; do du -sh $file_name; done;

Better strace command

# strace -fFittTvs300 -eraw=all -o output.txt <command or -p pid>

Run dos2unix on all files of a directory

# find . -type f -exec dos2unix '{}' ';'

Git: to get a particular revision of a file

# git show ecc653:my_program.php > k.php

Better grep command

# grep --color=always -i Quad /proc/cpuinfo

Cross gcc compilation




tar xvzf binutils-2.23.tar.gz

bunzip2 gmp-4.3.2.tar.bz2
tar xvf gmp-4.3.2.tar

bunzip2 mpfr-2.4.2.tar.bz2
tar xvf mpfr-2.4.2.tar

tar zxvf mpc-0.8.1.tar.gz

bunzip2 gcc-core-4.6.2.tar.bz2
tar xvf gcc-core-4.6.2.tar

mkdir src
mv gmp-4.3.2 mpc-0.8.1 mpfr-2.4.2 binutils-2.23 gcc-4.6.2 src

mkdir -p opt/cross

export PREFIX="$HOME/opt/cross"
export TARGET=i586-elf
export PATH="$PREFIX/bin:$PATH"

cd $HOME/src
mkdir build-binutils
cd build-binutils
../binutils-2.23/configure --target=$TARGET --prefix="$PREFIX" --disable-nls
make install

mv gmp-4.3.2 gcc-4.6.2/gmp
mv mpc-0.8.1 gcc-4.6.2/mpc
mv mpfr-2.4.2 gcc-4.6.2/mpfr
mkdir build-gcc
cd build-gcc
../gcc-4.6.2/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c --without-headers
make all-gcc
make all-target-libgcc
make install-gcc
make install-target-libgcc

$HOME/opt/cross/bin/$TARGET-gcc --version

#to add cross compiler location to system path
#export PATH="$HOME/opt/cross/bin:$PATH"
#if you like, you can copy the whole "cross" directory from your $HOME/opt to system /opt directory.
#and you add that path to your system path. You can place the export command in .bashrc in $HOME.

Search column name in a mysql db



WHERE table_schema = 'mydb'
AND column_name LIKE '%pattern%' ;

Using script to do something for all files of a folder

# here we try to separate files
#1. that are included in other files
# 2. that are not


echo > a.txt
find $folder | while read filepath
file=`basename "$filepath"`
usedin=`grep -r "$file" $folder`
if [ "$usedin" = "" ]
echo "$filepath" >> a.txt
echo "$filepath"
echo ======
echo $usedin

Sed: to add one space after comma

1. use sed to add a space after comma
2. use sed to replace multiple space(or tab) after comma with one space
3. use sed to perform 1. and 2. using -e option

# cat debug.txt
a, {, 9,L,%

# sed -i.bak -e 's/,/, /g' -e "s/,[ \t]*/, /g" debug.txt

# cat debug.txt
a, {, 9, L, %

Linux commands for set operations

Full explanation at:

Meld: three way merge (manually)

# meld --output o.txt l.txt b.txt r.txt

In general meld displays
l.txt (or local version in case of git, to which version merge is done) in left tab,
b.txt (or base version in case of git, common ancestor to both) in middle tab,
r.txt (or remote version in case of git, from where merge is done) in right tab.

Now make changes to b.txt and save.

Meld saves the changes to o.txt (or working copy in case of git, which is going to be commited).

Files l.txt, b.txt, r.txt all remains unchanged.

How to substitute an old pattern with a new pattern in matched lines of all files inside a directory

Say old pattern is "/old.php",
and new pattern is "/new.php"

# find . -type f -print0 | xargs -0 sed -i 's|/old.php|/new.php|g'
(sed -i for inplace editing of the file)

How to use vim to indent all files in a directory

run vim script indentme.scr to indent all files using vim

# find . -type f -name '*.c' -exec vim -s indentme.scr "{}" \;

[indentme.scr file shall contain vim commands like:

(gg means go to first line
= means auto indent
G means till the last line)

Delete lines containing a pattern from all files inside a directory

# find . -type f -print0 | xargs -0 sed -i '/my_pattern/d'
(sed -i is for inplace editing of the file)

Mysql: forcibly drop a table having foreign constraints

# /opt/lampp/bin/mysql -u root -p -D mydb -e "SET FOREIGN_KEY_CHECKS=0; drop table my_table; SET FOREIGN_KEY_CHECKS=1;"

How to add vim plugin manually

To manually add a plugin for php language to vim

0. vim plugins are of different types, (i) syntax (ii) indent etc.
1. create a folder like ~/.vim/indent or ~/.vim/syntax
2. collect the plugin for php.
Plugins generally have a name like php.vim.
Two different php.vim shall be available, one for syntax and one for indent.
3. put respective php.vim in respective folder.
4. when vim package is installed, it has its plugins in /usr/share/vim/ folder.
5. more info here (

Command find with exec

The below command would find any files with the extension '.doc' and copy them to your /tmp directory

# find / -name '*.doc' -exec cp '{}' /tmp/ ';'

The curly brackets are used in find to represent the current file which has been found. ie. If it found the file shopping.doc then {} would be substituted with shopping.doc. It would then continue to substitute {} for each file it finds. The brackets are normally protected by backslashes (\) or single-quotation marks ('), to stop bash expanding them (trying to interpret them as a special command eg. a wildcard). This ';' is the symbol used by find to signal the end of the commands. It's usually protected by a backslash (\) or quotes to stop bash from trying to expand it.

This finds all c-files and python files in the euler directory.
# find euler/ -iname "*.c*" -exec echo {} \; -or -iname "*.py" -exec echo {} \;

Delete empty lines from a file using vi editor

Use either of the following commands to delete all empty lines:


If you want to delete all lines that are empty or that contain only white space characters (spaces, tabs), use either of:


In the second command, v operates on lines that do not match, and \S matches anything that is not a white space, and d deletes the flagged lines (all lines that have no characters, or that have only white space characters).

Hexdump with output format

# hexdump -n 10 -s 512 -v -e '"x" 1/1 "%02X" " "' untitled.txt

-n for number of bytes.
-s for offset
-v for not showing a * mark for repetition
-e for output format

Qemu: redirect serial port data to stdio or a file, read it in text editor

redirect serial port data to stdio
# qemu-system-i386 -m 64 -serial stdio -hda ./harddisk.img

redirect serial port data to a file
# qemu-system-i386 -m 64 -serial file:a.txt -hda ./harddisk.img

redirect serial port data to a file (another way)
# qemu-system-i386 -m 64 -serial stdio -hda ./harddisk.img | tee a.txt

Now, a.txt can't be opened in a text editor.
Because it contains 0x00 (NULL character) in place of '\n' (new line character).

So use the following command:
# cat a.txt | xargs -0 echo > b.txt

Now b.txt is an ascii file, and can be read by text editors.

Some essential linux commands

to unrar into current folder
# unrar x a.rar

to unrar into a different folder
# mkdir a; unrar x a.rar ./a/

to zip a folder
#zip -r abc abc/

Remote debugging with gdb: debugging a kernel with qemu

1. in one terminal, run
#qemu -no-kqemu -m 64 -fda ./floppy.img -s -S
[floppy.img contains kernel.bin which is loaded by GRUB]

2. in other terminal, run
#gdb ./kernel.bin
[kernel.bin is an ELF executable]

(gdb) target remote localhost:1234
Remote debugging using localhost:1234
0x0000fff0 in ?? ()

(gdb) break _start
Note: breakpoint -1 also set at pc 0x1208a0.
Breakpoint 1 at 0x1208a0

(gdb) c
Stopped due to shared library event


3. Enjoy from here!

Check what is being installed: installwatch command on make install

# installwatch -o /tmp/pkginstall.list make install

Similarities and differences between spinlock, mutex and semaphore.

1. better to use when contention is heavy
2. when one task works in critical section, other tasks don't go to sleep
3. critical section can be used by at most one task
4. useful in multi-processor system but not in single processor system
5. useful when critical section is very small
6. if not acquired, spinlock doesn't make the task sleep, so it doesn't force a context switch

mutex ( ~ binary semaphore)
1. better to use when contention is not heavy
2. when one task works in critical section, other tasks go to sleep
3. critical section can be used by at most one task
4. useful in both multi-processor system and in single processor system
5. useful when critical section is not small
6. if not acquired, mutex makes the task sleep, so it forces a context switch

semaphore ( = counting semaphore)
1. better to use when contention is not heavy
2. when one task works in critical section, other tasks go to sleep
3. critical section can be used by at most N tasks
4. useful in both multi-processor system and in single processor system
5. useful when critical section is not small
6. if not acquired, semaphore makes the task sleep, so it forces a context switch

How to compact virtualbox vdi file.

# /opt/VirtualBox/VBoxManage modifyhd my_vbox.vdi --compact

Linux commands to assemble and link a.asm file written in nasm syntax.

# nasm -f elf32 -g a.asm
# gcc -g a.o -o a.out

How to set up and compile a SDL application.

Steps (Linux commands):

# tar vfxz SDL-1.2.15.tar.gz
# cd SDL-1.2.15
# ./configure
# make
# make install
# cd -
# /usr/local/bin/sdl-config --cflags --libs
# g++ a.cpp `/usr/local/bin/sdl-config --cflags --libs`
# ./a.out

Good link on c++ vtable vptr structure

Steps to program 8051 (P89V51RD2) microcontroller by ISP method using RS232

After long discussions and try, I was able to successfully able to program P89V51RD2 by In Situ Programming (ISP) method using RS232. In a hope that it shall be useful to others, I would like to post my experience with simple steps. For my success, I shall thank all our friends, who have helped me clearing my doubts and have provided their help and suggestions.

Here are the steps to program P89V51RD2 by ISP method using RS232.

My Need:
1. I had to find a microcontroller, which is self programmable, do not need a separate programmer.
2. Circuit should be simple, even a novice can make it and learn.
3. Other tools should be simple and freely available.

So, I choose:
1. P89V51RD2 microcontroller from Philips. Specifically P89V51RD2FN.
2. Circuit diagram from
3. SDCC for compiling C programs and FlashMagic for programming the microcontroller.

I made the circuit on a veroboard.
Connected the circuit to PC using serial cable(RS232).

vbox (winxp guest, linux host) setting:
check "enable serial port"
port number: com1
port mode: host device
port path: /dev/ttyS0

Test code:
void DeadDelay(unsigned int delay)
unsigned int indexI,indexJ;

for(indexI=0; indexI<delay; indexI++)
for(indexJ=0; indexJ<1275; indexJ++);
void main(void)
while (1)
/* Blink LED on Port 0 */


SDCC commands:
C:\>sdcc -I"c:\Program Files\SDCC\include\mcs51" test.c

C:\>packihx test.ihx > test.hex
packihx: read 25 lines, wrote 17: OK.

Flash Magic setting:
com port: com1
baud rate: 9600
device: 89v51rd2
interface: none(isp)
hex file: C:test.hex
check "Verify..."

DO NOT check the box near to "Erase all Flash".
DO NOT check the box near to "Erase Flash used by Hexfile".
It may damage the bootloader!
I didn't check these two boxes and programmed my P89V51RD2 twice, hence confirming that my bootloader was in safe condition.

Now click on "Start".
When it asks to reset the device, press reset button on circuit board and get p89v51rd2 programmed!


Securing puppy linux: Set password for root

After you boot up do the following:

1. open console type 'passwd root'. enter your new password twice.

2. run 'lock' on desktop and enter password from step 1

You may want to select 'blank' from the config to save on processor usage

3. edit /etc/inittab to look like this:

tty1::respawn:/sbin/getty 38400 tty1
tty2::respawn:/sbin/getty 38400 tty2

This keeps someone from killing lock with ctrl+alt+backspace and
logging back in automatically and also gives the option on bootup to
enter 'root' and 'password'.

4. run the firewall wizard at Menu->Setup->Linux-Firewall Wizard.
automagic works fine if you don't have to set up any local services.

5. shutdown and select 'heavy encryption' for save file.


Popular science books list

Mathematical recreations,-1,0,B/exact&FF=dmathematical+recreations&1,-1,/indexsort=r

Popular Science Collection

Symbol visibility in ELF shared library

Details on symbol visibility

Why symbol visibility is good

Position Independent Executable

Very good presentation on this from OpenBSD

Stripping shared libraries

Very good link on this

Good links on ELF (Executable and Linkable Format)

Ian Lance Taylor on Linkers

ELF format details

Very good details on ELF linker loader

Load and run DJGPP COFF, Win32 PE COFF, or ELF .o file

Steps to load ELF

ELF loader code

Code to dynamically link ELF files

Good code for dynamically linking/loading ELF shared library

The ELF Object File Format by Dissection

Very detailed description on ELF linking

How to write dynamically loadable libraries

Process address space, compiler stages etc. 

Linux linker code flow on last slide

Relocation types for i386

On Shared library

Some links collecting other links:

When ldd doesn't tell the details...

Sometimes, ldd does not show all the libraries that a program might use. The system call dlopen() can load a dynamic library on demand, and these libraries do not show up in the ldd output.

Then we can use strace command.
More here:

Some good links on Implementing Regular Expression Engine

Implementing Regular Expressions by Russ Cox

A Regular Expression Matcher, Code by Rob Pike, Exegesis by Brian Kernighan

Knuth-Morris-Pratt string matching algorithm