<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>[ Curiosity,Expermentation ]</title>
	<atom:link href="http://appusajeev.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://appusajeev.wordpress.com</link>
	<description>Some random stuff from the world of Programming</description>
	<lastBuildDate>Mon, 23 Jan 2012 05:59:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='appusajeev.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>[ Curiosity,Expermentation ]</title>
		<link>http://appusajeev.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://appusajeev.wordpress.com/osd.xml" title="[ Curiosity,Expermentation ]" />
	<atom:link rel='hub' href='http://appusajeev.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Writing a Linux character Device Driver</title>
		<link>http://appusajeev.wordpress.com/2011/06/18/writing-a-linux-character-device-driver/</link>
		<comments>http://appusajeev.wordpress.com/2011/06/18/writing-a-linux-character-device-driver/#comments</comments>
		<pubDate>Sat, 18 Jun 2011 06:05:53 +0000</pubDate>
		<dc:creator>appusajeev</dc:creator>
				<category><![CDATA[Kernel]]></category>
		<category><![CDATA[device driver]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[module]]></category>

		<guid isPermaLink="false">http://appusajeev.wordpress.com/?p=328</guid>
		<description><![CDATA[In this post, we would be writing a Linux device driver for a hypothetical character device which reverses any string that is given to it. i.e.  If we write any string to the device file represented by the device and then read that file, we get the string written earlier but reversed (for eg.,  myDev [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=appusajeev.wordpress.com&amp;blog=8252415&amp;post=328&amp;subd=appusajeev&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In this post, we would be writing a Linux device driver for a hypothetical character device which reverses any string that is given to it. i.e.  If we write any string to the device file represented by the device and then read that file, we get the string written earlier but reversed (for eg.,  <span style="color:#993366;">myDev</span> being our device,<span style="color:#993366;"> echo “hello” &gt;/dev/myDev ; cat /dev/ myDev</span> would print “olleh”).<br />
We will be simulating the functionality of the device in the driver itself (and this is precisely what is done in emulation tools like Daemon Tools, Alcohol etc).</p>
<p><a href="http://vectorkid.110mb.com/driver_code.zip" target="_blank"><strong>Download Code</strong></a></p>
<p>We will be dealing with</p>
<ul>
<li><strong>Introduction and some basics</strong></li>
</ul>
<ul>
<li><strong>Creating a device file</strong></li>
</ul>
<ul>
<li><strong>The driver code<br />
</strong></li>
</ul>
<ul>
<li><strong>Compiling the driver</strong></li>
</ul>
<ul>
<li><strong>Loading and unloading the driver</strong></li>
</ul>
<ul>
<li><strong>Testing the driver</strong></li>
</ul>
<p>The post helps understand how to write a device driver, the significance of a device file and its role in the interaction between a user program and device driver.</p>
<h2>Introduction</h2>
<p>The devices in UNIX fall in two categories- <strong>Character devices</strong> and <strong>Block devices</strong>. Character devices can be compared to normal files in that we can read/write arbitrary bytes at a time (although for most part, seeking is not supported).They work with a stream of bytes. Block devices, on the other hand, operate on blocks of data, not arbitrary bytes. Usual block size is 512 bytes or larger powers of two. However, block devices can be accessed the same was as character devices, the driver does the block management. (Networking devices do not belong to these categories, the interface provided by these drivers in entirely different from that of char/block devices)</p>
<p>The beauty of UNIX is that devices are represented as files. Both character devices and block devices are represented by respective files in the /dev directory. This means that you can read and write into the device by manipulating those file using standard system calls like open, read, write, close etc.<br />
For eg, you could directly write or read the hard disk by accessing /dev/sd* file &#8211; a dangerous act unless you know what you are doing (for those interested, try<span style="color:#993366;"><strong> hexdump  –C  /dev/sda   –n  512</strong></span> – what you see then is the boot sector of your hard disk !). As another example, you could directly see the contents of the RAM by reading /dev/mem.</p>
<p>Every device file represented in this manner is associated with the device driver of that device which is actually responsible for interacting with the device on behalf of the user request. So when you access a device file, the request is forwarded to the respective device driver which does the processing and returns the result.</p>
<p>For instance, you might be knowing about the files /dev/zero (an infinite source of zeroes), /dev/null (a data black hole), /dev/random ( a source of random numbers) etc. When you actually read these files, what happens is that a particular function in the device driver registered for the file is invoked which returns the respective data.</p>
<p>In our example, we will be developing a character device represented by the device file /dev/myDev.  The mechanisms for creating this file will be explained later.</p>
<h2>Under the hood</h2>
<p>Now how does Linux know which driver is associated with which file? For that, each device and its device file has associated with it, a unique <strong>Major number</strong> and a <strong>Minor number</strong>. No two devices have the same major number.  When a device file is opened, Linux examines its major number and forwards the call to the driver registered for that device.  Subsequent calls for read/write/close too are processed by the same driver. As far as kernel is concerned, only major number is important. Minor number is used to identify the specific device instance if the driver controls more than one device of a type.</p>
<p>To know the major, minor number of devices, use the <span style="color:#993366;"><strong>ls – l</strong></span> command as shown below.</p>
<div id="attachment_348" class="wp-caption aligncenter" style="width: 464px"><a href="http://appusajeev.files.wordpress.com/2011/06/ls2.png"><img class="size-full wp-image-348" title="ls -l" src="http://appusajeev.files.wordpress.com/2011/06/ls2.png" alt="ls -l" width="454" height="55" /></a><p class="wp-caption-text">ls -l</p></div>
<p>The starting &#8216;c&#8217;  means its a character device, 1 is the major number and 8 is the minor number.</p>
<p>A Linux driver is a Linux module which can be loaded and linked to the kernel at runtime. The driver operates in <strong>kernel space</strong> and becomes part of the kernel once loaded, the kernel being monolithic. It can then access the symbols exported by the kernel.</p>
<p>When the device driver module is loaded, the driver first registers itself as a driver for a particular device specifying a particular Major number.</p>
<p>It uses the call <span style="color:#993366;"><strong>register_chrdev</strong></span> function for registration. The call takes the Major number, Minor number, device name and an address of a structure of the type file_operations(discussed later) as argument. In our example, we will be using a major number of 89 . The choice of major number is arbitrary but it has to be unique on the system.</p>
<p>The syntax of register_chrdev is</p>
<p><span style="color:#993366;"><strong>int register_chrdev(unsigned int major,const char *name,struct file_operations *fops)</strong></span></p>
<p>Driver is unregistered by calling the <span style="color:#993366;"><strong>unregister_chrdev</strong></span> function.</p>
<p>Since device driver is a kernel module, it should implement <span style="color:#993366;"><strong>init_module</strong></span> and <strong><span style="color:#993366;">cleanup_module</span></strong> functions. The register_chrdev call is done in the init_module function  and unregister_chrdev call is done in the cleanup_module function.</p>
<p>The register_chrdev call returns a non-negative number on success. If we specify the Major number as 0, the kernel returns a Major number unique at that instant which can be used to create a device file.<br />
A device file can be created either before the driver is loaded if we know the major and minor number beforehand or it can be created later after letting the driver specify a major number for us.</p>
<p>Apart from those, the driver must also define certain <strong>callback functions</strong> that would be invoked when file operations are done on the device file. Ie. It must define functions that would be invoked by the kernel when a process uses open, read, write, close system calls on the file. Every driver must implement functions for processing these requests.<br />
When register_chrdev call is done, the fourth argument is a structure that contains the addresses of these callback functions, callbacks for open, read, write, close system calls. The structure is of the type <span style="color:#993366;"><strong>file_operations</strong></span> and has 4 main fields that should be set  –<strong> read,write,open and release</strong>. Each field must be assigned an address of a function that would be called when open, read,write , close system calls are called respectively.  For eg</p>
<div id="attachment_332" class="wp-caption aligncenter" style="width: 397px"><a href="http://appusajeev.files.wordpress.com/2011/06/struct.png"><img class="size-full wp-image-332" title="file_operations structure initialisation" src="http://appusajeev.files.wordpress.com/2011/06/struct.png" alt="file_operations structure initialisation" width="387" height="133" /></a><p class="wp-caption-text">file_operations structure initialisation</p></div>
<p>It is important to note that all these callback functions have a predefined prototype although the name can be any.</p>
<h2>Creating a device file</h2>
<p>A device file is a special file. It can’t just be created using cat or gedit or shell redirection for that matter. The shell command mknod is usually used to create device file. The syntax of mknod is</p>
<p style="text-align:center;"><span style="color:#993366;"><strong>mknod path type major minor</strong></span></p>
<p>path:-path where the file to be created. It’s not necessary that the device file needs to be created in the /dev directory. It’s a mere convention. A device file can be created just about anywhere.</p>
<p>type: -‘c’ or ‘b’ . Whether the device being represented is a character device or a block device. In our example, we will be simulating a character device and hence we choose &#8216;c&#8217;.</p>
<p>major, minor:- the major and minor number of the device.</p>
<p>Heres how</p>
<div id="attachment_333" class="wp-caption aligncenter" style="width: 410px"><a href="http://appusajeev.files.wordpress.com/2011/06/mknod.png"><img class="size-full wp-image-333" title="mknod command" src="http://appusajeev.files.wordpress.com/2011/06/mknod.png" alt="mknod command" width="400" height="61" /></a><p class="wp-caption-text">mknod command</p></div>
<p><strong>chmod</strong>, though not necessary is done because, if not done, only processes will root permission can read or write to our device file.</p>
<h2><strong>The driver code</strong></h2>
<p>Given below is the code of the device driver</p>
<p><strong> <a href="http://vectorkid.110mb.com/driver_code.zip" target="_blank">Download Code</a><br />
</strong></p>
<div id="attachment_334" class="wp-caption aligncenter" style="width: 709px"><a href="http://appusajeev.files.wordpress.com/2011/06/code.gif"><img class="size-full wp-image-334" title="Device Driver Code" src="http://appusajeev.files.wordpress.com/2011/06/code.gif" alt="Device Driver Code" width="699" height="1383" /></a><p class="wp-caption-text">Device Driver Code</p></div>
<p>For debugging, I have included some printk messages in the code. To see those messages while the driver is in action, do <span style="color:#993366;"><strong>dmesg|tail</strong></span></p>
<h2><strong>Compiling the driver</strong></h2>
<p>A Linux module cannot just be compiled the way we compile normal C files.  cc filename.c won’t work. Compiling a Linux module is a separate process by its own. We use the help of kernel Makefile for compilation. The makefile we will be using is.</p>
<div id="attachment_335" class="wp-caption aligncenter" style="width: 523px"><a href="http://appusajeev.files.wordpress.com/2011/06/makefile.png"><img class="size-full wp-image-335" title="makefile for module compilation" src="http://appusajeev.files.wordpress.com/2011/06/makefile.png" alt="makefile for module compilation" width="513" height="152" /></a><p class="wp-caption-text">makefile for module compilation</p></div>
<p>Here, we are making use of the <strong>kbuild</strong> mechanism used for compiling the kernel.<br />
The result of compilation is a <strong>ko</strong> file (kernel object) which can then be loaded dynamically when required.</p>
<h2><strong>Loading and Unloading the Driver</strong></h2>
<p>Once the compilation is complete, we can use either <strong>insmod</strong> or <strong>modprobe</strong> command ( <span style="color:#993366;"><strong>insmod myDev.ko</strong></span>  or <span style="color:#993366;"><strong>modprobe myDev.ko</strong></span>, of course assuming the current directory contains the compiled module). The difference between insmod and modprobe is that modprobe automatically reads our module to find any dependencies on other modules and loads them before loading our module (these modules must be present in the standard path though!). insmod lacks this feature.</p>
<p>To test if the driver has been loaded successfully, do <span style="color:#993366;"><strong>cat /proc/modules</strong></span> and <span style="color:#993366;"><strong>cat /proc/devices</strong></span>.  We should see our module name in the first case and device name in the second.</p>
<div id="attachment_336" class="wp-caption aligncenter" style="width: 434px"><a href="http://appusajeev.files.wordpress.com/2011/06/proc-modules.png"><img class="size-full wp-image-336" title="cat /proc/modules" src="http://appusajeev.files.wordpress.com/2011/06/proc-modules.png" alt="cat /proc/modules" width="424" height="148" /></a><p class="wp-caption-text">cat /proc/modules</p></div>
<div id="attachment_337" class="wp-caption aligncenter" style="width: 371px"><a href="http://appusajeev.files.wordpress.com/2011/06/proc-devices.png"><img class="size-full wp-image-337" title="cat /proc/devices" src="http://appusajeev.files.wordpress.com/2011/06/proc-devices.png" alt="cat /proc/devices" width="361" height="317" /></a><p class="wp-caption-text">cat /proc/modules</p></div>
<p>To unload the driver, use <strong>rmmod</strong> command. (<span style="color:#993366;"><strong>rmmod myDev.ko</strong></span>)</p>
<h2>Testing the driver</h2>
<p>To test the driver, we try writing something to the device file and then reading it. For example,</p>
<div id="attachment_343" class="wp-caption aligncenter" style="width: 408px"><a href="http://appusajeev.files.wordpress.com/2011/06/demo1.png"><img class="size-full wp-image-343" title="Testing the driver" src="http://appusajeev.files.wordpress.com/2011/06/demo1.png" alt="Testing the driver" width="398" height="75" /></a><p class="wp-caption-text">Testing the driver</p></div>
<p>See the output. (The reason for the &#8216;ugly&#8217; output is because echo automatically writes a newline character to the end of string. When the driver reverses the string, the newline is shifted to the front of the string and there is no newline at the end. Hence the result being &#8216;ugly&#8217;)</p>
<p>To see how this can be done from our program, I wrote a demo program given below</p>
<div id="attachment_340" class="wp-caption aligncenter" style="width: 408px"><a href="http://appusajeev.files.wordpress.com/2011/06/test-c.png"><img class="size-full wp-image-340" title="Interacting with the driver" src="http://appusajeev.files.wordpress.com/2011/06/test-c.png" alt="Interacting with the driver" width="398" height="378" /></a><p class="wp-caption-text">Interacting with the driver</p></div>
<p>Compile it normally(or run <strong>make test</strong>) and run <span style="color:#993366;">.<strong>/test  some_string</strong></span>  and see the output.</p>
<div id="attachment_345" class="wp-caption aligncenter" style="width: 408px"><a href="http://appusajeev.files.wordpress.com/2011/06/demo-2.png"><img class="size-full wp-image-345" title="Testing the driver" src="http://appusajeev.files.wordpress.com/2011/06/demo-2.png" alt="Testing the driver" width="398" height="71" /></a><p class="wp-caption-text">Testing the driver</p></div>
<p>Note: You need to be root to compile the module, load the module and unload the module.</p>
<p>This driver interface presented here is an old one, there is a newer one but the old one is still supported.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/appusajeev.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/appusajeev.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/appusajeev.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/appusajeev.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/appusajeev.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/appusajeev.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/appusajeev.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/appusajeev.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/appusajeev.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/appusajeev.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/appusajeev.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/appusajeev.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/appusajeev.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/appusajeev.wordpress.com/328/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=appusajeev.wordpress.com&amp;blog=8252415&amp;post=328&amp;subd=appusajeev&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://appusajeev.wordpress.com/2011/06/18/writing-a-linux-character-device-driver/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/98e456523a3648aef486ab07e8917b6d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">appusajeev</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2011/06/ls2.png" medium="image">
			<media:title type="html">ls -l</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2011/06/struct.png" medium="image">
			<media:title type="html">file_operations structure initialisation</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2011/06/mknod.png" medium="image">
			<media:title type="html">mknod command</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2011/06/code.gif" medium="image">
			<media:title type="html">Device Driver Code</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2011/06/makefile.png" medium="image">
			<media:title type="html">makefile for module compilation</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2011/06/proc-modules.png" medium="image">
			<media:title type="html">cat /proc/modules</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2011/06/proc-devices.png" medium="image">
			<media:title type="html">cat /proc/devices</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2011/06/demo1.png" medium="image">
			<media:title type="html">Testing the driver</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2011/06/test-c.png" medium="image">
			<media:title type="html">Interacting with the driver</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2011/06/demo-2.png" medium="image">
			<media:title type="html">Testing the driver</media:title>
		</media:content>
	</item>
		<item>
		<title>Writing a 16-bit Real mode OS [NASM]</title>
		<link>http://appusajeev.wordpress.com/2011/01/27/writing-a-16-bit-real-mode-os-nasm/</link>
		<comments>http://appusajeev.wordpress.com/2011/01/27/writing-a-16-bit-real-mode-os-nasm/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 11:21:52 +0000</pubDate>
		<dc:creator>appusajeev</dc:creator>
				<category><![CDATA[Kernel]]></category>
		<category><![CDATA[NASM]]></category>
		<category><![CDATA[OS]]></category>

		<guid isPermaLink="false">http://appusajeev.wordpress.com/?p=295</guid>
		<description><![CDATA[This article is about writing a minimal 16 bit, real modes DOS like operating system that boots off a pen drive and provides a shell to run pure binary executables(aka COM files in the DOS era)  with a custom file system implemented. This means that the OS could run COM files directly if you have [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=appusajeev.wordpress.com&amp;blog=8252415&amp;post=295&amp;subd=appusajeev&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This article is about writing a <strong>minimal 16 bit, real modes DOS like operating system that boots off a pen drive and provides a shell to run pure binary executables(aka COM files in the DOS era)  with a custom file system implemented</strong>. This means that the OS could run COM files directly if you have one. COM files are pure binary files in the sense that they don’t have a header, contains machine instructions only. For demonstration, I have developed some sample executables which could be run using our OS (apps like clone of unix echo, register dump etc). See the end of the post to see some pictures of the OS in action.</p>
<p><a title="Download Source" href="http://vectorkid.110mb.com/os.zip" target="_blank">Download Source</a></p>
<p>The post explains how to</p>
<ul>
<li><strong>Write a bootloader</strong></li>
</ul>
<ul>
<li><strong>Write a shell, a kernel placeholder<br />
</strong></li>
</ul>
<ul>
<li><strong>Implement a basic filesystem</strong></li>
</ul>
<ul>
<li><strong>Write the OS to disk</strong></li>
</ul>
<ul>
<li><strong>Boot from the OS</strong></li>
</ul>
<p>The OS is written in the open source NASM assembler in Linux. To understand the working, you need to have some understanding about x86 booting process, bootloaders and real mode of processor operation.</p>
<h3><strong>Booting Process Basics<br />
</strong></h3>
<p>When the system is powered on, BIOS pops into action and performs what is known as <span style="color:#993366;">Power On Self Test</span>(POST) to verify the working of devices, initializing them etc. Immediately after that, POST loads BIOS executable code, present in the BIOS ROM into memory at address which is usually <span style="color:#993366;"><strong>0xF00000</strong></span>.  POST then places a jump instruction in the first byte of memory (CS:IP = 0). The jump instruction is nothing but a jump to the address 0xF00000 where the BIOS code is loaded.  Now the BIOS code takes control and performs certain operations like setting up the Interrupt Vector Table(IVT), finding a boot device, setting up certain information in RAM(BIOS Data Area), loading the bootloader  etc.</p>
<p>BIOS provides certain basic interrupts for the programmer for basic functions like loading and storing disk sectors, reading keyboard, printing to screen etc. These interrupts are similar to DOS interrupts but are not DOS interrupts.</p>
<p>Back to topic,  once these processes are over, the BIOS iterates through the list of boot devices and according the boot order preferences, the bios searches for a bootloader in each boot device. If a bootloader is found, its loaded into memory and is given control.</p>
<p>A point to note is that whenever system is powered on, whatever be the processor, Core 2 Duo or Core i7 or whatever operates in 16 bit real mode by default until it is explicitly asked to switch to 32  bit protected mode (by setting PE bit in CR0 register and doing a far jump to fix CS to point to a segment descriptor after setting up GDT).</p>
<h3><strong>Bootloader Basics<br />
</strong></h3>
<p>Bootloader is basically a 512 byte piece of machine code that is present in the first sector a boot device.  Bootloader is the first user defined program that’s loaded into memory and given control of. It is the duty of the bootloader to load the OS into memory and pass control to it.</p>
<p>A bootloader <span style="color:#993366;"><strong>must</strong></span> be exactly 512 bytes in size. BIOS identifies  a valid bootloader by means of a signature. The 511th byte of the bootloader should contain the value <strong><span style="color:#993366;">0&#215;55</span></strong> and 512<sup>th</sup> byte should have the value <span style="color:#993366;"><strong>0xAA</strong></span>.</p>
<p>A Bootloader will always be loaded at address <span style="color:#993366;"><strong>0x7C00</strong></span> in RAM. Usually, this corresponds to CS:IP pair of 00:0x7C00 but some BIOSes set CS:IP as 0x7C0:0 which is essentially points to the same address  but leads to issues in writing bootloader when we have to specify the offset where our code will be loaded.<br />
This can be easily dealt by defining an offset 0 and then jumping to 0x7C0:start , where start is the label of the next instruction following  the far jump. This jump sets CS = 0x7C0</p>
<p>(Note: <strong><span style="color:#993366;">Physical address = CS x 16 + IP</span></strong>)</p>
<h2><strong>Our Bootloader</strong></h2>
<p>Our bootloader, present in the first sector of the pendrive  (the mechanism for writing the bootloader and the OS into the pendrive will be discussed later) will be loaded into memory and execution will immediately start.</p>
<p>Our bootloader serves 3 purposes, it displays a welcome message and then loads the OS and file table from the disk and jumps to OS entry point. The os is loaded at address <strong><span style="color:#993366;">0&#215;1000</span></strong> and file table at address<span style="color:#993366;"><strong> 0&#215;2000</strong></span></p>
<p>Heres our bootloader source</p>
<p><a title="Download full source" href="http://vectorkid.110mb.com/os.zip" target="_blank">Download full Source</a></p>
<div id="attachment_309" class="wp-caption aligncenter" style="width: 527px"><a href="http://appusajeev.files.wordpress.com/2011/01/bootloader.png"><img class="size-full wp-image-309" title="Our Bootloader" src="http://appusajeev.files.wordpress.com/2011/01/bootloader.png" alt="Our Bootloader" width="517" height="1007" /></a><p class="wp-caption-text">Our Bootloader</p></div>
<p>Bios provides interrupts for displaying characters as well as strings on screen. Here I have used character display interrupt to write a function to print a zero-terminated string like in C.</p>
<p>The interrupt for displaying character on screen is</p>
<p><span style="color:#993366;"><strong>INT 0&#215;10, BX = character color, AL = character to display, AH = 0x0E</strong></span></p>
<p>Our bootloader, OS and executables are stored on  disk . For execution, they need to be loaded to RAM. The bootloader will be loaded by BIOS and the rest we have to load when required through the sector loading interrupt.A block of 512 bytes (in this case, need not always be) is called a sector.<br />
For loading sectors off the disk to RAM, bios provides an interrupt, <strong><span style="color:#993366;">INT 0&#215;13</span></strong> and the parameters to set are</p>
<p><span style="color:#993366;"><strong>AH = 2</strong></span></p>
<p><span style="color:#993366;"><strong>DL = drive,  DL = 0&#215;80 for hard disk and this applies to our pen drive</strong></span></p>
<p><span style="color:#993366;"><strong>DH = head number</strong></span></p>
<p><span style="color:#993366;"><strong>CH = track number</strong></span></p>
<p><span style="color:#993366;"><strong>CL= sector number of the sector to be loaded</strong></span></p>
<p><span style="color:#993366;"><strong>AL = number of sectors to load</strong></span></p>
<p><span style="color:#993366;"><strong>ES = segment to load the sector</strong></span></p>
<p><span style="color:#993366;"><strong>BX = offset from ES where the sector is to be loaded</strong></span></p>
<p>Our  bootloader occupies the first sector, file table in the third sector and OS in the third sector.</p>
<p>The bootloader loads the os into address 0&#215;1000:0 and filetable into address 0&#215;2000</p>
<h2><strong>Implementing Filesystem</strong></h2>
<p>The shell provided by our os enables the user  to type an executable name and run it. For that , the os must know where exactly each executable is located on disk and this is where the concept of file system surfaces. A file system, in simple words is basically a specification that tells how to locate of a file on disk given its name.</p>
<p>For our purpose, I made a simple filesystem. Each file is mapped to a sector in disk to form a string with following structure</p>
<p><strong><span style="color:#993366;">{file1-sector,file2-sector,…,filen-sector,}</span></strong></p>
<p>This filetable is loaded to <strong><span style="color:#993366;">0&#215;2000:0 </span></strong>by the bootloader this address space is scanned to find the sector where the requested file is stored on disk and that sector is brought to RAM.</p>
<h2><strong>Our OS/Shell</strong></h2>
<p>So, the bootloader has loaded our OS at 0&#215;1000:0 and filetable at 0&#215;2000:0 and it makes a far jump to 0&#215;1000:0, the OS entry point.  The working of the shell is simple, using BIOS interrupt to reach character, we read the executable name from the user. The filetable loaded at 0&#215;2000:0 is scanned for a match, the associated sector number is read and that sector is loaded into memory at <span style="color:#993366;"><strong>0&#215;9000:0</strong></span> and the shell makes a far jump to this address. If the name entered by the user cannot be matched in the file table, an error message is shown.  After the executable completes execution, for it to return back to the OS, it must make a far jump to 0&#215;1000:0, the OS entry point. This step is functionally similar to executing (AH= 4ch, INT 0&#215;21 in DOS)</p>
<p>The interrupt for reading a character from a keyboard is <strong><span style="color:#993366;">INT 0&#215;16 </span></strong><span style="color:#003300;">with</span><strong><span style="color:#993366;"> AH=0</span></strong>, the read character will be available in AL.</p>
<p>Heres our OS source</p>
<p><a title="Download full Source" href="http://vectorkid.110mb.com/os.zip" target="_blank">Download full Source</a></p>
<div id="attachment_310" class="wp-caption aligncenter" style="width: 544px"><a href="http://appusajeev.files.wordpress.com/2011/01/final.png"><img class="size-full wp-image-310" title="Our Shell" src="http://appusajeev.files.wordpress.com/2011/01/final.png" alt="Our Shell" width="534" height="3186" /></a><p class="wp-caption-text">Our Shell</p></div>
<h2><strong>Writing the OS to disk</strong></h2>
<p>Okay so everything is done, the final thing to do to boot from the pen drive, is to write our os into it. It can’t just be done by copying the files to the pen drive. It doesn’t work in our case for two reasons:</p>
<p><strong>1</strong>.   <span style="color:#993366;"><strong>We cannot write the bootloader to the first sector using this method. When we ask the OS to copy a file, it copies the file to some free sectors and add this information to the file table.</strong></span></p>
<p><span style="color:#993366;"><strong><span style="color:#003300;">2</span>. If were to copy the files to the pendrive, our filesystem has to be known to the host OS, like FAT  which means we have to write code to parse that file system during boot time which, well is an overkill for this os</strong></span></p>
<p>So, to write the bootloader and custom file system, we need to have low level disk access, for which the obvious choice is Linux.  Linux treats devices like <span style="color:#993366;">files</span> which can be read and written to. This is a really<span style="color:#993366;"> powerful and useful</span> concept. Commands like ‘dd’ use this concept. The file representing our harddisk would be something like sda, something  like sdc for pen drive, it varies. To know the file allocated, after the pen drive is attached, run <span style="color:#993366;">dmesg|tail</span> in the terminal.</p>
<p>Now whatever we write to the device file will be <span style="color:#993366;"><strong>written as such</strong></span> into the device which is exactly what we want. To write the bootloader, write the compiled boodloader into the first 512 bytes of the file and this would be written to first 512 bytes of the disk and this can be done using C file operations. Pretty easy, isn’t it. Now to write the file table and OS  to the 2<sup>nd</sup> and 3<sup>rd</sup> sectors of the disk, just write these files to the next two 512 bytes of the device file . The same procedure applies to writing the executables to disk</p>
<p>The following C code does the job of writing everything to disk. It takes the path of device file, bootloader, os and list of executable as command line arguments.</p>
<div id="attachment_312" class="wp-caption aligncenter" style="width: 526px"><a href="http://appusajeev.files.wordpress.com/2011/01/cpy.png"><img class="size-full wp-image-312" title="Copy Program" src="http://appusajeev.files.wordpress.com/2011/01/cpy.png" alt="Copy Program" width="516" height="860" /></a><p class="wp-caption-text">Copy Program</p></div>
<h2><strong>Booting the OS</strong></h2>
<p>Plug in the  pen drive, find its corresponding device file (use dmesg|tail), open the makefile, substitute its path in <span style="color:#993366;">dev</span> variable. Then run ‘make’ , reboot the system, choose to boot from mass storage and you could see the sweet sight of our OS booting . At the prompt,  type ‘<span style="color:#993366;">help</span>’ and hit enter, you could see list of available executables, like <span style="color:#993366;">reg </span>to dump registers,<span style="color:#993366;"> echo</span> that echoes back a string read from the user</p>
<p>Here are some pics of the OS in action(Click to enlarge).</p>
<div id="attachment_319" class="wp-caption aligncenter" style="width: 310px"><a href="http://appusajeev.files.wordpress.com/2011/01/1.jpg"><img class="size-medium wp-image-319" title="OS in action" src="http://appusajeev.files.wordpress.com/2011/01/1.jpg?w=300&#038;h=225" alt="OS in action" width="300" height="225" /></a><p class="wp-caption-text">OS in action</p></div>
<div id="attachment_320" class="wp-caption aligncenter" style="width: 310px"><a href="http://appusajeev.files.wordpress.com/2011/01/2.jpg"><img class="size-medium wp-image-320" title="Our OS in action" src="http://appusajeev.files.wordpress.com/2011/01/2.jpg?w=300&#038;h=225" alt="Our OS in action" width="300" height="225" /></a><p class="wp-caption-text">Our OS in action</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/appusajeev.wordpress.com/295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/appusajeev.wordpress.com/295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/appusajeev.wordpress.com/295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/appusajeev.wordpress.com/295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/appusajeev.wordpress.com/295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/appusajeev.wordpress.com/295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/appusajeev.wordpress.com/295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/appusajeev.wordpress.com/295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/appusajeev.wordpress.com/295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/appusajeev.wordpress.com/295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/appusajeev.wordpress.com/295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/appusajeev.wordpress.com/295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/appusajeev.wordpress.com/295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/appusajeev.wordpress.com/295/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=appusajeev.wordpress.com&amp;blog=8252415&amp;post=295&amp;subd=appusajeev&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://appusajeev.wordpress.com/2011/01/27/writing-a-16-bit-real-mode-os-nasm/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/98e456523a3648aef486ab07e8917b6d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">appusajeev</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2011/01/bootloader.png" medium="image">
			<media:title type="html">Our Bootloader</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2011/01/final.png" medium="image">
			<media:title type="html">Our Shell</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2011/01/cpy.png" medium="image">
			<media:title type="html">Copy Program</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2011/01/1.jpg?w=300" medium="image">
			<media:title type="html">OS in action</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2011/01/2.jpg?w=300" medium="image">
			<media:title type="html">Our OS in action</media:title>
		</media:content>
	</item>
		<item>
		<title>Implementing a System call in Linux Kernel 2.6.35</title>
		<link>http://appusajeev.wordpress.com/2010/11/13/implementing-a-system-call-in-linux-kernel-2-6-35/</link>
		<comments>http://appusajeev.wordpress.com/2010/11/13/implementing-a-system-call-in-linux-kernel-2-6-35/#comments</comments>
		<pubDate>Fri, 12 Nov 2010 18:33:49 +0000</pubDate>
		<dc:creator>appusajeev</dc:creator>
				<category><![CDATA[Kernel]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://appusajeev.wordpress.com/?p=268</guid>
		<description><![CDATA[As we know, System calls are a set of services/functions provided to the programmer by the OS. These functions can be invoked in any language that provide some interface to the System call mechanism of the OS. Some common linux system calls are open,read,write etc. While executing a system call, the calling process moves from [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=appusajeev.wordpress.com&amp;blog=8252415&amp;post=268&amp;subd=appusajeev&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>As we know, System calls are a set of services/functions provided to the programmer by the OS. These functions can be invoked in any language that provide some interface to the System call mechanism of the OS. Some common linux system calls are open,read,write etc.<br />
While executing a system call, the calling process moves from user space to kernel space and back to user space when its completes executing the call.</p>
<p>There are around 338 system calls in linux kernel  2.6.35.7 by default. Presented here a howto on adding a new one into the kernel, a 339<sup>th</sup> one so that it will be available globally for any program. As example, we will implement the <strong>strcpy</strong> function as a system call so that it can be used without including string.h.</p>
<p>Obviously, you need the kernel source tree since some kernel modification is involved. Get it from <a title="Linux Kernel Source" href="http://www.kernel.org/" target="_blank">kernel.org </a> (any kernel version higher than 2.6.35.7 would work fine )and untar it to get linux-2.6.35.7. The paths used below all will be relative to this path.<br />
We need to edit 4 files and 2 files need to be created.</p>
<h2>The Code</h2>
<p>First, lets start off writing the code for strcpy. We need to include the file <strong>linux/linkage.h</strong> because it contains the macro <strong>asmlinkage</strong> which means that the system call expects the arguments on the stack and not in registers. Printk is the kernel alternative of printf, but with certain peculiar properties.</p>
<div id="attachment_269" class="wp-caption aligncenter" style="width: 437px"><a href="http://appusajeev.files.wordpress.com/2010/11/strcpy.png"><img class="size-full wp-image-269" title="Code for the system call " src="http://appusajeev.files.wordpress.com/2010/11/strcpy.png" alt="Code for the system call " width="427" height="266" /></a><p class="wp-caption-text">Code for the system call </p></div>
<p><!--[if gte mso 9]&gt;  Normal 0     false false false  EN-IN X-NONE X-NONE                           &lt;![endif]--><!--[if gte mso 9]&gt;                                                                                                                                            &lt;![endif]--><!--[if gte mso 10]&gt; &lt;!   /* Style Definitions */  table.MsoNormalTable 	{mso-style-name:&quot;Table Normal&quot;; 	mso-tstyle-rowband-size:0; 	mso-tstyle-colband-size:0; 	mso-style-noshow:yes; 	mso-style-priority:99; 	mso-style-qformat:yes; 	mso-style-parent:&quot;&quot;; 	mso-padding-alt:0mm 5.4pt 0mm 5.4pt; 	mso-para-margin-top:0mm; 	mso-para-margin-right:0mm; 	mso-para-margin-bottom:10.0pt; 	mso-para-margin-left:0mm; 	line-height:115%; 	mso-pagination:widow-orphan; 	font-size:11.0pt; 	font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;; 	mso-ascii-font-family:Calibri; 	mso-ascii-theme-font:minor-latin; 	mso-fareast-font-family:&quot;Times New Roman&quot;; 	mso-fareast-theme-font:minor-fareast; 	mso-hansi-font-family:Calibri; 	mso-hansi-theme-font:minor-latin; 	mso-bidi-font-family:&quot;Times New Roman&quot;; 	mso-bidi-theme-font:minor-bidi;} --><!--[endif]--><!--[if gte mso 9]&gt;  Normal 0     false false false  EN-IN X-NONE X-NONE                           &lt;![endif]--><!--[if gte mso 9]&gt;                                                                                                                                            &lt;![endif]--><!--[if gte mso 10]&gt; &lt;!   /* Style Definitions */  table.MsoNormalTable 	{mso-style-name:&quot;Table Normal&quot;; 	mso-tstyle-rowband-size:0; 	mso-tstyle-colband-size:0; 	mso-style-noshow:yes; 	mso-style-priority:99; 	mso-style-qformat:yes; 	mso-style-parent:&quot;&quot;; 	mso-padding-alt:0mm 5.4pt 0mm 5.4pt; 	mso-para-margin-top:0mm; 	mso-para-margin-right:0mm; 	mso-para-margin-bottom:10.0pt; 	mso-para-margin-left:0mm; 	line-height:115%; 	mso-pagination:widow-orphan; 	font-size:11.0pt; 	font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;; 	mso-ascii-font-family:Calibri; 	mso-ascii-theme-font:minor-latin; 	mso-fareast-font-family:&quot;Times New Roman&quot;; 	mso-fareast-theme-font:minor-fareast; 	mso-hansi-font-family:Calibri; 	mso-hansi-theme-font:minor-latin; 	mso-bidi-font-family:&quot;Times New Roman&quot;; 	mso-bidi-theme-font:minor-bidi;} --> <!--[endif]-->&#8220;<strong>&lt;1&gt;</strong>&#8221; tells printk that we are giving that message the highest priority.</p>
<p>Create a folder named ‘<strong>test</strong>’ in the root of  linux source directory and save this file as <strong>strcpy.c</strong> in that directory. Create a <strong>Makefile</strong> in that directory containing only the line.</p>
<p><!--[if gte mso 9]&gt;  Normal 0     false false false  EN-IN X-NONE X-NONE                           &lt;![endif]--><!--[if gte mso 9]&gt;                                                                                                                                            &lt;![endif]--><!--[if gte mso 10]&gt; &lt;!   /* Style Definitions */  table.MsoNormalTable 	{mso-style-name:&quot;Table Normal&quot;; 	mso-tstyle-rowband-size:0; 	mso-tstyle-colband-size:0; 	mso-style-noshow:yes; 	mso-style-priority:99; 	mso-style-qformat:yes; 	mso-style-parent:&quot;&quot;; 	mso-padding-alt:0mm 5.4pt 0mm 5.4pt; 	mso-para-margin-top:0mm; 	mso-para-margin-right:0mm; 	mso-para-margin-bottom:10.0pt; 	mso-para-margin-left:0mm; 	line-height:115%; 	mso-pagination:widow-orphan; 	font-size:11.0pt; 	font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;; 	mso-ascii-font-family:Calibri; 	mso-ascii-theme-font:minor-latin; 	mso-fareast-font-family:&quot;Times New Roman&quot;; 	mso-fareast-theme-font:minor-fareast; 	mso-hansi-font-family:Calibri; 	mso-hansi-theme-font:minor-latin; 	mso-bidi-font-family:&quot;Times New Roman&quot;; 	mso-bidi-theme-font:minor-bidi;} --><!--[endif]--><span style="color:#993366;"><strong>obj-y:=strcpy.o</strong></span></p>
<p>Thus, now the strcpy.c file and Makefile are present in<strong> </strong></p>
<p><strong>linux-2.6.35.7/test/strcpy.c </strong><br />
<strong> linux-2.6.35.7/test/Makefile</strong><span style="color:#993366;"><strong> </strong></span></p>
<h2><strong>The Edits</strong></h2>
<p>The following files need to be edited.<strong><br />
</strong></p>
<h3><!--[if gte mso 9]&gt;  Normal 0     false false false  EN-IN X-NONE X-NONE                           &lt;![endif]--><!--[if gte mso 9]&gt;                                                                                                                                            &lt;![endif]--><!--[if gte mso 10]&gt; &lt;!   /* Style Definitions */  table.MsoNormalTable 	{mso-style-name:&quot;Table Normal&quot;; 	mso-tstyle-rowband-size:0; 	mso-tstyle-colband-size:0; 	mso-style-noshow:yes; 	mso-style-priority:99; 	mso-style-qformat:yes; 	mso-style-parent:&quot;&quot;; 	mso-padding-alt:0mm 5.4pt 0mm 5.4pt; 	mso-para-margin-top:0mm; 	mso-para-margin-right:0mm; 	mso-para-margin-bottom:10.0pt; 	mso-para-margin-left:0mm; 	line-height:115%; 	mso-pagination:widow-orphan; 	font-size:11.0pt; 	font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;; 	mso-ascii-font-family:Calibri; 	mso-ascii-theme-font:minor-latin; 	mso-fareast-font-family:&quot;Times New Roman&quot;; 	mso-fareast-theme-font:minor-fareast; 	mso-hansi-font-family:Calibri; 	mso-hansi-theme-font:minor-latin; 	mso-bidi-font-family:&quot;Times New Roman&quot;; 	mso-bidi-theme-font:minor-bidi;} --> <!--[endif]--> 1. linux-2.6.35.7/arch/x86/kernel/syscall_table_32.S</h3>
<p>Append to the file the following line<span style="font-size:11pt;line-height:115%;font-family:&quot;"> </span></p>
<p><span style="color:#993366;"><strong>.long sys_strcpy </strong></span><span style="font-size:11pt;line-height:115%;font-family:&quot;"><br />
</span></p>
<p><span style="font-size:11pt;line-height:115%;font-family:&quot;"> </span></p>
<div id="attachment_275" class="wp-caption aligncenter" style="width: 390px"><a href="http://appusajeev.files.wordpress.com/2010/11/table.png"><img class="size-full wp-image-275" title="linux-2.6.35/arch/x86/kernel/syscall_table_32.S" src="http://appusajeev.files.wordpress.com/2010/11/table.png" alt="linux-2.6.35/arch/x86/kernel/syscall_table_32.S" width="380" height="198" /></a><p class="wp-caption-text">linux-2.6.35/arch/x86/kernel/syscall_table_32.S</p></div>
<h3>2. linux-2.6.35.7/arch/x86/include/asm/unistd_32.h</h3>
<p><!--[if gte mso 9]&gt;  Normal 0     false false false  EN-IN X-NONE X-NONE                           &lt;![endif]--><!--[if gte mso 9]&gt;                                                                                                                                            &lt;![endif]--><!--[if gte mso 10]&gt; &lt;!   /* Style Definitions */  table.MsoNormalTable 	{mso-style-name:&quot;Table Normal&quot;; 	mso-tstyle-rowband-size:0; 	mso-tstyle-colband-size:0; 	mso-style-noshow:yes; 	mso-style-priority:99; 	mso-style-qformat:yes; 	mso-style-parent:&quot;&quot;; 	mso-padding-alt:0mm 5.4pt 0mm 5.4pt; 	mso-para-margin-top:0mm; 	mso-para-margin-right:0mm; 	mso-para-margin-bottom:10.0pt; 	mso-para-margin-left:0mm; 	line-height:115%; 	mso-pagination:widow-orphan; 	font-size:11.0pt; 	font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;; 	mso-ascii-font-family:Calibri; 	mso-ascii-theme-font:minor-latin; 	mso-fareast-font-family:&quot;Times New Roman&quot;; 	mso-fareast-theme-font:minor-fareast; 	mso-hansi-font-family:Calibri; 	mso-hansi-theme-font:minor-latin; 	mso-bidi-font-family:&quot;Times New Roman&quot;; 	mso-bidi-theme-font:minor-bidi;} --> <!--[endif]--></p>
<p>This file contains the unique number associated with each system call. We can see the names of all the system calls and the number associated with each. After the last system call-number pair (around line 345), add a line</p>
<p><span style="color:#993366;"><strong>#define __NR_strcpy  338 </strong></span></p>
<p><span style="color:#993366;"><strong> </strong></span>(if 337 was the number associated with the last system call).<br />
Then replace NR_syscalls directive denoting the total number of system calls by the previous number incremented by 1 ie the new value is</p>
<p><strong><span style="color:#993366;">#define NR_syscalls 339</span></strong></p>
<p>Note down the number 338, we need it later.</p>
<div id="attachment_276" class="wp-caption aligncenter" style="width: 317px"><a href="http://appusajeev.files.wordpress.com/2010/11/numbers.png"><img class="size-full wp-image-276" title="linux-2.6.35/arch/x86/include/asm/unistd_32.h" src="http://appusajeev.files.wordpress.com/2010/11/numbers.png" alt="linux-2.6.35/arch/x86/include/asm/unistd_32.h" width="307" height="188" /></a><p class="wp-caption-text">linux-2.6.35/arch/x86/include/asm/unistd_32.h</p></div>
<h3>3. linux-2.6.35.7/include/linux/syscalls.h</h3>
<p>This file contains the prototype of all the system calls. Here we append to the file the prototype of our function.<br />
ie. We add  the line</p>
<p><strong><span style="color:#993366;">asmlinkage  long sys_strcpy(char *dest,char *src);</span></strong></p>
<p><strong><span style="color:#993366;"><br />
</span></strong></p>
<p><strong><span style="color:#993366;"> </span></strong></p>
<div id="attachment_277" class="wp-caption aligncenter" style="width: 554px"><strong><a href="http://appusajeev.files.wordpress.com/2010/11/proto.png"><img class="size-full wp-image-277" title="linux-2.6.35/include/linux/syscalls.h" src="http://appusajeev.files.wordpress.com/2010/11/proto.png" alt="linux-2.6.35/include/linux/syscalls.h" width="544" height="110" /></a></strong><p class="wp-caption-text">linux-2.6.35/include/linux/syscalls.h</p></div>
<p><strong> </strong></p>
<h3>4. Makefile</h3>
<p>Open Makefile present in the root of source directory and find the area where <strong>core-y</strong> is defined and add the folder <strong>test</strong> to the end of that line as shown below</p>
<div id="attachment_278" class="wp-caption aligncenter" style="width: 576px"><a href="http://appusajeev.files.wordpress.com/2010/11/makefile.png"><img class="size-full wp-image-278" title="Makefile" src="http://appusajeev.files.wordpress.com/2010/11/makefile.png" alt="Makefile" width="566" height="123" /></a><p class="wp-caption-text">Makefile</p></div>
<p>Next compile the kernel. Assuming you are familiar with kernel compilation, execute<br />
<span style="color:#993366;"><strong>make bzImage  –j4</strong></span><br />
The last argument is optional and is intended to speed up compilation on dual core CPUS<br />
Once compilation is complete, install the kernel by executing the following command with root permission<br />
<span style="color:#993366;"><strong>make install</strong></span><br />
Once the kernel image is installed to /boot, reboot the system.</p>
<h2>Testing</h2>
<p>Now we need to check the newly done system call. Run the code below and feel the satisfaction <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div id="attachment_279" class="wp-caption aligncenter" style="width: 401px"><a href="http://appusajeev.files.wordpress.com/2010/11/sample.png"><img class="size-full wp-image-279" title="System call test" src="http://appusajeev.files.wordpress.com/2010/11/sample.png" alt="System call test" width="391" height="171" /></a><p class="wp-caption-text">System call test</p></div>
<p><strong>The kernel has performed the strcpy for us. Cool ! isn’t it</strong></p>
<p><strong>Note:</strong><br />
Execute the command <span style="color:#993366;"><strong>dmesg</strong></span> now, you can see done printed in the last. Printk by default doesnt  print to the terminal. It writes to the kernel ring buffer which is printed by the dmesg</p>
<p>Try putting an infinite loop inside a system call, the system just drops dead. As it goes, the linux kernel does not preempt itself.</p>
<p><!--[if gte mso 9]&gt;  Normal 0     false false false  EN-IN X-NONE X-NONE                           &lt;![endif]--><!--[if gte mso 9]&gt;                                                                                                                                            &lt;![endif]--><!--[if gte mso 10]&gt; &lt;!   /* Style Definitions */  table.MsoNormalTable 	{mso-style-name:&quot;Table Normal&quot;; 	mso-tstyle-rowband-size:0; 	mso-tstyle-colband-size:0; 	mso-style-noshow:yes; 	mso-style-priority:99; 	mso-style-qformat:yes; 	mso-style-parent:&quot;&quot;; 	mso-padding-alt:0mm 5.4pt 0mm 5.4pt; 	mso-para-margin-top:0mm; 	mso-para-margin-right:0mm; 	mso-para-margin-bottom:10.0pt; 	mso-para-margin-left:0mm; 	line-height:115%; 	mso-pagination:widow-orphan; 	font-size:11.0pt; 	font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;; 	mso-ascii-font-family:Calibri; 	mso-ascii-theme-font:minor-latin; 	mso-fareast-font-family:&quot;Times New Roman&quot;; 	mso-fareast-theme-font:minor-fareast; 	mso-hansi-font-family:Calibri; 	mso-hansi-theme-font:minor-latin; 	mso-bidi-font-family:&quot;Times New Roman&quot;; 	mso-bidi-theme-font:minor-bidi;} --> <!--[endif]--></p>
<p><!--[if gte mso 9]&gt;  Normal 0     false false false  EN-IN X-NONE X-NONE                           &lt;![endif]--><!--[if gte mso 9]&gt;                                                                                                                                            &lt;![endif]--><!--[if gte mso 10]&gt; &lt;!   /* Style Definitions */  table.MsoNormalTable 	{mso-style-name:&quot;Table Normal&quot;; 	mso-tstyle-rowband-size:0; 	mso-tstyle-colband-size:0; 	mso-style-noshow:yes; 	mso-style-priority:99; 	mso-style-qformat:yes; 	mso-style-parent:&quot;&quot;; 	mso-padding-alt:0mm 5.4pt 0mm 5.4pt; 	mso-para-margin-top:0mm; 	mso-para-margin-right:0mm; 	mso-para-margin-bottom:10.0pt; 	mso-para-margin-left:0mm; 	line-height:115%; 	mso-pagination:widow-orphan; 	font-size:11.0pt; 	font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;; 	mso-ascii-font-family:Calibri; 	mso-ascii-theme-font:minor-latin; 	mso-fareast-font-family:&quot;Times New Roman&quot;; 	mso-fareast-theme-font:minor-fareast; 	mso-hansi-font-family:Calibri; 	mso-hansi-theme-font:minor-latin; 	mso-bidi-font-family:&quot;Times New Roman&quot;; 	mso-bidi-theme-font:minor-bidi;} --> <!--[endif]--></p>
<p><!--[if gte mso 9]&gt;  Normal 0     false false false  EN-IN X-NONE X-NONE                           &lt;![endif]--><!--[if gte mso 9]&gt;                                                                                                                                            &lt;![endif]--><!--[if gte mso 10]&gt; &lt;!   /* Style Definitions */  table.MsoNormalTable 	{mso-style-name:&quot;Table Normal&quot;; 	mso-tstyle-rowband-size:0; 	mso-tstyle-colband-size:0; 	mso-style-noshow:yes; 	mso-style-priority:99; 	mso-style-qformat:yes; 	mso-style-parent:&quot;&quot;; 	mso-padding-alt:0mm 5.4pt 0mm 5.4pt; 	mso-para-margin-top:0mm; 	mso-para-margin-right:0mm; 	mso-para-margin-bottom:10.0pt; 	mso-para-margin-left:0mm; 	line-height:115%; 	mso-pagination:widow-orphan; 	font-size:11.0pt; 	font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;; 	mso-ascii-font-family:Calibri; 	mso-ascii-theme-font:minor-latin; 	mso-fareast-font-family:&quot;Times New Roman&quot;; 	mso-fareast-theme-font:minor-fareast; 	mso-hansi-font-family:Calibri; 	mso-hansi-theme-font:minor-latin; 	mso-bidi-font-family:&quot;Times New Roman&quot;; 	mso-bidi-theme-font:minor-bidi;} --> <!--[endif]--></p>
<div id="_mcePaste" class="mcePaste" style="position:absolute;left:-10000px;top:539px;width:1px;height:1px;overflow:hidden;"><!--[if gte mso 9]&gt;  Normal 0     false false false  EN-IN X-NONE X-NONE                           &lt;![endif]--><!--[if gte mso 9]&gt;                                                                                                                                            &lt;![endif]--><!--[if gte mso 10]&gt; &lt;!   /* Style Definitions */  table.MsoNormalTable 	{mso-style-name:&quot;Table Normal&quot;; 	mso-tstyle-rowband-size:0; 	mso-tstyle-colband-size:0; 	mso-style-noshow:yes; 	mso-style-priority:99; 	mso-style-qformat:yes; 	mso-style-parent:&quot;&quot;; 	mso-padding-alt:0mm 5.4pt 0mm 5.4pt; 	mso-para-margin-top:0mm; 	mso-para-margin-right:0mm; 	mso-para-margin-bottom:10.0pt; 	mso-para-margin-left:0mm; 	line-height:115%; 	mso-pagination:widow-orphan; 	font-size:11.0pt; 	font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;; 	mso-ascii-font-family:Calibri; 	mso-ascii-theme-font:minor-latin; 	mso-fareast-font-family:&quot;Times New Roman&quot;; 	mso-fareast-theme-font:minor-fareast; 	mso-hansi-font-family:Calibri; 	mso-hansi-theme-font:minor-latin; 	mso-bidi-font-family:&quot;Times New Roman&quot;; 	mso-bidi-theme-font:minor-bidi;} --> <!--[endif]--> &lt;1&gt; means that we are giving that message the highest priority Create a folder name ‘test’ in the root of  linux source directory and save this file as strcpy.c in that directory . Create a makefile in that directory containing the line Obj-y:=strcpy.o The Edits The files to be edited are <strong><span style="font-family:&quot;">1 . /usr/src/linux-2.6.32.5/arch/x86/kernel/syscall_table_32.S</span></strong> <strong><span style="font-family:&quot;">Append the line  .long sys_strcpy to the file(Replace sys_strcpy with whatever name you want)</span></strong> <strong><span style="font-family:&quot;">2. </span></strong><strong><span style="font-family:&quot;">/usr/src/linux-2.6.32.5/arch/x86/include/asm/unistd_32.h</span></strong> <strong><span style="font-family:&quot;">This file contains the unique number associated with each system call. We can see the names of all the system calls and the number associated with each.  After the last system call-number pair (around line 345), add a line<br />
#define __NR_strcpy  338  (if 337 was the number associated with the last system call).<br />
Then replace NR_syscalls directive denoting the total number of system calls by the previous number incremented by 1 ie the new value id</span></strong> <strong><span style="font-family:&quot;">#define NR_syscalls 339</span></strong> <strong><span style="font-family:&quot;">Note down the number 338, we need it later.</span></strong> <strong><span style="font-family:&quot;">3. </span></strong><strong><span style="font-family:&quot;">/usr/src/linux-2.6.32.5/include/linux/syscalls.h</span></strong> <strong><span style="font-family:&quot;">This file contains the prototypes of system calls. Here we append to the file the prototype of our file.<br />
ie. We add  the line<br />
asmlinkage  long sys_strcpy(char *dest,char *src);</span></strong> <strong><span style="font-family:&quot;"> </span></strong> <strong><span style="font-family:&quot;">4. Makefile<br />
Open makefile and find the area where core-y is defined and add the folder test to the end of that line as shown below</span></strong></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/appusajeev.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/appusajeev.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/appusajeev.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/appusajeev.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/appusajeev.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/appusajeev.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/appusajeev.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/appusajeev.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/appusajeev.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/appusajeev.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/appusajeev.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/appusajeev.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/appusajeev.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/appusajeev.wordpress.com/268/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=appusajeev.wordpress.com&amp;blog=8252415&amp;post=268&amp;subd=appusajeev&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://appusajeev.wordpress.com/2010/11/13/implementing-a-system-call-in-linux-kernel-2-6-35/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/98e456523a3648aef486ab07e8917b6d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">appusajeev</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2010/11/strcpy.png" medium="image">
			<media:title type="html">Code for the system call </media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2010/11/table.png" medium="image">
			<media:title type="html">linux-2.6.35/arch/x86/kernel/syscall_table_32.S</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2010/11/numbers.png" medium="image">
			<media:title type="html">linux-2.6.35/arch/x86/include/asm/unistd_32.h</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2010/11/proto.png" medium="image">
			<media:title type="html">linux-2.6.35/include/linux/syscalls.h</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2010/11/makefile.png" medium="image">
			<media:title type="html">Makefile</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2010/11/sample.png" medium="image">
			<media:title type="html">System call test</media:title>
		</media:content>
	</item>
		<item>
		<title>PWM in AVR</title>
		<link>http://appusajeev.wordpress.com/2010/09/30/pwm-in-avr/</link>
		<comments>http://appusajeev.wordpress.com/2010/09/30/pwm-in-avr/#comments</comments>
		<pubDate>Wed, 29 Sep 2010 20:49:53 +0000</pubDate>
		<dc:creator>appusajeev</dc:creator>
				<category><![CDATA[avr]]></category>
		<category><![CDATA[avr-gcc]]></category>
		<category><![CDATA[atmega]]></category>

		<guid isPermaLink="false">http://appusajeev.wordpress.com/?p=249</guid>
		<description><![CDATA[In the last post, we discussed about timers in AVR microcontrollers and how to go about programming them. Presented here is a how-to on generating PWM waves in AVR. PWM waves of required duty cycle can be generating by configuring the timers with suitable values. PWM can be used to control servo motors, perform DAC [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=appusajeev.wordpress.com&amp;blog=8252415&amp;post=249&amp;subd=appusajeev&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In the last <a href="http://appusajeev.wordpress.com/2010/07/16/programming-the-timers-in-avr-avr-gcc/" target="_blank">post</a>, we discussed about timers in AVR microcontrollers and how to go about programming them. Presented here is a how-to on generating PWM waves in AVR. PWM waves of required duty cycle can be generating by configuring the timers with suitable values. PWM can be used to control servo motors, perform DAC (Digital to Analogue Conversion) etc.<br />
PWM waves will be available on the <span style="color:#993366;"><strong>OC1A pin</strong></span> (pin 15 on ATMEGA8,  19 on ATMEGA16) once it is setup.</p>
<p>In PWM, we generate square waves whose <span style="color:#993366;"><strong>duty cycle</strong></span> can be varied. Duty cycle refers to the fraction of the time period of the wave for which the signal is in high state (or simply ON state).<br />
For example, for a square wave of period 100ms, if the duty cycle=50%,<br />
the signal will be in high state for precisely 50 ms and in low state for the next 50ms that make up the period.<br />
If the duty cycle =20 %, the signal  will be high for 20ms and low for the remaining 80ms.</p>
<p>An interesting aspect of PWM is that we can actually perform <span style="color:#993366;"><strong>DAC</strong></span> (Digital to Analogue Conversion), the output voltage is proportional to the duty cycle we set. i.e. for example, if the duty cycle is 50%, the average voltage available on the OCA1 pin will be 2.5V  and for duty cycle = 30 %, the output voltage= .3x 5 V = 1.5 V and so on.</p>
<h3><strong>PWM modes</strong></h3>
<p>There are several modes of PWM generation provided by AVR  which decides the shape of the square wave generated (the placement of the high section in each cycle). The modes are Fast PWM, Phase correct PWM and Phase and Frequency Correct PWM. Each of these is explained with figures below.</p>
<h4>Fast PWM</h4>
<p>In Fast PWM, the timer starts form zero, sets OC1A pin high and starts counting up. When the count equals the <span style="color:#888888;"><span style="color:#993366;"><strong>compare</strong></span> </span>value (set in the OCR1A register), the OC1A pin is pulled low and the timer continues counting till TOP, resets to zero and repeats the whole cycle again. This is the normal non-inverted mode of operation. In inverted mode, OC1A is pulled low when counter resets and pulled high when compare match occurs.</p>
<div id="attachment_251" class="wp-caption aligncenter" style="width: 350px"><a href="http://appusajeev.files.wordpress.com/2010/09/fast.png"><img class="size-full wp-image-251" title="Fast PWM mode" src="http://appusajeev.files.wordpress.com/2010/09/fast.png" alt="Fast PWM mode" width="340" height="285" /></a><p class="wp-caption-text">Fast PWM mode</p></div>
<h4>Phase Correct PWM</h4>
<p>In this mode, when the timer count reaches TOP, it doesn’t reset to 0. Instead, it starts counting down towards zero. While counting up, when the count equals the <span style="color:#993366;"><strong>compare</strong></span> value, the OC1A pin is pulled high and while downcounting, when the count again becomes equal to <span style="color:#993366;"><strong>compare</strong></span> value, the OC1A pin is pulled low. The process repeats again when the count becomes zero.</p>
<div id="attachment_252" class="wp-caption aligncenter" style="width: 459px"><a href="http://appusajeev.files.wordpress.com/2010/09/pfc.png"><img class="size-full wp-image-252" title="Phase Correct PWM mode" src="http://appusajeev.files.wordpress.com/2010/09/pfc.png" alt="Phase Correct PWM mode" width="449" height="297" /></a><p class="wp-caption-text">Phase Correct PWM mode</p></div>
<h4>Phase and frequency correct PWM</h4>
<p>This is essentially the same as Phase Correct PWM but has the added feature that we can change the frequency of the square waves at any instant as opposed to the above modes where frequency is constant defined by the relation given in the section below.</p>
<h3>Starting PWM generation</h3>
<p>First, we need to configure the clock source for the timer. As discussed in the <a href="http://appusajeev.wordpress.com/2010/07/16/programming-the-timers-in-avr-avr-gcc/" target="_blank">earlier</a> post, a suitable prescaler may be chosen by setting the CS10,CS11,CS12 bits in the TCCR1B register.<br />
The value of prescaler is a factor in the frequency of waves generated. The frequency of generated square wave (independent of the duty cycle) is given by</p>
<div id="attachment_256" class="wp-caption aligncenter" style="width: 243px"><a href="http://appusajeev.files.wordpress.com/2010/09/formula.png"><img class="size-full wp-image-256" title="PWM formula" src="http://appusajeev.files.wordpress.com/2010/09/formula.png" alt="PWM formula" width="233" height="57" /></a><p class="wp-caption-text">PWM formula</p></div>
<p>N is the value of prescaler and TOP is value to which the counter counts and then resets to zero.<br />
In this discussion, we use the timer in <span style="color:#993366;"><strong>mode 5</strong></span> (refer datasheet), i.e. <span style="color:#993366;"><strong>8 bit Fast PWM</strong></span> with <span style="color:#993366;"><strong>TOP = 255</strong></span>. This is a fairly simple mode and can be used to control the speed of motors on the go.<br />
This mode can be selected by setting <span style="color:#993366;"><strong>WGM10</strong></span> bit in <span style="color:#993366;"><strong>TCCR1A</strong></span> register and <span style="color:#993366;"><strong>WGM12</strong></span> bit in<span style="color:#993366;"> <strong><span style="color:#993366;">TCCR1B</span></strong></span> register. 9 bit and 10 bit PWM with TOP=511 and TOP=1023 can be selected by setting suitable WGM (Waveform Generation Mode) bits. Refer datasheet for the values.<br />
Next we need to choose between normal or non-inverted PWM and inverted PWM (described above) . Inverted PWM is nothing but here duty cycle determines the off time of the waveform. Here we choose normal PWM by setting <span style="color:#993366;"><strong>COM1A1</strong></span> bit in TCCR1A register.</p>
<p>The duty cycle is given by setting a<strong> <span style="color:#993366;">value between 0 and 255 in the OCR1A register</span></strong><span style="color:#993366;"> </span>(similarly, 0 to 1023 for 10 bit PWM and 0 to 511 for 9 bit PWM). Value of 255 corresponds to a duty cycle of 100 % and value of 0 in OCR1A corresponds to 0 % duty cycle.<br />
ie OCR1A= 255 x duty cycle.<br />
For example, for duty cycles 20 %, 37 %, 70 %, set OCR1A = 51, 94,  178 respectively<br />
In our example given at the bottom, the frequency of waveform generated  = 46.875 KHz</p>
<h3>An example</h3>
<p>A simple straightforward way to demonstrate PWM is to hook up an LED to the OC1A pin. In PWM generation, since the average voltage is proportional to the duty cycle (DAC in effect), the brightness of the LED is proportional to the duty cycle with the LED being maximum bright at 100% duty cycle (OCR1A=255).<br />
The following program increases the brightness of the LED connected to OC1A pin gradually from zero to maximum and then from maximum to zero and so on. Observe the power LED of your laptop while sleeping (laptop, not you <img src='http://s0.wp.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> ). We are reproducing the same.</p>
<div id="attachment_250" class="wp-caption aligncenter" style="width: 348px"><a href="http://appusajeev.files.wordpress.com/2010/09/pwm-code.png"><img class="size-full wp-image-250" title="PWM demo code" src="http://appusajeev.files.wordpress.com/2010/09/pwm-code.png" alt="PWM demo code" width="338" height="513" /></a><p class="wp-caption-text">PWM demo code </p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/appusajeev.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/appusajeev.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/appusajeev.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/appusajeev.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/appusajeev.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/appusajeev.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/appusajeev.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/appusajeev.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/appusajeev.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/appusajeev.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/appusajeev.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/appusajeev.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/appusajeev.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/appusajeev.wordpress.com/249/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=appusajeev.wordpress.com&amp;blog=8252415&amp;post=249&amp;subd=appusajeev&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://appusajeev.wordpress.com/2010/09/30/pwm-in-avr/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/98e456523a3648aef486ab07e8917b6d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">appusajeev</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2010/09/fast.png" medium="image">
			<media:title type="html">Fast PWM mode</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2010/09/pfc.png" medium="image">
			<media:title type="html">Phase Correct PWM mode</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2010/09/formula.png" medium="image">
			<media:title type="html">PWM formula</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2010/09/pwm-code.png" medium="image">
			<media:title type="html">PWM demo code</media:title>
		</media:content>
	</item>
		<item>
		<title>Programming the Timers in AVR [avr-gcc]</title>
		<link>http://appusajeev.wordpress.com/2010/07/16/programming-the-timers-in-avr-avr-gcc/</link>
		<comments>http://appusajeev.wordpress.com/2010/07/16/programming-the-timers-in-avr-avr-gcc/#comments</comments>
		<pubDate>Fri, 16 Jul 2010 16:54:08 +0000</pubDate>
		<dc:creator>appusajeev</dc:creator>
				<category><![CDATA[avr-gcc]]></category>
		<category><![CDATA[atmega]]></category>
		<category><![CDATA[timers]]></category>

		<guid isPermaLink="false">http://appusajeev.wordpress.com/?p=229</guid>
		<description><![CDATA[All the Atmel® AVR microcontrollers have TIMERs as an inbuilt peripheral . They can be used to generate PWM(Pulse Width Modulation)waves, for generating accurately timed pulses and for registering the timestamp of external events(Input capture mode). ATMEGA 16 and ATMEGA 8(which we focus on in this post) microcontrollers have 3 inbuilt timers in them, one [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=appusajeev.wordpress.com&amp;blog=8252415&amp;post=229&amp;subd=appusajeev&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>All the Atmel® AVR microcontrollers have TIMERs as an inbuilt peripheral . They can be used to generate PWM(Pulse Width Modulation)waves, for generating accurately timed pulses and for registering the timestamp of external events(Input capture mode). <strong>ATMEGA 16</strong> and <strong>ATMEGA 8</strong>(which we focus on in this post) microcontrollers have 3 inbuilt timers in them, one 16 bit timer and two 8 bit timers. These timers run independent of the program being executed and are capable of generating interrupts. Here i will be explaining about the 16 bit timer 1 of ATMEGA 8 and the same applies to ATMEGA 16 as well.</p>
<p>A timer (also called as counter) is simply a device that counts upon receiving clock pulses. The timer increments (or decrements in certain cases)its count with each clock tick it receives.  A timer is usually specified by the maximum value to which it can count (called <strong>MAX</strong>) beyond which it overflows and resets to zero(<strong>BOTTOM</strong>). Thus an <strong>8-bit</strong> timer/counter can count from 0 through 255 and a <strong>16-bit</strong> timer/counter can count from 0 through 65,535.<br />
The speed of counting can be controlled by varying the speed of clock input to it. This is done by <strong>prescaling</strong> the clock of the microcontroller. By prescaling, we feed a fraction of the CPU clock to the timer.</p>
<p style="padding-left:30px;"><span style="color:#0000ff;"><strong>Timer speed =F_CPU / prescaler</strong></span></p>
<p>where F_CPU is the AVR CPU clock speed.Normal prescaler values are <strong>1,8,64,256 </strong>and<strong> 1024</strong>. For example, for a 1MHZ clock, if prescaler is 64, then timer speed is  1000000/64=15625 i.e.  in one second,  the timer can count from 0 to 15625.<br />
When the timer reaches the maximum value it can count, it is said to overflow and it automatically resets to 0 and starts counting again. We can set an interrupt to occur when the timer overflows.<br />
When the timer 1 overflows, <span style="color:#993366;"><strong>TOV1</strong></span> bit will be set in the <span style="color:#993366;"><strong>TIFR</strong></span> (Timer Interrupt Flag Register) register which will be cleared automatically when the corresponding ISR executes or it can be cleared by setting TOV1 bit to 1(odd, but that’s how ATMEL guys want us to clear it !).</p>
<h3>Starting the timer</h3>
<p>A counter starts counting as soon as it is set to use a clock source, by setting a suitable prescaler value bits <span style="color:#993366;"><strong>CS10</strong></span>,<span style="color:#993366;"><strong>CS11</strong></span> and <span style="color:#993366;"><strong>CS12</strong></span> in the<span style="color:#993366;"><strong> TCCR1B</strong></span> (Timer Counter Control Register 1 B )register . The instantaneous value of the timer is available anytime in the <span style="color:#993366;"><strong>TCNT </strong></span>(Timer Count) register. Refer datasheet for more information on various bits in the register.</p>
<h3>Timer in Compare Mode (CTC mode)</h3>
<p>The timer used in above mode is of not much use as such. The AVR timer can be operated in a mode called CTC mode or <strong>Clear on Timer Capture mode</strong> in which we can set the timer to compare its count with a certain value set in <span style="color:#993366;"><strong>OCR1A</strong></span> (Output Compare Register 1 A) register or OCR1B register and generate an interrupt or manipulate the OC1A or OC1B pin whenever a match occurs (i.e. when <span style="color:#993366;"><strong>TCNT=OCR1A</strong></span> or <span style="color:#993366;"><strong>TCNT=OCR1B</strong></span>).CTC mode can be used to generate accurate timings and square waves of desired frequency.<br />
CTC mode can be enabled by setting <span style="color:#993366;"><strong>WGM 12</strong></span> bit=1 in TCCR1B register.<br />
When TCNT=OCR1A, the timer resets and starts from 0 again. The value of OCR1A can be changed anytime and the change in the output will be reflected immediately.</p>
<div id="attachment_235" class="wp-caption aligncenter" style="width: 510px"><a href="http://appusajeev.files.wordpress.com/2010/07/waveform.png"><img class="size-full wp-image-235" title="toggling OC1A pin in CTC mode" src="http://appusajeev.files.wordpress.com/2010/07/waveform.png" alt="toggling OC1A pin in CTC mode" width="500" height="269" /></a><p class="wp-caption-text">toggling OC1A pin in CTC mode</p></div>
<p>When a match occurs we can either Set,Clear or toggle the OC1A/OC1B  pin.The action of OC1A pin can be set by setting suitable values for <span style="color:#993366;"><strong>COM1A0</strong></span> and <strong><span style="color:#993366;">COM1A1</span></strong> bits in <span style="color:#993366;"><strong>TCCR1A </strong></span>(Timer Counter Control Register 1 A) register (refer datasheet).</p>
<p>Whenever a compare match occurs, the OCF1A bit will be set in the <span style="color:#993366;"><strong>TIFR</strong></span>(Timer Interrupt Flag Register) which will automatically be cleared when the associated ISR fires or else we have to manually clear it by setting it to 1.</p>
<p>The following example demonstrates CTC mode of Timer 1. An LED is connected to t0 PB1 pin(pin 15) of Atmega8 which flashes at 1 hz.</p>
<div id="attachment_231" class="wp-caption aligncenter" style="width: 513px"><a href="http://appusajeev.files.wordpress.com/2010/07/ctc_toggle.png"><img class="size-full wp-image-231" title="Atmega8 Timer 1 in CTC toggle mode" src="http://appusajeev.files.wordpress.com/2010/07/ctc_toggle.png" alt="Atmega8 Timer 1 in CTC toggle mode" width="503" height="331" /></a><p class="wp-caption-text">Atmega8 Timer 1 in CTC toggle mode</p></div>
<h3>Generating Square Waves</h3>
<p>With the OC1A pin set to toggle by setting <span style="color:#993366;"><strong>COM1A0=1</strong></span> in TCCR1A register, this mode can be used to generate a square wave of required frequency(50% duty cycle only though) by setting a suitable value in the OCR1A register.<br />
As said above, the value of OCR1A can be changed anytime and the change in the output will be reflected immediately</p>
<p>The frequency of the square wave generated is given by</p>
<p style="padding-left:120px;"><strong>f = F_CPU / (2.prescaler.(1+OCR1A))</strong></p>
<p style="padding-left:120px;">
<div id="attachment_232" class="wp-caption aligncenter" style="width: 538px"><a href="http://appusajeev.files.wordpress.com/2010/07/ctc_sq_wave.png"><img class="size-full wp-image-232" title="Generating Square waves in CTC mode" src="http://appusajeev.files.wordpress.com/2010/07/ctc_sq_wave.png" alt="Generating Square waves in CTC mode" width="528" height="489" /></a><p class="wp-caption-text">Generating Square waves in CTC mode</p></div>
<p>For eg, to generate a 50Hz square wave, set OCR1A=291</p>
<h3>Timers and interrupts</h3>
<p>AVR timers can generate 3 types of interrupts- <strong>Overflow, Compare match and Input Capture</strong> (Input capture is a mode in which we can have the value of the  TCNT register copied to the ICR register whenever  a rising/falling edge is found on the ICP1 pin of the ATMEGA) and we can execute a user specified ISR for these as demonstrated.<br />
To enable interrupts, first we have to enable compare match interrupt for  timer 1 by setting <strong><span style="color:#993366;">OCIE1A</span></strong> (Output Compare A Match Interrupt Enable) bit in the<span style="color:#993366;"><strong> TIMSK</strong></span> register and then we have to enable global interrupts with the <span style="color:#993366;"><strong>sei()</strong></span> macro. For handling overflow interrupts, set <span style="color:#993366;"><strong>TOIE1 </strong></span>bit in TIMSK register to 1<span style="color:#993366;"><strong>.<br />
</strong></span></p>
<p>The following example demonstrates the use of interrupts. The interrupt service routine(ISR) toggles the LED connected to pin 1 of port B.</p>
<div id="attachment_233" class="wp-caption aligncenter" style="width: 498px"><a href="http://appusajeev.files.wordpress.com/2010/07/isr.png"><img class="size-full wp-image-233" title="Timer 1 Interrupt Handling" src="http://appusajeev.files.wordpress.com/2010/07/isr.png" alt="Timer 1 Interrupt Handling" width="488" height="429" /></a><p class="wp-caption-text">Timer 1 Interrupt Handling</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/appusajeev.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/appusajeev.wordpress.com/229/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/appusajeev.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/appusajeev.wordpress.com/229/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/appusajeev.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/appusajeev.wordpress.com/229/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/appusajeev.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/appusajeev.wordpress.com/229/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/appusajeev.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/appusajeev.wordpress.com/229/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/appusajeev.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/appusajeev.wordpress.com/229/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/appusajeev.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/appusajeev.wordpress.com/229/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=appusajeev.wordpress.com&amp;blog=8252415&amp;post=229&amp;subd=appusajeev&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://appusajeev.wordpress.com/2010/07/16/programming-the-timers-in-avr-avr-gcc/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/98e456523a3648aef486ab07e8917b6d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">appusajeev</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2010/07/waveform.png" medium="image">
			<media:title type="html">toggling OC1A pin in CTC mode</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2010/07/ctc_toggle.png" medium="image">
			<media:title type="html">Atmega8 Timer 1 in CTC toggle mode</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2010/07/ctc_sq_wave.png" medium="image">
			<media:title type="html">Generating Square waves in CTC mode</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2010/07/isr.png" medium="image">
			<media:title type="html">Timer 1 Interrupt Handling</media:title>
		</media:content>
	</item>
		<item>
		<title>A Case of Regular Expression [Python]</title>
		<link>http://appusajeev.wordpress.com/2010/03/30/a-case-of-regular-expression-python/</link>
		<comments>http://appusajeev.wordpress.com/2010/03/30/a-case-of-regular-expression-python/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 12:30:46 +0000</pubDate>
		<dc:creator>appusajeev</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://appusajeev.wordpress.com/?p=201</guid>
		<description><![CDATA[Regular expressions can be so cool at times,when there a is need to search and find certain text patterns.Today there was this situation where it was needed to extract the Microprocessor Based Design(ironically,theres absolutely no design involved ) external marks of 66 students from different 66 web pages(as the result is obtained for each student [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=appusajeev.wordpress.com&amp;blog=8252415&amp;post=201&amp;subd=appusajeev&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Regular expressions can be so cool at times,when there a is need to search and find certain text patterns.Today there was this situation where it was needed to extract the Microprocessor Based Design(ironically,theres absolutely no design involved <img src='http://s0.wp.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> ) external marks of 66 students from different 66 web pages(as the result is obtained for each student separately  from the university site).<br />
Python was of course the obvious choice. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
First i thought of using  file operations and string matching to do the job but later decided use regular expressions. I tried to find all occurrences of the string between  &#8220;<strong>&lt;td&gt;MICROPROCESSOR BASED DESIGN&lt;/td&gt;</strong>&#8221; and the first following &#8220;<strong>&lt;/td&gt;</strong>&#8221; section in the html code(that was the pattern to be matched in this case if you observe the html code) of each page.<br />
This would produce the section containing the external mark but also would contain certain html tags in between which needs to be removed,the mark has to be extracted from the result and for this,usual string matching would suffice. The mark extracted is saved into a file.</p>
<p>The regular expresion for this would be &#8220;<strong>&lt;td&gt;MICROPROCESOR BASED DESIGN&lt;/td&gt;[a-zA-Z0-9=&lt;&gt;\s\n\t/]*&lt;\td&gt;</strong>&#8220;. Or better still,you could also try &#8220;<strong>BASED DESIGN&lt;/td&gt;[a-zA-Z0-9=&lt;&gt;\s\n\t/]*&lt;\td&gt;</strong>&#8220;<br />
The expression cannot start with &#8220;DESIGN&#8221; cuz it would match COMPUTER ORGANIZATION AND DESIGN,another subject !<br />
Symbols &lt;&gt;,\n,\t,/ need to be be included because the section to be extracted contains html tags.</p>
<p>Heres the complete code. As obvious,regular expressions make things a lot easier as opposed to the usual approach.</p>
<div id="attachment_205" class="wp-caption aligncenter" style="width: 581px"><a href="http://appusajeev.files.wordpress.com/2010/03/screenshot.png"><img class="size-full wp-image-205" title="Regular expression put to use" src="http://appusajeev.files.wordpress.com/2010/03/screenshot.png" alt="Regular expression put to use" width="571" height="221" /></a><p class="wp-caption-text">Regular expression put to use</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/appusajeev.wordpress.com/201/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/appusajeev.wordpress.com/201/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/appusajeev.wordpress.com/201/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/appusajeev.wordpress.com/201/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/appusajeev.wordpress.com/201/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/appusajeev.wordpress.com/201/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/appusajeev.wordpress.com/201/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/appusajeev.wordpress.com/201/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/appusajeev.wordpress.com/201/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/appusajeev.wordpress.com/201/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/appusajeev.wordpress.com/201/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/appusajeev.wordpress.com/201/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/appusajeev.wordpress.com/201/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/appusajeev.wordpress.com/201/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=appusajeev.wordpress.com&amp;blog=8252415&amp;post=201&amp;subd=appusajeev&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://appusajeev.wordpress.com/2010/03/30/a-case-of-regular-expression-python/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/98e456523a3648aef486ab07e8917b6d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">appusajeev</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2010/03/screenshot.png" medium="image">
			<media:title type="html">Regular expression put to use</media:title>
		</media:content>
	</item>
		<item>
		<title>Developing an Anti-Worm tool [VB 6]</title>
		<link>http://appusajeev.wordpress.com/2010/03/01/developing-an-anti-worm-tool-vb-6/</link>
		<comments>http://appusajeev.wordpress.com/2010/03/01/developing-an-anti-worm-tool-vb-6/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 15:20:35 +0000</pubDate>
		<dc:creator>appusajeev</dc:creator>
				<category><![CDATA[Visual Basic]]></category>
		<category><![CDATA[Worm]]></category>
		<category><![CDATA[VB]]></category>

		<guid isPermaLink="false">http://appusajeev.wordpress.com/?p=186</guid>
		<description><![CDATA[I am pretty sure that you must have certainly come across this malware which copies itself in each directory with the name same as the directory name and icon same as the default win xp folder icon to trick the user into executing the malware which he apparently thinks as a folder(see the post below [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=appusajeev.wordpress.com&amp;blog=8252415&amp;post=186&amp;subd=appusajeev&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I am pretty sure that you must have certainly come across this malware which copies itself in each directory with the name same as the directory name and icon same as the default win xp folder icon to trick the user into executing the malware which he apparently thinks as a folder(see the <a title="Malware replication" href="http://appusajeev.wordpress.com/2010/01/27/malware-replication-visual-basic-6/" target="_blank">post</a> below for an implementation of the same).So even if you somehow kill it,the chances of it bouncing back to action are pretty high.</p>
<p>Anyway this post is about creating a anti-worm tool in VB &#8211; a behaviour based detection tool that  searches for and removes such malware .</p>
<p>First, you gotta give it a sample of the malware to search for. The directory structure of each drive is traversed and each directory is searched for the presence of an exe with the name same as the directory name, if such an exe is found, its size is compared with the size of the exe given as sample. If there is match,it is reported(MD5 signature based comparison would have been an anytime better alternative but i dunno if theres is an md5 implementation for vb yet). This second level of checking is needed cuz an exe with name same as the directory name need not always be a malware.</p>
<p><a title="Anti-worm source" href="http://vectorkid.110mb.com/anti_worm_src.zip" target="_blank">Download Source</a></p>
<p><a title="Download anti-worm" href="http://vectorkid.110mb.com/anti_worm.zip" target="_blank">Download Tool</a></p>
<p>Heres the tool in action</p>
<div id="attachment_189" class="wp-caption aligncenter" style="width: 606px"><a href="http://appusajeev.files.wordpress.com/2010/03/untitled-1.gif"><img class="size-full wp-image-189" title="Ant-worm tool in action" src="http://appusajeev.files.wordpress.com/2010/03/untitled-1.gif" alt="Ant-worm tool in action" width="596" height="397" /></a><p class="wp-caption-text">Ant-worm tool in action</p></div>
<p>Given below is the souce, <a title="Download anti-worm source" href="http://vectorkid.110mb.com/anti_worm_src.zip" target="_blank">download source</a></p>
<div id="attachment_187" class="wp-caption aligncenter" style="width: 660px"><a href="http://appusajeev.files.wordpress.com/2010/03/malw.png"><img class="size-full wp-image-187" title="Anti-worm source in VB6" src="http://appusajeev.files.wordpress.com/2010/03/malw.png" alt="Anti-worm source in VB6" width="650" height="1277" /></a><p class="wp-caption-text">Anti-worm source in VB6</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/appusajeev.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/appusajeev.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/appusajeev.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/appusajeev.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/appusajeev.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/appusajeev.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/appusajeev.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/appusajeev.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/appusajeev.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/appusajeev.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/appusajeev.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/appusajeev.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/appusajeev.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/appusajeev.wordpress.com/186/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=appusajeev.wordpress.com&amp;blog=8252415&amp;post=186&amp;subd=appusajeev&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://appusajeev.wordpress.com/2010/03/01/developing-an-anti-worm-tool-vb-6/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/98e456523a3648aef486ab07e8917b6d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">appusajeev</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2010/03/untitled-1.gif" medium="image">
			<media:title type="html">Ant-worm tool in action</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2010/03/malw.png" medium="image">
			<media:title type="html">Anti-worm source in VB6</media:title>
		</media:content>
	</item>
		<item>
		<title>An alternative to If-Else and Switch [Python]</title>
		<link>http://appusajeev.wordpress.com/2010/02/04/an-alternative-to-if-else-and-switch-python/</link>
		<comments>http://appusajeev.wordpress.com/2010/02/04/an-alternative-to-if-else-and-switch-python/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 17:41:22 +0000</pubDate>
		<dc:creator>appusajeev</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://appusajeev.wordpress.com/?p=176</guid>
		<description><![CDATA[You know there are times (especially in lab programs) when we have to print a menu on screen,get the choice from the user,do the usual,mechanical if-else-if or switch checks to invoke a function. The same could be achieved with very little effort from Python avoiding condition checks Create a list(or a tuple) containing 3-element tuples [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=appusajeev.wordpress.com&amp;blog=8252415&amp;post=176&amp;subd=appusajeev&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>You know there are times (especially in lab programs) when we have to print a menu on screen,get the choice from the user,do the usual,mechanical<em> <strong>if-else-if</strong></em> or<em> <strong>switch</strong></em> checks to invoke a function.<br />
The same could be achieved with very little effort from Python avoiding condition checks</p>
<p>Create a list(or a tuple) containing 3-element tuples as the list members,the first member of the tuple being the string to be printed,the second being the function to be invoked and the third member being a list containing the arguments to be passed to the function.<br />
Print the 0th element of the tuples present in the list in order,read a choice from the user and invoke the function as shown in the code.</p>
<p>Since python supports functional paradigm,functions are treated as <a href="http://linuxgazette.net/109/pramode.html" target="_blank">first class members</a> and hence they can inserted into lists,tuples etc<br />
For dealing with function arguments,it would be good to have the function accept a list as its argument from which it can extract the needed arguments (which,well maynot be a good idea in cases we dont know the arguments to be passed to the function beforehand)<br />
In case the functions that do accept any arguments,make the function accept a dummy lust as argument.</p>
<p>Heres an example based on the good ol&#8217;  Binary Search Tree</p>
<div id="attachment_178" class="wp-caption aligncenter" style="width: 461px"><a href="http://appusajeev.files.wordpress.com/2010/02/alt.gif"><img class="size-full wp-image-178" title="An alternative to If-Else and Switch" src="http://appusajeev.files.wordpress.com/2010/02/alt.gif" alt="An alternative to If-Else and Switch" width="451" height="483" /></a><p class="wp-caption-text">An alternative to If-Else and Switch</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/appusajeev.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/appusajeev.wordpress.com/176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/appusajeev.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/appusajeev.wordpress.com/176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/appusajeev.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/appusajeev.wordpress.com/176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/appusajeev.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/appusajeev.wordpress.com/176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/appusajeev.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/appusajeev.wordpress.com/176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/appusajeev.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/appusajeev.wordpress.com/176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/appusajeev.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/appusajeev.wordpress.com/176/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=appusajeev.wordpress.com&amp;blog=8252415&amp;post=176&amp;subd=appusajeev&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://appusajeev.wordpress.com/2010/02/04/an-alternative-to-if-else-and-switch-python/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/98e456523a3648aef486ab07e8917b6d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">appusajeev</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2010/02/alt.gif" medium="image">
			<media:title type="html">An alternative to If-Else and Switch</media:title>
		</media:content>
	</item>
		<item>
		<title>Malware Replication [Visual Basic 6]</title>
		<link>http://appusajeev.wordpress.com/2010/01/27/malware-replication-visual-basic-6/</link>
		<comments>http://appusajeev.wordpress.com/2010/01/27/malware-replication-visual-basic-6/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 07:20:51 +0000</pubDate>
		<dc:creator>appusajeev</dc:creator>
				<category><![CDATA[Visual Basic]]></category>
		<category><![CDATA[Replication]]></category>
		<category><![CDATA[VB]]></category>

		<guid isPermaLink="false">http://appusajeev.wordpress.com/?p=171</guid>
		<description><![CDATA[Its been a while since something has been posted about malware. So this time,we gonna develop a mechanism whereby you can spread your malware ie, copy your malware to each and every directory in the system with name same as the name of the directory  (if you can provide a &#8216;folder&#8217; icon to the exe,you [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=appusajeev.wordpress.com&amp;blog=8252415&amp;post=171&amp;subd=appusajeev&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Its been a while since something has been posted about malware. So this time,we gonna develop a mechanism whereby you can spread your malware ie, copy your malware to each and every directory in the system with name same as the name of the directory  (if you can provide a &#8216;<em>folde</em>r&#8217; icon to the exe,you can trick the user into executing the file which he apparently thinks as a folder).</p>
<p>The code is straight forward.No rocket science. Just traverse the directories recursively and copy the exe to each directory renaming it to the name of the directory .</p>
<p><a title="Malware Replication Source" href="http://vectorkid.110mb.com/replicate.zip" target="_blank">Download Source</a></p>
<p>I have assumed below that &#8216;<em>malw.exe</em>&#8216; is the malware you want to spread and its present in the same directory where the code will be running.</p>
<div id="attachment_172" class="wp-caption aligncenter" style="width: 438px"><a href="http://appusajeev.files.wordpress.com/2010/01/rep.png"><img class="size-full wp-image-172" title="Malware Replication code [VB 6]" src="http://appusajeev.files.wordpress.com/2010/01/rep.png" alt="Malware Replication code [VB 6]" width="428" height="342" /></a><p class="wp-caption-text">Malware Replication code :VB 6</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/appusajeev.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/appusajeev.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/appusajeev.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/appusajeev.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/appusajeev.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/appusajeev.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/appusajeev.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/appusajeev.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/appusajeev.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/appusajeev.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/appusajeev.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/appusajeev.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/appusajeev.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/appusajeev.wordpress.com/171/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=appusajeev.wordpress.com&amp;blog=8252415&amp;post=171&amp;subd=appusajeev&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://appusajeev.wordpress.com/2010/01/27/malware-replication-visual-basic-6/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/98e456523a3648aef486ab07e8917b6d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">appusajeev</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2010/01/rep.png" medium="image">
			<media:title type="html">Malware Replication code [VB 6]</media:title>
		</media:content>
	</item>
		<item>
		<title>An abstraction for MySQL transactions in PHP</title>
		<link>http://appusajeev.wordpress.com/2010/01/16/an-abstraction-for-mysql-transactions-in-php/</link>
		<comments>http://appusajeev.wordpress.com/2010/01/16/an-abstraction-for-mysql-transactions-in-php/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 09:18:24 +0000</pubDate>
		<dc:creator>appusajeev</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://appusajeev.wordpress.com/?p=160</guid>
		<description><![CDATA[PHP comes with intrinsic support for MySQL. A handful of functions exist for performing various database transactions. But when the number of pages containing database interactions increases,it becomes a pain to handle the connections and query processing on each page.This also affects the portability of the app since you have to make quite a lot [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=appusajeev.wordpress.com&amp;blog=8252415&amp;post=160&amp;subd=appusajeev&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>PHP comes with intrinsic support for MySQL. A handful of functions exist for performing various database transactions. But when the number of pages containing database interactions increases,it becomes a pain to handle the connections and query processing on each page.This also affects the portability of the app since you have to make quite a lot modifications to up the app in the  new server environment. So i thought of creating a class ,which besides providing some abstraction ,can be reused.By reusing the class,ease of portability may be achieved since the environment dependent area has been isolated to a singe module/class(software engineering principles,he he).</p>
<p>The class exports  methods like <strong>connect(), selectdb(),query(),get_entry(),count_result()<em> </em></strong>which are nothing but  a higher level abstractions over functions like mysql_connect(),mysql_query() while the other parameters like <em>link identifier ,result resource</em> etc remains hidden from the user.<br />
The result of the query(<strong>SELECT</strong> query) becomes available in the array <strong>row</strong> with field name as the key.I think the rest is all self explanatory.<br />
The script file containing the class can be included in the required page using <strong>include()</strong><em> </em>PHP function.</p>
<p><a title="Download Source " href="http://vectorkid.110mb.com/dbOps.zip" target="_blank">Download Source</a></p>
<div id="attachment_166" class="wp-caption aligncenter" style="width: 363px"><a href="http://appusajeev.files.wordpress.com/2010/01/php.gif"><img class="size-full wp-image-166" title="An abstraction for PHP MySQL  transactions" src="http://appusajeev.files.wordpress.com/2010/01/php.gif" alt="An abstraction for PHP MySQL  transactions" width="353" height="589" /></a><p class="wp-caption-text">An abstraction for PHP MySQL  transactions</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/appusajeev.wordpress.com/160/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/appusajeev.wordpress.com/160/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/appusajeev.wordpress.com/160/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/appusajeev.wordpress.com/160/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/appusajeev.wordpress.com/160/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/appusajeev.wordpress.com/160/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/appusajeev.wordpress.com/160/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/appusajeev.wordpress.com/160/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/appusajeev.wordpress.com/160/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/appusajeev.wordpress.com/160/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/appusajeev.wordpress.com/160/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/appusajeev.wordpress.com/160/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/appusajeev.wordpress.com/160/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/appusajeev.wordpress.com/160/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=appusajeev.wordpress.com&amp;blog=8252415&amp;post=160&amp;subd=appusajeev&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://appusajeev.wordpress.com/2010/01/16/an-abstraction-for-mysql-transactions-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/98e456523a3648aef486ab07e8917b6d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">appusajeev</media:title>
		</media:content>

		<media:content url="http://appusajeev.files.wordpress.com/2010/01/php.gif" medium="image">
			<media:title type="html">An abstraction for PHP MySQL  transactions</media:title>
		</media:content>
	</item>
	</channel>
</rss>
