Pages

Friday 7 September 2012

computer language ကိုယ္ပိုင္တီထြင္ျခင္း+3

Performing Arithmetic

ဒီ tutorial ကိုဖတ္လာရင္း စိတ္ပ်က္ေကာင္းပ်က္လာႏိုင္ပါတယ္ :) ဟုတ္တယ္ေလ၊ virtual machine ထဲမွာ code ေတြမ်ားလာသေလာက္ programming ရဲ႕အေျခခံ ေပါင္းႏုတ္ေျမာက္စား ေလးေတာင္ လုပ္လို႔မရေသးဘူး။ ကဲ၊ စိတ္ပ်က္စရာမလိုေတာ့ဘူး။ ဒီ အခန္းမွာ ေအာက္ကလို equation မ်ိဳးတြက္ျပပါေတာ့မယ္ B-)


displacement = initial velocity x time + ½ acceleration x (time x time)

ဒီ equation က velocity နဲ႕ acceleration ကေန displacement တြက္တဲ့ Newton equation ပါ။ ဒီအခန္းမွာ complex ျဖစ္တဲ့ arithmetic ေတြတြက္ေတာ့မွာမို႔ တမင္ ရႈပ္တဲ့ equation ကိုေရြးထားတာပါ။ ဒီအခန္းရဲ႕ ရည္မွန္းခ်က္ကေတာ့ ေနာက္ဆုံးမွာ ကၽြန္ေတာ္တို႕ language ကိုသုံးၿပီး Newton ရဲ႕ equation ကိုတြက္ျပၿပီး testing လုပ္ဖို႕ပါ။

ကဲ၊ ဒါဆို သခ်ာၤတြက္နည္းေလးမ်ိဳးျဖစ္တဲ့ ေပါင္းႏုတ္ေျမာက္စား ေလးမ်ိဳး နဲ႕ modulus စုစုေပါင္း ၅ မ်ိဳးကို Op-code ေတြအျဖစ္ေၾကညာလိုက္ပါ။
enum OpCode {    .........
    OP_ADD,         // operator +

    OP_SUBTRACT,    // operator -

    OP_MULTIPLY,    // operator *

    OP_DIVIDE,      // operator /

    OP_MODULUS,     // operator %

    .........

    .........

};
ၿပီးရင္ Virtual Machine ထဲမွာ ေအာက္ကလို သြားေရးလိုက္ပါမယ္။
Instruction inst = program.Next();int Data1, Data2;
switch (inst.code)

{

    ......

case OP_ADD:

    Data1 = program.PopStack();

    Data2 = program.PopStack();

    program.PushStack( Data2 + Data1 );

    break;

case OP_SUBTRACT:

    Data1 = program.PopStack();

    Data2 = program.PopStack();

    program.PushStack( Data2 - Data1 );

    break;

case OP_MULTIPLY:

    Data1 = program.PopStack();

    Data2 = program.PopStack();

    program.PushStack( Data2 * Data1 );

    break;

case OP_DIVIDE:

    Data1 = program.PopStack();

    Data2 = program.PopStack();

    if (Data 1 != 0)

        program.PushStack( Data2 / Data1 );

    else

        program.PushStack( 0 );

    break;

case OP_MODULUS:

    Data1 = program.PopStack();

    Data2 = program.PopStack();

    program.PushStack( Data2 % Data1 );

    break;

    ......

}
Arithmetic ေတြ ေရးရတာလြယ္ပါတယ္။ Stack ေပၚက value ၂ ခုကိုယူ၊ ၿပီးရင္ လိုခ်င္တဲ့ ေပါင္းႏုတ္ေျမာက္စား လုပ္လိုက္ရုံပါ။ ၿပီးရင္ ရလာတဲ့ အေျဖကို Stack ကိုျပန္တင္ေပးလိုက္ပါတယ္။ ဒါမွာ ေနာက္တခါ တြက္ခ်င္တဲ့ အခါ stack ေပၚက value ကိုဘဲထပ္ယူတြက္လို႕ ရမွာေလ။ ဒီေနရာမွာ divided by zero အတြက္ error မတက္ေအာင္ If ခံေပးထားတာ သတိျပဳမိမွာပါ။ ဒီလို တခ်ိဳ႕ error ေတြကို programmer ကမလုပ္ခင္ language ကႀကိဳတင္ကာကြယ္ေပးထားလို႔ ရပါတယ္။ ဒါေၾကာင့္ C++ ထက္ေနာက္ပိုင္း Java လို language ေတြကပိုေကာင္းလာတာပါ။

