Placeholder Image

字幕表 動画を再生する

  • the point is the essential thing you need to build a CPU, and with that you can actually build a CB.

  • You get your instructions, remember, because you can point to them you could get your data because you can point to them.

  • But if you've got something else, you can you can be really clever.

  • You could build things that are quite cool on the other thing.

  • Is this idea off a subroutine?

  • Let's just think about what the program is.

  • A program is a set of instructions were gonna execute or routine for doing something.

  • So your routine is just the instructions you gonna execute to do something that might be to say, calculate Fibonacci might be to add the numbers together.

  • It might be to sort of play the latest game, and you could write that just one long sequence of instructions, perhaps with the old loop that would jump back.

  • And so you could do that in assembly and see in Java and C sharpen object of see whatever language you want to choose.

  • Certainly a procedural object already sought and write your whole program.

  • How does one go?

  • But if that's what you got, you prone becomes long.

  • You might wanna jump between different points, which could help, but you often find you writing the same bit of code time and time again.

  • So let's say you want Oh, just print a character out that might be done on some machines by writing to a certain location in memory he's appointed and right to it.

  • Now, if you have to write that code lots and lots of times, you're gonna get pretty bored of writing it.

  • It's going to the same things as boring, too.

  • Right?

  • Chances are you talking the same cut again.

  • You're gonna get bored of writing it seem more likely to make mistakes and more likely to introduce bugs.

  • And so it doesn't work.

  • So the way we do a loop in assembler is we say, Jump to this location on we jump back to a different point.

  • And it, Lex, you're saying bits of code time and again into the point he says, Go to this point and we had an offset removing offset and we jumped back.

  • I only said it to an exact values matter, which, depending on the machine when create leaps and we can also jump to other bits of code.

  • Now, the idea of the subroutine is when you I want to do something, say, like printed character rather than writing it many time.

  • He write it once and you put it somewhere and memory and you know the address of where it is or you have something that points to it.

  • The address.

  • And then whenever you want to print something rather than putting the coding again, you just put in destruction that says, jump to that part of the code that print something out.

  • So you have your main routine, which is, say, doing whatever it is you want to do, and then you have lots of these sub routines or smaller routines.

  • They're going to do parts of it that you just jumped to you when you need to print a character or really character will access the floppy disk or whatever it is you want to do, update your open geo gay.

  • So you just jumped to the subroutines, then you've got interesting problem.

  • How do you get back from that submerging?

  • Because we could Good.

  • So okay, I'll get to the end of the instructions and I just said OK, I've got here.

  • How do I get back?

  • What is easy, the 1st 1 So I'll just jump back to the next instruction.

  • But then you call it again and you end up and you jumped back up to the wrong place.

  • So how do you get around that issue that How do you know where to get back?

  • To try and get Go.

  • Let me try again.

  • So in the subroutine do you write in look att such and such a registers to see where to go back to.

  • So that's a one way, but what you need to do first initialize that register?

  • Yeah, So you say Okay, I'm gonna jump from the main routine to the subroutine Congress use a normal jump because that doesn't remember where you got to go back to.

  • So you have a special time for jump, which is well as jumping also memorizes the address or appointed to the next instruction.

  • Now, just as you said, if you've got something like an arm chip, it's unlikely our comedians are the rescue pie.

  • Then that will store that return addresses.

  • It's called in another register, so it'll copy it where it's got to go to Internet, the register, you execute that code and then you just set the instruction point of the program counter to have the same value.

  • That's in that with the register and jump you back.

  • And you carry on executing that BCP to allow that.

  • Now this problems with that.

  • Because if you want to implement something like a curse and you have a function which calls the subroutine and that super team is going to call itself, so if you do that, you lose the original bit of code.

  • Is that right?

  • Well, you leave the original return address so so straight way we've gone into a never ending like going to a never ending loop.

  • Or so you guess Massey in the problem crashes and things go wrong.

  • What most CPI's do on some of the arm.

  • You implement yourself because it's the way the arms it works.

  • And that's fine is you stole that return address in memory of Ira Pointer.

  • So rather than having a register that contains the return address, you could have register, and it's usually done on the stack using the Stack Point death that actually points to somewhere in memory, where you store that return address and then you get another one.

  • You can move on to another bit of memory and store that on there.

  • Then you can wind back up your stack to get the return address.

  • Is if I remember Professor Brailsford video well enough.

  • The last thing on the stack is the first kind of place Ugo, Then that would kind of come off the step.

  • Pop off this yet I'm remembering it.

  • That's right on pop off the stock and then give you access to where the next pointer is.

  • So you think about it.

  • You sort of think Imagine is probably piece of paper on top of each other.

  • It placed it there if you take the top posting their offer and then tells you to go back the next time it takes you until until you end up with the one that gets you back into the main routine.

  • So some CP use, like the X 86 family that started off with the 80 88 will do that automatically so that when you make that special jump to the subroutine, they will store the return address on the stack for you.

  • Others like the armed chip require you to do a story on register, and then you have to put it on the stack.

  • If it was just the architecture that these horses for courses, it doesn't make any real difference.

  • So that's depending on how many instructions district it's just It's just the design, you think.

  • What's the you making?

  • You chipped a more complex by automating that where that's what, if we just that the program doing because sometimes you don't need to do that.

  • They could make your program faster with those leaf subroutines, as they tend to be known.

  • And so once you've got that technology, either part of AI software or part vier that see hardware and you can move it between the two as much as you like.

  • Then you can use the subroutine toe, make CODIS and right, and from that you can build functions and see procedures, methods and objects all implement in another tortured language, all implemented in that way.

  • They using the idea of the separately.

  • So when you call them, they store the return address on dhe.

  • Excuse the code, and then they have some mechanism to get back from that return address toe what get back from the end of the routine via the return address to where you carry on.

  • Now.

  • The clever thing is how they originally came up with that idea because the original CPU something like Ed Sack, back in the late forties early fifties, didn't support subroutines in hardware.

  • And yet a tackle, David Wheeler, was able to create what was called the Wheeler Jump, which he used to implement the very first subroutines.

  • Now this is really clever, but absolutely nightmarish, and I should put up with this claim.

  • But do not try this at home because it used what's called self modifying code.

  • So let's give it a that let's use some sort of pseudo arm code, and we've got some instructions that are going off and they're going to jump to a silvery gene.

  • We'll call it Soup, and then we're gonna carry on here and then so we'd have a subroutine somewhere off in memory, and it would have the instructions it would need to execute.

  • And then at some point, you're going to get to a point here where you have to return.

  • That's the way most things is structured Now we have jumps and things like that.

  • But that's all we had so we could have a jump here that will get back, would have a branch here that would say Branch and let's just give it a label back.

  • But we don't know where back is because we don't know that until we actually call it.

  • So if we know where the silver routine is and because when we write the code, we know how many instructions there are here.

  • We can work out where in memory this branch back instruction is going to be a branch to back.

  • And so this is where the clever bit happens.

  • What David really did was say Okay, Before we make this branch or jump to the subroutine, we're gonna put in instructions here, which and we're using some really clunky Sucaco modify jump back.

  • So you had code that modified itself out.

  • It was running and it would alter this instruction to remove back there and put in Let's just call it return on modified that to be returned.

  • And then a bit later on, we call again, so we'd modify the jump.

  • He asked, What if I jumped back call the silver routine we branch to it on.

  • Then we will carry on here and we call this.

  • Let's call it Fred.

  • And so we replaced.

  • Return here with the address off Fred.

  • So that's now pointing to Fred so that when the subroutines called, it executes and then jump back to the right place.

  • There's only about the ability to jump to an address in your CPU and the ability to modify memory Bye monkey around.

  • We're using a pointer.

  • You could implement its of routine, even though the CPU didn't have any support for it.

  • Now this is what's called self modifying Kate, because there's the program is running.

  • It is changing itself.

  • Acid is running.

  • If you get this wrong, the program crashes and bad things happen.

  • So that's possibly want to do for fell apart.

  • Which is why this technology is then moved into the CPU and implemented in hardware for the same reason is something we got to do a lot off so that be implemented once we re use it in the same way that we going to do lots of things in our programs, we make a subroutine to do that and rather is.

  • This is so common we put it into the hardware because we're going to use it, or at least some sort of hardware support.

  • And, of course, the way modern CPS have got with cash is, and things in the self modifying code doesn't work properly anyway, because they're not true for Norman machines.

  • Based on my pretty train network, I'm going to produce 5000 characters of random text and let's see what looks like eso just boot up for a minute, and then it will run pretty quick once it gets going.

  • There we go so you can see we've got comments.

  • We've got replies.

  • At first glance, it looks plausible when you actually read into the comet, thereby bizarre.

the point is the essential thing you need to build a CPU, and with that you can actually build a CB.

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

B1 中級

エッセンシャルズサブルーチンとウィーラージャンプ - コンピュータマニア (ESSENTIALS: Subroutines & The Wheeler Jump - Computerphile)

  • 7 0
    林宜悉 に公開 2021 年 01 月 14 日
動画の中の単語