Placeholder Image

字幕列表 影片播放

  • Well, let me begin by asking you a question

  • Okay, what do you think the simplest program we could write

  • well

  • Not being a programmer but having done a lot of computerphile videos.

  • I suspect we're going for hello world here

  • So, yeah hello world

  • I mean, it's probably the classic one everyone start with if we... we can even write it on a piece of paper

  • I'm going to use C to talk about this. You could write it in any programming language of your choice

  • Let me guess you walked into Dixon's and wrote a rather rude version of hello world. I didn't you, probably did

  • Oh, so the scrolling on the screen thing your basic is effectively hello world

  • It's basically hello world

  • So I'd go some prints something and then yeah, that's the more advanced version. But yeah, I think so

  • We could write it and see we have our main function

  • we have to have a few arguments if we're doing this properly because otherwise we'll get

  • Complaints in the comments and then we can do a printer

  • hello dot dot

  • Backslash Ames we want to go to a new line at the end

  • and

  • A head of files and we've written Heather world

  • These pens are bought because they're a bit quieter and yet there's always a compromise somewhere isn't there's no other perfect pen

  • Now we've got pens that you can't actually read so there's a hello world program

  • And on the face of it it is one of the simplest programs you can write you can understand the instructions

  • That's going on and it says print whatever string it is. Hello world and it print it out. You compile it and you run it

  • But actually when you think about what's going is it a lot a lot of functionality is being hidden behind there

  • It's a good illustration

  • And one of the things you see a lot in computer science is that we build computer systems by using layers of abstraction

  • So we're making use of a code library. So what we've already got here is a level of abstraction in that

  • Printf does a lot of things it can do more than just print out a single line of text

  • we can actually get it to print out numbers integers as

  • Text so we can read them on the screen and it will convert them from the integer

  • to think so that's code libraries hiding a whole layer of

  • Instructions so we can think about this in terms of we get another bit paper. Let's turn it upside down and use the Australian version

  • We can think about we write our program and we're writing it

  • And I've used the wrong pen again

  • We're writing it in C and we're making use of that C standard library to actually make it do things

  • but of course as we know

  • The computers don't understand C. They understand their machine code. So actually we already have to convert this into

  • The machine code for the program so we convert it into the machine code for the CPU that we're running on in this case

  • It's an x86 thing, but we could run it on a six eight thousand. We could run another Zed 8006 five or a two

  • PowerPC whatever type of computer you wanted to so we've already got on two layers of abstraction there

  • We've got without stretching away how the hardware works

  • Because as soon as you write it and see we can then compile it to run on different CPUs

  • But we've also started to abstract away

  • How we actually print something out so we can use printf to print a number we can use it to print text

  • But we don't have to know how to convert a number

  • into

  • Text what does the F stand for?

  • Okay. Well, that's a very good thing printf time to print formatted and that's what I was talking about

  • So you can say I want to print this number and you can also say I want it formatted to look like this

  • So I want this many of leading zeroes. I want this many decimal places afterwards and so on you can format the output

  • Hence the app so we've got a program here

  • We've got a hello world program and we want to print out the variable with a 42 in it

  • We can say print out hello world

  • And we can put a % D in there. Just say we want to print out a number and

  • Then we can compile that program

  • Of course they have not made any mistakes and if we run that thing it prints out the number 42

  • And again, this is a good example of that abstraction. If I were to print out that number with four leading zeros, I can say

  • % 0 for D

  • Compile it again

  • And it prints it out 0 0 for 2 all that sort of complexity well do I print out that number

  • Okay, well had open time ago divide it by 10

  • The remainder will be that did you I need to put it last than the rest of the number and then I drew that again

  • That's all abstracted away from me as a programmer. I can think about what I want to write

  • At a high level, which is our different out hello world followed by the number

  • With two leading zeros, and we could write that so we've already abstracted that away

  • But that standard library then still needs to print that out. So it needs to take each of the characters its generated and

  • Print them out on screen

  • or send them to

  • the serial teletype that you're dealing with or however it is you're outputting these things because if you think about it a

  • modern computer system

  • I'm sitting at my computer here typing the commands in to run that program just written on this system

  • but I could have logged in remotely using SSH or something to another computer system and run the program there and

  • Then it would have had to do something different

  • So in this case, it's printing it on my local screen if I'd logged in to a different machine

  • You would have to run the program there send the characters. He wants to print having encrypted them

  • to my machine and then print them out sue got a different thing so

  • Even the C standard library is built to print things out but it's built on and I was stretching in this case that abstraction

  • Is the operating system so we can add another

  • Layer of abstraction, which is the operating system and most operating systems. Certainly the modern ones. We get UNIX windows mac

  • o s-- viscose whatever one you were to do will provide some level of abstraction so you can just say print this and

  • It will then send it to the right thing and do the right thing based on what you go

  • So I hello world program are suddenly gone from being one

  • Line that prints things out, but that's then opened a can of worms because it's using a library

  • It's also being converted to machine code we can see that so we can tell the compiler to just produce the machine code for us

  • And then if we open that up

  • We get the machine code version here. And as you can see that's quite a lot more lines of text that we've got

  • There we can see there the call to printf and we're doing various things to get that

  • How that works is perhaps another video on how we call the function in the machine code

  • So let's backtrack a bit from printf because as we've talked about that's a more complicated

  • Implementation it can format the text and let's just think about how we can just print a single string to the screen

  • so let's rewrite this program to use put's which is another

  • C standard library function that can print out. I think this one can't do formatted text

  • So remove our additions and so we now got a slightly simpler

  • Version of hello world that still does the same thing and then it prints out hello world

  • It also prints an extra new line because puts my app as a standard output

  • It adds a new line to use it got slightly from bit of functionality

  • But we can start to think about how that works. So what that is given as input is

  • the string hello world, which is

  • Arranged in memory suddenly. Our program is getting more complicated because we're actually having to think about how things are stored in there as

  • Basically a series of memory locations one after the other with the H followed by the e

  • Followed by the L followed by another L followed by an O and so on and so what pot-stirring would have to do

  • For a naive implementation would do is its points at the H it gets that character

  • Prints it out to the screen and it gets the next character prints it out to the screen

  • Then it gets the next character prints out of screen and so on until it gets to some end

  • marker

  • Which in C is done by putting a 0?

  • Character the ASCII code 0 to represent the end of string or the programming language do it differently pascal

  • For example, put the length of the string at the beginning as a number

  • So it's said to be x characters and then it print out X characters C says the strings ended by putting a 0

  • at the end

  • So we've already what looked like a simple program when we take away the layer of abstraction

  • becomes more complicated now actually

  • if you were to implement this you wouldn't print each character separately because

  • to print it we said we have to call the operating system and

  • Every time we call the operating system we have to pass control from the user mode to supervisor mode

  • The operating system's mode which takes some time. So actually what we would do

  • Send it directly as a string to the operating system and then the operating system will print out that whole string for us

  • So we can change our program to do that in that we now use the command

  • Write write consented lots of different places so we can set it to the standard output

  • Which on UNIX is number one?

  • Send the string and now we need to tell it how many characters there are so hello has got five worlds got five

  • That's ten. I've got a space and a new line. That's 12. So we want to send 12

  • Characters so let's compile that version up

  • And I haven't put the right header in for

  • That so we're getting a error message that was included to my harridans

  • It's to work and now if you run hello world, it's still prints. Hello world

  • But we've taken away another layer of abstraction in that this case my C program is directly calling

  • the operating system so we removed that level of

  • abstraction now as we said the operating system code then has to

  • interact with all the different things and on a system like UNIX

  • Because of the heritage of it back to the 1970s and earlier

  • it emulates a Tele time machine and a teletype machine was basically

  • Typewriter that was controlled by electronics and you sent the characters and in move things one after the other

  • Printing the characters out onto paper not unlike this and then when you sent the line feed character

  • You actually move to the next line

  • actually

  • the original teletypes required you to send both a carriage return and

  • A line feed because you have to send the thing back to the beginning of the carriage

  • So these are mechanical bits of there's a mechanical bits of things and actually you still see this today

  • the reason why UNIX and Windows differ on how text files are encoded is

  • That Windows and carries both the carriage return

  • Which sends things back to the end and the line feed to go on to the next line that you have to send?

  • To the teletype whereas the eunuch said. Well actually

  • What was going to send that different things have different things will just send a line feed and so the UNIX operating system

  • Abstracted that away so our hello world program

  • simple and is simple

  • Because there's a whole load of other things going on which are hidden from us

  • They're abstracted away so we can think about it in terms of I want to print out this line of text

  • But actually under the hood there's lots of things going on extra bits of code

  • Which are doing things even things like when it appears on screen?

  • The operating system is having to think. Well, actually, I've got to draw the dots that make up the edge here and

  • Because the way a teletype certainly in the Western world has been designed to work means of the ease following

  • That then I've got to fin the e

  • After that and work out what positions they're going on. And so if you were to look at all the code necessary

  • To write hello world

  • From the bare Hardware bearing in mind that you've got to find out what Hardware you've got on there

  • And then talk to it in the right place you find it would be a lot of code to do a very simple program

  • Like hello world. Is there a simpler program?

  • It's a program. It doesn't do anything

  • Well if I just

  • delete

  • That we think that a program that starts and stops and doesn't do anything

  • With that still call operating systems and open libraries and things

  • So this is interesting things that yeah, this is a good point

  • Is that actually that program if we compile it and have a look at the machine code?

  • We now see that actually there isn't much there. This is basically the whole code here. The rest is just setting up various things

  • So it works what the compiler have done things there then and yes, we've now got we've got about four or five instruction

  • but

  • That isn't the beginning of our program

  • When you compile the C program?

  • There's an extra bit that's joined on to the beginning that's linked onto the beginning

  • Which sets up the operators and research how much memory this program needs and so on and gets things to the point

  • Where that prone could work? So actually that isn't the whole program

  • But it's getting close enough

  • and of course

  • We still have to bear in mind that we've got to boot the computer when we turn the computer on

  • The computer starts in a defined way. It starts executing code at a defined location

  • Your BIOS is then there in a modern machine or you fei in a really modern machine

  • Executing things that will then start to the point where it can load the operating system the operating systems loading up

  • So it can talk to various other things and so on

  • And there's all these layers hidden behind this to the point where we can just literally type

  • run this program

  • Layers of abstraction are great in computer science because it means that we can think about the program. We want to write hello world

  • Rather than having to think about all the nitty-gritty

  • Details that we actually to get that implemented

  • Now the problem is if I send this off to the website I've just given them my password

  • I mean not quite

  • Because sha-1 is hash but that could be broken especially if my password is not good

  • Right and also he's got a bunch of these passwords and hashes already computed in this database

  • So as soon as he sees that I've got the hash

Well, let me begin by asking you a question

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級

你好(世界)抽象!- 電腦愛好者 (Hello (World) Abstraction! - Computerphile)

  • 2 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字