ကဲဒါဆို၊ ခုကၽြန္ေတာ္တို႕ရဲ႕ Newton ရဲ႕ displacement equation ကို ကိုယ္ပိုင္ language သုံးၿပီးတြက္ၾကည့္ရေအာင္၊
void main() {    Program MyProgram;


    // initialize data first

    // "time" variable. Address: 0x0000, value: 200

    MyProgram.Add(Instruction(OP_PUT_MEM, 0x0000, 200));

    // "acceleration" variable. Address: 0x0004, value: 2

    MyProgram.Add(Instruction(OP_PUT_MEM, 0x0004, 2));

    // "initial velocity" variable. Address: 0x0008, value: 35

    MyProgram.Add(Instruction(OP_PUT_MEM, 0x0008, 35));



    // calculate time * time

    MyProgram.Add(Instruction(OP_PUSH_STACK, 0, 0));

    MyProgram.Add(Instruction(OP_PUSH_STACK, 0, 0));

    MyProgram.Add(Instruction(OP_MULTIPLY, 0, 0));



    // calculate (acceleration * time2) / 2

    MyProgram.Add(Instruction(OP_PUSH_STACK, 0x0004, 0));

    MyProgram.Add(Instruction(OP_MULTIPLY, 0, 0));

    MyProgram.Add(Instruction(OP_PUT_STACK, 2, 0));

    MyProgram.Add(Instruction(OP_DIVIDE, 0, 0));



    // calculate (initial velocity * time)

    MyProgram.Add(Instruction(OP_PUSH_STACK, 0x0008, 0));

    MyProgram.Add(Instruction(OP_PUSH_STACK, 0x0000, 0));

    MyProgram.Add(Instruction(OP_MULTIPLY, 0, 0));



    // calculate (initial velocity * time) + (acceleration * time2) / 2

    MyProgram.Add(Instruction(OP_ADD, 0, 0));



    // save to "displacement" variable. Address: 0x000C

    MyProgram.Add(Instruction(OP_POP_STACK, 0x000C, 0));



    // print the displacement value on screen

    MyProgram.Add(Instruction(OP_PUSH_STACK, 0x000C, 0));

    MyProgram.Add(Instruction(OP_NUM, 0, 0));



    MyProgram.Add(Instruction(OP_END, 0, 0));



    vm.Run(MyProgram);

};
ကဲ ဒီေလာက္ဆို ကၽြန္ေတာ္တို႕ language ေလးက ဒီလိုခက္ခက္ခဲခဲ equation ေတြတြက္လို႕ရၿပီဆိုတာ ေတြ႕ႏိုင္ပါတယ္။ ေသခ်ာတာက သာမန္ calculator အဆင့္ထက္ေတာ့ အမ်ားႀကီး သာေနပါၿပီ။ Memory ေတြ၊ Stack ေတြကို အရင္ chapters ေတြတုန္းက အပင္ပန္းခံၿပီးေရးထားတဲ့ အက်ိဳးေက်းဇူးပါဘဲ ;) အေပၚက script ေတြေရးၿပီးရင္ run ၾကည့္ဖို႕လဲ မေမ့ပါနဲ႔။ တျခား equation ေတြလဲ ထည့္တြက္ၾကည့္လို႕ရေနပါၿပီ။ စမ္းၾကည့္ဖို႕ တိုက္တြန္းလုိက္ပါတယ္။

ခုကၽြန္ေတာ္ရဲ႔ compiler tutorial မွာ chapter 1 မွ 3 ထိ source code ကိုေအာက္က Link ကေန download လုပ္လို႔ရပါတယ္။

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

အေထြးေထြးနည္းပညာမ်ား