Sunday, February 10, 2013

Saturday, February 9, 2013

Basic Linux Commands


Basic BASH Commands:

ls: Displays the content of a directory
If no external option is given, it will display the contents of present working directory.

Eg: ls ./abc
ls
ls ~/user
- a : Option to print all files(including hidden files)
- l : Option to give a long of file details.
Eg: ls -a
ls -al
ls -l /

NB: ~ : Denotes the home directory of current user
/ : Denotes the root directory of the Linux System.

Eg: ls /
ls -a ~/

pwd: Prints the current(present) working directory.

cd <directory> : Changes the working directory to the directory specified.
Eg: cd ~
cd /
NB: cd .. will make the terminal to move to the parent directory of present working directory.
cd . Will make the terminal to move to the current directory itself.

passwd : Changes the password of current user account.

touch <filename> : Creates a new file
Eg: touch abc.txt
touch ~/abc.txt

nano : A simple text editor

cp <file_src> <file_dest> : Copies a source file to a destination directory with or without renaming (File will be renamed if file_dest is not a directory).
-r or -R or –recursive : Recursive copy or copy all the subdirectories as well.
Eg: cp ./abc.txt ./dir2/efg.txt
cp -r ./abc ./dir2

mkdir <dir_name> : Creates a new directory.
-p, --parents : Creates intervening parent directories if they do not exist.

mv <source> <dest> : Cut and paste source file to the destination and rename if dest is not a directory. mv is used also for renaming a file.
Eg: mv ./abc/sample.txt ./efg
mv ./efg/sample.txt ./efg/sample2.txt

rmdir <dir_name> : Deletes an empty directory.

rm <file_name>: Removes a file.
-r option can be used to remove directories as well recursively.
-i option can be used to warn before deleting.


command --help : Displays a simple help page for a command.

man <command> : To get the manual page for a command. Press 'q' to exit.

info <command> : To get the info page for a command.

whatis <command> : gives a short description about the command.
Eg: whatis ls

apropos <keyword> : search the manual page names and descriptions for commands
Eg: apropos editor

time : Displays the current system time.

cal : Displays the calender for the current month.
Eg: cal #Calender for the current month
cal 2 2013 #Calender for February 2103
cal 2012 #Calender for the year 2012

ps : Displays the processes created by the current user.

who : Displays all the users currently logged into the system.

w : Displays all the users currently logged into the system and their resours utilization.

cat : Display the content of a file on to the screen.

wc : Word Count – Counts the number of lines, words and characters in a text file.
Eg: wc sample.txt

which <command> : Displays the actual location of the command / application.

exit or logout : Terminates the current session.

Redirecting output of a command to file:
Eg: ls -al > new.txt

Typing two or more commands in a single line:
Eg: ls -al ; mkdir abc

Comments given after commands:
Eg: ls -al #Long listing of pwd contents.

echo “<TEXT>” : Prints a text into console.
Eg: echo “Hello World”

su : Used to change user account, either to super user or any other user account.

NB: In Ubuntu based linux systems, we use sudo (SUper User DO) to exicute a command with root privilage.
Eg: sudo mkdir /abc
sudo apt-get update

script
---------------------------
--------------------------- : Records the BASH Session to the file typescript
---------------------------
exit


sort <filename> : Sorts a text file

uname -a : Prints the information about the linux installed in the system.

lsb_release -a : Prints the version information about the linux installed.


Keyboard Shortcuts:
Key Combination
Use
Ctrl + D
Same as exit , exits or terminates current session
Ctrl + C
Closes the currently running program in foreground
Ctrl + Z
Suspends the currently running program
Ctrl + H
Same as backspace
Ctrl + L
Clears the terminal
Same as clear command
TAB
Predicts and completes a command
TAB TAB
Lists all command completion possibilities.



Files & Directories in Linux Systems:
  • In Linux all the things excepts processes are generally considered as files(devices, directories, etc).
  • Directories can be treated as special file that contains the list of files contained in it.
  • In the tree structure for directory all the directories contains two pointers . and .. which points to the current directory and parent directory respectively.
  • All the files also have an inode number which along with . And .. pointers identifies a file in the directory tree structure. Give -i option along ith ls to get the inode number as well.
  • Hidden files have names preceding with a '.'


Important Directiories in / (Root Directory):

bin : Common files shared by system, administrator and users.(Eg: commands)
boot : vmlinuz(Kernel Files), GRUB
dev : References to hardware peripherals.
etc : Configuration files(Similar to the control panel in windows)
home : Directories and home folders for users.
initrd : Information needed during booting.
lib : Library Files
lost + found : Files saved during failures.
var :Variable files like log details, server pages, temporary variables etc.
usr : Programs, libraries, documentation for all user related programs.
media : Mount points for media devices.


