Wednesday, 24 December 2014

Rules for writing an algorithm

Qualities of a good algorithm

  1. Inputs and outputs should be defined precisely.
  2. Each steps in algorithm should be clear and unambiguous.
  3. Algorithm should be most effective among many different ways to solve a problem.
  4. An algorithm shouldn't have computer code. Instead, the algorithm should be written in such a way that, it can be used in similar programming languages.
Write an algorithm to add two numbers entered by user.
Step 1: Start
Step 2: Declare variables num1, num2 and sum. 
Step 3: Read values num1 and num2. 
Step 4: Add num1 and num2 and assign the result to sum.
Step 5: Display sum 
Step 6: Stop

Rules for drawing a FLOWCHART

Terminator All flowcharts start and end with the terminator or terminal shape. This is a rounded rectangle and is shown below. You use this same shape for both the start and the end. You will see some charts with slightly different terminal shapes. For example, sometimes an oval is used. This is because there is no golden standard for flowcharting. Different companies and different technical areas use different shapes. There are a few basic rules and, beyond that, you can create your own rules for your organization. If you add your own shapes, it is important that you explain what they are used for, so others can understand your chart.
Just click on the terminator shape in the stencil and then type in the text.


To read a flowchart, you follow the arrows from shape to shape. To draw a line in RFFlow, click on the line in the stencil at the left and then drag the mouse to draw the line in your chart. It is faster to place all your shapes first and then draw all the lines at once.

Decision The decision shape asks a question. "Are you under 65 years of age?" If you are less than 65 years old, you follow the arrow going down out of the decision shape. If you are 65 or older, you follow the arrow to the right.
Process The rectangle is used for activities or action steps. These are things that must be done.
Circle A circle containing a letter or number means that this chart connects to another chart on a different page. The same circle and number are then the entry point on the new page. It is best not to use these, but sometimes they are necessary.
Title It is good to have a title on your chart. Just click on the word "Text" in the stencil and use a larger font size.
Putting it all together The chart shown below could be part of the 2006 Federal Income Tax return instructions. You start at the top and follow the arrows from shape to shape. Flowcharts usually go from top to bottom or left to right, but you can do whatever you want. The circle with the letter B refers to a chart on a different page. You would find that page and look for a B in a circle. Almost all flowchart can be done with only these four shapes. 
Moving you chart into a document After drawing your chart, you can copy and paste the chart into Microsoft Word or any other Windows program. If you want to put the chart on the internet, you can save your chart as a PNG or HTML file.
Flowchart Key If you are working in a group, it is advantageous to label each chart with the name of the person or group who created it and the date of creation. It is a also a good idea to have a key which lists the shapes you use in a chart and what they mean. An example is shown below:
Level of Detail You or your group must decide the level of detail to use in drawing your chart. Usually it is best to start specifying only general steps and leaving the details for later. Whatever you do, the level of detail should be consistent throughout the chart. For example, if you are flowcharting editing a Microsoft Word document, you might have a rectangle with the words "Print Document." A detailed chart might split this rectangle into several rectangles that show each step of clicking on the Print button in Word, selecting the appropriate printer, etc. Make sure you are not wasting time by specifying at too detailed a level. With RFFlow you can make hyperlinks in shapes so that when you click on the shape, you jump to another chart that has the details for that shape. You can also save RFFlow charts as HTML and have links to other Web pages.

MA: (hello world alike) 64-bit

section .data:
    inputLen: equ $-input

section .text
    global _start
    mov rax,1
    mov rdi,1
    mov rsi,input
    mov rdx,inputLen


    mov rax,60                ; The system call for exit (sys_exit)
     mov rbx,0                ; Exit with return code of 0 (no error)

[apcoer@localhost ~]$ nasm -f elf64 parashar.asm
[apcoer@localhost ~]$ ld -o parashar parashar.o
[apcoer@localhost ~]$ ./parashar

MA: hello world(32 bit)

section .data

    hello:db'Hello World',10
    hellolen:equ $-hello

section .text
    global _start

    mov eax,4;
    mov ebx,1;
    mov ecx,hello
    mov edx,hellolen;

    int 80h

    mov     ebx,0               ;first syscall argument: exit code
       mov     eax,1               ;system call number (sys_exit)
        int     80h                ;call kernel

[apcoer@localhost ~]$ nasm -f elf64 hellwrld.asm
[apcoer@localhost ~]$ ld -o hellwrld hellwrld.o
[apcoer@localhost ~]$ ./hellwrld
Hello World

MA: addition of 2 numbers

section .data
    input:db 'enter the number',10   ;
    inputLen:  equ $-input           ;
    display:db 'entered number is:',10   ;
    displayLen:  equ $-display       ;

    addition:db 'addtion of number is',10;
    additionLen: equ $-addition;


section .bss
a resb 3  
b resb 3
r resb 3

section .text
    global _start

    mov rax,1         ;string:enter number
    mov rdi,1            ;file discriptor
    mov rsi,input        ;input
    mov rdx,inputLen     ;lenght of input
    syscall              ; Call the kernel

    mov rax,0         ;input
    mov rdi,0            ;standard input
    mov rsi,a       ;
    mov rdx,2          ;read only first two number
    syscall              ;Call the kernel........

    mov rax,1         ;string2
    mov rdi,1            ;
    mov rsi,input        ;
    mov rdx,inputLen     ;
    syscall              ; Call the kernel

    mov rax,0         ;input2
    mov rdi,0            ;
    mov rsi,b        ;
    mov rdx,2     ;
    syscall              ; Call the kernel.....


    XOR RAX,RAX ;addition of 2 numbers
    MOV AL,[a]
    sub al,30h
    MOV BL,[b]
    sub bl,30h
    ADD al,30h
    MOV [r],AL

    mov rax,1         ;string2
    mov rdi,1            ;
    mov rsi,addition      ;
    mov rdx,additionLen    ;

    mov rax,1
    mov rdi,1
    mov rsi,r
    mov rdx,3

    mov rax,60                ; The system call for exit (sys_exit)
    mov rbx,0                ; Exit with return code of 0 (no error)
    syscall            ;

[apcoer@localhost ~]$ nasm -f elf64 add.asm
[apcoer@localhost ~]$ ld -o add add.o
[apcoer@localhost ~]$ ./add
enter the number
enter the number
addtion of number is