Number System – Part II


From the previous session we have seen how to convert binary to decimal and decimal to binary number. This section I will focus on the importance of number system and how they are used in operating systems.

 

When we first learn programming language we often tend to learn the constructs / functions provided by the language. One should think what happens when you assign

Create a variable with data type eg: int x . How does compiler converts the int to represent 4 bytes of memory space, what is a byte , what is a bit , where are these allocated etc..

 =================================================================================

 

 

Bit: bit can hold smallest data in computer systems. Bit can represent only 2 states, and these states can be anything, a bit can represent 2 countries, 2 football players , 2 actors etc.. You have to understand that bit by itself doesn’t represent anything , then how to say that Bit holds values ?, this is done using programming language with the help of Data structures.

 

Below examples show you the different values a bit can handle

 

                                                 Country 2

 

Eg :                        Bit                                                      

                                                

                                                 Country 2

 

 

 

 

                                                 Horse

 

                               Bit  

 

                                                 123488

 

 

 

 

 

Please remember that at the end of the day you would be only dealing with bits, because datatypes group number of bits and the actual data resides in the bits.

 

 =================================================================================

 

 

 Bytes: Operating systems uses byte addressable for memory and I/O operations. Byte is group of bits , 8 bit grouped together to form a byte, with each bit capable of representing 2 states, 8 bits can represent 256 different states / values. These values conversion to ASCII , UTF etc. To elaborate more programmers use ASCII character set where character ‘A’ is represent as 41h and not ‘A’, because processor doesn’t understand anything apart from binary ( 0’s and 1’s ) , so the binary equivalent of ‘A’ would be processed.

 

We will write small assembly programs to understand more about the bits and bytes.

So now we understand if someone says “ The bit value might be corrupted”

 

 

==================================================================================

 

 

 

Words: This forms another grouping and a word can be 16 bit or 32 bit depending on the architecture. If its 32 bit then 4 bytes constitutes a word so the maximum value we can represent is 2 32 = 4294967295.

 

I would like to emphasize the concept and not the calculation behind it . The concept is Grouping of bits , the grouping might be int datatype, float datatype, char datatype or any other datatype, but at the end its all grouping of bits. Binary conversion of 232  is

 

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

                                 16 bytes                                          16 bytes

High Order  byte                                                                                       low order byte

 

 

216  * 2 16 = 4294967295 / 4 bytes When you learn HLL ( like c , c++ ) , you would be exposed to data types which would allocate defined amount of memory , you should think about these data types as grouping of bits as I mentioned above.

 

If you have installed Microsoft Visual Studio ,you can quickly compile a c program playing around with bits

 

 

================================================================================

Exercise:

 

a)      What happens when you initialize int a = 4294967295 ( int datatype being 32 bit )

And try to print the value ?

 

 

b)      What is the flow of execution till the processor executing the bit starting from

Compiler ?

 

 

  

 

Advertisements

Number System -Part 1


Assembly Programing- Lets Start !

 

There is a misconception about the language, people think that learning assembly is not easy !, they think people who writes code in assembly are Hackers !, wow this sounds super crazy. It takes ages to master assembly language.If you browse through the online / offline book shelves you would hardly find a book explaining about assembly.

 

Removing the miconception about the subject, it is necessary to learn assembly before jumping to learn HLL, the fact is that there are many alogorithms that you can implement in assembly rather than HLL. It would take a while to become expert on assembly but you would interact closely with the compiler and understand how the compiler handles your data. As i said in my earlier blog , debugging would only be achieved when you understand HLL and Assembly.

 

 

Number System

 

It is important to understand the number systems to get good understanding of how data is handled by computers. Most operating systems uses binary number system to decimal data. So discussing about the binary number system would be real worth.

 

Computers uses binary value to represent values, and states ( in electronics ). So there are 2 conversions always possible

 

a) Decimal to Binary

b) Binary to Decimal

 

I would follow the method which is easy to understand

 

a) Binary To Decimal Convertion: Converting a binary number into decimal is easy but not the vice versa which we will see with the examples.

 

             Eg : What is the binary equivalent to 10110110

             Binary uses powers of 2 , and the decimal equivalent of above number is

       

