Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programs, Instructions, and Registers

Similar presentations


Presentation on theme: "Programs, Instructions, and Registers"— Presentation transcript:

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

2 Class announcements repeat after me:
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.
an instruction 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" a program 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? - no, it doesn't 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 MIPS asm MIPS machine language lw t0, 1200(t1) lw t1 t add t2, s2, t0 <math> s2 t0 t2 n/a add sw t2, 1200(t1) sw t1 t - notice I say "bit patterns" and not "numbers" – because these patterns have meaning beyond just being "numbers" - you DON'T need to know these bit patterns – useless skill CS447

6 Compilers (for more info, take 0449)
#include <stdio.h> int main() { printf("hello!"); return 0; } gcc hello.o libc.a Source Code Compiler Object Files converts C code to machine code ld sticks pieces of machine code together to make a program Linker Executable - you need to make a version of the compiler/linker for each architecture, version, OS, etc. - the resulting executable is tied to one architecture, version, OS etc. CS447

7 Intermediate Language Code Just-in-time Compiler
Virtual Machines class Hello { public static void System.out.printl } javac Hello.class Hello.jar Source Code Compiler Intermediate Language Code Machine Code converts Java code to machine code for a CPU that doesn't exist can be distributed to end users java JIT Virtual Machine Just-in-time Compiler - this splits the compilation step into two parts - now only the VM/JIT have to be made once for each arch/version/OS - the distributed IL code is "cross platform" – it'll work on any platform that has a VM 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 - but really that's what it does CS447

9 How a CPU runs a program + "C = A + B" …and repeat! Persistent Storage
Control Registers Datapath Processor Memory Persistent Storage Program Program 3 5 8 A B instruction C + "C = A + B" …and repeat! - fetch instruction - get the operands for the instruction - do the instruction's operation - write the results of the operation back - repeat! 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 does not define: how to design the hardware! …if there's any hardware at all - it's like a Java interface – just says WHAT it does and what expectations there are… doesn't define implementation 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 the previous version you can run programs written in 1978 on a 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 shorten and simplify programs "search a string for a character" "copy memory blocks" "check the bounds of an array access" x86 is very CISCy 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 compilers that make machine code a small number of instructions instructions are very simple MIPS is very RISCy 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. - modern x86 CPUs are, essentially, RISC CPUs that can read the weird x86 instructions - you'd be amazed how many things are still being run by some old IBM PC or C64 sitting in a closet somewhere 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) # 1 2, 3 4..7 8..15 16..23 24, 25 26, 27 28 29, 30 31 Name zero at v0, v1 a0..a3 t0..t7 s0..s7 t8, t9 k0, k1 gp sp, fp ra - don't memorize these names, they're just an illustration. 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, you have to put down whatever you're holding. - they're big blocks okay - please play along with the metaphor or uh, throw it away? CS447

20 Registers Memory The juggler IMPORTANT! less important 3 5 8 A B C
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 IMPORTANT! less important Registers Memory 3 5 8 A B C - 32b = 4B 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) arguments a0, a1, a2, a3 return values v0, v1 return 42; temporaries t0, t1, ... t9 "saved" s0, s1, ... s7 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 - a registers are used for passing values to functions - v registers are used for returning values from functions - t registers are what you'll probably use for most things - s registers are a bit special… and won't make more sense until we do more functions CS447

23 li t0, 3 li t1, 5 add t2, t0, t1 t0 = 3; t1 = 5; t2 = t0 + t1;
Doing math here are your first MIPS instructions, with pseudocode on the right: li t0, 3 li t1, 5 add t2, t0, t1 t0 = 3; t1 = 5; 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 - li puts a CONSTANT value into a register. - any "load" instruction will put a value into a register. - "immediate" means "number inside the instruction" 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? add t4, t0, t1 sub t4, t4, t2 mul t4, t4, t3 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? - sub subtracts; mul multiplies - t registers are meant for temporary values; the "intermediates" in this expression are a perfect match for t registers 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 jal print_int li a0, 20 # same argument register! 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) - you can't grow more hands every time you need to pick something else up. CS447


Download ppt "Programs, Instructions, and Registers"

Similar presentations


Ads by Google