preload preload preload preload

Friday, February 4, 2011

Creating a JavaDB with Netbeans

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.


Lets first start with creating and connecting to a JavaDB using netbeans.


db2.png


first go to the services tab.


Expand the Database entry.


right click the JavaDB and click start server.


Right click again the JavaDB to create a new Database. The database create wizard shall appear.


db4.png


Just type in the fields as indicated to continue this tutorial. the password is also "app".


connect to the newly created database. (right click and connect).


db7.png


Expand the APP. and right click "Tables" to create a table. The table creator wizard appears.


db8.png


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.

Sunday, February 7, 2010

C Programming Basic - Part 1

C 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.

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:
 
#include 
 

int main()
{
  printf( "Hello world!" );
  return 0;
}

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.

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.

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).

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.

The final brace closes off the function.

Interesting isn't it ? Start playing around with this. To know about compiler, you may refer to here

Compiler

What is a compiler?
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



Terminology:


  • Compile convert a source code file into an executable, but strictly speaking, compilation is an intermediate step

  • Link The act of taking compiled code and turning it into an executable

  • Build A build refers to the process of creating the end executable (what is often colloquially refered to as compilation).

  • Compiler Generally, compiler refers to both a compiler and a "linker"

  • Linker The program that generates the executable by linking

  • IDE Integrated Development Environment, a combination of a text editor and a compiler, such that you can compile and run your programs directly within the IDE. IDEs facilitates debugging.

    Compilers:

      Windows/DOS

    • Code::Blocks and MINGW Our recommended free compiler setup!
    • Borland Find out how to download and set up Borland's free command-line compiler
    • DJGPP Read about DJGPP, a DOS-based compiler
    • Dev-C++ and Digital Mars Read about Dev-C++, a good windows based compiler, and Digital Mars
    • Windows Only

    • Microsoft Visual C++ you must have heard about Visual C++
    • *nix

    • g++ is a C++ compiler that comes with most *nix distributions.
    • gcc is a C(and many other) compiler that comes with most *nix distributions.
    • Macintosh

    • Apple's own Macintosh Programmer's Workshop is a compiler I've never used, but it is direct from apple and free.
    • Codewarrior 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. 
    To know about how to compile you can refer to their manual.  GCC compilation process is given here

  • Sunday, January 24, 2010

    Assembly Language Part 3

    For 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

    In assembly program flow is managed by jumping isntructions. There are 2 types of jump

    1) Unconditional
                 The JMP instruction causes an unconditional transfer of control. The basic form is-
                JMP label

    2)Conditional
                The transfer of control depends on the previous instruction. It is often done by CMP. for example
              CMP operand1,operand2
              Jxx    end

    Here the jump Jxx can be any of the following -


    Jcc Instructions for Signed Comparisons
    Instruction
    Description
    Condition
    Aliases
    Opposite
    JG
    Jump if greater (>)
    Sign = Ovrflw or Zero=0
    JNLE
    JNG
    JNLE
    Jump if not less than or equal (not <=)
    Sign = Ovrflw or Zero=0  
    JG
    JLE
    JGE
    Jump if greater than or equal (>=)
    Sign = Ovrflw
    JNL
    JGE
    JNL
    Jump if not less than (not <)
    Sign = Ovrflw
    JGE
    JL
    JL
    Jump if less than (<)
    Sign Ovrflw
    JNGE
    JNL
    JNGE
    Jump if not greater or equal (not >=)
    Sign Ovrflw  
    JL
    JGE
    JLE
    Jump if less than or equal (<=)
    Sign Ovrflw or Zero = 1
    JNG
    JNLE
    JNG
    Jump if not greater than (not >)
    Sign Ovrflw or Zero = 1
    JLE
    JG
    JE
    Jump if equal (=)
    Zero = 1
    JZ
    JNE
    JNE
    Jump if not equal ()
    Zero = 0
    JNZ
    JE






    Jcc Instructions That Test Flags
    Instruction
    Description
    Condition
    Aliases
    Opposite
    JC
    Jump if carry
    Carry = 1
    JB, JNAE
    JNC
    JNC
    Jump if no carry
    Carry = 0
    JNB, JAE
    JC
    JZ
    Jump if zero
    Zero = 1
    JE
    JNZ
    JNZ
    Jump if not zero
    Zero = 0
    JNE
    JZ
    JS
    Jump if sign
    Sign = 1
    -
    JNS
    JNS
    Jump if no sign
    Sign = 0
    -
    JS
    JO
    Jump if overflow
    Ovrflw=1
    -
    JNO
    JNO
    Jump if no Ovrflw
    Ovrflw=0
    -
    JO
    JP
    Jump if parity
    Parity = 1
    JPE
    JNP
    JPE
    Jump if parity even
    Parity = 1
    JP
    JPO
    JNP
    Jump if no parity
    Parity = 0
    JPO
    JP
    JPO
    Jump if parity odd
    Parity = 0
    JNP
    JPE








    Jcc Instructions for Unsigned Comparisons
    Instruction
    Description
    Condition
    Aliases
    Opposite
    JA
    Jump if above (>)
    Carry=0, Zero=0
    JNBE
    JNA
    JNBE
    Jump if not below or equal (not <=)
    Carry=0, Zero=0
    JA
    JBE
    JAE
    Jump if above or equal (>=)
    Carry = 0
    JNC, JNB
    JNAE
    JNB
    Jump if not below (not <)
    Carry = 0
    JNC, JAE
    JB
    JB
    Jump if below (<)
    Carry = 1
    JC, JNAE
    JNB
    JNAE
    Jump if not above or equal (not >=)
    Carry = 1
    JC, JB
    JAE
    JBE
    Jump if below or equal (<=)
    Carry = 1 or Zero = 1
    JNA
    JNBE
    JNA
    Jump if not above (not >)
    Carry = 1 or Zero = 1
    JBE
    JA
    JE
    Jump if equal (=)
    Zero = 1
    JZ
    JNE
    JNE
    Jump if not equal ()
    Zero = 0
    JNZ
    JE




     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 -

           CMP AX,BX
           JL  axislower    ;  If AX is lower than BX then control transferred to axislower

    The TEST instruction
        The TEST instruction performs an AND operation, but does not store
    the result. It only sets the FLAGS register based on what the result would
    be (much like how the CMP instruction performs a subtraction but only sets
    FLAGS).

    TEST DESTINATION, SOURCE


    Effect on flags -
    SF,ZF, PF - Reflect the result
    AF  -  Undefined
    CF,OF - 0                    

    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 -

    IF AL is EVEN
       THEN JUMP TO END_X

    MASM code-

    TEST AL,1
    JZ END_X

    END_X:
      

    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.)
    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.

     Further reading:
     The art of Assembly Language             URL:  http://www.arl.wustl.edu/

     The Intel Microprocessor Architecture, Programming & Interfacing
        by  Barry B. Brey

    Assembly Language Part 4

    Conditional Branching in MASM
    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.

    In high level languages we use these basic structures -

    IF-THEN
    The if-then structure is the most common type of branching statement used. The pseudocode for IF-THEN is :


    IF  CONDITION IS TRUE
         THEN EXECUTE TRUE BRANCH STATEMENTS
    END_IF

    MASM example:
    Suppose we want to code the following pseudocode in MASM

    IF AX< 0
        THEN REPLACE AX WITH ITS COMPLIMENTED VALUE
    END_IF

    The MASM code will be:

    CMP AX,0
       JNL END_IF
       NEG AX

    END_IF:

    IF-THEN-ELSE
    The if-then-else structure is the expansion of most common type of branching statement IF-THEN. The pseudocode for IF-THEN-ELSE is :

    IF   Condition is TRUE
    THEN
    Execute true-branch statements
    ELSE
    Execute false-branch statements
    END_IF

    Lets explain by applying the following pseudocode in MASM

    IF AL<= BL
      THEN
          DISPLAY THE CHARACTER IN AL
      ELSE
          DISPLAY THE CHARACTER IN BL
     END_IF

    The MASM code will be -

    MOV AH,2
    CMP AL,BL
       JNBE ELSE_
       MOV DL,AL
       JMP DISPLAY_
    ELSE_:
       MOV DL,BL
    DISPLAY_:
       INT 21H

    END_IF:


    CASE
    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 :

    CASE expression
        value_1: statement_1
        value_2: statement_2
        value_3: statement_3
        value_4: statement_4
        .................................
        value_n: statement_n
    END_CASE

    Consider the following pseudocode in MASM

    IF AX a negative number
       THEN put -1 in BX
    IF AX contains 0
       THEN put 0 in BX
    IF AX contains a positive number
       THEN put 1 in BX

    The MASM code will be -

    CMP AX,0
       JL   NEGATIVE_
       JG   POSITIVE
     
       MOV BX,0
       JMP END_CASE

     NEGATIVE_:
       MOV BX,-1
       JMP END_CASE

    POSITIVE_:
      MOV BX,1
      JMP END_CASE

    END_CASE:


    further reading:
    http://www.cs.princeton.edu/

    Assembly Language Programming and Organization of IBM PC
                                                                        Ytha Yu
                                                                        Charles Marut

    Assembly Language Part 5

    Looping instructions in Assembly



    What comes to your mind after watching the loop at the left? That's it the Head is Touching the Tail at some point.

    In assembly a loop is a sequence of instructions that is repeated like the loop to the left.
    The final instruction (Tail) jumps (touches) to the first instruction (Head).

    The basic form of looping in Assembly is

    REPEAT UNTIL
    REPEAT
          STATEMENTS
    UNTIL CONDITION

    For example if we want to read all input characters given by the user until he/she inputs

        MOV AH,1
    REPEAT_:
        INT 21H
        CMP AL,' '
        JNE REPEAT_ 

    Some other frequently used looping structures are-


    FOR LOOP
    In these kind of lop structures the instructions are repeated for known number of times. pseudocode -

    FOR loop_count times DO
         statements
    END_FOR

    In assembly we use the instruction LOOP to implement for loop. The general form is-

    LOOP header_label

    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. 

    Let us implement it in the program to print 100 '*' in assembly.

    MOV CX,80
    MOV AH,2
    MOV DL,'*'

    TOP:                            ; header_label
       INT 21H
       LOOP TOP              



    WHILE LOOP
    These kind of loops depends on a condition. pseudocode -


    WHILE   condition DO
       statements
    END_WHILE

    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.

    Lets count the number of character in the input line.

    MOV DX,0
    MOV AH,1
    INT 21H

    WHILE_:
         CMP AL,13
         JE END_WHILE
         INC  DX
         INT 21H
         JMP WHILE_

    END_WHILE:


    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.
    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.

    Further reading:
    Assembly Language Programming and Organization of IBM PC
                                                                        Ytha Yu
                                                                        Charles Marut
    http://www.cs.princeton.edu/

    Assembly Language Part 6

    Logic Instructions

    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,



    AND 
     0 0 -> 0
     1 0 -> 0
     0 1 -> 0
     1 1 -> 1

    OR 
     0 0 -> 0
     1 0 -> 1
     0 1 -> 1
               1 1 -> 1            



    XOR 
     0 0 -> 0
     1 0 -> 1
     0 1 -> 1
     1 1 -> 0


    NOT

     0 -> 1
     1 -> 0
     

    Now lets see where we use these functions in Assembly -

    AND   ->  To clear
    OR     ->  To set
    XOR   ->  To compliment desired bit positions
    NOT   ->  To compliment all bits

    AND (Used to Clear )

    The form is
    AND DESTINATION, SOURCE

    Suppose we want to clear the sign bit of AL then the code will be

    AND AL,7Fh                            ;

    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

    AL   =   10101100
    Msk =   01111111

    -----------------------------
    rslt  =   00101100


    OR (used to set)

    The form is
    OR DESTINATION, SOURCE

    Suppose we want to set the sign bit of AL then the code will be

    OR AL,80h                            ;

    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

    AL   =   00101100
    Msk =   10000000
    -----------------------------
    rslt  =   10101100

    XOR (Used to compliment selected bits )

    The form is
    XOR DESTINATION, SOURCE

    Suppose we want to change the sign bit of AL then the code will be

    XOR AL,80h                            ;

    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


    AL   =   10101100
    Msk =   10000000
    -----------------------------
    rslt  =   00101100

    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'



    NOT (Used to compliment all bits )

    The form is
    NOT DESTINATION

    Suppose we want to invert all the bits of AL then the code will be

    NOT AL                            ;

    You can see that NOT takes only one argument.

    Suppose AL = 10110010 , after executing the NOT instruction it will be 01001101


    Problem (Convert ASCII value to number)
    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...

    If we AND a value with 0000  1111 then the relust will be 30 less than the original value

    AL   =   0011 0011   =33
    Msk =   0000 1111
    ------------------------------
                 0000 1111   =3

    so the code will be

    AND AL, 0Fh