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
Introduction

ဒီတခါ ကၽြန္ေတာ္ေရးမဲ့အေၾကာင္းအရာကေတာ့ computer language အသစ္တခုကို ကိုယ္ပိုင္တီထြင္ျခင္းျဖစ္ပါတယ္။ ဒီေနရာမွာ ေမးစရာ ေမးခြန္း ၂ ခုေပၚလာပါတယ္။ (၁) Language တစ္ခုတီထြင္ရတာ ခက္ပါသလား။ (၂) Language အသစ္ထြင္ဖို႔လိုအပ္ပါသလား။ Language အသစ္တခုထြင္ရာမွာ ရွိၿပီးသား C လို procedural language မ်ိဳးတီထြင္ဖို႔ အရမ္းလြယ္ပါတယ္။ Coding အနည္းငယ္နဲ႔ဘဲ တည္ေဆာက္လို႔ရပါတယ္။

Language အသစ္တီထြင္ဖို႔ လိုအပ္ပါသလားဆိုေတာ့ ဒီေခတ္ထဲမွာေတာ့ လုံး၀နည္းပါး မလိုအပ္ပါဘူး။ ရွိၿပီးသား java လို c# လို scripting language ေတြဟာ ေတာ္ေတာ္ေလးေကာင္းၿပီး၊ ေနရာ ေတာ္ေတာ္မ်ားမ်ားမွာ (embedded) သုံးလို႔ရပါတယ္။ ေနာက္ၿပီး ကိုယ္တီထြင္မဲ့ language ဆိုတာ ဘယ္လိုလုပ္ဒီလို ကမၻာေက်ာ္ languages ေတြနဲ႔ယွဥ္လို႔ရမွာလဲ။ မဟုတ္ဘူးလား Smile

ဒါေပမဲ့၊ ကိုယ္ပိုင္ language၊ compiler နဲ႔ interpreter ေတြကို ေရးႏိုင္သြားရင္၊ C# တို႕ Java တို႕ သုံးတဲ့အခါ၊ ေအာ္... ဒီ syntax ကဒီေနရာမွာ ဒါေၾကာင့္လိုအပ္တာကိုး၊ ဒီလိုေရးရင္ ဒါေၾကာင့္ error တက္တာ၊ ဒီလိုေရးရင္ Optimized ပိုျဖစ္တယ္ စသည္ျဖင့္ စဥ္းစားႏိုင္သြားပါတယ္။ Programming ဆိုတာႀကီးကို ဘာမွန္းမသိတဲ့ Black Box အျဖစ္ကေန၊ codes ေတြကို complier ကဘယ္လို compile လုပ္တယ္၊ Assembler က ဘယ္လို execute လုပ္တယ္ဆိုတာကို အေသးစိတ္သိသြားမွာ ျဖစ္ပါတယ္။ ကၽြန္ေတာ္အျမင္ကေတာ့ compiler ေရးတာကို programmer တိုင္းသိထားသင့္တယ္ ထင္ပါတယ္။ ကဲဒီေတာ့၊ ကၽြန္ေတာ္တို႕ programming language တခုကိုဘယ္လိုတည္ေဆာက္တယ္ ဆိုတာ ေလ့လာရင္း ကိုယ္ပိုင္ Programming language ေလးတခု ထြင္ေရးၾကည့္ရေအာင္။

ဒီ tutorial မွာေတာ့ programming language အသစ္ထြင္ရာမွာ C-based syntax ေတြကိုဘဲ သုံးပါမယ္။ CPU ေပၚ run တဲ့ assemblies codes ေတြထုတ္မဲ့အစား၊ Java လို ကိုယ္ပိုင္ virtual machine မွာ run မဲ့ byte-codes ေတြထုတ္ေပးတဲ့ complier မ်ိဳးေရးမွာ ျဖစ္ပါတယ္။ စာဖတ္သုူေတြ အေနနဲ႔ ဒီ tutorial series ကိုလိုက္လုပ္ရင္း ကိုယ္ေပးခ်င္တဲ့ syntax ေတြ၊ နာမည္ေတြလဲ ေပးႏိုင္ပါတယ္။ ကိုယ္ပိုင္ language ထြင္ေနတာဘဲ၊ မဟုတ္ဘူးလား? Smile Tutorial ကိုလိုက္လုပ္ရင္းနဲ႔ have fun ေပါ့ Wink

