Sharing! What gained.

Feb 1, 2016

How To Declare Variables and How to Use Them and basic Input Output statements


Variables Types:

Variables are the basic block's of any programming language now in some languages there are formal names for declaring them while on the other side of spectrum some languages do not demand specific names for declaring them i.e Python etc. In most of the language we follow the specific syntax for declaring them.
Variable  in programming is just like a variable that you used in calculus,Linear Algebra etc but here for declaring almost every thing you will first have to assign it to a variable to make your code more dynamic and less static,By dynamic I mean you can make you application or program more user friendly.

How TO Declare Them?

Almost all the language that I have personally used so far have a very slightest difference of declaring the data Type.We will stick to Vb(Visual Basic) here
So,Like in every other language variable declaration Structure, Visual Basic have also its own Variable declaration structure which is not much different from other languages

Dim <variable name> As <data type>

Now I will explain each and every part of the declaration structure.Let's start with DIM ,You must be thinking what does Dim do ?It's some thing that's different and new for a programming who have worked in other languages like C++,C# etc Dim actually tells the computer that what type of the data is been declared and what type of the data is given by the user or in other words Declares and allocate storage space for variables then the second part is the name of the variable which we will use to use it during writing out code just lie we declare  X in calculus or Linear Algebra and the last part is telling the computer what type of the data is declared i.e string,integer,double or float etc.
Now I will conclude by a piece of code which will show the declaration of variables.
 

 How to use them together in same code?How to use input output statements?

Now next we will see how to use each one of them through a coding examples


 In Visual basic we take output by writing "Console.WriteLine("Enter the variable name or string you want to print")" and for input you we will simply use "Console.ReadLine()" now if you see this code we have printed the string whose name is A1 then we have taken a number and by putting '='(assignment operator) we have assigned the value to the integer type variable we named var1 that we have taken from the user then we have repeated the same thing to enter the value in B1(name of variable declared) then by  / operator we have divide the values that we have assigned to A1 & B1 .



Dec 5, 2015

Getting Started with Cordova


What is Cordova?

Cordova: a mobile development tool for every platform: Android, iOS, WP, Firefox, Browser, Tizen, BB...

Installation

To start installation of the Cordova
First Download and install NodeJS
To confirm installation the installation open command prompt and type
node –v
Then we have to download Cordova. Cordova CLI is built on JavaScript to download Cordova the best option is to get it using NPM (node package manager).
NPM is installed with Node os you won’t have to download it from somewhere.
Run this command to download Cordova
npm install –g cordova
To confirm the insllation of cordova run this command
cordova –version
or just
cordova
Now at this point you can make an application and test it on browser but For building and running it on Android devices you will have to install Android SDK which have dependencies

  1. Download and install JDK (Java)
  2. Download and install Android SDK
  3. Download and install Apache Ant

After installation we have to add JDK, Android SDK and Apache Ant location to path variables.
Android SDK path are mostly %APPDATA%/Roaming/Android/sdk
%APPDATA%/Roaming/Android/sdk

%APPDATA%/Roaming/Android/sdk/tools

%APPDATA%/Roaming/Android/sdk/platform-tools

(PATH TO APACHE ANT)/bin

(PATH TO JDK)/bin

All these path will need to be set in system enviorment variables.

Setting Enviorment Path

  1. To add these the PATH,
  2. open the Control Panel
  3. click System and Security
  4. click System
  5. click Change settings
  6. click the Advanced tab
  7. click Environment Variables button.
  8. In the list User variables select PATH
  9. Then click the Edit button.
At the end of the field Variable value, add a semicolon followed by the path for every location (SDK, ANT, JDK).

We will have to create some enviorment variable also, which are
ANDROID_HOME = %APPDATA%/Roaming/Android/sdk
JAVA_HOME = (PATH TO JDK)
ANT_HOME = (PATH TO APACHE ANT)
You can add them in Environment variables windows you open previously.
Now to test the working, open command prompt and type
adb
Now we have to install a android platform in Android SDK to do that type android in command prompt and a windows will open which select Android 5 (Level 22) and install.

Creating First Application

Open command prompt
Type
cd Desktop
Then type
cordova create HelloWorld
Then type
cd HelloWorld
cordova platform add browser
To test it on browser, type
cordova serve
To build and run it on Android Device

Running on Android Device

We will have to add platform code to cordova app, Type
cordova platform add android
Then build the application
cordova build android
To check for the connected Android device, Type
adb devices
You will get the list of all connected device.
Note: If It doesn’t work check whether Debugging mode is enable in Android and MTP is disabled or not in phone. Then kill adb server if you didn’t get the device in first attempt
adb kill-server
adb start-server
adb devices
If issue continues here can get help here.

Hopefully you will get the result.
To run on connect device
cordova run android
The application will automaticallty start on the phone and Enjoy!

Adding UI in Application

When writing application, We have JavaScript, HTML and CSS in hand for everything. By default you application will appear as classic html element to add beauty to you would have to CSS, to design every element of application.
There are multiple frameworks available which will handle all these UI related stuff for us.

  1. Ionic Framework (Recommended)
  2. Onsen Framework  

Cordova Online

Everyone want quick start to start developing now there is an amazing codova online IDE available with Build option. Give it a try!

  1. Monaca - IDE + Build (Free)
  2. Build PhoneGap - Build your code online (Free)
  3. Appery - IDE + Build (Paid)
  4. ShoutEm

