Monday, May 2, 2011

Physical & virtual addresses

Physical addresses refer to the hardware addresses of physical memory.
Virtual addresses refer to the virtual store viewed by the process.

Virtual to physical address mapping is done by Memory Management Unit(MMU)

Virtual space is limited by the size of virtual addresses.

Virtual memory space and physical memory space are different.

Paging is the most common method of memory management:
Virtual space of process is divided into fixed-size pages
Virtual address is composed of page number and page offset.
Physical memory is divided into fixed-size frames
Page in virtual space fits into frame in physical memory.

Mapping of VA to PA is done with a page table.
Each process has an associated page table.

Physical address = Frame number * Page size + Page offset
-----------------------------

Modern systems keep more than one program in memory at a time.
All these programs together require more memory than what is visible.
Use a part of disk and make it look like memory - This is called virtual memory.

Friday, April 8, 2011

#C_CODE: Integer to Ascii conversion

#include <stdio.h>
#include <string.h>
void reverse(char *s)
{
 int len = strlen(s) - 1;
 int i;
 for(i=0;i < strlen(s)/2; i++)
 {
  s[i]   ^= s[len];
  s[len] ^= s[i];
  s[i]   ^= s[len--];
 }
}

char *intoa(int num)
{
 static char buff[10];
 char *s = &buff[0];
 int sign;

 if((sign = num) < 0)
 {
  num = -num; //Make num positive  
 }

 do
 {
  *(s++) = (num%10) + '0';
 }while(num /= 10);
 
 if(sign < 0)
  *(s++) = '-';

 *(s) = '\0';
 reverse(&buff[0]);
 return (&buff[0]);
}

int main()
{
 int num,i;
 char *s;

 printf("Enter the num: ");
 scanf("%d",&num);
 s = intoa(num);
 printf("\nThe num %d in ascii is: ",num);
 for(i=0; i < strlen(s); i++)
  printf("\n%c",s[i]);

 fflush(stdin);
 getchar();
}

REF: C Programming Language-K&R

#C_CODE: Reverse String

#include <stdio.h>
#include <string.h>
 
void reverse(char *s)
{
 int len = strlen(s)-1;
 int i;
 for(i=0; i < strlen(s)/2; i++)
 {
  s[i]   ^= s[len];
  s[len] ^= s[i];
  s[i]   ^= s[len--];
 }
}

int main(void)
{
 char buff[35];
 fputs("Input string: ",stdout);
 fgets(buff,sizeof(buff),stdin);
 fflush(stdin);
 *(buff + strlen(buff) -1 ) = '\0';

 reverse(buff);
 printf("\n The reversed string is: %s\n",buff);
 getchar();
}

REF: C Programming Language-K&R

#C_CODE: Binary Search

#include <stdio.h>
 
void InsertionSort(int *ptr,int size)
{
 int i,j,index;
 for(i=1;i {
  index = ptr[i];
  for(j=i; j>0&&ptr[j-1] >index; j--)
   ptr[j] = ptr[j-1];

  ptr[j] = index;
 }
}

int BinarySearch(int *ptr,int key,int size)
{
 int low,mid,high;
 low = 0;
 high = size-1;
 while(low <= high)
 {
  mid = (low + high)/2;
  if(key < ptr[mid])
   high = mid - 1;
  else if(key > ptr[mid])
   low = mid + 1;
  else
   return mid;
 }
 return -1;
}

int main()
{
 int i;
 int a[] = {5,7,0,8,4,9,6,1,3,2};
 int res;
 
 InsertionSort(a,10);
 printf("\nInsertion sort: ");
 for(i=0;i<10;i++)
  printf("%d ",a[i]);
 
 for(i=0; i<10;i++)
 {
  res = BinarySearch(a,i,10);
  if(res != -1)
   printf("\nThe value %d is found in position: %d\n",i,res);
 }

 fflush(stdin);
 getchar();
}

REF: C Programming Language-K&R

#C_CODE: Sorting Algorithms

#include <stdio.h>
 
void BubbleSort(int *ptr, int size)
{
 int i, j;
 for(i=0; i < size-1; i++)
 {
  for(j=0; j < size-1-i; j++)
  {
   if(ptr[j] > ptr[j+1])
   {
    ptr[j]   ^= ptr[j+1];
    ptr[j+1] ^= ptr[j];
    ptr[j]   ^= ptr[j+1];
   }
  }
 }

}

void SelectionSort(int *ptr, int size)
{
 int i,j,min;
 for(i=0; i < size-1;i++)
 {
  min = i;
  for(j=i+1;j < size;j++)
  {
   if(ptr[j] < ptr[min])
    min = j;
  }
  ptr[i]   ^= ptr[min];
  ptr[min] ^= ptr[i];
  ptr[i]   ^= ptr[min];
 }
}

void InsertionSort(int *ptr,int size)
{
 int i,j,index;

 for(i=1; i < size;i++)
 {
  index = ptr[i];
  for(j=i; j>0 && ptr[j-1] > index; j--)
   ptr[j] = ptr[j-1];
  ptr[j] = index;
 }
}

int main()
{
 int i;
 int a[] = {5,7,0,8,4,9,6,1,3,2};
 int b[] = {5,7,0,8,4,9,6,1,3,2};
 int c[] = {5,7,0,8,4,9,6,1,3,2};

 BubbleSort(a,10);
 printf("\nBubble sort: ");
 for(i=0;i<10;i++)
  printf("%d ",a[i]);

 SelectionSort(b,10);
 printf("\nSelection sort: ");
 for(i=0;i<10;i++)
  printf("%d ",a[i]);

 InsertionSort(c,10);
 printf("\nInsertion sort: ");
 for(i=0;i<10;i++)
  printf("%d ",a[i]);
 
 fflush(stdin);
 getchar();
}

Ref: blog.Codebeach.com

#C_CODE: Bit Set Count

#include <stdio.h>
 
int bitsetcount(int num)
{
 int i;
 for(i=0; num; i++)
  num &= num - 1;
 return i;
}

int main()
{
 int num;
 printf("Enter the num: ");
 scanf("%d",&num);
 printf("The no. of bits set in %d is: %d\n",num,bitsetcount(num));
 fflush(stdin);
 getchar();
}

Ref: C Programming Language - K&R

#C_CODE: Clear Bits

#include <stdio.h>
 
int main()
{
 int num,bits,pos,res;

 printf("Enter the num: ");
 scanf("%d",&num);
 printf("\nEnter the no. of bits to be cleared: ");
 scanf("%d",&bits);
 printf("\nEnter the pos(0 onwards): ");
 scanf("%d",&pos);
    
 printf("The result: %d\n", 
( num & (~((~(~0 << bits)) << pos + 1 - bits)) ) );
 fflush(stdin);
 getchar(); 
}

Example: num = 255(0b1111,1111), bits = 3, pos = 5, Result = 199(0b11000111)


Ref: C Programming Language - K&R