Note: ခုကၽြန္ေတာ္တင္ျပမဲ့ tutorial ဟာလြယ္ေပမဲ့၊ programming ကို beginner ကစေလ့လာေနသူေတြ အတြက္ရည္ရြယ္ၿပီး ေရးထားတဲ့ basic programming tutorial ေတာ့မဟုတ္ပါ။ ဒီ tutorial ကို ဖတ္ဖို႕ programming concept အေျခခံေတာ့ ရွိဖို႕လိုပါလိမ့္မယ္။ ဒီ tutorial ကိုမဖတ္ခင္ သိထားသင့္တာေတြကေတာ့ stack အပါအ၀င္ အေျခခံ data structure မ်ား၊ CRT ႏွင့္ STL (Standard Template Library) မွ function မ်ား၊ vector, list ကဲ့သို႕ template class မ်ား၊ File အေရး/အဖတ္ တို႕ကို သိထားသင့္ပါတယ္။ စာဖတ္သူမ်ား သိထားၿပီးလို႔ ယူဆၿပီး ဒီ tutorial မွာအေသးစိတ္ ရွင္းျပေပးသြားမွာ မဟုတ္ပါ။

C++ ကိုအေျခခံေရးထားေပမဲ့၊ တျခား third generation languages မ်ားျဖင့္လဲ implement လုပ္ႏိုင္ပါတယ္။ scripting language မ်ားႏွင့္ေတာ့ implement လုပ္ရန္ recommend မလုပ္ပါ။


Simplest Programming Language

ခု language အသစ္တခုဘယ္လို ေရးရမလဲဆိုတာ ေလ့လာဖို႕ ကၽြန္ေတာ္တို႕ ကမၻာေပၚမွာ အရိုးရွင္းဆုံး language တခုကို စေရးၾကည့္ရေအာင္။ ကၽြန္ေတာ္တို႕ language မွာ Op-code ၂ခုဘဲပါပါမယ္။ TALK နဲ႔ END ပါ။ ကိုယ္က TALK လို႔ေရးလိုက္ရင္ computer က "Hello, I am simplest language!" လို႔ screen ေပၚမွာ ေရးျပပါမယ္။ END လို႔ေရးလိုက္ရင္ program က exit ထြက္ပါတယ္။ ကဲ ဒီေလာက္လြယ္တဲ့ language ေလးကိုဘဲ နမူနာအေနနဲ႔ စမ္းေရးၾကည့္ရေအာင္။

Note: Op-code ဆိုတာ Assembler မွာ execute လုပ္ႏိုင္တဲ့ single instruction ပါ။ Assembly ရဲ႕ syntax က high-level language ေတြနဲ႔မတူပါဘူး။ သူမွာ Op-code ေတြကိုတန္းစီၿပီး ေရးရပါတယ္။ Op-code တခုမွာ အမ်ားအားျဖင့္ parameter သုည၊ တခု (သို႕) ႏွစ္ခုပါတတ္ပါတယ္။ ဥပမာ၊ 3 နဲ႔ 4 ေပါင္းတာ (3+4) ကို pseudo assembly နဲ႔ေအာက္ကလို ေရးပါတယ္။
1: PUSH 3
2: PUSH 4
3: ADD
4: POP


ကဲ၊ ခုကၽြန္ေတာ္တို႔ရဲ႕ simplest language ဆီျပန္သြားရေအာင္ Smile ကၽြန္ေတာ္တို႔ရဲ႕ Op-code ၂ ခုကို define လုပ္လိုက္ပါ။

Code:
enum OpCode {
    OP_TALK,
    OP_END
};

