11: There’s A Method For All This.
Take Up Code - En podcast af Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Kategorier:
We talk about methods in this episode. There’re many kinds of methods and we’ll need to split this topic into multiple episodes. I’ll explain how methods are called and how they return when they’re done. And a bit about how viruses can sneak in and gain control. It doesn’t matter what language you’re using, there will be methods involved at some level. Methods allow you to wrap up a bunch of instructions so you can call on them whenever you need. But what does this mean? How can you just call something? It’s not like you’re ringing a dinner bell and your instructions come running towards you. You’ll learn about two very important registers inside a microprocessor called the instruction pointer and the stack pointer. I explain how the instruction pointer gets updated and always points to the next instruction in line to be executed. And how the stack is a section of memory that helps coordinate methods with the help of the stack pointer. A modern processor has support to help eliminate the threat of viruses of the type I talked about but it’s a constant struggle to keep up. Learning about how methods work, how the compiler prepares to make a method call, how the processor transfers control to the method, and then how the method returns are critical to understanding how your code behaves and how to keep it secure. Listen to the full episode or read the full transcript below. Transcript It doesn’t matter what language you’re using, there will be methods involved at some level. Methods allow you to wrap up a bunch of instructions so you can call on them whenever you need. But what does this mean? How can you just call something? It’s not like you’re ringing a dinner bell and your instructions come running towards you. Both microprocessors and microcontrollers have builtin support for methods. I mentioned in an earlier episode that a microprocessor knows how to request its first instruction and then its next instruction and so on. This is possible because the processor has a set of registers it uses for keeping track of information inside of itself. These are like internal memory slots. Some registers are general purpose and can be used by your program for whatever you want. For example, you can read a couple values from memory, add them together, and save the result in a register. This is sort of like how you can use the memory function of your calculator to temporarily store a number. There are two registers we’ll talk about now that have special purposes. The first is the instruction pointer. It points to the address of the next instruction to be executed. The second is the stack pointer which points to a section of memory used to coordinate method calls. The instruction pointer updates itself automatically whenever an instruction is read, the processor can figure out how many bytes the instruction requires and will update the instruction pointer to point to the next memory address. Some high level programming concepts such as loops really come down to an instruction that changes the instruction pointer to point to an address that has already been executed. And if statements also come down to changing the instruction pointer so that it will skip over some other instructions. Calling a method is similar. Just change the instruction pointer to point to the first instruction in the method. To be clear, it’s the processor that changes the instruction pointer. But we can get it to make the change with certain instructions that alter the normal flow. It’s sort of like how when you have your day all planned and full of things to do when a friend asks if you can help real quick with something else. What do you do? You first make sure that you remember what it was you were going to do next, then you help your friend. And when you’re done, you remind yourself what the next task was before you were interrupted and go back your plan.