Sharing! What gained.

Showing posts with label Computer. Show all posts
Showing posts with label Computer. Show all posts

Oct 12, 2016

Regular Expression and Applications


Regular Expression is an expression which is used for defining a language, this definition look very simple but whole world reside this definition. Some core functionality which are provided by Regular Expression or we can say it’s implementation are as follow:

  1. String matching
  2. Search
  3. Replace

In Programming Language

It was first basic implemented in SNOBOL language in earlies 1960s. Later on enhanced version of RE was implemented in text editor for example in grep and ed, but the new boost was added in the field of regular expression when more complex engine and complicated regular expression were introduced in Perl which was originally derived from work of Henry Spencer in 1986 who later implemented his work in Tcl language which used NFA/DFA implementation for Regular Expression implementation which was highly praised.

  1. Perl - Available in it syntax
  2. Python
    1. Import re
  3. Java
    1. import java.util.regex
  4. C++ 
    1. #include<regex> was introduce in C++ in C11
  5. PHP 
    1. PCRE library (Perl Compatible) 

And today regular expression in part of standard library, hence it is available in all languages available today eg. R, Rust, ECMAScript, Python, Go, C# and etc. 

Today, Perl syntax for regular expression is followed in Java, JavaScript, PHP, Python, Ruby, Microsoft's .NET Framework, and XML Schema.

Applications

