Processor Execution Mode – I


One has to understand the relation between the Microprocessor / Processor , operating system and the applications , I thought putting it in the form of a diagram

If you look at the diagram , every layer consists of 32bit or 64bit modes, which means You can code / write your applications either in 32bit or 64bit and compile and run them on 32 bit / 64 bit . You cannot execute 64bit application on 32bit operating system but vice versa is possible which is running 32bit applications on 64bit operating system.

Later you operating system also needs to communicate with microprocessor / processor which can be again 32bit or 64 bit. Please understand that processor modes should be enabled by the Operating system on code segment , which means 64bit operating system can execute 64bit applications on 64bit processor mode

Processor Execution Modes.


I am a great follower of Intel architecture , so most of my explanations or articles are based on Intel architectures. Intel 80386 is the base for modern operating systems, one of the challenge for Intel was Real Address Mode. Today’s Intel architecture has 3 operating modes

  • Protected Mode
  • Real Address Mode
  • System Management Mode

 

 

Protected Mode : Most of the system programmers are aware of the benefits of protected mode, but for those who are new to this term, it’s a protection introduced for memory. Prior to protected mode, applications were able to access any region of memory and any malicious code could access any region of code. Protected mode introduced with interrupt. Programs can perform multitasking in this mode.

Real Address Mode: A simple explanation would be , programs executing in Real mode would have access to all the memory locations and any malicious code could corrupt memory and provide undesired results.

System Management Mode : This is the new feature introduced SL ,  IA32 architectures and above. SMM manages power management and some of the interesting facts are

  • Microprocessor output pin is used to trigger SMM mode
  • While servicing the interrupt / external interrupt , processor saves the current state and executes the code in SMM and then switch back to normal mode.
  • SMM uses RSM ( Return from SMM Mode ) instruction to restore microprocessor state.

 

Intel 64 Architecture adds new mode called IA-32e mode , the difference is that this has more general purpose registers which are extend to 64 bit and enabled at code segment .

DNS External And Internal Namespace


Naming Convention For Forest

 

Following are the factors which needs to be taken care while creating a forest. You have to plan registering a public domain name along with internal AD domain naming convention. You should choose the namespace wisely.

 

Seperate External Namespace And Internal Namespace:

Most of the administrators choose to have separate namespace for internal corporate network and external users eg: sainath.com as external or registered with ISP and sai.loc as internal. The only criteria should be non-routable domain names.

Most of the external namespaces are belong to DNS namespace and DNS namespace is very different from AD namespace. There are many arguments so as to choose separate namespace for AD domains or use Single namespace. The answer lies in the security model you design for any company which I will discuss in below “same external and internal namespace”. Most of the organizations follow this model.

 

A typical example is explained below

 

From the above diagram you can place the DNS in DMZ zone which is facing Extranet and most customers either configure this as Microsoft DNS or BIND DNS server with the appropriate domain name ( eg: Sainath.com.) and the Internal DNS server would be configured as Sainath.internal . Users on internet will resolve sainath.com and internal users will resolve to sainath.internal. You have to provide appropriate routing between the external facing DNS server and internal DNS server.

 

There are also situation where internal users wants to resolve the hostname on external dns server. Eg: users configured on sainath.internal wants to resolve hostname configured on sainath.com. To perform this you could create a zone name sainath.com on the internal DNS server. And other possible way for the internal users to resolve to the external namespace is to create a host record. Please remember that dnsclient service uses host file as first name resolution method, so if you manually configure the internal users to point to host record on external dns server , you could still have the internal users to access external hosts.

Eg: sharepoint.sainath.com 13.12.12.12

 

 

PS : I have seen issues with domain names such as .local so please use anything apart from .local.

 

Same External And Internal Namespace:

There are no restrictions from Active Directory to not to use same namespace for both external and internal users / access. Example : company can register sai.com as external namespace and still use developers.sai.com and testing.sai.com as internals namespaces, everything would work seamlessly after you configure appropriate DNS routing. Administrators has concern about the exposure of the AD namespace to the external world and they think that hackers might have better option to hack the internal servers and the data.

Well, if you provide better routing capabilities by blocking the required ports , I wouldn’t see a problem. But a small configuration change is required which is mentioned in

http://support.microsoft.com/kb/267855 ( concentrate on RegisterDnsARecords = 0x1 / 0x0 )

 

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 ?

 

 

  

 

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

Cannot convert const char to lpcwstr


#include <windows.h>

#include

<tchar.h>

#include

<winuser.h>

const

TCHAR sai_classname[]= TEXT("saiwindowcls");

LRESULT CALLBACK wndproc(HWND hwnd, UINT m1,WPARAM wparam, LPARAM lparam)

{

switch

(m1)

{

case

WM_CLOSE: DestroyWindow(hwnd);

break

;

case

WM_DESTROY: PostQuitMessage(0);

break

;

default

: return DefWindowProc(hwnd, m1, wparam, lparam);

}

return

0 ;

}

int

WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

{

//sai1 is the object of wndclassex structure

WNDCLASSEX sai1;

HWND hwnd;

MSG msg;

//registering class

sai1.cbSize = sizeof(WNDCLASSEX);

sai1.style = 0;

sai1.lpfnWndProc = wndproc;

sai1.cbWndExtra = 0;

sai1.cbClsExtra = 0;

sai1.hbrBackground = (HBRUSH)(COLOR_BACKGROUND,COLOR_MENU +2);

sai1.hCursor = LoadCursor(NULL,IDC_ARROW);

sai1.hIcon =LoadIcon(NULL,IDI_APPLICATION);

sai1.hInstance = hInstance;

sai1.lpszClassName = sai_classname;

sai1.lpszMenuName = NULL;

sai1.hIcon = LoadIcon(NULL,IDI_APPLICATION);

if (!RegisterClassEx( &sai1))

{

MessageBox(NULL,L"Unable To Register",L"HAZARD", MB_ICONERROR);

return

0 ;

}

//Creating window

hwnd=CreateWindowEx(WS_EX_CLIENTEDGE,sai_classname,L

"Sai Window",

WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,900,600,

NULL,NULL,hInstance,NULL);

if

(hwnd == NULL)

{

MessageBox(NULL,L

"window creation failes",L"BUMP",MB_ICONHAND);

return

0 ;

}

Notice the above code would not execute if you declare the following code as and get error message as "cannot convert const char to lpcwstr"

Above is the solution for the error message and below is the possible mistakes.

 

const CHAR sai_classname[]= "saiwindowcls";

"window creation failes", ( here you should declare either _T(x) or L "string")

 

 

VS 2005 compiling Win32 Program


 
#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow)
{
    MessageBox(NULL, "sainath musings", "info", MB_ICONASTERISK);
    return 0;
}
The above code will generate error.cannot convert parameter 2 from 'const char [3]' to 'LPCWSTR'
The solution For the above code is simply add Tchar.h. 

#include <windows.h>

#include

<tchar.h>

int

WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

LPSTR lpCmdLine,

int nCmdShow)

{

MessageBoxW(NULL, _T(

"hi"), _T("Note"), MB_ICONASTERISK);

return 0;

}