1*2(pow)7 + 0*2(pow)6 + 1*2(pow)5 + 1*2(pow)4 + 0*2(pow)3+ 1*2(pow)2+1*2(pow)1+0*2(pow)0

 

Decimal equivalent is 128+0+32+16+0+8+4+0 = 188

 

Please practice 5 different examples to understand and concept

 

b) Decimal To Binary Conversion: Ah!, this is tricky , but will play around with it. The task is to convert Decimal number to Binary equivalent. You can have your mathematical calculator handy for all the calculations, but knowing these both plays an vital part in debugging which is why I emphasize learning assembly basics.

Lets take an example ( larger the number , lengthy the math !) 7044.Now that we have number we should convert to binary equivalent

 

PS : We represent 0 or 1  with respect to locations

  

Method 1:

 

Step1 : analyze which is the closest 2 pow integer which match / fall near the number , in

            Our example 2(pow) 12  = 4096 and 2(pow) 13  =8192 , so our number fall in

            Between both, so I will take 2(pow)12 . Now write 1 representing 2(pow)12

 

Step2 : Minus 4096 from 7044 = 2948 , we need to find the closest to represent 2948

 

Step3 : 2(pow)11 is 2048 which is closest number 2948 , so represent this with 1 and we

            Minus 2048 from 2948 = 900

 

Step4 : 2(pow)10 is 1024 which is greater than 900 , so represent this location as 0

 

Step5 : 2(pow)9 is 512 which is smaller than 900 , so represent this location as 1 and

            Minus the value from 900 i.e 900 – 512 = 388

 

Step6: 2(pow)8 is 256 which is smaller than 388 , so represent this location as 1 and

           Minus the value from 388 i.e 388 – 256 = 132

 

Step7: 2(pow)7 is 128 which is smaller than 132 , so represent this location as 1 and

           Minus the value from 132 i.e 132-128 = 4

 

Step6: 2(pow)6 is 64 which is greater than 4 , so represent this location as 0

 

Step7: 2(pow)5 is 32 which is greater than 4 , so represent this location as 0

 

Step8: 2(pow)4 is 16 which is also greater than 4 , so represent this location as 0

 

Step9: 2(pow)3 is 8 which is again greater than 4 , so represent this location as 0

 

Step10: 2(pow)2 is 4 which is equal to 4 , so represent this location to 1, now value is 0

 

Step11: 2(pow)1 is 2 which is less than 0 , so represent this location to 0

 

Stpe12:2(pow)0 is 0 which is 1 , so represent this location to 0

 

 

So 7044 equivalent is 1101110000100

 

Method 2:

 

This method is useful to convert  the deimal number to any other base  numbersystem .i.e to binary, octal, hexadecimal…. etc

 

 Step 1:  Divide the  given decimal  number  with the base of the system  , and  notedown the remainder.

 

 Step 2:   Divide the quotient  with  the base of the system & notedown the reaminder .

 

 Step 3 : Continue this process until quotient is less than  the divider .

 

 Step 4 :  Starting with the last quotient until the first remainder writedown all the reaminders  , the resulting number gives  you the equivivalant  number in          

               the choosed based system .

 

 

  For  clear understanding follow the  example  given 

 

  Example  :  conversion of    decimal number  20 to equivalant binary number:

                

                  1. Divide 20 with 2    ,   quotient  =10 , remainder =0. 

 

                  2. Divide  10 with 2  ,    quotient  =5 , remainder =0.

 

                       3. Divide  5 with 2  ,    quotient =2, remainder =1.

 

                  4. Divide  2  with 2 , quotient  =1, remainder = 0.

 

                   Starting with the last quotient until  first remainder write  the remainders one by one 

                  

                   So the binary equivalant of  20 is  = 10100

   

Exercise : Please perform the above steps and deduce for 1000, 3366, 6843

 

 

This representation is very important while you are debugging. You might see the below patterns such as 00000110 which is equivalent to 6 and this representation completely depends on the machine architecture . I will be addressing the modes of operation of INTEL processor once I complete the basics of number system.

 

 

You might have read trillion times about the below conventions but they are very useful.

 

4 bit = A nibble

8 bit = 1 byte

16 / 32 bit = 1 word , this depends on the processor architecture.

 

 

Will discuss about  INTEL modes of operation along with working with bits and bytes. Its already 1.00 am , time to sleep J