官术网_书友最值得收藏!

Why do we have programming languages?

Machine code is very difficult. As we saw in the previous chapter, machine code is not made for us humans. It is perfect for computers, but we need something more comfortable to read, write, and understand.

The time it takes to write a program,  find errors and bugs in code, and update a program to add new features costs money. If the language we use can help us reduce the chance of introducing errors in code, it will reduce the costs. If it helps us understand the code when we read it, it will let us add new features faster, and so reduce costs. One goal of a programming language is that it must help us be efficient when we write programs.

It is at this point that the higher-level programming languages enter the scene. They enable us to write our code in something that often, at least to some degree, resembles English. In Chapter 1, Introduction to Computer Programs we saw one attempt to do this: assembly language. The introduction to this language helped somewhat, but it was still not good enough. What we need is something closer to human language.

Look at the following code snippet:

.data

    msgEqual db "Equal","$"

    msgNotEqual  db "Not Equal","$"

.code

main proc

    mov bl,"Alice"                  

    mov bh,"Bob"                  

    cmp bh,bl                   

    jne NotEqual                

    mov ax, seg msgEqual        

    mov ds, ax                 

    mov ah, 09h                 

    lea dx, msgEqual            

    int 21h                     

    mov ah, 4Ch                 

    int 21h                     

NotEqual:

    mov ax, seg msgNotEqual

    mov ds, ax

    mov ah, 09h

    lea dx, msgNotEqual

    int 21h

    

    mov ah, 4Ch   

    int 21h   

main endp

end main

Now, compare it to the following code:

IF "Alice" == "Bob" THEN

    print "Equal"

ELSE

   print "Not Equal"

ENDIF

Believe it or not, they both do the same thing. The first one is in assembly language and the second one is something that resembles a high-level language. Even if you have never seen code before, it is not hard to understand what this program is doing. It compares two text strings, Alice and Bob, and if they are equal, prints this result to the screen, and if not, prints Not Equal. Of course, they are not equal, so the output here is Not Equal.

What these two examples show is the leap that was taken to prove how easy code could be if we compare machine code and assembly code.

In Chapter 1, Introduction to Computer Programs we saw a program that was first written in machine code and then in assembly that printed the text Hello, World to the screen. What would that program look like in some of the high-level languages that we use today? Let's look at some examples.

In Python, it would look as follows:

print("Hello, World")

In C, it looks as follows:

#include <stdio.h>

int main(void)

{

  printf("Hello, World");

  return 0;

}

In C++, we have the following:

#include <iostream.h>

int main()

{

    std::cout << "Hello, World" << std::endl;

    return 0;

}

In Java, we would see the following:

class HelloWorld {

  static public void main( String args[] ) {

    System.out.println("Hello, World");

  }

}

In C#, we have the following:

class HelloWorld

{

    static void Main()

    {

        System.Console.WriteLine("Hello, World");

    }

}

Finally, in JavaScript, we would observe the following:

console.log("Hello, World");

We can see that they all are different and that some have some extra stuff surrounding the part that prints the text, but this comparison makes clear that the step from machine code is huge.

This step clears the path for several different ways to organize and structure code, and since the advent of the first high-level programming languages in the 50s, we have seen tremendous development. Right up to today, a vast amount of languages have been developed.

主站蜘蛛池模板: 攀枝花市| 灵寿县| 抚松县| 阜阳市| 凤翔县| 枣强县| 桂林市| 晋州市| 集贤县| 革吉县| 望城县| 故城县| 高平市| 永济市| 拜泉县| 聊城市| 炎陵县| 东源县| 凭祥市| 稻城县| 玛纳斯县| 二手房| 元谋县| 伊宁市| 文安县| 卫辉市| 无为县| 闸北区| 巴林左旗| 城市| 麻栗坡县| 通城县| 含山县| 华阴市| 田林县| 日喀则市| 铅山县| 陈巴尔虎旗| 鹤岗市| 宁强县| 墨脱县|