Using Visual Studio 2015

Instead of going through all these steps you can just get a copy of Visual Studio Community and  check Cordova and Git in Custom options while installing and it will install all these stuff: Java, Android SDK, Apache Ant, Cordova and any dependecies needed to run.

However both these step needs 3-5 hour of your time, downloads 5+ GB of stuff from internet and It take about 30+ GB space in Harddrive. 

Conclusion

I would personally suggest to use Visual Studio 2015 because of it simplicity. However I started working on Cordova through CLI in start of this year when there was no tools like these and best of luck with mobile app development.

Comprehensive Guide on Cordova Installation: https://evothings.com/doc/build/cordova-install-windows.html


Oct 19, 2015

Interpreting Floating Binary for Printing in Assembly


Splitting of REAL4 Binary

As we know that the binary of single precision float contains 1 sign bit, 8 exponent bit and 22 mantissa bit.

Sign Bit

For extracting sign bit we have to shift number, 31 times to right.

.data
            num REAL4 -1.5
.code
            mov eax, num
            shr eax, 31
            ; eax = 1 then num is negative
            ; eax = 0 then num is positive.

Exponent

For extracting exponent from REAL4 binary, we will first shift the number 23 times to right and then we will set all other bit to 0 except bit 0-7, with the help of AND instruction.

.data
            num REAL4 -1.5
.code
            mov eax, num
            shr eax, 23
            and eax, 0FFh
            ; Now eax will contain exponent
            ; you can access it in al as well.

            sub al, 127 ; for further processing

For getting appropriate value from these bit you would have to subtract -127 from the extracted exponent bits.

Mantissa

We can extract mantissa from the number, with the help of a simple AND instruction, then we will set all the bit from 22-31 by 0 and keep the rest of the mantissa bits as it is.
In this case we won’t need shifting.

.data
            num REAL4 -1.5
.code
            mov eax, num
            and eax, 07FFFFFh
            ; eax not contain 22 bit mantissa

Printing of floating

Sign: You must be kidding. (Print ‘+’ if sign bit is 0 else print ‘-‘)

Calculating Mantissa value (Value after point)

Let say we have 8 bit REAL number, with 3 exponent bit and 4 bits mantissa. In this case we will have exponent max value of 7 and mid value 3

Num = 1 100 0100 (-3.50000)
Sign = 1
Exp = 100
Mantissa = 0100

Getting integer value from mantissa
Exp = 4 - 3 = 1

Now for further processing we will add 1 in starting of mantissa like this: 1.0100
And then move point as per exponent value, then we will get 11.100, then
We can simply print left value of mantissa which is equal to 3 but the problem is with 100 right bit in mantissa which is 100, if we simply look at them, then they are equal to 4 but this is not the case. It is equal to 0.5000

For solving this issue we calculate in a following way:
We will first take the right most bit then get its power of 2 which is in this case equal to 2^1 = 2.
You should know that REAL4 numbers can only give precise 10 digit value and others are in power. That is why our dividend will be 1000000000

Mantissa Right Bit 1 0 0 0
Power 2 (2^1) 4 (2^2) 8 (2^3) 16 (2^4)
Note: We are not dealing with 2^-1 which is the normally we do on paper.

1000/2 = 500
We can print it like .500
We can change value of dividend to 1000000 for REAL4 case. Or equal to 2^22 aligned to zero like 4194304 to 1000000.

Let say we have mantissa: 1110
Mantissa Right Bit 1 1 1 0
Power 2 (2^1) 4 (2^2) 8 (2^3) 16 (2^4)

1000/2 = 500
1000/4 = 250
1000/8 = 125

Result = 500 + 250 + 125 = 875
Which we can write as 0.875

Example Comparision Table

Power of 2Division by 1 (floating number)Division by 1000 (integer number)
21/2 = 0.50001000/2 = 500
41/4 = 0.25001000/2 = 250
81/8 = 0.12501000/2 = 125


Hope I conveyed the logic behind the printing of Floating number. I had spend two days to grab this concept. I would love to hear from you.

There are more thing to handle like Infinity, NAN and exceeding exponent but those can be deal later.

My Implementation:
https://gist.github.com/soachishti/b4bf2f55b99ef123a1a3

Irvine Lib Implementation (WriteFloat):
https://gist.github.com/soachishti/a4d7bb15ee6519f61c0a


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 27, 2015

Notepad++: A popular Windows text editor




Normally we found text editor useful for taking note and side work but that is the only case for normal user. While for developer and CS Student text editor are used for coding and modifing source code.  Developer text editor like Notepad++ (Windows), Vim (Linux), Gedit (Linux) and Sublime Text are leading text editor on different platform.

Jul 25, 2015

Getting Started with Boost C++ Library




Boost library is a C++ library which contain 80+ library to play with. Boost library is licensed under Boost Software License which allows to use it in free and propietary software. Some of the Boost feature have been migrated to Standard C++ library and some are in process of migrations. It is portable and support almost every c++ compiler.

Jul 23, 2015

Best Place to learn C++




Here are the some source which I find very usefull for learning C++

Cplusplus.com

To start learning new programming I always look for kickstart skipping history jumping Hello World program and continue to learn paradign of the language. For starting learning C++ I found cplusplus the best source for starting and they have a great active commnuity in their forum for solving program issues.