Myanmar IT Resource Forum
Myanmar IT Resource Forum
Myanmar IT Resource Forum

You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

sHa92

sHa92
Founder



Founder
Performing Arithmetic


ဒီ tutorial ကိုဖတ်လာရင်း စိတ်ပျက်ကောင်းပျက်လာနိုင်ပါတယ် Smile ဟုတ်တယ်လေ၊
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 တွေအဖြစ်ကြေညာလိုက်ပါ။

Code:
enum OpCode {    .........
    OP_ADD,        // operator +

    OP_SUBTRACT,    // operator -

    OP_MULTIPLY,    // operator *

    OP_DIVIDE,      // operator /

    OP_MODULUS,    // operator %

    .........

    .........

};

ပြီးရင် Virtual Machine ထဲမှာ အောက်ကလို သွားရေးလိုက်ပါမယ်။

Code:
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 သုံးပြီးတွက်ကြည့်ရအောင်၊

Code:
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 တွေတုန်းက အပင်ပန်းခံပြီးရေးထားတဲ့ အကျိုးကျေးဇူးပါဘဲ Wink အပေါ်က
script တွေရေးပြီးရင် run ကြည့်ဖို့လဲ မမေ့ပါနဲ့။ တခြား equation တွေလဲ
ထည့်တွက်ကြည့်လို့ရနေပါပြီ။ စမ်းကြည့်ဖို့ တိုက်တွန်းလိုက်ပါတယ်။

ခုကျွန်တော်ရဲ့ compiler tutorial မှာ chapter 1 မှ 3 ထိ source code ကိုအောက်က Link ကနေ download လုပ်လို့ရပါတယ်။

[You must be registered and logged in to see this link.]

Note: ဒီ tutorial အတွက် source codes တွေရေးပေးတဲ့ ကိုအောင်စည်သူ ကိုကျေဇူးအထူးတင်ရှိပါတယ်။

author :-: Myint Kyaw Thu

http://www.myanmaritresource.info

x-zip

x-zip
MITR New User



i appreciate with these posts . thx bro .. God bless yah

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum

 

Create a forum on Forumotion | ©phpBB | Free forum support | Report an abuse | Forumotion.com