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);
    }
}