Op-code တခုခ်င္းဆီကို store လုပ္ဖို႔ Instruction ဆိုတဲ့ class တခုေဆာက္လိုက္ပါမယ္။ ဒါမွ Op-code တခုခ်င္းဆီနဲ႔ သက္ဆိုင္တဲ့ data ေတြကိုပါ တပါတည္း သိမ္းထားလို႔ရမွာ။

Code:
class Instruction {
public:
    OpCode    code;
    Instruction(OpCode _c) : code(_c) { };
};

ေလာေလာဆယ္ေတာ့ OpCode ဆိုတဲ့ data field တခုဘဲရွိပါေသးတယ္။ ဘာလို႕လဲဆိုေတာ့ ခုကၽြန္ေတာ္တို႕ေရးေနတာ simplest language ေလ Smile Parameter ေတြပါထည့္ေရးခ်င္ရင္ ဒီ Instruction class မွာဘဲ member data ေတြထပ္တိုးလိုက္ရုံပါဘဲ။

Program ေရးတဲ့အခါ Instruction က ၁ ခုထဲရွိမွာ မဟုတ္ပါဘူး။ Program ဆိုတာ Instruction ေတြအမ်ားၾကီး နဲ႔တည္ေဆာက္ထားတာပါ။ ဒီေတာ့ Instruction ေတြသိမ္းထားဖို႔ Program ဆိုတဲ့ class တခုထပ္ေဆာက္လိုက္ပါ အုံးမယ္။

Code:
class Program {
public:
    vector<Instruction>    InstructionList;
    int            nCurrent;
    Program() : nCurrent(0) {};
};

ဒီ Program class ရဲ႕တာ၀န္က instructions ေတြကို store လုပ္ဖို႔နဲ႔ retrieve လုပ္ဖို႔္ပါ။ ဒီေတာ့ store လုပ္ဖို႔အတြက္ function နဲ႔ retrieve လုပ္ဖို႔ function ၂ခုေရးလိုက္ပါမယ္။

Code:
class Program {
public:
    vector<Instruction>        InstructionList;
    int                nCurrent;
    Program() : nCurrent(0) {};

    // Add a new instruction to the program
    void Add(Instruction inst)    {
        InstructionList.push_back(inst);
    };

    // Retrieve current instruction and move to next instruction
    Instruction Next() {
        return InstructionList[nCurrent++];
    };
};

Program ဆိုတဲ့ class က instruction ေတြကို store လုပ္ရုံပါ။ Instruction ေတြ run ဖို႔အတြက္ Virtual Machine ဆိုတဲ့ class တခုထပ္ေဆာက္လိုက္ပါမယ္။ Virtual Machine ကို interpreter လို႕လဲေခၚပါတယ္။ ဒါက Java မွာပါတဲ့ Virtual Machine နဲ႔သေဘာတရားျခင္း အတူတူပါဘဲ။ ဒီ Virtual Machine ကကၽြန္ေတာ္တို႔ရဲ႕ instruction တခုခ်င္းဆီကို ဖတ္ၿပီး execute လုပ္ေပးမွာ ျဖစ္ပါတယ္။ ကဲ ကၽြန္ေတာ္တို႔ရဲ႕ Virtual Machine ေလးကို ေရးၾကည့္ရေအာင္၊

Code:
class Virtual Machine {
public:
    void Run(Program& program);
};

Virtual Machine မွာေတာ့ ေလာေလာဆယ္ program ကို run မဲ့ function တစ္ခုဘဲရွိပါတယ္။ ကဲ Run() function ကိုေရးၾကည့္ရေအာင္။ ကၽြန္ေတာ္တို႕မွာ instruction ၂ မ်ိဳးဘဲရွိပါတယ္။ TALK နဲ႕ END ပါ။ ဒီေတာ့ ေအာက္ကလို instruction ရဲ႕ Op-code ကိုဖတ္ၿပီး switch နဲ႕ခံၿပီး လိုခ်င္တာ ေရးလိုက္ရင္ရပါၿပီ။

Code:
switch (program.Next().code)
{
case OP_TALK:
    printf("Hello, I am simplest language!");
    break;
case OP_END:
    return;
}

