1 / 26

Programs, Instructions, and Registers

Learn about programs and instructions in CS447, including machine language, assembly language, compilers, and virtual machines.

emackenzie
Télécharger la présentation

Programs, Instructions, and Registers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programs, Instructions, and Registers CS/COE 0447 Jarrett Billingsley

  2. Class announcements • lab1 is available! finish by saturday night • repeat after me: • 8 bits makes a byte • a byte is 2 hex digits • loads copy from memory to registers • stores copy from registers to memory • it'll make more sense soon CS447

  3. Programs and Instructions CS447

  4. What are they? We just don't know. • aninstruction is a single operation for the computer to do, like: • "add two numbers together" • "move a number from one location to another" • "go to a different place in the program" • "search a string for a character" • aprogram is a series of these tiny instructions • how do we create these instructions? • and how does the computer "understand" them? • does the computer understand anything? CS447

  5. Machine language and Assembly language • machine language instructions are the patterns of bits that a processor reads to know what to do • assembly language (or "asm") is a human-readable, textual representation of machine language CS447

  6. Compilers (for more info, take 0449) #include <stdio.h> int main() { printf("hello!"); return 0; } gcc hello.o libc.a Object Files Compiler Source Code ld sticks pieces of machine code together to make a program converts C code to machine code Linker Executable CS447

  7. Virtual Machines class Hello { public static void System.out.printl } } javac 0010100011100000011101101110100110010100010010010100111 Hello.class Hello.jar Intermediate Language Code Machine Code Compiler Source Code java JIT can be distributed to end users converts Java code to machine code for a CPU that doesn't exist Virtual Machine Just-in-time Compiler CS447

  8. How a CPU runs a program • read an instruction • do what it says • go to step 1 • ...okay there's a little more to it than that CS447

  9. How a CPU runs a program Memory Persistent Storage Program Program Control Registers A B 3 5 8 instruction C Datapath + "C = A + B" …and repeat! Processor CS447

  10. ISAs CS447

  11. Instruction Set Architecture (ISA) • an ISA is the interface that a CPU presents to the programmer • when we say "architecture," this is what we mean • it defines: • what the CPU can do (add, subtract, call functions, etc.) • what registers it has (we'll get to those) • the machine language • that is, the bit patterns used to encode instructions • it doesnot define: • how to design the hardware! • …if there's any hardware at all CS447

  12. An ISA example: x86 • descended from 16-bit 8086 CPU from 1978 • extended to 32 bits, then 64 • each version can run all programs from theprevious version • you can run programs written in 1978 ona brand new CPU! • so why don't we learn x86 in this course? • it can do a lot of things • its machine language is very complex • making an x86 CPU is… difficult • ultimately, we would waste a ton of time CS447

  13. All three processors run the exact same programs… • but they're TOTALLY different on the inside I’m an x86 CPU! I’m an x86 CPU! VIA Nano AMD Zen I’m an x86 CPU! Intel Core i7 CS447

  14. Kinds of ISAs: CISC • CISC: "Complex Instruction Set Computer" • ISA designed for humans to write asm • from the days before compilers! • lots of instructions and ways to use them • complex (multi-step) instructions to shortenand simplify programs • "search a string for a character" • "copy memory blocks" • "check the bounds of an array access" • x86 is veryCISCy prguitarman.com CS447

  15. Kinds of ISAs: RISC • RISC: "Reduced Instruction Set Computer" • ISA designed to make it easy to: • build the CPU hardware • make that hardware run fast • write compilersthat make machine code • a small number of instructions • instructions are very simple • MIPS is veryRISCy • MIPS and RISC were the original RISC architectures developed at two universities in California • the research leads were… Patterson and Hennessy… CS447

  16. Popular ISAs today • x86 (today, it’s x86-64 or “x64”) • most laptops/desktops/servers have one • ARM • almost everything else has one • Everything else: Alpha, Sparc, POWER/PPC, z, z80, 29K, 68K, 8051, PIC, AVR, Xtensa, SH2/3/4, 68C05, 6502, SHARC, MIPS... • microcontrollers • mainframes • some video game consoles • historical/legacy applications • despite its limited use today, MIPS has been incredibly influential! • modern ARM is very similar. CS447

  17. The MIPS ISA:Registers CS447

  18. The registers • registers are a kind of small, fast, temporary memory inside the CPU • the CPU can only operate on data in registers • MIPS has 32 registers, and each is 32 bits (one word) • the registers are numbered 0 to 31… • …but they also have nice names • (I don't like the dollar signs) • (I modified MARS so you don't have to use them) CS447

  19. Are registers variables? • nNNNnnnnnnNNNnNNnnnnnnnnnno. • registers are more like… hands. if you want to build something by hand, you only have two hands to do it. if you want to pick something else up, youhave to put down whatever you're holding. or uh, throw it away? CS447

  20. The juggler • you only have a few registers (32)… • …and they can only hold small things (32 bits; how many bytes?) • your program's values are in memory • the registers are a temporary stopping point for those values Memory IMPORTANT! Registers 3 5 8 A B C less important CS447

  21. Really, you don't have that many • you cannot write every program using only registers • don't try to • please. • every piece of your program has to share the registers. • unlike high-level languages • where everyone gets their own locals • nuh uh CS447

  22. The four* kinds of registers • most of your programs will use just these: foo(1, 2, 3) return42; for anything that isn't an argument or return value… 1. use a t register. 2. unless you need the value to persist across a function call (jal). what CS447

  23. Doing math • here are your first MIPS instructions, with pseudocode on the right: t0 = 3; t1 = 5; t2 = t0 + t1; li t0, 3 li t1, 5 add t2, t0, t1 • li stands for "load immediate." what does it look like it does? • add does… what? ;) • just like in Java, C, whatever: the destination is on the left CS447

  24. Please excuse my dear aunt Sally • say we had a longer expression: t4 = (t0 + t1 – t2) * t3 • what order do we do these operations in? • if add adds… what instruction subtracts? or multiplies? I just decided to put the intermediate value into t4. I could have used t5 or whatever, but since the value's gonna end up in t4 anyway, why not? add t4, t0, t1 sub t4, t4, t2 mul t4, t4, t3 CS447

  25. Yes, you can and should reuse registers • you only have a limited number! you must reuse registers. • if you make two function calls… • …you will use the same argument registers for both. li a0, 10 jalprint_int li a0, 20# same argument register! jalprint_int • your friends t0, t1, and t2 will be getting a lot of use. • (honestly? I rarely ever use more than those at one time) • (there are so many t registers for compilers' sake) CS447

  26. Computers abhor a vacuum • every register and variable always holds something. • when you put something in a register, it will stay there forever • until you put something else into it. • values are always, always copied. • when you do this: int x = 10; int y = x; • x doesn't become "empty", does it? • no! we just have two copies of 10 now. • we will see instructions named "move" or whatever • but they always copy. CS447

More Related