bloggerads

2016年1月14日 星期四

Hook my own interrupt vector in DOS

因為工作上需要驗證中斷的tool, 所以把塵封已久的Watcom DOS程式拿出來改。以下是一個基本的掛中斷、呼叫中斷、移除中斷的範例
// Author: Martin Lee  

 #include <stdio.h>
 #include <conio.h>
 #include <dos.h>

 #define getvect(n) _dos_getvect(n)

 #define setvect(n,v) {\
   _disable();\
   _dos_setvect(n,v);\
   _enable();\
 }

 #define HOOKVECTOR 0x7F

 int sig;

 void interrupt  (*origin_int)();

 void interrupt handle_7Fh(void)
 {
     sig=1;
     //_chain_intr( origin_int); // not execute original vector
 }

 void hook_int(void)
 {
     origin_int = getvect(HOOKVECTOR);
     setvect (HOOKVECTOR, handle_7Fh);
 }

 void restore_int(void)
 {
     setvect(HOOKVECTOR,origin_int);
 }

 void main()
 {
     sig = 0;

     hook_int();

     printf("Before int 7fh, sig = %d\n", sig);

     __asm  int 7Fh    
   
     printf("After int 7fh, sig = %d\n", sig);
       
     restore_int();
   
 }

----------------------執行結果-----------------------------
Before int 7fh, sig = 0
After int 7fh, sig = 1


2016年1月3日 星期日

C# : 測試程式執行的時間 (Time measurement)

using System.Diagnostics; // Stopwatch
using System.Threading; // Thread.Sleep

static void Main(string[] args)
{
    Stopwatch stopWatch = new Stopwatch();
    
    stopWatch.Start();
    Thread.Sleep(1000);
    stopWatch.Stop();

    // Get the elapsed time as a TimeSpan value.
    TimeSpan ts = stopWatch.Elapsed;

    // Format and display the TimeSpan value.
    string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}",
        ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds);

    Console.WriteLine("RunTime " + elapsedTime);
    Console.ReadLine();
}


Reference:
https://msdn.microsoft.com/zh-tw/library/system.diagnostics.stopwatch(v=vs.110).aspx

2016年1月1日 星期五

C# : shuffling a given array

Shuffling a given array in C#

static Random _random = new Random();

static void Shuffle<T>(T[] array)
{
    int n = array.Length;
    for (int i = 0; i < n; i++)
    {
        // NextDouble Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.
        int r = i + (int)(_random.NextDouble() * (n - i)); 
        T t = array[r];
        array[r] = array[i];
        array[i] = t;
    }
}

static void Main(string[] args)
{
    int[] array = new int [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    Shuffle(array);
    foreach (int value in array)
    {
        Console.WriteLine(value);
    }
}

2015年12月31日 星期四

C# : algorithm "List" sample code

namespace List
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>(new int[] { 19, 23, 29 });

            int index = list.IndexOf(23); // Check if 23 Exists.
            Console.WriteLine(index);

            index = list.IndexOf(10); // Does not exist.
            Console.WriteLine(index);

            list.Add(35);
            list.Insert(1, 40);
            list.Remove(23); // RemoveAt(index) method, or the Remove(obj)
            list.RemoveAt(0); // RemoveAt(index) method, or the Remove(obj)

            foreach (int x in list)
            {
                Console.WriteLine(x);    
            }

            Console.ReadLine();
        }
    }
}


C# : algorithm "Dictionary" sample code

The following sample code was searched from internet, It's a good example to show how C++ map work on C# as Dictionary


// Algorithm map
namespace Map
{
    class Program
    {
        static void Main(string[] args)
        {
            // Use Dictionary as a map.
            var map = new Dictionary<string, string>();

            // ... Add some keys and values.
            map.Add("cat", "orange");
            map.Add("dog", "brown");

            // ... Loop over the map.
            foreach (var pair in map)
            {
                string key = pair.Key;
                string value = pair.Value;
                Console.WriteLine(key + "/" + value);
            }

            // ... Get value at a known key.
            string result = map["cat"];
            Console.WriteLine(result);

            // ... Use TryGetValue to safely look up a value in the map.
            string mapValue;
            if (map.TryGetValue("dog", out mapValue))
            {
                Console.WriteLine(mapValue);
            }
        }
    }
}

2015年12月30日 星期三

C# : The difference between C# and C in initializing a struct array

The difference between  C# and C in initializing a struct array

// C#
struct st
{
    public int x;
    public int y;
} 
public static void Main(string[] args)
{
    st [] o = new st [] {
        new st { x=2, y=3 },
        new st { x=3, y=4 }
    };
}

// C/C++
struct st
{
    int x;
    int y;
};

int main()
{
    st o[] = {
        {.x=2, .y=3},
        {.x=3, .y=4}
    };
}

2015年12月29日 星期二

ACPI: Power Management

ACPI: Advanced Configuration & Power Interface

ACPI分別對系統(Gx), 周邊裝置(Dx), 睡覺層級(Sx), CPU(Cx) 定義電源層級, 如下

1. Gx (Global state, 整個系統) :
  • G0 = Working state (C0~C3)
  • G1 = Sleeping state (S2~S4)
  • G2 = Soft off (S5)正常關機,電源還插著因此主機板還有standby power,可被外部如LAN喚醒開機
  • G3 = 拔電源(Mechanical off)
2. Dx (Device state, 周邊設備) :
  • D0 = 正常運作
  • 耗電 D1 > D2
  • D3 = 斷電