Introduction CS2253 ● Goal: Write a Simple C Program and Understand ● Why and What for 2253 How the Computer Actually Executes It
Total Page:16
File Type:pdf, Size:1020Kb
Introduction CS2253 ● Goal: write a simple C program and understand ● Why and what for 2253 how the computer actually executes it. ● This year, we study the ARM7TDMI processor. ● Levels of abstraction ● Last year, we used the fictional LC-3. ● Instruction Set Architectures ● The Church-Turing thesis essentially states that all systems with a certain minimal computational ● Major parts of any computer capability are able to compute the same things ● von Neumann architecture as each other. So LC3 vs ARM vs Intel x86 does ● Flow of control not matter, at least theoretically. ● And in actuality, LC3 and ARM etc. are fundamentally similar. ● Easy to pick up a second machine.... Levels of Abstraction Textbook Fig 1.9 ● From atoms, we build transistors. [Lowest level] ● From transistors, we built gates (ECE courses). ● From gates, we build microarchitectures (CS3813) that implement machine instructions. ● From machine instructions, we can make simple programs directly (first part of this course). ● Or a compiler can string together machine instructions corresponding to our C code (second part). ● From simple pieces of C code, we can build OSes, databases, other complex software systems. [Highest level] Why Should I Care About the Lower Microarchitecture example (block Levels? diagram, book Figure 1.4) ● If you want to work in the computer hardware field, it's obvious. ● If you want to work in software: – it's sad if you aren't intellectually a little bit curious about how things really happen. – when things fail, you tend to need to “see behind the veil” to understand what is going wrong. Debugging often requires a lower-level view. – it helps you understand why some operations would be fast, while others would be slow. (Performance debugging.) Instruction Set Architecture Multiple implementation of an ISA ● ISA is an important concept. In Java terms, it is like an ● In Java, several classes can implement the same Interface. Interface to the microarchitecture (hardware). ● So, several microarchitectures can have the same ISA. They ● It specifies all the things that you would need to know, to can run the same machine instructions as each other. write a machine-instruction program: ● IBM mainframes in the 1960s: fast implementation if you're – what are the basic instructions? rich, slow ones if not. – how does the machine find the data for instructions? ● Today: AMD and Intel processors can run the same code. – what are the basic data types supported (bit lengths)? ● But computer designers like to extend ISAs over time. – how is memory organized? Backward compatibility is the goal. No bad idea ever – how and where are the instructions stored? dropped. Ugly messes like Intel x64 architecture. Textbook Fig 1.5 Major Parts of Any Computer ● An input and an output facility (from/to people or devices) ● Memory/Memories to store data and programs ● A “datapath” with the logic needed to add, multiply, store ... binary values, etc. ● A “controller” that goes through the program and makes the datapath do the required operations, one at a time. ● Controller + Datapath = Processor (aka CPU) ● Controller is the “puppeteer” and the datapath is the “puppet”. Textbook Figure 1.2: Block diagram John von Neumann's architecture of a SOC (system on chip) ● John von Neumann was a brilliant mathematician (and statistician and nuclear weapons guy and father of game theory and inventor of MergeSort and ...) who wrote an influential report in 1946 with a computer design. ● A von Neumann architecture stores the program in (a different part of) the same memory that stores the data. ● A Harvard architecture uses separate memories. ● Modern computers appear to be von Neumann, but behind the scenes are a bit Harvard-ish. ● Except for some special-purpose machines. Textbook Figure 1.8 von Neumann and the IAS, 1952 How a von Neumann Architecture Be a Human CPU Works ● The program is a list of instructions located in memory. Memory Address ● “Hokey Pokey” Part of the control unit is the Program Counter (PC), 1000 right hand in program which points to the exact place for the current instruction. 1001 right hand out 1002 right hand in ● PC starts at 1000 ● while (true) { 1003 shake it all about Fetch current instruction from memory 1004 turn yourself around ● Fetch and do “right Increment PC 1005 go back to address hand in”; PC ← 1001 Inspect current instruction and do what it says 1000 } ● Fetch and do “right ● This is the Fetch-Execute cycle. hand out”;PC ← 1002 ● …. Control Flow More Realistic Instructions ● Normally, when you finish one instruction you advance to the (sequentially) next one. ● Instead of “right hand in”, a CPU instruction will be something simple like a request to take two integers ● This is called straight line flow of control. PC just stored locally in the CPU, add them, and store the result increments. in the CPU ● But there are instructions like “go back to the ● Or “go to memory location 2000 and load the 8-bit instruction at address 1000” that disrupt this. integer there into the CPU” ● Good thing, since that's how we can do IF and ● Or “go back to memory address 1000”, as in the Hokey WHILE in a high-level language. Pokey program. A jump or branch instr. ● Control flow is often disrupted conditionally, ● A compiler or a programmer needs a lot of simple based on status flags that record what happened instructions to do something more complicated. earlier (eg, did last addition give -ve result?) Data Bits and Bit Sequences ● (Some repeating CS1083, ECE course) ● Fundamentally, we have the binary digit, 0 or 1. ● More interesting forms of data can be encoded into a bit sequence. ● bits and bit sequences ● 00100 = “drop the secret package by the park entrance” ● integers (signed and unsigned) 00111 = “Keel Meester Bond” ● bit vectors ● A given bit sequence has no meaning unless you ● strings and characters know how it has been encoded. ● ● floating point numbers Common things to encode: integers, doubles, chars. And machine instructions. ● hexadecimal and octal notations Encoding things in bit sequences How Many Bit Patterns? (From textbook) ● Floats ● With k bits, you can have 2k different patterns ● 00..00, 00..01, 00..10, … , 11..10, 11..11 ● Remember this! It explains much... ● Machine Instructions ● E.g., if you represent numbers with 8 bits, you can represent only 256 different numbers. Names for Groups of Bits Unsigned Binary (review) ● nibble or nybble: 4 bits ● We can encode non-negative integers in unsigned binary. (base 2) ● octet: 8 bits, always. Seems pedantic. ● 10110 = 1*24 + 0*23 + 1*22 + 1*21 +1*20 represents the ● byte: 8 bits except with some legacy systems. In mathematical concept of “twenty-two”. In decimal, this course, byte == octet. this same concept is written as 22 = 2*101 + 2*100. ● after that, it gets fuzzy (platform dependent). ● Converting binary to decimal is just a matter of adding For 32-bit ARM, up powers of 2, and writing the result in decimal. ● – halfword: 16 bits Going from decimal to binary is trickier. – word: 32 bits Division Method (decimal → binary) Subtract-powers method ● Repeatedly divide by 2. Record remainders as ● Find the largest power of 2, say 2p, that is not you do this. larger than N (your number). The binary number has a 1 in the 2p's position. ● Stop when you hit zero. ● Then similarly encode N-2p. ● Write down the remainders (left to right), starting with the most recent remainder. ● Eg, 22 has a 1 in the 16's position 22-16=6, which has a 1 in the 4's position 6-4 = 2, which has a 1 in the 2's position 2-2=0, so we can stop.... Adding in Unsigned Binary Fixed-Width Binary Integers ● Just like grade school, except your addition ● Inside the computer, we work with fixed-width values. table is really easy: ● Eg, an instruction might add together two 16-bit unsigned binary values and compute a 16-bit result. ● No carry in: 0+0=0 (no carry out) ● Hope your result doesn't exceed 65535 = 216-1. Otherwise, you 0+1= 1+0 = 1 (no carry out) have overflow. Can be detected by a carry from the leftmost 1+1= 0 (with carry out) stage. ● If a result would really doesn't need all 16 bits, a process called ● Have carry in: 0+0=1 (no carry out) zero-extension just prepends the required number of zeros. 0+1 = 1+0 = 0 (with carry out) ● 10111 becomes 0000000000010111. 1+1 = 1 (with carry out) ● Mathematically, these bit strings both represent the same number. Some Ways to Encode Signed Signed Numbers Numbers ● A signed number can be positive, negative or ● All assume fixed width; examples below for 4 bits zero. ● Sign/magnitude: first bit = 1 iff number -ve ● An unsigned number can be positive or zero. Remaining bits are the magnitude, unsigned binary ● Note: “signed number” does NOT necessarily Ex: 1010 means -2 mean “negative number”. ● Biased: Store X+bias in unsigned binary Ex: 0110 means -2 if the bias is 8. (8+(-2) = 6) In Java, ints are signed numbers. Can they be positive?? Can they be negative?? ● Two's complement: Sign bit's weight is the negative of what it'd be for an unsigned number Ex: 1110 means -2: -8+4+2 = -2 ● You can generally assume 2's complement... Why 2's Complement? 2's Complement Tricks ● There is only one representation of 0. (Other ● +ve numbers are exactly as in unsigned binary representations have -0 and +0.) ● Given a 2's complement number X (where X may be -ve, +ve or zero), compute -X using the twos ● To add 2's complement numbers, you use complementation algorithm (“flip and increment”) exactly the same steps as unsigned binary.