Implementation အျပည့္အစုံကိုေအာက္မွာ ၾကည့္ပါ။

Code:
class Virtual Machine {
public:
    void Run(Program& program) {
        while (true)
        {
            switch (program.Next().code)
            {
            case OP_TALK:
                printf("Hello, I am simplest language!\n");
                break;
            case OP_END:
                return;
            }
        }
    };
};

ကဲဒါဆို ကၽြန္ေတာ္တို႕ရဲ႕ simplest language ေလးဟာၿပီးသြားပါၿပီ။ ကဲ ေရးရတာ ဘယ္ေလာက္လြယ္သလဲ Wink Simplest language ဆိုေပမဲ့ Virtual Machine ရဲ႕ run ပုံ concept အျပည့္အစုံသုံးသြားပါတယ္။ ခုေလာေလာဆယ္ ဘာမွသိပ္အလုပ္မလုပ္ေသးေပမဲ့ ေနာက္ chapter ေတြက်ရင္ ဒီ code base ကိုဘဲ တကယ့္ language ျဖစ္ေအာင္ခ်ဲ႕သြားပါမယ္။ ေလာေလာဆယ္ေတာ့ ကၽြန္ေတာ္တို႔ရဲ႕ Virtual Machine ေလးကို စမ္းၾကည့္ရေအာင္၊

Code:
VirtualMachine vm;

void main() {
    Program MyProgram;

    // my script :)
    MyProgram.Add(Instruction(OP_TALK));
    MyProgram.Add(Instruction(OP_TALK));
    MyProgram.Add(Instruction(OP_END));

    vm.Run(MyProgram);
};

ဒါဆို ကၽြန္ေတာ္တို႔ရဲ႕ “script” ကေအာက္ကလို print ထုတ္ေပးပါလိမ့္မယ္။

Hello, I am simplest language!
Hello, I am simplest language!


သိပ္ေတာ့ စိတ္လႈပ္ရွားစရာ မရွိေသးဘူး Smile ဒီေနရာမွာ သတိထားစရာက OP_END ဆိုတဲ့ instruction ကို ထည့္မေပးရင္ error တက္ပါလိမ့္မယ္ (ဘာေၾကာင့္ဆိုတာ Virtual Machine ရဲ႕ Run() function ကိုျပန္ၾကည့္ပါ)။ ဒါက ကၽြန္ေတာ္တို႕ simplest language ေလးရဲ႕ rule တခုပါ၊ Syntax လို႔လဲ ေျပာလို႔ရပါတယ္။ ဒီေနရာမွာ၊ ဒီလို အလြယ္ဆုံး language ေလးေတာင္ syntax ဆိုတာရွိတယ္၊ ရွိတဲ့ syntax ကိုမလိုက္နာရင္ ဘာေၾကာင့္ error တက္တတ္တယ္ဆိုတာ သတိျပဳႏိုင္ပါတယ္။

author :-: Myint Kyaw Thu

http://www.myanmaritresource.info

Law Shay

Law Shay
VIP Member



VIP Member
ေကာင္းတယ္။ ေနာက္မွ ေသခ်ာ လာေလ့လာဦးမယ္။

sawkyawphonenaing

sawkyawphonenaing
MITR New User



အစ္ကုိ တို႔ေရ assembly language ကို အေျခခံက်က်သိခ်င္တယ္ဗ်ာ။
ကူညီၾကပါဦး။ Smile

sawkyawphonenaing

sawkyawphonenaing
MITR New User



ေက်းဇူးပါပဲ ဗ်ာ assembly ကို ျမန္မာလိုေတြ႕ဘူးတာ ဒီမွာပဲ ရွိတယ္။
အပင္ပန္းခံျပီး ေရးထားေပးတဲ့ အစ္ကိုေတြကို ေက်းဇူး အထူးတင္ပါတယ္ဗ်ာ။
မိန္းမ ေခ်ာေခ်ာ နဲ႕ ေကာင္းေကာင္းေလး ရၾကပါေစဗ်ာ။

Sponsored content


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