df -h : Gives information about the partitions.
-h : Human readable, ie. Prints the details in a way easily unstaandable by human beings.

df -h . : Give the details of the disk partition that contains the present working directory.

du -h : Prints the disk usage details for the current directory.

which <command>: Give the absolute path for the command / program.
Eg: which ls
/bin/ls

$PATH is an environment variable that contains the list of directories which has executable files in it. So if we give a command it will search all the folders in the $PATH variable and if it is there then it will get executed, else it will print and error “Command not found..”.

echo $PATH : Prints the content of PATH variable.

We can temporarily change the $PATH variable using export command.
Eg:
export PATH=/usr/local/bin:/bin:/usr/sbin
or
export PATH=$PATH:/usr/sbin

$HOME – Contains the home folder for current user.
Eg: echo $HOME

file <file_name> : Displays the type of the file.

cmp <file_1> <file_2> : Compares two files

Searching in Linux:

Wild char symbols:
* : Matches any number of characters
? : Matches exactly one character.
[a-z] : Matches any character within the specified range.

Eg: ls *.txt
mv ./abc/*.txt ./abc/*.c


find <path> -name <search_string>
find <path> -size <size>

Eg: find /temp -name “abc.txt”
find . -size +50k

locate <search_string> : Similar to find but more friendly and less efficient.

~/.bash_history : is a file that contains the bash usage history.

grep: Command used to filter input lines and return only some patterns.

Eg: grep find ~/.bash_history
grep apple ./fruits.txt

NB: /usr/share/dict/words is a dictionary containing all the English words

Eg: grep man /usr/share/dict/words

cat : Concatenates and displays the output of one or more files to the screen. Output displayed in an uncontrolled way.

more : Displays the output with scrolling downward option.

nano : A simple text editor.

head : Display first few lines of a text file.

tail : Display last few lines of a text file.


File and Directory Permissions:

For each file any users can be classified into any of the three categories:
  • u – User / Creator of the file
  • g – Group User
  • o – Any other user

ls -l gives a detailed listing along with the user and privilege details. There are separate privileges for read(r), write(w) and execute(x) for user, group user and other users.

chmod : Used to change the user privileges of any file.
Use -r or –recursive to apply privileges excursively to all subfiles in a directory.
Eg: chmod u+x abc.txt #Add user the privilege to execute.
chmod ug+rwx abc.txt #Add user and group user the privilege to read,write andexecute.
chmod o-x abc.txt #Deducing the power of other user to execute the file.
chmod 777 abc.txt #Give all privileges to all categories of users.

Processes:

A program executing in memory is called a process.
Using a terminal we can execute a process in:
  • Foreground
    OR
  • Background

<regular_command> : Runs the command in background.

<regular_command> & : Runs the command in background.

jobs : Command to display all the jobs running in background.

Ctrl + Z : Suspends a program running in foreground.

Ctrl + C : Terminate and quit a process running in foreground.

%n : Every process running in background has got a unique number, we can get that using job command.

bg <%n> : Reactivate a suspended program in background.

fg <%n> : Puts the job back in background.

kill <%n> : Terminates a background process.


  • All the process has got a unique identification number called PID or Process ID.
  • A process is being created by another by the process of forking in which the address space of one process is being cloned.
  • Every process also have a Parent Process Id or PPID.

ps : Displays the processes created by the current user.
-f : Option to display PPID as well.
-e or -A : Display all the system process as well.
-u <username> : diplay all the processes created by any particular user.
Eg:
ps -e | grep tty
ps -e

kill <PID> :Terminates a process.

top : Displays the currently running tasks and their CPU utilization.

nohup <command> : Executes the command even if we log out of the system and writes the output to the file nohup.out.
Eg: nohup wget -c www.athena.nitc.ac.in/file.txt &
NB: wget is used to download a file.

pstree : Displays all currently running processes along with their parent child relationships.


time <command>: Executes the command and displays how much time it takes.

netstat : Displays the current network usage and traffic.

vmstat : Displays the current virtual memory usage.

sleep <seconds> : Do nothing for sometime.
Eg: sleep 5
(sleep 1800 ; echo “Lunch Time”) &


I/O Redirection:
> : Used to direct the output to a file.
>> : Used to append the output to a file.
| : Used to take the output of one command to another.

Eg: ls -al > ls.list
top > current.txt


Package Managers:

Redhat Package Manager : RPM

installing an rpm:
rpm -Uvh <path>
-U : Upgrade
-v : Generate more verbose output
-h : Gives a progress bar of installation

To check whether a program is being installed:
rpm -qa | grep gimp

Uninstalling:
rpm -e application

Debian Package Manager : dpkg

To install an application:
sudo dpkg -i package.deb

To check version and details of an installed application:
dpkg -l application
OR
dpkg -l *application*

APT – Advanced Package Manager

sudo apt-get update : Will update the database with list of available packages.

sudo apt-get install package : Installs a new package.

sudo apt-get upgrade : Performs and upgrade.



ssh user@host : Secure Shell Login to a remote server

scp sort.cpp abijith_bcs10@athena.nitc.ac.in: : SSH File Copy

write user [tty] : Used to send messages to other users.

Using gcc:

gcc <filename.c> -o <output_filename>
./output_filename


Different Shells:

sh : Traditional Unix Shell or Bourne Shell
bash : Bourne Again Shell, the default linux shell
ksh : Korn Shell
csh : C-Shell
tcsh : Turbo C Shell

$SHELL : Environment variable that contains the current shell

/etc/shells - File containing the list of available shells.

Shell scripts starts with
#! /bin/bash
so that when they are executed, it will be using BASH by default.
Eg:

#! /bin/bash

echo “Hello World!..”

Save it as sample.sh and give chmod +x sample.txt
Execute it as: ./sample.sh

Tuesday, January 29, 2013

Matrix Addition in NASM

section .bss
  num: resw 1 ;For storing a number, to be read of printed....
  nod: resb 1 ;For storing the number of digits....
  temp: resb 2
  matrix1: resw 200
  matrix2: resw 200
  matrix3: resw 200
  m: resw 1
  n: resw 1
  i: resw 1
  j: resw 1
  
  
section .text
  msg1: db "Enter the number of rows in the matrix : "
  msg_size1: equ $-msg1
  msg2:  db "Enter the elements one by one(row by row) : "
  msg_size2: equ $-msg2
  msg3: db "Enter the number of columns in the matrix : "
  msg_size3: equ $-msg3
  
  msg_matrix1: db "First matrix : ", 10
  msg_matrix1_size: equ $-msg_matrix1
  
  msg_matrix2: db "Second matrix : ", 10
  msg_matrix2_size: equ $-msg_matrix2
  
  
  msg_matrix3: db "Second matrix : ", 10
  msg_matrix3_size: equ $-msg_matrix3

  
  tab: db  9 ;ASCII for vertical tab
  new_line: db 10 ;ASCII for new line
  
section .text
  global _start

_start:
  mov eax, 4
  mov ebx, 1
  mov ecx, msg1
  mov edx, msg_size1
  int 80h
  
  mov ecx, 0
  call read_num  
  mov cx, word[num]
  mov word[m], cx
  
  
  mov eax, 4
  mov ebx, 1
  mov ecx, msg3
  mov edx, msg_size3
  int 80h
  
  mov ecx, 0
  call read_num  
  mov cx, word[num]
  mov word[n], cx
  
  

  
  mov eax, 4
  mov ebx, 1
  mov ecx, msg2
  mov edx, msg_size2
  int 80h
  
  
  ;Reading each element of the matrix........
  mov eax, 0
  mov ebx, matrix1  
  
  mov word[i], 0
  mov word[j], 0
  
  
  i_loop:
    mov word[j], 0
    j_loop:
 
 call read_num
 mov dx , word[num]
 ;eax will contain the array index and each element is 2 bytes(1 word) long
 mov  word[ebx + 2 * eax], dx
 inc eax    ;Incrementing array index by one....
     
 
 
 inc word[j]
 mov cx, word[j]
 cmp cx, word[n]
 jb j_loop
 
    inc word[i]
    mov cx, word[i]
    cmp cx, word[m]
    jb i_loop
  
  
  
  
  
  mov eax, 4
  mov ebx, 1
  mov ecx, msg2
  mov edx, msg_size2
  int 80h
   ;Reading each element of the matrix........
  mov eax, 0
  mov ebx, matrix2  
  
  mov word[i], 0
  mov word[j], 0
  
  
  i_loop2:
    mov word[j], 0
    j_loop2:
 
 call read_num
 mov dx , word[num]
 ;eax will contain the array index and each element is 2 bytes(1 word) long
 mov  word[ebx + 2 * eax], dx
 inc eax    ;Incrementing array index by one....
     
 
 
 inc word[j]
 mov cx, word[j]
 cmp cx, word[n]
 jb j_loop2
 
  
    inc word[i]
    mov cx, word[i]
    cmp cx, word[m]
    jb i_loop2 
  
  
  
;Adding the two matrices ie. matrix3 = matrxi1 + matrix2
  mov eax, 0
  ;Initializing base pointers.....
  mov ebx, matrix1  
  mov esi, matrix2
  mov edi, matrix3
  
  mov word[i], 0
  mov word[j], 0
  
  
  i_loop3:
    mov word[j], 0
    j_loop3:
 
 ;eax will contain the array index and each element is 2 bytes(1 word) long
 mov  dx , word[ebx + 2 * eax]
 add dx, word[esi + 2 * eax]
 mov word[edi + 2 * eax], dx
 
 inc eax    ;Incrementing array index by one....
     
 
 
 inc word[j]
 mov cx, word[j]
 cmp cx, word[n]
 jb j_loop3
 
  
    inc word[i]
    mov cx, word[i]
    cmp cx, word[m]
    jb i_loop3 
  
  
  
  
  
   mov eax, 4
   mov ebx, 1
   mov ecx, msg_matrix1
   mov edx, msg_matrix1_size
   int 80h
  
  
  
  ;Printing the  matrix1....
  mov eax, 0
  mov ebx, matrix1  
  
  mov word[i], 0
  mov word[j], 0
  
  
  i_loop4:
    mov word[j], 0
    j_loop4:
 
   ;eax will contain the array index and each element is 2 bytes(1 word) long
   mov  dx, word[ebx + 2 * eax]   ;
   mov word[num] , dx
   call print_num
   
   ;Printing a space after each element.....
   pusha
     mov eax, 4
     mov ebx, 1
     mov ecx, tab
     mov edx, 1
     int 80h    
   popa
   
   inc eax  
     
     
 inc word[j]
 mov cx, word[j]
 cmp cx, word[n]
 jb j_loop4
 
  pusha
     mov eax, 4
     mov ebx, 1
     mov ecx, new_line
     mov edx, 1
     int 80h    
   popa
 
    inc word[i]
    mov cx, word[i]
    cmp cx, word[m]
    jb i_loop4
  
  
  
  
  
  
  
  
  
  ;Printing the final matrix2....
  
   mov eax, 4
   mov ebx, 1
   mov ecx, msg_matrix2
   mov edx, msg_matrix2_size
   int 80h
   
  mov eax, 0
  mov ebx, matrix2  
  
  mov word[i], 0
  mov word[j], 0
  
  
  i_loop5:
    mov word[j], 0
    j_loop5:
 
   ;eax will contain the array index and each element is 2 bytes(1 word) long
   mov  dx, word[ebx + 2 * eax]   ;
   mov word[num] , dx
   call print_num
   
   ;Printing a space after each element.....
   pusha
     mov eax, 4
     mov ebx, 1
     mov ecx, tab
     mov edx, 1
     int 80h    
   popa
   
   inc eax  
     
     
 inc word[j]
 mov cx, word[j]
 cmp cx, word[n]
 jb j_loop5
 
  pusha
     mov eax, 4
     mov ebx, 1
     mov ecx, new_line
     mov edx, 1
     int 80h    
   popa
 
    inc word[i]
    mov cx, word[i]
    cmp cx, word[m]
    jb i_loop5
  
  
  
  
  
    ;Printing the final matrix....
   mov eax, 4
   mov ebx, 1
   mov ecx, msg_matrix3
   mov edx, msg_matrix3_size
   int 80h
  mov eax, 0
  mov ebx, matrix3  
  
  mov word[i], 0
  mov word[j], 0
  
  
  i_loop6:
    mov word[j], 0
    j_loop6:
 
   ;eax will contain the array index and each element is 2 bytes(1 word) long
   mov  dx, word[ebx + 2 * eax]   ;
   mov word[num] , dx
   call print_num
   
   ;Printing a space after each element.....
   pusha
     mov eax, 4
     mov ebx, 1
     mov ecx, tab
     mov edx, 1
     int 80h    
   popa
   
   inc eax  
     
     
 inc word[j]
 mov cx, word[j]
 cmp cx, word[n]
 jb j_loop6
 
  pusha
     mov eax, 4
     mov ebx, 1
     mov ecx, new_line
     mov edx, 1
     int 80h    
   popa
 
    inc word[i]
    mov cx, word[i]
    cmp cx, word[m]
    jb i_loop6
  

  
  

  
;Exit System Call.....
exit:
  mov eax, 1
  mov ebx, 0
  int 80h

  
  
;Function to read a number from console and to store that in num 
read_num:

  pusha
  mov word[num], 0
  
  loop_read:
    mov eax, 3
    mov ebx, 0
    mov ecx, temp
    mov edx, 1
    int 80h
    
    cmp byte[temp], 10
      je end_read
    
    mov ax, word[num]
    mov bx, 10
    mul bx
    mov bl, byte[temp]
    sub bl, 30h
    mov bh, 0
    add ax, bx
    mov word[num], ax
    jmp loop_read 
  end_read:
  popa
  
ret


;Function to print any number stored in num...
print_num:
  pusha
  extract_no:
    cmp word[num], 0
    je print_no
    inc byte[nod]
    mov dx, 0
    mov ax, word[num]
    mov bx, 10
    div bx
    push dx
    mov word[num], ax
  jmp extract_no

  print_no:
    cmp byte[nod], 0
    je end_print
    dec byte[nod]
    pop dx
    mov byte[temp], dl
    add byte[temp], 30h


    mov eax, 4
    mov ebx, 1
    mov ecx, temp
    mov edx, 1
    int 80h
    
    jmp print_no
    
  end_print:   
  popa
ret 

Matrix in NASM

section .bss
  num: resw 1 ;For storing a number, to be read of printed....
  nod: resb 1 ;For storing the number of digits....
  temp: resb 2
  matrix1: resw 200
  m: resw 1
  n: resw 1
  i: resw 1
  j: resw 1
  
  
section .text
  msg1: db "Enter the number of rows in the matrix : "
  msg_size1: equ $-msg1
  msg2:  db "Enter the elements one by one(row by row) : "
  msg_size2: equ $-msg2
  msg3: db "Enter the number of columns in the matrix : "
  msg_size3: equ $-msg3
  tab: db  9 ;ASCII for vertical tab
  new_line: db 10 ;ASCII for new line
  
section .text
  global _start

_start:
  mov eax, 4
  mov ebx, 1
  mov ecx, msg1
  mov edx, msg_size1
  int 80h
  
  mov ecx, 0
  call read_num  
  mov cx, word[num]
  mov word[m], cx
  
  
  mov eax, 4
  mov ebx, 1
  mov ecx, msg3
  mov edx, msg_size3
  int 80h
  
  mov ecx, 0
  call read_num  
  mov cx, word[num]
  mov word[n], cx
  
  
  
  mov eax, 4
  mov ebx, 1
  mov ecx, msg2
  mov edx, msg_size2
  int 80h
  
  
  ;Reading each element of the matrix........
  mov eax, 0
  mov ebx, matrix1  
  
  mov word[i], 0
  mov word[j], 0
  
  
  i_loop:
    mov word[j], 0
    j_loop:
 
 call read_num
 mov dx , word[num]
 ;eax will contain the array index and each element is 2 bytes(1 word) long
 mov  word[ebx + 2 * eax], dx
 inc eax    ;Incrementing array index by one....
     
 
 
 inc word[j]
 mov cx, word[j]
 cmp cx, word[n]
 jb j_loop
 
    inc word[i]
    mov cx, word[i]
    cmp cx, word[m]
    jb i_loop
  
  
  
  
  
  
  
    ;Reading each element of the matrix.(Storing the elements in row major order).......
  mov eax, 0
  mov ebx, matrix1  
  
  mov word[i], 0
  mov word[j], 0
  
  
  i_loop2:
    mov word[j], 0
    j_loop2:
 
   ;eax will contain the array index and each element is 2 bytes(1 word) long
   mov  dx, word[ebx + 2 * eax]   ;
   mov word[num] , dx
   call print_num
   
   ;Printing a space after each element.....
   pusha
     mov eax, 4
     mov ebx, 1
     mov ecx, tab
     mov edx, 1
     int 80h    
   popa
   
   inc eax  
     
     
 inc word[j]
 mov cx, word[j]
 cmp cx, word[n]
 jb j_loop2
 
  pusha
     mov eax, 4
     mov ebx, 1
     mov ecx, new_line
     mov edx, 1
     int 80h    
   popa
 
    inc word[i]
    mov cx, word[i]
    cmp cx, word[m]
    jb i_loop2
  

  
  

  
;Exit System Call.....
exit:
  mov eax, 1
  mov ebx, 0
  int 80h

  
  
;Function to read a number from console and to store that in num 
read_num:

  pusha
  mov word[num], 0
  
  loop_read:
    mov eax, 3
    mov ebx, 0
    mov ecx, temp
    mov edx, 1
    int 80h
    
    cmp byte[temp], 10
      je end_read
    
    mov ax, word[num]
    mov bx, 10
    mul bx
    mov bl, byte[temp]
    sub bl, 30h
    mov bh, 0
    add ax, bx
    mov word[num], ax
    jmp loop_read 
  end_read:
  popa
  
ret


;Function to print any number stored in num...
print_num:
  pusha
  extract_no:
    cmp word[num], 0
    je print_no
    inc byte[nod]
    mov dx, 0
    mov ax, word[num]
    mov bx, 10
    div bx
    push dx
    mov word[num], ax
  jmp extract_no

  print_no:
    cmp byte[nod], 0
    je end_print
    dec byte[nod]
    pop dx
    mov byte[temp], dl
    add byte[temp], 30h


    mov eax, 4
    mov ebx, 1
    mov ecx, temp
    mov edx, 1
    int 80h
    
    jmp print_no
    
  end_print:   
  popa
ret 

NASM: Searching for an element in Array

;Program to search for an element in an array...
section .bss
  num: resw 1 ;For storing a number, to be read of printed....
  nod: resb 1 ;For storing the number of digits....
  temp: resb 2
  array1: resw 50
  size1: resw 1

  
section .text
  msg1: db "Enter the number of elements in the array : "
  msg_size1: equ $-msg1
  msg2:  db "Enter the elements one by one"
  msg_size2: equ $-msg2
  space: db  " "
  msg3:  db "Enter the element to be searched : "
  msg_size3: equ $-msg3
  msg_found: db "Element Found..."
  msg_found_size: equ $-msg_found
  msg_not_found: db "Element Not Found..."
  msg_not_found_size: equ $-msg_not_found
  
  
  
  
section .text
  global _start

_start:
  mov eax, 4
  mov ebx, 1
  mov ecx, msg1
  mov edx, msg_size1
  int 80h
  
  mov ecx, 0
  call read_num  
  mov cx, word[num]
  mov word[size1], cx
  
  push ecx
  
  mov eax, 4
  mov ebx, 1
  mov ecx, msg2
  mov edx, msg_size2
  int 80h
  
  pop ecx ;Array length in ecx, so we can loop to read elements
  ;Reading the array elements....
  mov eax, 0
  mov ebx, array1
  
  read_element:
    call read_num
    mov dx , word[num]
    ;eax will contain the array index and each element is 2 bytes(1 word) long
    mov  word[ebx + 2 * eax], dx
    inc eax    ;Incrementing array index by one....
  loop read_element
  

    mov eax, 4
    mov ebx, 1
    mov ecx, msg3
    mov edx, msg_size3
    int 80h
  
    call read_num
    mov ax, word[num] ;Element to be searched....
  
  ;Searching for the element....
     CLD ;Clears the Direction Flag
     mov  edi, array1 ;Copy Base address of array to index registers
     mov  ecx, 10 ;No: of element in the array

      scan:
       SCASW
       je found
      loop scan
       ;Code to display element not found..
       mov eax, 4
       mov ebx, 1
       mov ecx, msg_not_found
       mov edx, msg_not_found_size
       int 80h
       jmp exit
       
      
      found:
       mov eax, 4
       mov ebx, 1
       mov ecx, msg_found
       mov edx, msg_found_size
       int 80h
      

  
;Exit System Call.....
exit:
  mov eax, 1
  mov ebx, 0
  int 80h

  
  
;Function to read a number from console and to store that in num 
read_num:

  pusha
  mov word[num], 0
  
  loop_read:
    mov eax, 3
    mov ebx, 0
    mov ecx, temp
    mov edx, 1
    int 80h
    
    cmp byte[temp], 10
      je end_read
    
    mov ax, word[num]
    mov bx, 10
    mul bx
    mov bl, byte[temp]
    sub bl, 30h
    mov bh, 0
    add ax, bx
    mov word[num], ax
    jmp loop_read 
  end_read:
  popa
  
ret


;Function to print any number stored in num...
print_num:
  pusha
  extract_no:
    cmp word[num], 0
    je print_no
    inc byte[nod]
    mov dx, 0
    mov ax, word[num]
    mov bx, 10
    div bx
    push dx
    mov word[num], ax
  jmp extract_no

  print_no:
    cmp byte[nod], 0
    je end_print
    dec byte[nod]
    pop dx
    mov byte[temp], dl
    add byte[temp], 30h


    mov eax, 4
    mov ebx, 1
    mov ecx, temp
    mov edx, 1
    int 80h
    
    jmp print_no
    
  end_print:   
  popa
ret 

NASM: Searching for an element in Array

;Program to search for an element in an array...
section .bss
  num: resw 1 ;For storing a number, to be read of printed....
  nod: resb 1 ;For storing the number of digits....
  temp: resb 2
  array1: resw 50
  size1: resw 1

  
section .text
  msg1: db "Enter the number of elements in the array : "
  msg_size1: equ $-msg1
  msg2:  db "Enter the elements one by one"
  msg_size2: equ $-msg2
  space: db  " "
  msg3:  db "Enter the element to be searched : "
  msg_size3: equ $-msg3
  msg_found: db "Element Found..."
  msg_found_size: equ $-msg_found
  msg_not_found: db "Element Not Found..."
  msg_not_found_size: equ $-msg_not_found
  
  
  
  
section .text
  global _start

_start:
  mov eax, 4
  mov ebx, 1
  mov ecx, msg1
  mov edx, msg_size1
  int 80h
  
  mov ecx, 0
  call read_num  
  mov cx, word[num]
  mov word[size1], cx
  
  push ecx
  
  mov eax, 4
  mov ebx, 1
  mov ecx, msg2
  mov edx, msg_size2
  int 80h
  
  pop ecx ;Array length in ecx, so we can loop to read elements
  ;Reading the array elements....
  mov eax, 0
  mov ebx, array1
  
  read_element:
    call read_num
    mov dx , word[num]
    ;eax will contain the array index and each element is 2 bytes(1 word) long
    mov  word[ebx + 2 * eax], dx
    inc eax    ;Incrementing array index by one....
  loop read_element
  

    mov eax, 4
    mov ebx, 1
    mov ecx, msg3
    mov edx, msg_size3
    int 80h
  
    call read_num
    mov ax, word[num] ;Element to be searched....
  
  ;Searching for the element....
     CLD ;Clears the Direction Flag
     mov  edi, array1 ;Copy Base address of array to index registers
     mov  ecx, 10 ;No: of element in the array

      scan:
       SCASW
       je found
      loop scan
       ;Code to display element not found..
       mov eax, 4
       mov ebx, 1
       mov ecx, msg_not_found
       mov edx, msg_not_found_size
       int 80h
       jmp exit
       
      
      found:
       mov eax, 4
       mov ebx, 1
       mov ecx, msg_found
       mov edx, msg_found_size
       int 80h
      

  
;Exit System Call.....
exit:
  mov eax, 1
  mov ebx, 0
  int 80h

  
  
;Function to read a number from console and to store that in num 
read_num:

  pusha
  mov word[num], 0
  
  loop_read:
    mov eax, 3
    mov ebx, 0
    mov ecx, temp
    mov edx, 1
    int 80h
    
    cmp byte[temp], 10
      je end_read
    
    mov ax, word[num]
    mov bx, 10
    mul bx
    mov bl, byte[temp]
    sub bl, 30h
    mov bh, 0
    add ax, bx
    mov word[num], ax
    jmp loop_read 
  end_read:
  popa
  
ret


;Function to print any number stored in num...
print_num:
  pusha
  extract_no:
    cmp word[num], 0
    je print_no
    inc byte[nod]
    mov dx, 0
    mov ax, word[num]
    mov bx, 10
    div bx
    push dx
    mov word[num], ax
  jmp extract_no

  print_no:
    cmp byte[nod], 0
    je end_print
    dec byte[nod]
    pop dx
    mov byte[temp], dl
    add byte[temp], 30h


    mov eax, 4
    mov ebx, 1
    mov ecx, temp
    mov edx, 1
    int 80h
    
    jmp print_no
    
  end_print:   
  popa
ret 

NASM: Copying one array to another

section .bss
  num: resw 1 ;For storing a number, to be read of printed....
  nod: resb 1 ;For storing the number of digits....
  temp: resb 2
section .text
  global _start

_start:
  call read_num
  call print_num

exit:
  mov eax, 1
  mov ebx, 0
  int 80h
 
;Function to read a number from console and to store that in num 
read_num:

  pusha
  mov word[num], 0
  
  loop_read:
    mov eax, 3
    mov ebx, 0
    mov ecx, temp
    mov edx, 1
    int 80h
    
    cmp byte[temp], 10
      je end_read
    
    mov ax, word[num]
    mov bx, 10
    mul bx
    mov bl, byte[temp]
    sub bl, 30h
    mov bh, 0
    add ax, bx
    mov word[num], ax
    jmp loop_read 
  end_read:
  popa
  
ret


;Function to print any number
print_num:
  pusha
  extract_no:
    cmp word[num], 0
    je print_no
    inc byte[nod]
    mov dx, 0
    mov ax, word[num]
    mov bx, 10
    div bx
    push dx
    mov word[num], ax
  jmp extract_no

  print_no:
    cmp byte[nod], 0
    je end_print
    dec byte[nod]
    pop dx
    mov byte[temp], dl
    add byte[temp], 30h


    mov eax, 4
    mov ebx, 1
    mov ecx, temp
    mov edx, 1
    int 80h
    
    jmp print_no
    
  end_print:   
  popa
ret 

Reading a 1-Dimensional array in NASM

section .bss
  num: resw 1 ;For storing a number, to be read of printed....
  nod: resb 1 ;For storing the number of digits....
  temp: resb 2
  array1: resw 50
  size1: resw 1
  
section .text
  msg1: db "Enter the number of elements in the array : "
  siz1: equ $-msg1
  msg2:  db "Enter the elements one by one"
  size2: equ $-msg2
  space: db  " "
  
section .text
  global _start

_start:
  mov eax, 4
  mov ebx, 1
  mov ecx, msg1
  mov edx, siz1
  int 80h
  
  mov ecx, 0
  call read_num  
  mov cx, word[num]
  mov word[size1], cx
  
  push ecx
  
  mov eax, 4
  mov ebx, 1
  mov ecx, msg2
  mov edx, size2
  int 80h
  
  pop ecx ;Array length in ecx, so we can loop to read elements
  ;Reading the array elements....
  mov eax, 0
  mov ebx, array1
  
  read_element:
    call read_num
    mov dx , word[num]
    ;eax will contain the array index and each element is 2 bytes(1 word) long
    mov  word[ebx + 2 * eax], dx
    inc eax    ;Incrementing array index by one....
  loop read_element
  
  
  ;Printing the elements.....
  movzx  ecx, word[size1]
  mov eax, 0
  mov ebx, array1
  
  print_element:
  ;eax will contain the array index and each element is 2 bytes(1 word) long
    mov  dx, word[ebx + 2 * eax]   ;
    mov word[num] , dx
    call print_num
    
    ;Printing a space after each element.....
    pusha
      mov eax, 4
      mov ebx, 1
      mov ecx, space
      mov edx, 1
      int 80h    
    popa
    
    inc eax  
  loop print_element
  
  

  
;Exit System Call.....
exit:
  mov eax, 1
  mov ebx, 0
  int 80h

  
  
;Function to read a number from console and to store that in num 
read_num:

  pusha
  mov word[num], 0
  
  loop_read:
    mov eax, 3
    mov ebx, 0
    mov ecx, temp
    mov edx, 1
    int 80h
    
    cmp byte[temp], 10
      je end_read
    
    mov ax, word[num]
    mov bx, 10
    mul bx
    mov bl, byte[temp]
    sub bl, 30h
    mov bh, 0
    add ax, bx
    mov word[num], ax
    jmp loop_read 
  end_read:
  popa
  
ret


;Function to print any number stored in num...
print_num:
  pusha
  extract_no:
    cmp word[num], 0
    je print_no
    inc byte[nod]
    mov dx, 0
    mov ax, word[num]
    mov bx, 10
    div bx
    push dx
    mov word[num], ax
  jmp extract_no

  print_no:
    cmp byte[nod], 0
    je end_print
    dec byte[nod]
    pop dx
    mov byte[temp], dl
    add byte[temp], 30h


    mov eax, 4
    mov ebx, 1
    mov ecx, temp
    mov edx, 1
    int 80h
    
    jmp print_no
    
  end_print:   
  popa
ret 

Read and print a 16 bit number in NASM

section .bss
  num: resw 1 ;For storing a number, to be read of printed....
  nod: resb 1 ;For storing the number of digits....
  temp: resb 2
section .text
  global _start

_start:
  call read_num
  call print_num

exit:
  mov eax, 1
  mov ebx, 0
  int 80h
 
;Function to read a number from console and to store that in num 
read_num:

  pusha
  mov word[num], 0
  
  loop_read:
    mov eax, 3
    mov ebx, 0
    mov ecx, temp
    mov edx, 1
    int 80h
    
    cmp byte[temp], 10
      je end_read
    
    mov ax, word[num]
    mov bx, 10
    mul bx
    mov bl, byte[temp]
    sub bl, 30h
    mov bh, 0
    add ax, bx
    mov word[num], ax
    jmp loop_read 
  end_read:
  popa
  
ret


;Function to print any number
print_num:
  pusha
  extract_no:
    cmp word[num], 0
    je print_no
    inc byte[nod]
    mov dx, 0
    mov ax, word[num]
    mov bx, 10
    div bx
    push dx
    mov word[num], ax
  jmp extract_no

  print_no:
    cmp byte[nod], 0
    je end_print
    dec byte[nod]
    pop dx
    mov byte[temp], dl
    add byte[temp], 30h


    mov eax, 4
    mov ebx, 1
    mov ecx, temp
    mov edx, 1
    int 80h
    
    jmp print_no
    
  end_print:   
  popa
ret