tag:blogger.com,1999:blog-23548537946770960922024-02-19T15:42:47.323-08:00Programming BasicsShabab Haider Siddiquehttp://www.blogger.com/profile/14896191923433512555noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-2354853794677096092.post-3089813473826063702011-02-04T22:11:00.000-08:002011-02-04T22:11:00.569-08:00Creating a JavaDB with Netbeans<p>I am starting to put my hands on Java. Its quite amazing to see how Java connects to databases. Almost the same for all cases(tried Oracle,JavaDB). Just puting the code here to get in future.</p><br /><p>Lets first start with creating and connecting to a JavaDB using netbeans.</p><br /><p style="TEXT-ALIGN: center"><img src="http://img513.imageshack.us/img513/4683/db2ou.png" alt="db2.png" height="92" width="422"/></p><br /><p>first go to the services tab.</p><br /><p>Expand the Database entry.</p><br /><p>right click the JavaDB and click start server.</p><br /><p>Right click again the JavaDB to create a new Database. The database create wizard shall appear.</p><br /><p style="TEXT-ALIGN: center"><img src="http://img513.imageshack.us/img513/5308/db4.png" alt="db4.png" height="293" width="549"/></p><br /><p>Just type in the fields as indicated to continue this tutorial. the password is also "app".</p><br /><p>connect to the newly created database. (right click and connect).</p><br /><p style="TEXT-ALIGN: center"><img src="http://img513.imageshack.us/img513/6530/db7g.png" alt="db7.png" height="173" width="415"/></p><br /><p>Expand the APP. and right click "Tables" to create a table. The table creator wizard appears.</p><br /><p style="TEXT-ALIGN: center"><img src="http://img51.imageshack.us/img51/8773/db8c.png" alt="db8.png" height="374" width="668"/></p><br /><p style="TEXT-ALIGN: left">Create your table using this wizard. When done Expand the Tables section to view your newly created table. You can add data to it if you want.</p>Shabab Haider Siddiquehttp://www.blogger.com/profile/14896191923433512555noreply@blogger.com0tag:blogger.com,1999:blog-2354853794677096092.post-35585277549207616852010-02-07T07:27:00.000-08:002010-02-07T07:27:50.818-08:00C Programming Basic - Part 1C is a widely used language to program, specially useful for learning basic concepts of porgramming. C uses various compilers. Each of these compilers is slightly different. Each one should support the ANSI standard C functions, but each compiler will also have nonstandard functions (these functions are similar to slang spoken in different parts of a country). Sometimes the use of nonstandard functions will cause problems when you attempt to compile source code (the actual C code written by a programmer and saved as a text file) with a different compiler. These tutorials use ANSI standard C and should not suffer from this problem; fortunately, since C has been around for quite a while, there shouldn't be too many compatability issues except when your compiler tries to create C++ code.<br />
<br />
Every full C program begins inside a function called "main". A function is simply a collection of commands that do "something". The main function is always called when the program first executes. From main, we can call other functions, whether they be written by us or by others or use built-in language features. To access the standard functions that comes with your compiler, you need to include a header with the #include directive. What this does is effectively take everything in the header and paste it into your program. Let's look at a working program: <br />
<pre class="example"> </pre><pre class="example">#include <stdio.h>
</stdio.h></pre><pre class="example"><code>
int main()
{
printf( "Hello world!" );
return 0;</pre><pre class="example">}
</code>
</pre><pre class="example"></pre>Let's look at the elements of the program. The #include is a "preprocessor" directive that tells the compiler to put code from the header called stdio.h into our program before actually creating the executable. By including header files, you can gain access to many different functions ( the printf ) function is included in stdio.h.<br />
<br />
The next imporant line is int main(). This line tells the compiler that there is a function named main, and that the function returns an integer, hence int. The "curly braces" ({ and }) indicate the beginning and end of the function. <br />
<br />
The printf function is the standard C way of displaying output on the screen. The quotes tell the compiler that you want to output the literal string as-is (almost). <br />
<br />
Finally, at the end of the program, we return a value from main to the operating system by using the return statement. This return value is important as it can be used to tell the operating system whether our program succeeded or not. A return value of 0 means success. <br />
<br />
The final brace closes off the function. <br />
<br />
Interesting isn't it ? Start playing around with this. To know about <a href="http://programminbasics.blogspot.com/2010/02/compiler.html">compiler</a>, you may refer to <a href="http://programminbasics.blogspot.com/2010/02/compiler.html">here</a>Shabab Haider Siddiquehttp://www.blogger.com/profile/14896191923433512555noreply@blogger.com0tag:blogger.com,1999:blog-2354853794677096092.post-43701868170270195192010-02-07T07:25:00.000-08:002010-02-07T07:25:54.462-08:00Compiler<span style="font-size: large;">What is a compiler?</span><br />
A compiler is necessary to make your source code (..c, .cpp, or .cc files) into a running program. If you're just starting out, you'll need to make sure that you have one before you start. There are many compilers available<br />
<br />
<br />
<br />
Terminology:<br />
<br />
<div class="content2"><br />
<li><b>Compile</b> convert a source code file into an executable, but strictly speaking, compilation is an intermediate step </li><br />
<li><b>Link</b> The act of taking compiled code and turning it into an executable </li><br />
<li><b>Build</b> A build refers to the process of creating the end executable (what is often colloquially refered to as compilation). </li><br />
<li><b>Compiler</b> Generally, compiler refers to both a compiler and a "linker" </li><br />
<li><b>Linker</b> The program that generates the executable by linking </li><br />
<li><b>IDE</b> <b>I</b>ntegrated <b>D</b>evelopment <b>E</b>nvironment, a combination of a text editor<a href="http://www.cprogramming.com/texteditors.html"></a> and a compiler, such that you can compile and run your programs directly within the IDE. IDEs facilitates debugging.<h2> Compilers:</h2><ul><h3>Windows/DOS</h3><li><a href="http://www.cprogramming.com/code_blocks/">Code::Blocks and MINGW</a> Our recommended free compiler setup! </li>
<li><a href="http://www.cprogramming.com/borland.html">Borland</a> Find out how to download and set up Borland's free command-line compiler </li>
<li><a href="http://www.cprogramming.com/djgpp.html">DJGPP</a> Read about DJGPP, a DOS-based compiler</li>
<li><a href="http://www.cprogramming.com/other.html">Dev-C++ and Digital Mars</a> Read about Dev-C++, a good windows based compiler, and Digital Mars </li>
<h3>Windows Only</h3><li><a href="http://www.cprogramming.com/visual.html">Microsoft Visual C++</a> you must have heard about Visual C++</li>
<h3>*nix</h3><li><a href="http://www.cprogramming.com/g++.html">g++</a> is a C++ compiler that comes with most *nix distributions.</li>
<li><a href="http://www.cprogramming.com/gcc.html">gcc</a> is a C(and many other) compiler that comes with most *nix distributions.</li>
<h3>Macintosh</h3><li><a href="http://developer.apple.com/tools/mpw-tools/">Apple's own Macintosh Programmer's Workshop</a> is a compiler I've never used, but it is direct from apple and free. </li>
<li><a href="http://www.amazon.com/exec/obidos/ASIN/B00003IELF/lynnallain">Codewarrior </a> My experiences with Codewarrior are limited to Java programming, though it's gotten good reviews in the past. It's a full IDE rather than just a compiler, meaning that it has a text editor and debugger integrated with the compiler so you can do all your work from one place. </li>
</ul>To know about how to compile you can refer to their manual. GCC compilation process is given <a href="http://ubuntu50mg.blogspot.com/2010/01/blog-post.html">here</a><br />
</li><br />
</div>Shabab Haider Siddiquehttp://www.blogger.com/profile/14896191923433512555noreply@blogger.com0tag:blogger.com,1999:blog-2354853794677096092.post-83175828656342349132010-01-24T06:33:00.000-08:002010-01-24T06:33:40.517-08:00Assembly Language Part 3For 8086 programs to perform useful tasks, there must be a way to make decisions and repeat sections of the code. Inthis section we will discuss the the basic flow control instructions of MASM<br />
<br />
In assembly program flow is managed by jumping isntructions. There are 2 types of jump<br />
<br />
1) Unconditional<br />
The JMP instruction causes an unconditional transfer of control. The basic form is-<br />
JMP label<br />
<br />
2)Conditional<br />
The transfer of control depends on the previous instruction. It is often done by CMP. for example<br />
CMP operand1,operand2<br />
Jxx end<br />
<br />
Here the jump Jxx can be any of the following -<br />
<br />
<div style="background-color: #9fc5e8; text-align: center;"><br />
</div><table border="1" bordercolor="#c0c0c0" cellpadding="0" cellspacing="0" style="background-color: #9fc5e8; margin-left: auto; margin-right: auto; text-align: left;"><caption><span style="font-family: Arial; font-size: x-small;"><b>Jcc Instructions for Signed Comparisons</b></span></caption> <tbody>
<tr> <th align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">Instruction</span></b><br />
</th> <th align="center" bgcolor="#f0f0f0"><span style="font-family: Arial; font-size: x-small;">Description</span><br />
</th> <th align="center" bgcolor="#f0f0f0"><span style="font-family: Arial; font-size: x-small;">Condition</span><br />
</th> <th align="center" bgcolor="#f0f0f0"><span style="font-family: Arial; font-size: x-small;">Aliases</span><br />
</th> <th align="center" bgcolor="#f0f0f0"><span style="font-family: Arial; font-size: x-small;">Opposite</span><br />
</th> </tr>
<tr> <td align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">JG</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if greater (>)</span><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Sign = Ovrflw or Zero=0</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JNLE</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JNG</span><br />
</td> </tr>
<tr> <td align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">JNLE</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if not less than or equal (not <=)</span><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Sign = Ovrflw or Zero=0 </span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JG</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JLE</span><br />
</td> </tr>
<tr> <td align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">JGE</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if greater than or equal (>=)</span><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Sign = Ovrflw</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JNL</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JGE</span><br />
</td> </tr>
<tr> <td align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">JNL</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if not less than (not <)</span><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Sign = Ovrflw</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JGE</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JL</span><br />
</td> </tr>
<tr> <td align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">JL</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if less than (<)</span><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Sign Ovrflw</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JNGE</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JNL</span><br />
</td> </tr>
<tr> <td align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">JNGE</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if not greater or equal (not >=)</span><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Sign Ovrflw </span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JL</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JGE</span><br />
</td> </tr>
<tr> <td align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">JLE</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if less than or equal (<=)</span><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Sign Ovrflw or Zero = 1</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JNG</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JNLE</span><br />
</td> </tr>
<tr> <td align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">JNG</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if not greater than (not >)</span><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Sign Ovrflw or Zero = 1</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JLE</span><br />
</td> <td style="text-align: center;"><span style="font-family: Arial; font-size: x-small;">JG</span><br />
</td> </tr>
<tr> <td align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">JE</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if equal (=)</span><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Zero = 1</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JZ</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JNE</span><br />
</td> </tr>
<tr> <td align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">JNE</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if not equal ()</span><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Zero = 0</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JNZ</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JE</span><br />
</td></tr>
</tbody></table><span style="font-size: x-small;"><br />
</span><br />
<div align="center"><br />
<br />
<center> <br />
<br />
<table border="1" bordercolor="#c0c0c0" cellpadding="0" cellspacing="0"><caption><span style="font-size: x-small;"><b><span style="font-family: Arial;">Jcc Instructions That Test Flags</span></b></span></caption> <tbody style="background-color: #9fc5e8;">
<tr> <th align="center" bgcolor="#f0f0f0" style="background-color: #eeeeee;"><span style="font-family: Arial; font-size: x-small;">Instruction</span><br />
</th> <th align="center" bgcolor="#f0f0f0"><span style="font-family: Arial; font-size: x-small;">Description</span><br />
</th> <th align="center" bgcolor="#f0f0f0"><span style="font-family: Arial; font-size: x-small;">Condition</span><br />
</th> <th align="center" bgcolor="#f0f0f0"><span style="font-family: Arial; font-size: x-small;">Aliases</span><br />
</th> <th align="center" bgcolor="#f0f0f0"><span style="font-family: Arial; font-size: x-small;">Opposite</span><br />
</th> </tr>
<tr> <td align="center" style="background-color: #eeeeee;"><b><span style="font-family: Arial; font-size: x-small;">JC</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if carry</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">Carry = 1</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JB, JNAE</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JNC</span><br />
</td> </tr>
<tr> <td align="center" style="background-color: #eeeeee;"><b><span style="font-family: Arial; font-size: x-small;">JNC</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if no carry</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">Carry = 0</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JNB, JAE</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JC</span><br />
</td> </tr>
<tr> <td align="center" style="background-color: #eeeeee;"><b><span style="font-family: Arial; font-size: x-small;">JZ</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if zero</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">Zero = 1</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JE</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JNZ</span><br />
</td> </tr>
<tr> <td align="center" style="background-color: #eeeeee;"><b><span style="font-family: Arial; font-size: x-small;">JNZ</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if not zero</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">Zero = 0</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JNE</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JZ</span><br />
</td> </tr>
<tr> <td align="center" style="background-color: #eeeeee;"><b><span style="font-family: Arial; font-size: x-small;">JS</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if sign</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">Sign = 1</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">-</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JNS</span><br />
</td> </tr>
<tr> <td align="center" style="background-color: #eeeeee;"><b><span style="font-family: Arial; font-size: x-small;">JNS</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if no sign</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">Sign = 0</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">-</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JS</span><br />
</td> </tr>
<tr> <td align="center" style="background-color: #eeeeee;"><b><span style="font-family: Arial; font-size: x-small;">JO</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if overflow</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">Ovrflw=1</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">-</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JNO</span><br />
</td> </tr>
<tr> <td align="center" style="background-color: #eeeeee;"><b><span style="font-family: Arial; font-size: x-small;">JNO</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if no Ovrflw</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">Ovrflw=0</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">-</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JO</span><br />
</td> </tr>
<tr> <td align="center" style="background-color: #eeeeee;"><b><span style="font-family: Arial; font-size: x-small;">JP</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if parity</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">Parity = 1</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JPE</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JNP</span><br />
</td> </tr>
<tr> <td align="center" style="background-color: #eeeeee;"><b><span style="font-family: Arial; font-size: x-small;">JPE</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if parity even</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">Parity = 1</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JP</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JPO</span><br />
</td> </tr>
<tr> <td align="center" style="background-color: #eeeeee;"><b><span style="font-family: Arial; font-size: x-small;">JNP</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if no parity</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">Parity = 0</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JPO</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JP</span><br />
</td> </tr>
<tr> <td align="center" style="background-color: #eeeeee;"><b><span style="font-family: Arial; font-size: x-small;">JPO</span></b><br />
</td> <td><span style="font-family: Arial; font-size: x-small;">Jump if parity odd</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">Parity = 0</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JNP</span><br />
</td> <td align="center"><span style="font-family: Arial; font-size: x-small;">JPE</span><br />
</td> </tr>
</tbody></table><br />
<br />
</center><br />
</div><br />
<div align="center"><br />
<br />
<center> <br />
<br />
<table border="1" bordercolor="#c0c0c0" cellpadding="0" cellspacing="0"><caption><span style="font-family: Arial; font-size: x-small;"><b>Jcc Instructions for Unsigned Comparisons</b></span></caption> <tbody>
<tr> <th align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">Instruction</span></b><br />
</th> <th align="center" bgcolor="#f0f0f0"><span style="font-family: Arial; font-size: x-small;">Description</span><br />
</th> <th align="center" bgcolor="#f0f0f0"><span style="font-family: Arial; font-size: x-small;">Condition</span><br />
</th> <th align="center" bgcolor="#f0f0f0"><span style="font-family: Arial; font-size: x-small;">Aliases</span><br />
</th> <th align="center" bgcolor="#f0f0f0"><span style="font-family: Arial; font-size: x-small;">Opposite</span><br />
</th> </tr>
<tr> <td align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">JA</span></b><br />
</td> <td style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">Jump if above (>)</span><br />
</td> <td style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">Carry=0, Zero=0</span><br />
</td> <td align="center" style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">JNBE</span><br />
</td> <td align="center" style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">JNA</span><br />
</td> </tr>
<tr> <td align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">JNBE</span></b><br />
</td> <td style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">Jump if not below or equal (not <=)</span><br />
</td> <td style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">Carry=0, Zero=0</span><br />
</td> <td align="center" style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">JA</span><br />
</td> <td align="center" style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">JBE</span><br />
</td> </tr>
<tr> <td align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">JAE</span></b><br />
</td> <td style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">Jump if above or equal (>=)</span><br />
</td> <td style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">Carry = 0</span><br />
</td> <td align="center" style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">JNC, JNB</span><br />
</td> <td align="center" style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">JNAE</span><br />
</td> </tr>
<tr> <td align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">JNB</span></b><br />
</td> <td style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">Jump if not below (not <)</span><br />
</td> <td style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">Carry = 0</span><br />
</td> <td align="center" style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">JNC, JAE</span><br />
</td> <td align="center" style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">JB</span><br />
</td> </tr>
<tr> <td align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">JB</span></b><br />
</td> <td style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">Jump if below (<)</span><br />
</td> <td style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">Carry = 1</span><br />
</td> <td align="center" style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">JC, JNAE</span><br />
</td> <td align="center" style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">JNB</span><br />
</td> </tr>
<tr> <td align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">JNAE</span></b><br />
</td> <td style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">Jump if not above or equal (not >=)</span><br />
</td> <td style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">Carry = 1</span><br />
</td> <td align="center" style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">JC, JB</span><br />
</td> <td align="center" style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">JAE</span><br />
</td> </tr>
<tr> <td align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">JBE</span></b><br />
</td> <td style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">Jump if below or equal (<=)</span><br />
</td> <td style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">Carry = 1 or Zero = 1</span><br />
</td> <td align="center" style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">JNA</span><br />
</td> <td align="center" style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">JNBE</span><br />
</td> </tr>
<tr> <td align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">JNA</span></b><br />
</td> <td style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">Jump if not above (not >)</span><br />
</td> <td style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">Carry = 1 or Zero = 1</span><br />
</td> <td align="center" style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">JBE</span><br />
</td> <td align="center" style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">JA</span><br />
</td> </tr>
<tr> <td align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">JE</span></b><br />
</td> <td style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">Jump if equal (=)</span><br />
</td> <td style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">Zero = 1</span><br />
</td> <td align="center" style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">JZ</span><br />
</td> <td align="center" style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">JNE</span><br />
</td> </tr>
<tr> <td align="center" bgcolor="#f0f0f0"><b><span style="font-family: Arial; font-size: x-small;">JNE</span></b><br />
</td> <td style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">Jump if not equal ()</span><br />
</td> <td style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">Zero = 0</span><br />
</td> <td align="center" style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">JNZ</span><br />
</td> <td align="center" style="background-color: #9fc5e8;"><span style="font-family: Arial; font-size: x-small;">JE</span><br />
</td> </tr>
</tbody></table><br />
<br />
</center><br />
</div><br />
As we have seen earlier that the conditional jump isntruction is often followed by the CMP instruction. Wnen CPU executes the CMP instruction it compares the first operand with the second operand. Then if the condition described by the following jump instruction is true - a change of flow control is occured. You may want to see the example -<br />
<br />
CMP AX,BX<br />
JL axislower ; If AX is lower than BX then control transferred to axislower<br />
<br />
<span style="font-size: large;">The TEST instruction</span><br />
The TEST instruction performs an AND operation, but does not store<br />
the result. It only sets the FLAGS register based on what the result would<br />
be (much like how the CMP instruction performs a subtraction but only sets<br />
FLAGS).<br />
<br />
TEST DESTINATION, SOURCE<br />
<br />
<br />
Effect on flags -<br />
<div style="text-align: center;">SF,ZF, PF - Reflect the result<br />
</div><div style="text-align: center;">AF - Undefined<br />
</div><div style="text-align: center;">CF,OF - 0 <br />
</div><br />
The test instruction can be used to examine individual bits in a operand. For that case the mask(source) should contain 1s in the desired bit positions and 0s in the rest. See the pseudocode and MASM code -<br />
<br />
IF AL is EVEN<br />
THEN JUMP TO END_X<br />
<br />
MASM code-<br />
<br />
TEST AL,1<br />
JZ END_X<br />
<br />
END_X:<br />
<br />
<br />
If we wish to check the 1st bit we TEST it by 1. (here the mask is 1 i.e = 0000 0001 in binary. And we know that an even number shall have 1 in the 1st bit position.)<br />
The and product should be zero if 1st bit is 0, the AL would then remain same but the ZF (zero flag = 0 if result is zero) is 0. So the JZ instruction shall be executed.<br />
<br />
Further reading:<br />
The art of Assembly Language URL: http://www.arl.wustl.edu/<br />
<br />
The Intel Microprocessor Architecture, Programming & Interfacing<br />
by Barry B. BreyShabab Haider Siddiquehttp://www.blogger.com/profile/14896191923433512555noreply@blogger.com0tag:blogger.com,1999:blog-2354853794677096092.post-15199602425566665212010-01-24T06:32:00.001-08:002010-01-24T06:32:31.271-08:00Assembly Language Part 4Conditional Branching in MASM<br />
We have shown that the jump instructions can be used to implement branches and looops. However these instructions are difficult for beginners to apply as a substitute of high level branching statements.<br />
<br />
In high level languages we use these basic structures -<br />
<br />
<span style="font-size: large;">IF-THEN</span><br />
The if-then structure is the most common type of branching statement used. The pseudocode for IF-THEN is :<br />
<br />
<br />
IF CONDITION IS TRUE<br />
THEN EXECUTE TRUE BRANCH STATEMENTS<br />
END_IF<br />
<br />
MASM example:<br />
Suppose we want to code the following pseudocode in MASM<br />
<br />
IF AX< 0<br />
THEN REPLACE AX WITH ITS COMPLIMENTED VALUE<br />
END_IF<br />
<br />
The MASM code will be:<br />
<br />
CMP AX,0<br />
JNL END_IF<br />
NEG AX<br />
<br />
END_IF:<br />
<br />
<span style="font-size: large;">IF-THEN-ELSE</span><br />
The if-then-else structure is the expansion of most common type of branching statement IF-THEN. The pseudocode for IF-THEN-ELSE is :<br />
<br />
IF Condition is TRUE<br />
THEN<br />
Execute true-branch statements<br />
ELSE<br />
Execute false-branch statements<br />
END_IF<br />
<br />
Lets explain by applying the following pseudocode in MASM<br />
<br />
IF AL<= BL<br />
THEN<br />
DISPLAY THE CHARACTER IN AL<br />
ELSE<br />
DISPLAY THE CHARACTER IN BL<br />
END_IF<br />
<br />
The MASM code will be -<br />
<br />
MOV AH,2<br />
CMP AL,BL<br />
JNBE ELSE_<br />
MOV DL,AL<br />
JMP DISPLAY_<br />
ELSE_:<br />
MOV DL,BL<br />
DISPLAY_:<br />
INT 21H<br />
<br />
END_IF:<br />
<br />
<br />
<span style="font-size: large;">CASE</span><br />
The case in high-level language is a multiway branch structure that tests a register, variable or expression for particular values or range of values. The pseudocode is :<br />
<br />
CASE expression<br />
value_1: statement_1<br />
value_2: statement_2<br />
value_3: statement_3<br />
value_4: statement_4<br />
.................................<br />
value_n: statement_n<br />
END_CASE<br />
<br />
Consider the following pseudocode in MASM<br />
<br />
IF AX a negative number<br />
THEN put -1 in BX<br />
IF AX contains 0<br />
THEN put 0 in BX<br />
IF AX contains a positive number<br />
THEN put 1 in BX<br />
<br />
The MASM code will be -<br />
<br />
CMP AX,0<br />
JL NEGATIVE_<br />
JG POSITIVE<br />
<br />
MOV BX,0<br />
JMP END_CASE<br />
<br />
NEGATIVE_:<br />
MOV BX,-1<br />
JMP END_CASE<br />
<br />
POSITIVE_:<br />
MOV BX,1<br />
JMP END_CASE<br />
<br />
END_CASE:<br />
<br />
<br />
further reading:<br />
http://www.cs.princeton.edu/<br />
<br />
Assembly Language Programming and Organization of IBM PC<br />
Ytha Yu<br />
Charles MarutShabab Haider Siddiquehttp://www.blogger.com/profile/14896191923433512555noreply@blogger.com1tag:blogger.com,1999:blog-2354853794677096092.post-67859905640570378492010-01-24T06:31:00.000-08:002010-01-24T06:31:39.807-08:00Assembly Language Part 5Looping instructions in Assembly<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="http://t1.gstatic.com/images?q=tbn:1cWBKKSiHY7swM:http://www.artestuff.com/images/perlon-cord-loop.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://t1.gstatic.com/images?q=tbn:1cWBKKSiHY7swM:http://www.artestuff.com/images/perlon-cord-loop.jpg" /></a><br />
</div><br />
<div style="text-align: left;">What comes to your mind after watching the loop at the left? That's it the Head is Touching the Tail at some point. <br />
</div><div style="text-align: left;"><br />
</div>In assembly a loop is a sequence of instructions that is repeated like the loop to the left.<br />
The final instruction (Tail) jumps (touches) to the first instruction (Head).<br />
<br />
The basic form of looping in Assembly is<br />
<br />
<span style="font-size: large;">REPEAT UNTIL</span> <br />
REPEAT<br />
STATEMENTS<br />
UNTIL CONDITION<br />
<br />
For example if we want to read all input characters given by the user until he/she inputs <space></space><br />
<br />
MOV AH,1<br />
REPEAT_:<br />
INT 21H<br />
CMP AL,' '<br />
JNE REPEAT_ <br />
<br />
Some other frequently used looping structures are-<br />
<span style="font-size: large;"><br />
</span><br />
<span style="font-size: large;">FOR LOOP</span><br />
In these kind of lop structures the instructions are repeated for known number of times. pseudocode -<br />
<br />
FOR loop_count times DO<br />
statements<br />
END_FOR<br />
<br />
In assembly we use the instruction LOOP to implement for loop. The general form is-<br />
<br />
LOOP header_label<br />
<br />
Here header label is the name or label of the instruction block to be executed in the loop. The LOOP instruction uses CX as the counter so it has to be defined first. When the program executes LOOP it first checks if CX = 0 if true it continues to execute the next instructions. If the CX !=0 then control is transferred to the "header_label" , at the same time it also decreases the value of CX by 1. <br />
<br />
Let us implement it in the program to print 100 '*' in assembly.<br />
<br />
MOV CX,80<br />
MOV AH,2<br />
MOV DL,'*'<br />
<br />
TOP: ; header_label<br />
INT 21H<br />
LOOP TOP <br />
<br />
<br />
<br />
<span style="font-size: large;">WHILE LOOP</span><br />
These kind of loops depends on a condition. pseudocode -<br />
<br />
<br />
WHILE condition DO<br />
statements<br />
END_WHILE<br />
<br />
The condition is checked at the top of the loop. If the condition is satisfied the the statements are executed. If false the program skips to whatever follows. It is possible that the condition is never satisfied (even at the initial stage) in that case the body is never executed.<br />
<br />
Lets count the number of character in the input line.<br />
<br />
MOV DX,0<br />
MOV AH,1<br />
INT 21H<br />
<br />
WHILE_:<br />
CMP AL,13<br />
JE END_WHILE <br />
INC DX<br />
INT 21H<br />
JMP WHILE_<br />
<br />
END_WHILE:<br />
<br />
<br />
The basic difference between WHILE and REPEAT loop is that using WHILE loop gives opportunity to skip the whole process even at the initial stage if the condition is false.<br />
On the other hand the statements of REPEAT has to be done atleast once. REPEAT is useful on other cases as it leads to shorter coding.<br />
<br />
Further reading:<br />
Assembly Language Programming and Organization of IBM PC<br />
Ytha Yu<br />
Charles Marut<br />
http://www.cs.princeton.edu/Shabab Haider Siddiquehttp://www.blogger.com/profile/14896191923433512555noreply@blogger.com0tag:blogger.com,1999:blog-2354853794677096092.post-25251336146414995432010-01-24T06:30:00.000-08:002010-01-24T06:30:16.109-08:00Assembly Language Part 6Logic Instructions<br />
<br />
The ability to manipulate the individual bits of a Data is one of the key advantages of assembly language. In order to do so we use Logic instructions. Let us first remind the basic ligic instructions for now,<br />
<br />
<br />
<div style="color: #3d85c6;"><br />
</div><div style="background-color: #3d85c6; color: #cfe2f3; text-align: center;"><b>AND </b><br />
</div><div style="background-color: #3d85c6; color: #cfe2f3; text-align: center;"><b> 0 0 -> 0</b><br />
</div><div style="background-color: #3d85c6; color: #cfe2f3; text-align: center;"><b> 1 0 -> 0</b><br />
</div><div style="background-color: #3d85c6; color: #cfe2f3; text-align: center;"><b> 0 1 -> 0</b><br />
</div><div style="background-color: #3d85c6; color: #cfe2f3; text-align: center;"><b> 1 1 -> 1</b><br />
</div><div style="color: #0b5394; text-align: center;"><br />
</div><div style="background-color: #3d85c6; color: #cfe2f3; text-align: center;"><b>OR </b><br />
</div><div style="background-color: #3d85c6; color: #cfe2f3; text-align: center;"><b> 0 0 -> 0</b><br />
</div><div style="background-color: #3d85c6; color: #cfe2f3; text-align: center;"><b> 1 0 -> 1</b><br />
</div><div style="background-color: #3d85c6; color: #cfe2f3; text-align: center;"><b> 0 1 -> 1</b><br />
</div><div style="background-color: #3d85c6; color: #cfe2f3; text-align: center;"><b> 1 1 -> 1</b> <br />
<br />
</div><div style="color: #0b5394; text-align: center;"><br />
</div><div style="color: #0b5394; text-align: center;"> <br />
</div><div style="background-color: #3d85c6; color: #cfe2f3; text-align: center;"><b>XOR </b><br />
</div><div style="background-color: #3d85c6; color: #cfe2f3; text-align: center;"><b> 0 0 -> 0</b><br />
</div><div style="background-color: #3d85c6; color: #cfe2f3; text-align: center;"><b> 1 0 -> 1</b><br />
</div><div style="background-color: #3d85c6; color: #cfe2f3; text-align: center;"><b> 0 1 -> 1</b><br />
</div><div style="background-color: #3d85c6; color: #cfe2f3; text-align: center;"><b> 1 1 -> 0</b><br />
</div><div style="color: #3d85c6; text-align: center;"><br />
</div><div style="color: #3d85c6; text-align: center;"> <br />
</div><div style="background-color: #3d85c6; color: #cfe2f3; text-align: center;"><b>NOT <br />
</b><br />
</div><div style="background-color: #3d85c6; color: #cfe2f3; text-align: center;"><b> 0 -> 1</b><br />
</div><div style="background-color: #3d85c6; color: #cfe2f3; text-align: center;"><b> 1 -> 0</b><br />
</div><div style="color: #0b5394; text-align: center;"><b> <br />
</b><br />
</div><div style="text-align: left;">Now lets see where we use these functions in Assembly -<br />
</div><div style="text-align: left;"><br />
</div><div style="color: #3d85c6; text-align: left;"><b>AND -> To clear<br />
OR -> To set<br />
</b><b>XOR -> To compliment desired bit positions</b><br />
<b>NOT -> To compliment all bits</b><br />
</div><div style="color: #3d85c6; text-align: left;"><br />
</div><div style="color: black; text-align: left;"><span style="font-size: large;">AND</span><b> <span style="color: #0b5394;">(Used to Clear )</span><br style="color: #0b5394;" /></b><br />
</div><div style="color: black; text-align: left;">The form is<br />
</div><div style="color: black; text-align: left;">AND DESTINATION, SOURCE<br />
</div><div style="color: black; text-align: left;"><br />
Suppose we want to clear the sign bit of AL then the code will be<br />
</div><div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;">AND AL,7Fh ;<br />
</div><div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;">we know that the sign bit is the MSB so if we AND it with the value which has all 1s except of the MSB as 0 then<br />
</div><div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;">AL = 10101100<br />
</div><div style="color: black; text-align: left;">Msk = 01111111<br />
</div><div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;">-----------------------------<br />
</div><div style="color: black; text-align: left;">rslt = 00101100<br />
</div><div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;"><span style="font-size: large;">OR</span><b> <span style="color: #0b5394;">(used to set)</span><br style="color: #0b5394;" /> </b><br />
</div><div style="color: black; text-align: left;">The form is<br />
</div><div style="color: black; text-align: left;">OR DESTINATION, SOURCE<br />
</div><div style="color: black; text-align: left;"><br />
Suppose we want to set the sign bit of AL then the code will be<br />
</div><div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;">OR AL,80h ;<br />
</div><div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;">we know that the sign bit is the MSB so if we OR it with the value which has all 0s except of the MSB as 1 then<br />
</div><div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;">AL = 00101100<br />
</div><div style="color: black; text-align: left;">Msk = 10000000<br />
</div><div style="color: black; text-align: left;">-----------------------------<br />
</div><div style="color: black; text-align: left;">rslt = 10101100<br />
</div><div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;"><span style="font-size: large;">XOR</span><b> <span style="color: #0b5394;">(Used to compliment selected bits )</span><br style="color: #0b5394;" /> </b><br />
</div><div style="color: black; text-align: left;">The form is<br />
</div><div style="color: black; text-align: left;">XOR DESTINATION, SOURCE<br />
</div><div style="color: black; text-align: left;"><br />
Suppose we want to change the sign bit of AL then the code will be<br />
</div><div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;">XOR AL,80h ;<br />
</div><div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;">we know that the sign bit is the MSB so if we XOR it with the value which has all 0s except of the MSB which is 1 then<br />
</div><div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;">AL = 10101100<br />
</div><div style="color: black; text-align: left;">Msk = 10000000<br />
</div><div style="color: black; text-align: left;">-----------------------------<br />
</div><div style="color: black; text-align: left;">rslt = 00101100<br />
</div><div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;">Here you may have noticed that destination bit is complimented if that bit position of the Mask is 1 and unchanged if it is 0. (XOR x , 0 - x) (XOR x,1)= x'<br />
</div><div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;"><span style="font-size: large;">NOT</span><b> <span style="color: #0b5394;">(Used to compliment all bits )</span><br style="color: #0b5394;" /> </b><br />
</div><div style="color: black; text-align: left;">The form is<br />
</div><div style="color: black; text-align: left;">NOT DESTINATION<br />
</div><div style="color: black; text-align: left;"><br />
Suppose we want to invert all the bits of AL then the code will be<br />
</div><div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;">NOT AL ;<br />
</div><div style="color: black; text-align: left;"><br />
</div>You can see that NOT takes only one argument.<br />
<div style="color: black; text-align: left;"><br />
</div>Suppose AL = 10110010 , after executing the NOT instruction it will be 01001101<br />
<div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;"><br />
</div><div style="color: black; text-align: left;"><span style="font-size: large;">Problem</span><b> <span style="color: #0b5394;">(Convert ASCII value to number)</span></b><br />
</div>When we enter a character in Assembly the equivalent ASCII value is stored in the memory. Say we pressed 3 then the ASCII value 33h will be saved in AL. we have to convert it to 3...<br />
<br />
If we AND a value with 0000 1111 then the relust will be 30 less than the original value<br />
<br />
AL = 0011 0011 =33<br />
Msk = 0000 1111 <br />
------------------------------<br />
0000 1111 =3<br />
<br />
so the code will be<br />
<br />
AND AL, 0FhShabab Haider Siddiquehttp://www.blogger.com/profile/14896191923433512555noreply@blogger.com0tag:blogger.com,1999:blog-2354853794677096092.post-91149955725073395702010-01-24T06:29:00.000-08:002010-01-24T06:29:08.248-08:00Assembly Language Part 7Shift Instructions in Assembly<br />
<br />
The shift and rotate instructions shift the bits of the operand by one or more bit positions. The shifting can be either left or right. The basic shift instructions are -<br />
<br />
<span style="font-size: large;">SHL <b style="color: #3d85c6;"><span style="font-size: small;">(shift left)</span></b><br />
</span><br />
The SHL instruction shifts the bit of the destination to the left. The format for calling the SHL is-<br />
<br />
SHL destination,steps<br />
<br />
<br />
Applying this the MSB is shifted to the carry flag and LSB is filled with 0 from right. The bits of the middle is shifted to 1 bit left when steps are 1<br />
<br />
<br />
CF <- [<-][<-][<-][<-][<-][<-][<-][<-] <- 0<br />
7 6 5 4 3 2 1 0<br />
<br />
Suppose DH contains 8Ah and CL contains 3. Then the value of DL after executing SHL DH, CL <br />
<br />
<br />
<br />
[1 ][0 ][0 ][0 ][1 ][0 ][1 ][0 ] <br />
7 6 5 4 3 2 1 0<br />
<br />
after first shift<br />
<br />
CF [ 1 ] <- [0 ][0 ][0 ][1 ][0 ][1 ][0 ][ 0 ] <- 0 <br />
7 6 5 4 3 2 1 0<br />
<br />
2nd shift<br />
<br />
CF [ 0 ] <- [0 ][ 0 ][ 1 ][ 0 ][ 1 ][ 0 ][ 0 ][0 ] <- 0 <br />
7 6 5 4 3 2 1 0<br />
<br />
After 3rd and the final shift<br />
CF [ 0 ] <- [0 ][ 1 ][ 0 ][ 1 ][ 0 ][ 0 ][ 0 ][ 0 ] <- 0 <br />
7 6 5 4 3 2 1 0<br />
<br />
<br />
<br />
<span style="font-size: large;">SHR <b style="color: #3d85c6;"><span style="font-size: small;">(shift right)</span></b><br />
</span><br />
The SHR works like the reverse function of SHL. It shifts the bits of the destination operand to the right. The format is<br />
<br />
SHR destination, steps<br />
<br />
If we apply SHR the MSB is filled with 0 and the LSB is moved to CF and the bits between are mvoved to the same direction.<br />
<br />
0 -> [->][->][->][->][->][->][->][->] -> CF<br />
7 6 5 4 3 2 1 0<br />
<br />
<br />
Suppose DH contains 8Ah and CL contains 3. Then the value of DL after executing SHR DH, CLworks as follow -<br />
<br />
<br />
[1 ][0 ][0 ][0 ][1 ][0 ][1 ][0 ] <br />
7 6 5 4 3 2 1 0<br />
<br />
after first shift<br />
<br />
0 -> [ 0 ][ 1 ][ 0 ][ 0 ][ 0 ][ 1 ][ 0 ][ 1 ] -> [0] CF <br />
7 6 5 4 3 2 1 0<br />
<br />
then the 2nd<br />
0 -> [ 0 ][ 0 ][ 1 ][ 0 ][ 0 ][ 0 ][ 1 ][ 0 ] -> [1]CF <br />
7 6 5 4 3 2 1 0<br />
<br />
<br />
Finally the 3rd<br />
<br />
0 -> [ 0 ][0 ][0 ][1 ][0 ][0 ][0 ][ 1 ] -> [0]CF <br />
7 6 5 4 3 2 1 0<br />
<br />
<br />
<span style="font-size: large;">SAR <b style="color: #3d85c6;"><span style="font-size: small;">(shift arithmetic right)</span></b><br />
</span><br />
The only difference between SHR and SAR is that apllying SAR the MSB retains the original value. <br />
<br />
SAR destination, steps<br />
<br />
<br />
If we apply SAR the MSB is unchaged and it is also copied to the next bit to the right of MSB, the LSB is moved to CF and the bits between are mvoved to the<br />
same direction.<br />
<br />
<br />
[^>][->][->][->][->][->][->][->] -> CF<br />
7 6 5 4 3 2 1 0<br />
<br />
<br />
Suppose DH contains 8Ah and CL contains 3. Then the value of DL after executing SHR DH, CLworks as follow -<br />
<br />
<br />
<br />
[1 ][0 ][0 ][0 ][1 ][0 ][1 ][0 ] <br />
7 6 5 4 3 2 1 0<br />
<br />
after first shift<br />
<br />
[ 1 ][ 1 ][ 0 ][ 0 ][ 0 ][ 1 ][ 0 ][ 1 ] -> [0] CF <br />
7 6 5 4 3 2 1 0<br />
<br />
then the 2nd<br />
[ 1 ][ 1 ][ 1 ][ 0 ][ 0 ][ 0 ][ 1 ][ 0 ] -> [1]CF <br />
7 6 5 4 3 2 1 0<br />
<br />
<br />
<br />
Finally the 3rd<br />
<br />
0 -> [ 1 ][ 1 ][ 1 ][ 1 ][ 0 ][ 0 ][ 0 ][ 1 ] -> [0]CF <br />
7 6 5 4 3 2 1 0<br />
<br />
These shifting instructions are handy for multiplication and division. As you may notice that the difference of subsequent binary digits change as if they are multuplied by 2<br />
16 8 4 2 1 . . . .<br />
So if we shift the bits to the right it is simply multiplied by 2 !<br />
<br />
0 1 1 1 b = 7d<br />
<br />
after 1 SHL<br />
<br />
1 1 1 0 b = 14d !<br />
<br />
<br />
Similarly shifting the bits right divides them by 2<br />
<br />
<br />
<br />
0 1 0 0 b = 4d<br />
<br />
after 1 SHR<br />
<br />
<br />
0 0 1 0 b = 2d !<br />
<br />
<br />
so if we want to divide the binary number by 4 all we need to do is to shift it right 2 times.<br />
<br />
<br />
<span style="font-size: large;">ROR and ROL <b style="color: #3d85c6;"><span style="font-size: small;">(rotate right and rotate left)</span></b><br />
</span><br />
The ROR and ROL not only shift the bits to certain direction but also move the LSB to MSB or MSB to LSB<br />
<br />
<div style="text-align: center;"><br />
</div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiC8k5y3z9Tkq8I6befrcYSfhRikfhHTGjOEuUS3-dZtIx-3xIpM5DOHYh80ikACxvnoxSLefV_VBqP6DUjCJDppKEvtiKDnTF-_PAZ-9mtMIdw6hSRWYH2j5xK_sOnwwv4I6RaUnscuzqd/s1600-h/11.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiC8k5y3z9Tkq8I6befrcYSfhRikfhHTGjOEuUS3-dZtIx-3xIpM5DOHYh80ikACxvnoxSLefV_VBqP6DUjCJDppKEvtiKDnTF-_PAZ-9mtMIdw6hSRWYH2j5xK_sOnwwv4I6RaUnscuzqd/s320/11.gif" /></a><br />
</div><div class="separator" style="clear: both; text-align: center;">ROR <br />
</div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1_8KiloYyT_iUdFHwfxsywPMRAAUaqQRxORYR3W1RA7KoiSehNPrM6k7BIvzH0H-36wIZ7xCNTO0pLcYPE9izeWHXkasKlFs5XK4NrLYKtpe0U1oXlOh2dpkuZLTVvAai0qQCixMaMN6R/s1600-h/12.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1_8KiloYyT_iUdFHwfxsywPMRAAUaqQRxORYR3W1RA7KoiSehNPrM6k7BIvzH0H-36wIZ7xCNTO0pLcYPE9izeWHXkasKlFs5XK4NrLYKtpe0U1oXlOh2dpkuZLTVvAai0qQCixMaMN6R/s320/12.gif" /></a><br />
</div><div style="text-align: center;"> ROL<br />
</div><div style="text-align: center;"><br />
</div><div style="text-align: left;">As you can see that there is a loop. For the case of ROR the LSB is also copied to CF. and for the case of ROL the MSB is copied to CF<br />
</div><div style="text-align: left;"><br />
</div><div style="text-align: left;"><br />
</div><span style="font-size: large;">RCR and RCL <b style="color: #3d85c6;"><span style="font-size: small;">(rotate right /left through carry )</span></b><br />
</span><br />
In the case of rotate throug carry the loop is also gone through CF. i.e for the case of RCL the MSB is moved to CF, the CF is moved to LSB and LSB is moved to LSB +1 , and so on <br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsbfN09vggKDytOsn-RYZqLo7S3a5RRddXXO1ZsIiDiGWWNEvetotdOkHlbnF10nsp2NCu7RT1kqt-83gLqNaG4kP3z_E6Olnh2HYgKkxgMLxnPWQe-qjxUmhpO9K6aHD6aleQlMYeS1WW/s1600-h/94.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsbfN09vggKDytOsn-RYZqLo7S3a5RRddXXO1ZsIiDiGWWNEvetotdOkHlbnF10nsp2NCu7RT1kqt-83gLqNaG4kP3z_E6Olnh2HYgKkxgMLxnPWQe-qjxUmhpO9K6aHD6aleQlMYeS1WW/s320/94.gif" /></a><br />
</div><div class="separator" style="clear: both; text-align: center;"> RCL<br />
</div><div class="separator" style="clear: both; text-align: center;"><br />
</div><div class="separator" style="clear: both; text-align: center;"><br />
</div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgoG4YA52v6Pu43by0uE5hBRIaTKz1JdwKdz7F-Cejsai8aifNTuacicBEFd-b3QphbyiXy62V-jKeEM8L1EOFi0AVEWnV58jJ1BwIY8a3Kmkxhs5qUiZoaftQ9jiuxWRFde84884UyYg8l/s1600-h/97.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgoG4YA52v6Pu43by0uE5hBRIaTKz1JdwKdz7F-Cejsai8aifNTuacicBEFd-b3QphbyiXy62V-jKeEM8L1EOFi0AVEWnV58jJ1BwIY8a3Kmkxhs5qUiZoaftQ9jiuxWRFde84884UyYg8l/s320/97.gif" /></a><br />
</div><div style="text-align: center;">RCR<br />
</div><div style="text-align: center;"><br />
</div><div style="text-align: center;"><br />
</div><div style="text-align: left;">Further reading :<br />
</div>The Intel Microprocessor Architecture, Programming and Interfacing<br />
Barry B. BreyShabab Haider Siddiquehttp://www.blogger.com/profile/14896191923433512555noreply@blogger.com0tag:blogger.com,1999:blog-2354853794677096092.post-78992780893385680902010-01-24T06:28:00.000-08:002010-01-24T06:28:06.255-08:00Assembly Language Part 8<h3 class="post-title entry-title"> </h3><div class="post-body entry-content"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifNRP4VRTBph-ZzJfMKQtHAKJ0tQAdWvutixAoMM_bprWxBG-Z6JebK2Sc2aS6m8sUCW732hd_bDXbIzl7bFPYp_skEIiqO4VkY1Z7VKtOIrVw20uk8jsO6iQn1-In7gZwOJgrcWgifbKv/s1600-h/shay_alkalay_stack_1800mm_o.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifNRP4VRTBph-ZzJfMKQtHAKJ0tQAdWvutixAoMM_bprWxBG-Z6JebK2Sc2aS6m8sUCW732hd_bDXbIzl7bFPYp_skEIiqO4VkY1Z7VKtOIrVw20uk8jsO6iQn1-In7gZwOJgrcWgifbKv/s320/shay_alkalay_stack_1800mm_o.jpg" /></a>Stack operations in Assembly<br />
<br />
A stack is a one dimensional data structure. The elements of the stack is added and removed from one end. That is why it is also known as <br />
Last-In-First-Out or LIFO system. That end of the stack is called TOP.<br />
<br />
You can have the Idea by watching the stack of papers to the right. It is only possible to add or remove new paper holders from the TOP.<br />
<br />
The add operation on a stack is called push and the remove operation on the stack is called pop.<br />
<br />
As we mentioned earlier the operations are only done at the top.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhe2tlHVsI8sPPSxJEBFpxBzvnv7sL32sHB9JoL-P_H5inhem2N6nW9fan5MlDC30b84cVJJ1zcUvJuvTwHuMC4lgVJ6nT7E7i3nuueDJPIBNZOxySotsNEwNuRF-4xktWtA7n4NcRou4oh/s1600-h/Stack.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhe2tlHVsI8sPPSxJEBFpxBzvnv7sL32sHB9JoL-P_H5inhem2N6nW9fan5MlDC30b84cVJJ1zcUvJuvTwHuMC4lgVJ6nT7E7i3nuueDJPIBNZOxySotsNEwNuRF-4xktWtA7n4NcRou4oh/s320/Stack.png" /></a><br />
</div><div class="separator" style="clear: both; text-align: center;"><br />
</div><div class="separator" style="clear: both; text-align: center;">Stack Operations Push and Pop<br />
</div><div class="separator" style="clear: both; text-align: center;"> <br />
</div><span style="font-size: large;">PUSH </span><b><span style="color: #3d85c6; font-size: small;">( add element to top)</span></b><br />
The push instruction adds a new element to the top. The syntax is<br />
<br />
PUSH source ; no destination defined because destination is always TOP<br />
<br />
A few things to notice is that the PUSH operation works as follows<br />
<div style="color: white;"><span style="background-color: #3d85c6;"> 1) A copy of the source content is copied to the address specified by the TOP <br />
</span><br />
</div><div style="background-color: #3d85c6;"><span style="color: white;"> 2) SP or TOP is<b><span style="color: #cc0000;"> <span style="color: white;">decreased </span></span></b></span><br />
</div><br />
<div class="separator" style="clear: both; text-align: center;"><br />
</div><div class="separator" style="clear: both; text-align: center;"><br />
</div><div class="separator" style="clear: both; text-align: center;"><br />
</div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFaSZ9jaJ882sosY16TFY-mu0J-1zkM0Zze1Kr3dkKPNf5l4UKeroA9XYL7xc9wDbVhPgn2yGSJxWBs44kaQlgrnj2e-zABe5J26HaY-523SDScCsN53uKECZdSVHwsrQgMyRP6uZHNjl7/s1600-h/Lifo.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFaSZ9jaJ882sosY16TFY-mu0J-1zkM0Zze1Kr3dkKPNf5l4UKeroA9XYL7xc9wDbVhPgn2yGSJxWBs44kaQlgrnj2e-zABe5J26HaY-523SDScCsN53uKECZdSVHwsrQgMyRP6uZHNjl7/s320/Lifo.png" /></a><br />
</div> Push Pop<br />
<br />
<span style="font-size: large;">POP </span><b><span style="color: #3d85c6; font-size: small;">( remove element to top)</span></b><br />
The pop instruction removes the top element. The syntax is<br />
<br />
<br />
POP destination ; no source defined because source is always TOP<br />
<br />
<br />
<br />
A few things to notice is that the PUSH operation works as follows<br />
<div style="background-color: #3d85c6; color: white;"> 1) SP or TOP is<b> increased</b> <br />
<br />
</div><div style="background-color: #3d85c6; color: white;"> 2) The content of the TOP is copied to the destination <br />
</div><br />
<br />
The PUSH and the POP instructions take 16 bit registers only. So<br />
<div style="color: #cc0000;">PUSH DL<br />
</div><div style="color: #cc0000;">and<br />
</div><div style="color: #cc0000;">PUSH 3<br />
</div>are both illegal<br />
<br />
<span style="font-size: large;">PUSHF </span><b><span style="color: #3d85c6; font-size: small;">( a variation of PUSH)</span></b><br />
There are no operand of PUSHF. It just copies the FLAGS register to the stack.<br />
<br />
<span style="font-size: large;">POPF </span><b><span style="color: #3d85c6; font-size: small;">( a variation of POP)</span></b><br />
There are no operand of POPF either , it does the reverse of PUSHF. Executing it restores the FLAGS register from the stack TOP.<br />
<br />
<span style="font-size: large;">Example </span><b><span style="color: #3d85c6; font-size: small;">( Reversing a line of text )</span></b><br />
The program shall take one character at a time and PUSH it to the stack until its a carriage return. When "Enter" is pressed the characters are POPed out and printed.<br />
<b><span style="color: #3d85c6; font-size: small;"> <br />
</span></b><br />
<b><span style="color: #3d85c6; font-size: small;">CODE:</span></b><br />
<b style="font-family: "Trebuchet MS",sans-serif;"><br style="color: #3d85c6;" /></b><div style="color: #3d85c6; font-family: "Trebuchet MS",sans-serif;"><b>.MODEL SMALL<br />
.STACK 100H<br />
.CODE<br />
MAIN PROC<br />
<br />
MOV AH, 2<br />
MOV DL, ‘?’<br />
INT 21H<br />
<br />
XOR CX, CX<br />
<br />
MOV AH, 1<br />
INT 21H</b><br />
</div><div style="color: #3d85c6; font-family: "Trebuchet MS",sans-serif;"><br />
</div><div style="color: #3d85c6; font-family: "Trebuchet MS",sans-serif;"><b>WHILE:<br />
CMP AL, 0DH<br />
JE END_WHILE<br />
PUSH AX<br />
INC CX<br />
INT 21H<br />
JMP WHILE<br />
</b><br />
</div><div style="color: #3d85c6; font-family: "Trebuchet MS",sans-serif;"><b>END_WHILE:<br />
</b><br />
</div><div style="color: #3d85c6; font-family: "Trebuchet MS",sans-serif;"><b> MOV AH, 2<br />
MOV DL, 0DH<br />
INT 21H<br />
MOV DL, 0AH<br />
INT 21H<br />
JCXZ EXIT</b><br />
</div><div style="color: #3d85c6; font-family: "Trebuchet MS",sans-serif;"><br />
</div><b style="font-family: "Trebuchet MS",sans-serif;"><span style="color: #3d85c6;">TOP: </span><br style="color: #3d85c6;" /><span style="color: #3d85c6;"> POP DX</span><br style="color: #3d85c6;" /><span style="color: #3d85c6;"> INT 21H</span><br style="color: #3d85c6;" /><span style="color: #3d85c6;"> LOOP TOP</span><br style="color: #3d85c6;" /><br style="color: #3d85c6;" /><span style="color: #3d85c6;">EXIT:</span><br style="color: #3d85c6;" /><span style="color: #3d85c6;"> MOV AH, 4CH</span><br style="color: #3d85c6;" /><span style="color: #3d85c6;"> INT 21H</span><br style="color: #3d85c6;" /><br style="color: #3d85c6;" /><span style="color: #3d85c6;">MAIN ENDP</span><br style="color: #3d85c6;" /><span style="color: #3d85c6;">END MAIN</span></b></div>Shabab Haider Siddiquehttp://www.blogger.com/profile/14896191923433512555noreply@blogger.com0tag:blogger.com,1999:blog-2354853794677096092.post-71441137364217635342010-01-24T06:26:00.001-08:002010-01-24T06:27:06.664-08:00Assembly Language Part 9<div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><b>Procedures in Assembly</b><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">The Idea of procedure is to divide the main problem into sub problems and acquire the solution by solving the individual sub problems and combining through calling them from a mother problem.<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">The Idea is similar to functions() as used in High level languages. When a procedure call one another the control is transferred from the caller to the called, when the called procedure is done it usually returns the control to the caler function immediately after the calling statement.<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">The basic form of the procedure is<br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><b>name PROC type</b><br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><b>; body of the procedure</b><br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><b>RET</b><br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><b>name ENDP</b><br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><span style="color: black;">There are 2 types of procedures. NEAR and FAR. A procedure is called near if the procedure and the main program is in the same segment. If the procedure is in other segment then the proc is called FAR.</span><br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><span style="color: black;">To invoke a procedure we write the following command</span><br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><span style="color: black;"> <br />
</span><br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><b>CALL name_of_proc</b><br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><span style="color: black;"> </span><br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><span style="color: black;">How is control returned?</span><br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><span style="color: black;">The control is returned from the procedure to caller program when the RET instruction is executed. Every procedure should have a RET instruction in some place.</span><br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><br />
</div><blockquote style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><b>MAIN PROC</b><br />
<b> ..............</b><br />
<b> ..............</b><br />
<b> CALL PROC_1</b><br />
<b> statement_x</b><br />
<b> ..............</b><br />
<b> ..............</b><br />
<b><br />
</b><br />
<b><br />
</b><br />
<b>PROC_1 PROC</b><br />
<b> ..............</b><br />
<b> ..............</b><br />
<b> RET</b><br />
</blockquote><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">In the program mentioned above in a stage of execution of MAIN PROC the statement CALL PROC_1 will occur. This will transfer the control of the program to PROC_1 and the instructions in PROC_1 shall continue to execute. When the instruction RET is reached the control is returned to MAIN PROC and the program shall resume to execute from statement_x<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><b><span style="font-size: large;">Example</span></b> <b style="color: #3d85c6;">( factorial ! )</b><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">Let us describe the application of proc by finding the factorial<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;">.MODEL SMALL<br />
.STACK 100H<br />
.DATA<br />
MSG1 DB “This is from procedure_1$”<br />
MSG2 DB 0DH, 0AH, “This is from main procedure$”<br />
.CODE<br />
MAIN PROC<br />
MOV AX, @DATA<br />
MOV DS, AX<br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;">CALL PROCEDURE_1<br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"> LEA DX,MSG2<br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"> MOV AH,9<br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"> INT 21H<br />
</div><div style="color: #3d85c6; font-family: Arial,Helvetica,sans-serif;"><br />
MOV AH, 4CH<br />
INT 21H<br />
MAIN ENDP<br />
<br />
PROCEDURE_1 PROC<br />
LEA DX, MSG1<br />
MOV AH, 9<br />
INT 21H<br />
RET<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: #3d85c6;">PROCEDURE_1 ENDP</span><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: #3d85c6;">END MAIN</span><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><b>Macro in Assembly</b><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">The Idea of Macro is somewhat similar of procedure, divide the main problem into sub problems and acquire the solution by solving the individual sub problems. But macro at all is not like a procedure. It is just a block of program that has a name. The syntax for defining macro is -<br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;">macro_name MACRO operand1,operand2, . . . .<br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"> statements<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: #0b5394;">ENDM</span><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">Inside the Program we call the macro just by -<br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="color: #0b5394;">macro_name (operands</span>)<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">When a macro is invoked, the program control does not jump as it does in the case of procedure. Instead the block of program defined by the macro name is copied to the place where it occurs before executing.<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><b>Example: Factorial using MACRO</b><br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><br />
</div><span style="color: #0b5394;">.model small </span><br />
<span style="color: #0b5394;">fact macro </span><br />
<span style="color: #0b5394;"> mov cx,var1 </span><br />
<span style="color: #0b5394;"> xor ax,ax </span><br />
<span style="color: #0b5394;"> mov ax,1 </span><br />
<span style="color: #0b5394;"> top: </span><br />
<span style="color: #0b5394;"> mul cx </span><br />
<span style="color: #0b5394;"> loop top </span><br />
<span style="color: #0b5394;"> </span><br />
<span style="color: #0b5394;"> call outdec </span><br />
<span style="color: #0b5394;"> endm </span><br />
<span style="color: #0b5394;"> </span><br />
<span style="color: #0b5394;">.stack 100h </span><br />
<span style="color: #0b5394;">.data </span><br />
<span style="color: #0b5394;">msg1 db 0ah,0dh,"Factorial is=$" </span><br />
<span style="color: #0b5394;">msg2 db "Enter a number:$" </span><br />
<span style="color: #0b5394;">;msg3 db 0ah,0dh,"your entered value is out of range!!$" </span><br />
<span style="color: #0b5394;">var1 dw 0 </span><br />
<span style="color: #0b5394;">;limit dw 0 </span><br />
<span style="color: #0b5394;">.code </span><br />
<span style="color: #0b5394;">main proc </span><br />
<span style="color: #0b5394;"> mov ax,@data </span><br />
<span style="color: #0b5394;"> mov ds,ax </span><br />
<span style="color: #0b5394;"> lea dx,msg2 </span><br />
<span style="color: #0b5394;"> mov ah,9 </span><br />
<span style="color: #0b5394;"> int 21h </span><br />
<span style="color: #0b5394;"> call indec </span><br />
<span style="color: #0b5394;"> mov var1,ax </span><br />
<span style="color: #0b5394;"> lea dx,msg1 </span><br />
<span style="color: #0b5394;"> mov ah,9 </span><br />
<span style="color: #0b5394;"> int 21h </span><br />
<span style="color: #0b5394;"> fact </span><br />
<span style="color: #0b5394;"> mov cx,ax </span><br />
<span style="color: #0b5394;"> mov ah,4ch </span><br />
<span style="color: #0b5394;"> int 21h </span><br />
<span style="color: #0b5394;"> </span><br />
<span style="color: #0b5394;"> main endp </span><br />
<span style="color: #0b5394;"> include outdec.asm </span><br />
<span style="color: #0b5394;"> include indec.asm </span><br />
<span style="color: #0b5394;">end main</span>Shabab Haider Siddiquehttp://www.blogger.com/profile/14896191923433512555noreply@blogger.com0tag:blogger.com,1999:blog-2354853794677096092.post-70927190709538851392010-01-24T06:25:00.001-08:002010-01-24T06:25:44.984-08:00Assembly Language Part 10<div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><b>Multiplication instructions in MASM</b><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">The process of multiplying and dividing is different for signed and unsigned numbers. So they use different instructions. The intructions are also of byte and word form. When we multiply 2 numbers of byte form the result will be word form or 2 bytes. When we multiply 2 number of word form the result is double word. The basic multiplication instructions are -<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"><b><span style="color: #0b5394;">MUL (Multiply)</span></b></span><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">MUL (Multiply) is used for multiplying unsigned numbers. The form of invokation is-<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">MUL source<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">The instruction shall multiply the value of AL/AX with source and store it in DX:AX. The source may be a register or a memory byte but not a constant number. To invoke a byte form of multiplication the source should be 8bit and the destination should be in AL. For word form of multiplication the source is 16bit and the destination is stored in AX. In this case the result is stored in AX(lower word) and DX(upper word)<br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><b><br />
</b><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><b><span style="color: #0b5394;">Example </span></b><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: x-small;"><b><span style="color: #0b5394;">Byte form of MUL</span></b></span><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">Suppose AL contains 80h and BL contains 0ffh<br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;">MUL BL <br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><b>Dec Product Hex Product AH AL CF/OF</b><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">128 7F80 7F 80 1*<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: x-small;"><b><span style="color: #0b5394;">Word form of MUL</span></b></span><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">Suppose AX contains FFFh and BX contains FFFFh<br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><br />
<br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;">MUL BX <br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><b>Dec Product Hex Product DX AX CF/OF</b><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">4294836225 FFFE0001 FFFE 0001 1*<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"><b><span style="color: #0b5394;">IMUL (Integer Multiply)</span></b></span><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">IMUL is used for multiplying signed numbers. The form of invokation is-<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">IMUL source<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">The instruction shall multiply the value of AX /ALwith source and store it in AX/DX:AX. The source may be a register or a memory byte but not a constant number. To invoke a byte form of multiplication the source should be 8bit and the destination should be in AL. For word form of multiplication the source is 16bit and the destination is stored in AX. In this case the result is stored in AX(lower word) and DX(upper word)<br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><b><br />
</b><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><b><span style="color: #0b5394;">Example </span></b><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: x-small;"><b><span style="color: #0b5394;">Byte form of IMUL</span></b></span><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">Suppose AL contains 80h and BL contains 0ffh<br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;">MUL BL <br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><b>Dec Product Hex Product AH AL CF/OF</b><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">128 0080 00 80 1*<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: x-small;"><b><span style="color: #0b5394;">Word form of IMUL</span></b></span><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">Suppose AX contains FFFh and BX contains FFFFh<br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><br />
<br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;">IMUL BX <br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><b>Dec Product Hex Product DX AX CF/OF</b><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">1 00000001 0000 0001 0*<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;">*Effect of MUL and IMUL in CF and OF<br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;"> after invoking MUL and IMUL the flags SF,ZF,AF,PF are unchanged. The flags CF and OF are changed as-<br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;">After MUL CF/OF = 0 if upper half of the result is 0<br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;"> = 1 other wise<br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;">After IMUL CF/OF = 0 if the upper half of the result is the sign extension of the lower half<br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;"> = 1 otherwise<br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><br />
<br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;">For both case the CF/OF =1 means that the result is too big to fit in the lower half of destination. <br />
</div><br />
<br />
<div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><b>Division instructions in MASM</b><br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;">The process of dividing is also different for signed and unsigned numbers. So they use different instructions. The intructions are also of byte and word form. When we divide a number of word form with a divider of byte form the result is also a byte form. We can also divide a 32 bit dividend by a 16 bit divisor. In this case the dividend is in DX:AX. The instructions are -<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"><b><span style="color: #0b5394;">DIV (Divide)</span></b></span><br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;">DIV is used for unsigned division. The form is-<br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;">DIV divisor<br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;">The instruction shall divide the value of AX/DX:AX with divisor and store the quotient in AL/AX and remainder in AH/DX. The divisor may be a register or a memory location but not a constant number. <br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><b><br />
</b><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><b><span style="color: #0b5394;">Example </span></b><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: x-small;"><b><span style="color: #0b5394;">Word form of DIV</span></b></span><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;">Suppose AX contains 00FBh and BL contains FFh<br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;">DIV BL <br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;"><b>Dec Quotient Dec Remainder AH AL </b><br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;">0 251 FBh 00h <br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: x-small;"><b><span style="color: #0b5394;">Double Word form of DIV</span></b></span><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
<br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;">Suppose DX contains 0000h and AX contains 0005h and BX contains 0002h<br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><br />
<br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;">DIV BX <br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
<br />
<div style="color: black; font-family: Arial,Helvetica,sans-serif;"><b>Dec Quotient Dec Remainder AH AL </b><br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;">2 1 0002h 0001h <br />
</div></div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: small;"><b><span style="color: #0b5394;">IDIV (Integer Divide)</span></b></span><br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;">IDIV is used for dividng signed numbers. The form is-<br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;">IDIV divisor<br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;">The instruction shall divide the signed value of AX/DX:AX with divisor and store the quotient in AL/AX and remainder in AH/DX. The divisor may be a register or a memory location but not a constant number. <br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><b><br />
</b><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><b><span style="color: #0b5394;">Example </span></b><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: x-small;"><b><span style="color: #0b5394;">Word form of IDIV</span></b></span><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;">Suppose AX contains 00FBh and BL contains FFh<br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;">IDIV BL <br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;"><b>Dec Quotient Dec Remainder AH AL </b><br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;">Dvide overflow** ** ** ** <br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><span style="font-size: x-small;"><b><span style="color: #0b5394;">Double Word form of IDIV</span></b></span><br />
</div><div style="font-family: Arial,Helvetica,sans-serif;"><br />
</div><div style="color: black; font-family: Arial,Helvetica,sans-serif;">Suppose DX contains 0000h and AX contains 0005h and BX contains FFFEh<br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;"><br />
<br />
</div><div style="color: #0b5394; font-family: Arial,Helvetica,sans-serif;">IDIV BX <br />
</div><br />
<div style="color: black; font-family: Arial,Helvetica,sans-serif;"><b>Dec Quotient Dec Remainder AH AL </b><br />
</div>-2 1 FFFEh 0001h <br />
<br />
<span style="color: #0b5394;">**Division overflow</span> <br />
It is possible that overflow might occur when diving, because the quotient and reminder are set to be half of the dividend by default. In any case were the divisor is so much small that can cause a larger result causes overflow. In the example above the dividend is FBh = 251 and the divisor is -1 so the quotient should be -251 which cannot be fit in ALShabab Haider Siddiquehttp://www.blogger.com/profile/14896191923433512555noreply@blogger.com0