Archive

Archive for March, 2011

Install Firefox 4 On Ubuntu 10.04/10.10

March 24th, 2011 No comments

Firefox 4 is out and is much better, faster, sleeker and safer ! Here’s how you can install or upgrade your Firefox in Ubuntu to Firefox 4. Type the following commands into your Terminal.

Step 1 : Add the official Mozilla Firefox Ubuntu PPA by
sudo add-apt-repository ppa:mozillateam/firefox-stable
Step 2 : Update the repository.
sudo apt-get update
Step 3 : Then, install Firefox 4 package.
sudo apt-get install firefox ubufox

Note : ubufox adds Ubuntu-specific modifications to Firefox.
That’s it !

The mozillateam is the group who maintains Firefox packages for Ubuntu. The stable release is in firefox-stable. Note, that this will upgrade your current Firefox version to 4.0. The packages in this PPA will not be parallel installable with the packages in the official Ubuntu archive. Installing Firefox from this PPA will provide Firefox 4 in English. For other languages you’ll have to install an extension.

Enjoy :)

Check if a given number is power of 2

March 8th, 2011 No comments

Given a number N, we will have to check if N is a power of 2. Powers of 2 include 1,2,4,8,16,32… and so on. Following are FIVE different approaches to solve the problem.

I. This is the shortest method. We perform bitwise AND operation on the given number N and N-1. If the result is 0, then N is a power of 2. If the result is non-zero, it’s not a power of two.
Bitwise AND Operator


int power_of_two(int n)
{
return !(n&(n-1));
}

II. This method uses the basic definition of a logarithmic function. For N to be a power of 2,(log N to the base 2) must be a whole number and not a fraction.


int power_of_two(int n)
{
float x;
x=log(n)/log(2);
if(x-(int)x==0.0) return 1;
else return 0;
}

III. This method makes use of the fact that any number that is a power of 2, has exactly ONE 1s in its binary representation. Hence the above code just counts the no. of ones in the binary representation of the given number.


int power_of_two(int n)
{
int count=0,rem;
while(n>0)
{
rem=n%2;
if(rem==1||n==1) count++;
if(count>=2) break;
n=n/2;
}
if(count==1) return 1;
else return 0;
}

IV. This is a brute-force method which generates powers of two and checks if it is equal to the given number N.


int power_of_two(int n)
{
int count=0,temp;
do
{
temp=(int)pow(2,count);
if(temp==n) return 1;
count++;
}while( temp < n );
return 0;
}

V. This is a recursive implementation. When a number N which is a power of two is divided by 2, the quotient obtained will also be a power of two.


int power_of_two(int n)
{
if(n==1) return 1;
if(n%2!=0) return 0;
if(!(power_of_two(n/2))) return 0;
}

Run MASM 8086 Assembler in Ubuntu or Windows 7(x64) using DOSBox

March 4th, 2011 77 comments

Here’s how to run 16 bit DOS executables like the MASM assembler or Turbo C compiler in Ubuntu (GNU/Linux) or in 64 bit editions of Windows 7 using DOSBox, a DOS environment emulator. DOSBox is available for Linux as well as Windows.

DOSBox installation

For Ubuntu users (using repository)
Open the terminal and type in the following commands to download and install DOSbox in Ubuntu
sudo apt-get update
sudo apt-get install dosbox

You will find it installed under Applications_Menu->Games->DOSBox Emulator

For other GNU/Linux users
Download DOSbox from here.
Open terminal and cd to the directory containing the downloaded tar.gz file. Type in the following commands to build and install :
tar -xzvf dosbox-0.74.tar.gz
cd dosbox-0.74
./configure
make

Check the src subdir for the binary.

For Windows users
Download DOSbox from here.
Run the downloaded .exe file and install it like any other software.

Now that you’ve installed DOSBox, you’ll be able to run any 16bit or 32bit DOS executable inside it.

Download the 8086 MASM Assembler from here. The zip file contains the following files :
masm.exe, tasm.exe, link.exe, bin2hex.exe, exe2bin.exe, td.exe, edit.com and debug.exe

Windows users extract the .zip file into C:\ so that the path C:\8086 contains all the above mentioned files. GNU/Linux users can extract it and place it in say /home/prashanth/8086

Launch DOSBox and type the following commands :
For Linux users :
mount c /home/prashanth/8086
c:

For Windows :
mount c c:\8086
c:

DOSBox in Ubuntu

DOSBox running in Ubuntu

Now the contents of the folder /home/prashanth/8086 or c:\8086 is mounted as c: drive inside the DOS emulator. You can assemble programs inside DOSBox as you do in your Microprocessor Lab under Windows XP; i.e your usual sequence of commands -
edit file.asm
masm file.asm
link file
debug file.exe

When you are done, type exit to quit DOSBox.

P.S : For GNU/Linux users, there’s an alternative assembler known as the NASM. NASM is considered to be one of the most popular assemblers for GNU/Linux.

Downloads :
DOSBox for Linux : dosbox-0.74.tar.gz
DOSBox for Windows : DOSBox0.74-win32-installer.exe
8086 Assembler : 8086_Assembler.zip