1 Computer Architecture COSC 3430 Lecture 3: Instructions
The Instruction Set Architecture instruction set architecture software hardware The interface description separating the software and hardware.
Computer’s Internal Clock Frequency: Frequency is number of complete waves passing per unit time. It is measured in Hertz (Hz), the number of cycles per second. 1 KHz = 10 3 Hz. 1 MHz = 10 6 Hz. Series of electrical pulses with fixed interval between pulses (cycle time) 800 MHZ machine = 800x10 6 cyc/sec = 1.25x10 -9 sec/cyc = 1.25 ns/cyc Synchronizes all components Updates state: inputs only change stored values at specific instances Regulate instruction execution: e.g., 1/clock cycle cycle time time for one full cycle
Operations of the Computer Hardware Chapter 2.2
Assembly Language Instructions Language of the machine More primitive than higher level languages e.g., no sophisticated control flow Very restrictive e.g., MIPS arithmetic instructions We’ll be working with the MIPS instruction set architecture similar to other architectures developed since the 1980's used by NEC, Nintendo, Silicon Graphics, Sony, … Design goals: maximize performance, minimize cost, reduce design time, minimize memory space (embedded systems), minimize power consumption (mobile systems)
RISC - Reduced Instruction Set Computer RISC philosophy fixed instruction lengths load-store instruction sets limited addressing modes limited operations MIPS, Sun SPARC, HP PA-RISC, IBM PowerPC, Intel (Compaq) Alpha, … Instruction sets are measured by how well compilers use them as opposed to how well assembly language programmers use them
MIPS Reference Data
MIPS Arithmetic Instruction MIPS assembly language arithmetic statement add$t0, $s1, $s2 sub$t0, $s1, $s2 Each arithmetic instruction performs only one operation Each arithmetic instruction specifies exactly three operands destination source1 op source2 Those operands are contained in the datapath’s register file ( $t0, $s1,$s2 ) Operand order is fixed (destination first)
MIPS Arithmetic Instruction MIPS assembly language arithmetic statement add$t0, $s1, $s2 sub$t0, $s1, $s2 Each arithmetic instruction performs only one operation Each arithmetic instruction specifies exactly three operands destination source1 op source2 Those operands are contained in the datapath’s register file ( $t0,$s1,$s2 ) Operand order is fixed (destination first) rd rs rt
Compiling More Complex Statements Assuming variable b is stored in register $s1, c is stored in $s2, and d is stored in $s3 and the result is to be left in $s0, what is the assembler equivalent to the C statement h = (b - c) + d
Compiling More Complex Statements Assuming variable b is stored in register $s1, c is stored in $s2, and d is stored in $s3 and the result is to be left in $s0, what is the assembler equivalent to the C statement h = (b - c) + d sub$t0, $s1, $s2 add$s0, $t0, $s3 rd rs rt rd b c rd b-c d
Operands of the Computer Hardware Chapter 2 section 2.3
MIPS Register File Operands of arithmetic instructions must be from a limited number of special locations contained in the datapath’s register file Holds thirty-two 32-bit registers With two read ports and One write port Registers are Faster than main memory Easier for a compiler to use Can hold variables so that code density improves (since register are named with fewer bits than a memory location) Register addresses are indicated by using $ Register File src1 addr src2 addr dst addr write data 32 bits src1 data src2 data 32 locations
0$zero constant 0 (Hdware) 1$atreserved for assembler 2$v0expression evaluation & 3$v1function results 4$a0arguments 5$a1 6$a2 7$a3 8$t0temporary: caller saves...(callee can clobber) 15$t7 Naming Conventions for Registers 16$s0callee saves... (caller can clobber) 23$s7 24$t8 temporary (cont’d) 25$t9 26$k0reserved for OS kernel 27$k1 28$gppointer to global area 29$spstack pointer 30$fpframe pointer 31$rareturn address (Hdware)
Registers vs. Memory Arithmetic instructions operands must be registers, — only thirty-two registers provided Compiler associates variables with registers What about programs with lots of variables? Processor Control Datapath Memory Devices Input Output
Registers vs. Memory Arithmetic instructions operands must be registers, — only thirty-two registers provided Compiler associates variables with registers What about programs with lots of variables? Processor Control Datapath Memory Devices Input Output
Accessing Memory MIPS has two basic data transfer instructions for accessing memory lw$t0, 4($s3) #load word from memory sw$t0, 8($s3) #store word to memory (assume $s3 holds ) The data transfer instruction must specify where in memory to read from (load) or write to (store) – memory address where in the register file to write to (load) or read from (store) – register destination (source) The memory address is formed by summing the constant portion of the instruction and the contents of the second register
Memory is viewed as a large, single-dimension array (usually of bytes), with addresses starting at 0 to 2 32 – 1. A memory address is an index into the array Processor – Memory Interconnections Processor Memory 32 bits ? locations read addr/ write addr read data write data
Memory is viewed as a large, single-dimension array, with an address A memory address is an index into the array Processor – Memory Interconnections Processor Memory 32 bits ? locations read addr/ write addr read data write data 2 30 words
MIPS Data Types Bit: 0, 1 Bit String: sequence of bits of a particular length 4 bits is a nibble 8 bits is a byte 16 bits is a half-word 32 bits (4 bytes) is a word 64 bits is a double-word Character: ASCII 7 bit code Decimal: digits 0-9 encoded as 0000b thru 1001b two decimal digits packed per 8 bit byte Integers: 2's complement Floating Point
Byte Addresses Since 8-bit bytes are so useful, most architectures address individual bytes in memory Therefore, the memory address of a word must be a multiple of 4 (alignment restriction) Big Endian: leftmost (MSB) byte is word address IBM 360/370, Motorola 68k, MIPS, Sparc, HP PA Little Endian:rightmost (LSB) byte is word address Intel 80x86, DEC Vax, DEC Alpha (Windows NT)
Addressing Objects: Endianess and Alignment Big Endian: leftmost byte is word address Little Endian: rightmost byte is word address msblsb little endian big endian Alignment restriction: requires that objects fall on address that is multiple of their size Aligned Not Aligned
Addressing Objects: Endianess and Alignment Big Endian: leftmost byte is word address Little Endian:rightmost byte is word address msblsb little endian byte big endian byte 0 Alignment restriction: requires that objects fall on address that is multiple of their size Aligned Not Aligned
MIPS Memory Addressing The memory address is formed by summing the constant portion of the instruction and the contents of the second (base) register lw$t0, 4($s3) #what? is loaded into $t0 sw$t0, 8($s3) #$t0 is stored where? Memory DataWord Address $s3 holds 8
MIPS Memory Addressing The memory address is formed by summing the constant portion of the instruction and the contents of the second (base) register lw$t0, 4($s3) #what? is loaded into $t0 sw$t0, 8($s3) #$t0 is stored where? Memory DataWord Address $s3 holds 8 in location
Compiling with Loads and Stores Assuming variable b is stored in $s2 and that the base address of array A is in $s3, what is the MIPS assembly code for the C statement A[8] = A[2] - b $s3 $s3 +4 $s3 +8 $s A[2] A[3]... A[1] A[0]
Compiling with Loads and Stores Assuming variable b is stored in $s2 and that the base address of array A is in $s3, what is the MIPS assembly code for the C statement A[8] = A[2] - b $s3 $s3 +4 $s3 +8 $s A[2] A[3]... A[1] A[0] lw$t0, 8($s3) sub$t0, $t0, $s2 sw$t0, 32($s3)
Compiling with a Variable Array Index Assuming A is an array of 50 elements whose base is in register $s4, and variables b, c, and i are in $s1, $s2, and $s3, respectively, what is the MIPS assembly code for the C statement c = A[i] - b add$t1, $s3, $s3#array index i is in $s3 add$t1, $t1, $t1#temp reg $t1 holds 4*i
Compiling with a Variable Array Index Assuming A is an array of 50 elements whose base is in register $s4, and variables b, c, and i are in $s1, $s2, and $s3, respectively, what is the MIPS assembly code for the C statement c = A[i] - b add$t1, $s3, $s3#array index i is in $s3 add$t1, $t1, $t1#temp reg $t1 holds 4*i add$t1, $t1, $s4#addr of A[i] lw$t0, 0($t1) sub$s2, $t0, $s1
MIPS Instructions, so far CategoryInstrOp CodeExampleMeaning Arithmetic (R format) add0 and 32add $s1, $s2, $s3$s1 = $s2 + $s3 subtract0 and 34sub $s1, $s2, $s3$s1 = $s2 - $s3 Data transfer (I format) load word35lw $s1, 100($s2)$s1 = Memory($s2+100) store word43sw $s1, 100($s2)Memory($s2+100) = $s1
Review: MIPS Organization Processor Memory 32 bits 2 30 words read/write addr read data write data word address (binary) 0…0000 0…0100 0…1000 0…1100 1…1100 Register File src1 addr src2 addr dst addr write data 32 bits src1 data src2 data 32 registers ($zero - $ra) ALU byte address (big Endian) Arithmetic instructions – to/from the register file Load/store instructions - to/from memory
0$zero constant 0 (Hdware) 1$atreserved for assembler 2$v0expression evaluation & 3$v1function results 4$a0arguments 5$a1 6$a2 7$a3 8$t0temporary: caller saves...(callee can clobber) 15$t7 Review: Naming Conventions for Registers 16$s0callee saves... (caller can clobber) 23$s7 24$t8 temporary (cont’d) 25$t9 26$k0reserved for OS kernel 27$k1 28$gppointer to global area 29$spstack pointer 30$fpframe pointer 31$rareturn address (Hdware)
Review: Unsigned Binary Representation HexBinaryDecimal 0x … x … x … x … x … x … x … x … x … x …10019 … 0xFFFFFFFC1…1100 0xFFFFFFFD1…1101 0xFFFFFFFE1…1110 0xFFFFFFFF1… bit bit position bit weight
Review: Signed Binary Representation 2’sc binarydecimal = -( ) = -2 3 = 1010 complement all the bits 1011 and add a 1