Regular have ignited further computer science field for example Natural Language Processing, Data Mining, Compiler, Automata and etc.

  1. Validation of registration form.
    1. Some default implementation available in HTML5 are email, number, url, tel (telephone), text for type attribute in HTML5.

  2. Apache Web Server, Some of it uses in created wild card domain used when created sub domain in server side. 

    1. Sub domain: *.example.com, This will redirect all sub domain queries to main script file of example.com. This approach is used in all shared hosting provider for example wordpress.com, yola.com, weebly.com and etc.
    2. Redirect Rule: In some cases we know want some core file to not get public so we hide that page using Redirect Rule which redirect based on specific query for example we want to hide all cache folder,  ini and file then query will be something like cache/*, *.ini
  3. Google Search Engine, It provied some special command in search engine, such as:
    1. “HTML Tutorial  – w3schools.com” this will bring all result that don’t contain w3schools in it page.
    2. “a * saved is a * earned” * can be used in place of unknown words.
  4. Text editor, We have built-in regular expression added earlier in Ed editor and in todays century it is available in Notepad++, Sublime Text, Atom, Visual Studio.
    1. Using in Notepad++, Check Regular expression option and write your expression in search box.
  5. grep, When there wasn’t GUI thing around and command line was the only interface, grep was introduced in 1974  to string matching using regular expression and it is still king for string matching in command line and for its easy syntax.
example.txt file contains name of fruits.

Where have I used RE?

  1. To minify HTML, JS and CSS. I’ve use Notepad++ RE functionality to replace newlines and repeated spaces.
  2. While configuring Web server and defining Rewrite rule in .htaccess files.
  3. For validating input in site for valid email, username, password.
  4. Used in one of my project for tokenizing SQL in htmSQL where we can parse HTML using SQL.
  5. Sometime use negative sign in Google search engine.
  6. For defining routes in MVC framework such as ExpressJS, Laravel, Django.
  7. Web scraping; when extracting product name and product price from a page.
  8. SQL LIKE operator eg. “%input%”
  9. Last not the least, In Automata course exams for defining RE from given description definition.

Hacking

  1. As RE syntax was enhanced: recursive and sub expression functionality was added, which allow us to write more complex and complicated expression but It can be exploited by analyst by inputting such string which take so long to process that server crashes (Regular expression Denial of Service). 
  2. Regular Expression, to validate whether your term is search term or url, Google Chrome have RE written for that, which was exploited earlier this year which make phishing attack easier on Firefox and Chrome. (LINK)



Sep 3, 2015

Overview of SAP 1 (Simple as Possible)


Introduction

Making of ALU is always a very simple task for a motivated DLD student, they only have to pour dozens of NAND gates, spoon of multiplexer, some latches as per taste and half tea spoon of Clocks, Then stir it with constant supply of electron, In the end we get our dearest ALU which talk to itself only.
However If you been into making them you would understand every bit of ALU now. You would know the internal structure and how every bit sum up to do awesome calculation for us. To understand the internal behavior of CPU, Albert Paul Malvino designed a simple model for understanding the basic functionality of Microprocessor. That design he presented is called “Simple as Possible Processor” or in short SAP.

SAP 1

Understanding these processor help the student to grab the actual logic and concept which run behind Computer to perform amazing task which can be seen in Game, Graph, Video Processing and Operating System.

Specification

- 8 bit architecture
- 16 Bytes RAM
- 8 bit W bus
    - 4 bit address
    - 8 bit data
- Register
    - A
    - B
    - Output Register
- Control Unit
- 8 LEDS

SAP are so simple processor that it have only 4 bit address, which mean it can only address 16 addresses of memory that is the reason it have only 16 Bytes Ram.
Normally instruction occupies 3+ byte in modern computer and SAP instruction are maximum of size 1Byte and It memory can hold at-most 8 byte and 8 byte for data only whereas modern processor can contain trillions of instruction and can compute billions of them in a second.

SAP have only 5 instruction

Instruction
Op Code
Description
LDA
0000
Load memory content to accumulator

ADD
0001
Add the only operand to accumulator
SUB
0010
Subtract only operand to accumulator
OUT
1110
Move accumulator data to output register
HLT
1111
HLT Stop Processing

Example Code

LDA  8H                 ; Load 8H memory data to A register
ADD 9H
ADD AH
SUB BH
OUT
HLT

Only LDA, ADD, SUB have 1 operand while other don’t.

Code and data is loaded to TTL (Transister-Transister Logic) before starting processor. That is the reason there is no MOV command for moving data to memory.

Instruction Format

ADD                       [16]
0001                       0010
Op Code              Address

Working

Everything start from Control Unit, Control Unit turn on Counter which generate address and move it to bus then CU turn on MAR which fetch address from bus and return data at given address to the bus, then CU instruct Instruction Register (IR) to collect data from bus, Then IR move opcode to Control Sequencer (CU) and address bus to Bus. Depended on the opcode CU perform further action.

For example if instruction is LDA then CU will first resolve the address in bus to data and then instruct A register to collect it from bus. Then CU will again move to Counter to get address of another instruction for example of ADD then in this case. Then the instruction will move to IR and address to bus. According to the opcode CU first resolve address to data by consulting MAR and then move that data to B register and then move instruct ADD/SUB unit to perform appropriate work.  Depending on the further instruction CU can output A register data to output LEDs
Note: Control Sequencer have 12 output to control every element of processor according to operation code.

SAP 2

SAP 2 is also in market which have 8 bit data bus and 16 bit data bus, Because of increased data bus It can address 64K of RAM. It have two more register B and TMP. SAP 2 have more instruction then SAP 1 such as JMP, SHF, MUL and etc. Detail can be seen in below table.

SAP 1
SAP 2
Full Form
Simple As Possible 1
Simple as Possible 2
Logical operations
Cannot be handled
Can be handled
Addresses
8 bits
16 bits
Opcodes
4 bits
8 bits
Instructions
Fixed length
 Variable length.
Adder/substactor unit
Carries out simple operations like addition and subtraction
Carries out logical operations too and therefore also known as ARITHMETIC/LOGIC UNIT
Size of control sequencer
Smaller
Bigger due to extra registers
Number of instructions
Comparatively lesser
Comparatively greater
Register to hold the data being added or subtracted from the accumulator
B
TMP
Jump instructions
Not available
JM, JZ and JNZ – conditional jumps
JMP – unconditional jumps
Instructions
Includes instruction – LDA, ADD, SUB, OUT and HLT
Includes instructions like ADD, SUB, INR, JMP, DCR, ANA, ORA, XRA, ANI, ORI, XRI, etc.
Display to show outputs in the LEDs
Binary
Hexadecimal

Further Reading:


Jul 20, 2015

C++ Shadowing


Shadowing in common English means the Shade of one thing over the other.  Shadowing is the concept of OOP  which allow the programmer to reuse the same Datatypes in different classes or in other words its the overriding of the Datatypes,Its advantage is that we can use less memory and take more work from Datatypes .


Jul 13, 2015

C++ Friends Function


One of the features of OOP is know as friends function ,I explain this to my students by giving the example ,That lets suppose that you have a friend to whom to tell your are private stuff like secrets and that friend does not belong to your family like he is your friend but not member of your family and you tell him whats happening in your personal life similarly friend function in not a member function of class but have the capability to use the private Datatypes members of class outside of the class such a function is known as friends function .It will be further cleared by looking at this example.


C++ Constructor Overloading


Construction overloading is no different than simple overloading the function they are all most the same ,Lets follow the process first made simple constructor with no argument,Then made another constructor with 2 or more argument according to your requirement and as we know that for each object that we make in main body there is constructor called separately so now when we want to call 2 argument or more argument constructor that we have declared in class we will declare the object then after the object we will made the round bracket i.e () and we will enter the values that we want to pass with the following example it will be further cleared.

Jul 10, 2015

Idea Behind Pentium 4, Hyper-threading and Multi-core


You would have come across word like dual core, hyper threading or may be single core in these time where mobile are quad core at least.

Let me explain it the way I understand it my-self. Let consider there is chef in kitchen who is instructed to bake pizza,  fry egg and cook tea. Now for time, let name this kitchen Pentium 4. The way he will follow instruction is that he will first bake pizza then fry egg and at last cook tea. The total time he would consume will be a hour for baking pizza, 10 minutes in egg and 5 minutes for tea. In this kitchen 1 hour and 15 minutes will be required for these task.