Computer Architecture CSCE 350 Rabi Mahapatra Spring 2015.

Slides:



Advertisements
Similar presentations
Goal: Write Programs in Assembly
Advertisements

4.
Branches Two branch instructions:
ISA Issues; Performance Considerations. Testing / System Verilog: ECE385.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
MIPS Assembly Language CPSC 321 Computer Architecture Andreas Klappenecker.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 5 MIPS ISA & Assembly Language Programming.
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
Computer Architecture CSCE 350
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 6 – Introduction to MIPS Data Transfer & Decisions I Pieter Abbeel’s recent.
COMP3221 lec08-arith.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 8: C/Assembler Data Processing
Chap.2: Instructions: Language of the computer Jen-Chang Liu, Spring 2006 Adapted from
CS 61C L06 MIPS Intro (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #6: Intro to MIPS
CS61C L09 Introduction to MIPS: Data Transfer & Decisions I (1) Garcia © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
Normal C Memory Management °A program’s address space contains 4 regions: stack: local variables, grows downward heap: space requested for pointers via.
CS 61C L09 Introduction to MIPS: Data Transfer & Decisions I (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Elec2041 lec-11-mem-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 11: Memory Access - I
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 I´ve been getting this a lot lately So, what are you teaching this term? Computer.
CPSC CPSC 3615 Computer Architecture Chapter 3 Instructions and Addressing (Text Book: Chapter 5)
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
RISC Concepts, MIPS ISA and the Mini–MIPS project
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
CS1104 Assembly Language: Part 1
CS 61C L08 Introduction to MIPS Assembly Language: Arithmetic (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Computer Architecture CPSC 321 Andreas Klappenecker.
From C to Assembly Language Jen-Chang Liu, Spring 2006 Adapted from
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
CS61C L09 Introduction to MIPS : Data Transfer and Decisions (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
On Textbook CD: SPIM Jen-Chang Liu, Simulation of a virtual machine Virtual machine (simulator) Differences between SPIM and real MIPS? No delayed.
MIPS Instruction Set Advantages
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Csci 136 Computer Architecture II – Summary of MIPS ISA Xiuzhen Cheng
Chapter 2 Decision-Making Instructions (Instructions: Language of the Computer Part V)
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 7, 8 Instruction Set Architecture.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations.
Computer Organization Instructions Language of The Computer (MIPS) 2.
Assembly Variables: Registers Unlike HLL like C or Java, assembly cannot use variables – Why not? Keep Hardware Simple Assembly Operands are registers.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Assembly Language Basic job of a CPU: execute lots of instructions. Instructions are the primitive operations that the CPU may execute. Different CPUs.
Instructor: Dr. Mike Turi Department of Computer Science and Computer Engineering Pacific Lutheran University Lecture slides adapted from Part 4, EE 334.
MIPS Instruction Set Advantages
CS2100 Computer Organisation
MIPS Coding Continued.
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Instructions - Type and Format
The University of Adelaide, School of Computer Science
ECE232: Hardware Organization and Design
C second, objective-c, Go up!
Part II Instruction-Set Architecture
Computer Instructions
3.
COMS 361 Computer Organization
Instructions in Machine Language
MIPS Coding Continued.
March 2006 Saeid Nooshabadi
MIPS Assembly Language Programming Computer Architecture
9/27: Lecture Topics Memory Data transfer instructions
Presentation transcript:

Computer Architecture CSCE 350 Rabi Mahapatra Spring 2015

A Translation Hierarchy High Level Language (HLL) programs first compiled (possibly into assembly), then linked and finally loaded into main memory. Assembler Assembly language program Compiler C program Linker Executable: Machine language program Loader Memory Object: Machine language moduleObject: Library routine (machine language)

MIPS R3000 Instruction Set Architecture ° Machine Environment Target ° Instruction Categories  Load/Store  Computational  Jump and Branch  Floating Point (coprocessor) OP Rs Rt Rdsafunct OP Rs Rt Immediate OP jump target 3 Instruction Formats: all 32 bits wide R0 - R31 PC HI LO Registers I: R: J:

Review C Operators/Operands Operators: +, -, *, /, % (mod); ( 7/4==1, 7%4==3) Operands: Variables: fahr, celsius Constants: 0, 1000, -17, 15.4 In C (and most High Level Languages) variables declared and given a type first  Example: int fahr, celsius; int a, b, c, d, e;

Assembly Operators ° Syntax of Assembly Operator 1) operation by name 2) operand getting resultRegister or Memory 3) 1st operand for operation 4) 2nd operand for operation ° Ex. add b to c and put the result in a: add a, b, c Called an Assembly Language Instruction ° Equivalent assignment statement in C: a = b + c;

Assembly Operators/Instructions ° MIPS Assembly Syntax is rigid: 1 operation, 3 variables Why? Keep Hardware simple via regularity ° How to do the following C statement? a = b + c + d - e; ° Break into multiple instructions add a, b, c # a = sum of b & c add a, a, d # a = sum of b,c,d sub a, a, e # a = b+c+d-e ° To right of sharp sign (#) is a comment terminated by end of the line. Applies only to current line. C comments have format /* comment */, can span many lines

Compilation ° How to turn the notation that programmers prefer into notation computer understands? ° Program to translate C statements into Assembly Language instructions; called a compiler ° Example: compile by hand this C code: a = b + c; d = a - e; ° Easy: add a, b, c sub d, a, e ° Big Idea: compiler translates notation from one level of computing abstraction to lower level

Compilation 2 ° Example: compile by hand this C code: f = (g + h) - (i + j); ° First sum of g and h. Where to put result? add f, g, h# f contains g+h ° Now sum of i and j. Where to put result? Cannot use f ! Compiler creates temporary variable to hold sum: t1 add t1, i, j # t1 contains i+j ° Finally produce difference sub f, f, t1 # f = (g+h)-(i+j)

Compilation -- Summary ° C statement (5 operands, 3 operators): f = (g + h) - (i + j); ° Becomes 3 assembly instructions (6 unique operands, 3 operators): add f,g,h# f contains g+h add t1,i,j# t1 contains i+j sub f,f,t1# f=(g+h)-(i+j) ° In general, each line of C produces many assembly instructions One reason why people program in C vs. Assembly; fewer lines of code Other reasons? (many!)

Assembly Design: Key Concepts Assembly language is essentially directly supported in hardware, therefore... It is kept very simple! Limit on the type of operands Limit on the set operations that can be done to absolute minimum. if an operation can be decomposed into a simpler operation, don’t include it.

Basic ISA Classes Accumulator (1 register): 1 address add A acc  acc + mem[A] 1+x address addx A acc  acc + mem[A + x] Stack: 0 address add tos  tos + next General Purpose Register (can be memory/memory): 2 address add A B EA[A]  EA[A] + EA[B] 3 address add A B C EA[A]  [B] + EA[C] Load/Store: 3 address add Ra Rb Rc Ra  Rb + Rc load Ra Rb Ra  mem[Rb] store Ra Rb mem[Rb]  Ra Comparison: Bytes per instruction? Number of Instructions? Cycles per instruction? Most real machines are hybrids of these:

Comparing Number of Instructions Code sequence for (C = A + B) for four classes of instruction sets: Stack Accumulator Register (load-store) Push ALoad ALoad R1,A Push BAdd BLoad R2,B AddStore C Register (register-memory) Load R1,A Add R1,B Store C, R1 Add R3,R1,R2 Pop CStore C,R3 MIPS

Assembly Variables: Registers (1/4) Unlike HLL, assembly cannot use variables Why not? Keep Hardware Simple Assembly Operands are registers limited number of special locations built directly into the hardware operations can only be performed on these! Benefit: Since registers are directly in hardware, they are very fast

Assembly Variables: Registers (2/4) Drawback: Since registers are in hardware, there are a predetermined number of them Solution: MIPS code must be very carefully put together to efficiently use registers 32 registers in MIPS Why 32? Smaller is faster Each MIPS register is 32 bits wide Groups of 32 bits called a word in MIPS

Assembly Variables: Registers (3/4) Registers are numbered from 0 to 31 Each register can be referred to by number or name Number references: $0, $1, $2, … $30, $31

Assembly Variables: Registers (4/4) By convention, each register also has a name to make it easier to code For now: $16 - $22 $s0 - $s7 (correspond to C variables) $8 - $15 $t0 - $t7 (correspond to temporary variables) In general, use register names to make your code more readable

Comments in Assembly Another way to make your code more readable: comments! Hash (#) is used for MIPS comments anything from hash mark to end of line is a comment and will be ignored Note: Different from C. C comments have format /* comment */, so they can span many lines

Assembly Instructions In assembly language, each statement (called an Instruction), executes exactly one of a short list of simple commands Unlike in C (and most other High Level Languages), where each line could represent multiple operations

Addition and Subtraction (1/4) Syntax of Instructions: 1 2, 3, 4 where: 1) operation by name 2) operand getting result (destination) 3) 1st operand for operation (source1) 4) 2nd operand for operation (source2) Syntax is rigid: 1 operator, 3 operands Why? Keep Hardware simple via regularity

Addition and Subtraction (2/4) Addition in Assembly Example (in MIPS):add $s0, $s1, $s2 Equivalent to (in C):a = b + c where registers $s0, $s1, $s2 are associated with variables a, b, c Subtraction in Assembly Example (in MIPS):sub $s3, $s4, $s5 Equivalent to (in C):d = e - f where registers $s3, $s4, $s5 are associated with variables d, e, f

Addition and Subtraction (3/4) How to do the following C statement? a = b + c + d - e; Break it into multiple instructions: add $s0, $s1, $s2 # a = b + c add $s0, $s0, $s3 # a = a + d sub $s0, $s0, $s4 # a = a - e

Immediates Immediates are numerical constants. They appear often in code, so there are special instructions for them. ''Add Immediate'': addi $s0, $s1, 10 (in MIPS) f = g + 10 (in C) where registers $s0, $s1 are associated with variables f, g Syntax similar to add instruction, except that last argument is a number instead of a register.

Register Zero One particular immediate, the number zero (0), appears very often in code. So we define register zero ($0 or $zero) to always have the value 0. This is defined in hardware, so an instruction like addi $0, $0, 5 will not do anything. Use this register, it’s very handy!

Assembly Operands: Memory C variables map onto registers; what about large data structures like arrays? 1 of 5 components of a computer: memory contains such data structures But MIPS arithmetic instructions only operate on registers, never directly on memory. ° Data transfer instructions transfer data between registers and memory: Memory to register Register to memory

MIPS Addressing Formats (Summary)

Data Transfer: Memory to Reg (1/4) To transfer a word of data, we need to specify two things: Register: specify this by number (0 - 31) Memory address: more difficult - Think of memory as a single one-dimensional array, so we can address it simply by supplying a pointer to a memory address. - Other times, we want to be able to offset from this pointer.

Data Transfer: Memory to Reg (2/4) To specify a memory address to copy from, specify two things: A register which contains a pointer to memory A numerical offset (in bytes) The desired memory address is the sum of these two values. Example:8($t0) specifies the memory address pointed to by the value in $t0, plus 8 bytes

Data Transfer: Memory to Reg (3/4) Load Instruction Syntax: 1 2, 3(4) where 1) operation (instruction) name 2) register that will receive value 3) numerical offset in bytes 4) register containing pointer to memory Instruction Name: lw (meaning Load Word, so 32 bits or one word are loaded at a time)

Data Transfer: Memory to Reg (4/4) Example:lw $t0, 12($s0) This instruction will take the pointer in $s0, add 12 bytes to it, and then load the value from the memory pointed to by this calculated sum into register $t0 Notes: $s0 is called the base register 12 is called the offset offset is generally used in accessing elements of array or structure: base register points to beginning of array or structure

Data Transfer: Reg to Memory Also want to store value from a register into memory Store instruction syntax is identical to Load instruction syntax Instruction Name: sw (meaning Store Word, so 32 bits or one word are loaded at a time) Example:sw $t0, 12($s0) This instruction will take the pointer in $s0, add 12 bytes to it, and then store the value from register $t0 into the memory address pointed to by the calculated sum

MIPS Assembly Instructions add $t0, $t1, $t2 # $t0=$t1+$t2 sub $t0, $t1, $t2 # $t0=$t1-$t2 la $t1, a_addr # $t1=Mem[a_addr] sa $s1, a_addr# Mem[a_addr]=$t1

Assembler directives.text assembly instructions follow.datadata follows.globlglobally visible label = symbolic address

Hello World!.text# code section.globl main main:li $v0, 4# system call for print string la $a0, str# load address of string to print syscall# print the string li $v0, 10# system call for exit syscall# exit.data str:.asciiz “Hello world!\n” # NUL terminated string, as in C

Addressing modes La $s1, addr # load $s1 from addr lw $s1, 8($s0)# $s1 = Mem[$s0+8] register $s0 contains the base address access the address ($s0) possibly add an offset 8($s0)

Load and move instructions la $a0, addr# load address addr into $a0 li $a0, 12# load immediate $a0 = 12 lb $a0, c($s1)# load byte $a0 = Mem[$s1+c] lh $a0, c($s1) # load half word lw $a0, c($s1)# load word move $s0, $s1# $s0 = $s1

Control Structures Assembly language has very few control structures:  Branch instructions if cond then goto label  Jump instructions goto label We can build while loops, for loops, repeat-until loops, if-then-else structures from these primitives

Branch instructions beqz $s0, labelif $s0==0goto label bnez $s0, labelif $s0!=0 goto label bge $s0, $s1, labelif $s0>=$s1 goto label ble $s0, $s1, label if $s0<=$s1 goto label blt $s0, $s1, label if $s0<$s1 goto label beq $s0, $s1, label if $s0==$s1 goto label bgez $s0, label if $s0>=0 goto label

if-then-else structures if ($t0==$t1) then /* blockA */ else /* blockB */ beq $t0, $t1, blockA j blockB blockA: … instructions of then block … j exit blockB: … instructions of else block … exit:… subsequent instructions …

repeat-until loop repeat … until $t0>$t1 loop: … instructions of loop … ble $t0, $t1, loop # if $t0<=$t1 goto loop Other loop structures are similar… Exercise: Derive templates for various loop structures

System calls load argument registers load call code syscall li $a0, 10# load argument $a0=10 li $v0, 1# call code to print integer syscall# print $a0

SPIM system calls procedure code $v0 argument print int 1 $a0 contains number print float 2 $f12 contains number print double 3 $f12 contains number print string 4 $a0 address of string

SPIM system calls procedure code $v0 result read int 5res returned in $v0 read float 6res returned in $f0 read double 7res returned in $f0 read string 8

I am the beginning of the end, and the end of time and space. I am essential to creation, and I surround every place. What am I? You can see me twice in a decade but once in a year. Not in day but once in june and twice in a week.

Example programs Loop printing integers 1 to 10 Increasing array elements by for(i=0; i<len; i++) { a[i] = a[i] + 5; }

main:li $s0, 1# $s0 = loop counter li $s1, 10# $s1 = upper bound of loop loop:move $a0, $s0# print loop counter $s0 li $v0, 1 syscall li $v0, 4# print “\n” la $a0, linebrk# linebrk:.asciiz “\n” syscall addi $s0, $s0, 1# increase counter by 1 ble $s0, $s1, loop# if ($s0<=$s1) goto loop li $v0, 10# exit syscall Print numbers 1 to 10

Increase array elements by 5.text.globl main main: la $t0, Aaddr # $t0 = pointer to array A lw $t1, len # $t1 = length (of array A) sll $t1, $t1, 2 # $t1 = 4*length add $t1, $t1, $t0 # $t1 = address(A)+4*length loop: lw $t2, 0($t0) # $t2 = A[i] addi $t2, $t2, 5 # $t2 = $t2 + 5 sw $t2, 0($t0) # A[i] = $t2 addi $t0, $t0, 4 # i = i+1 bne $t0, $t1, loop # if $t0<$t1 goto loop.data Aaddr:.word 0,2,1,4,5 # array with 5 elements len:.word 5

Increase array elements by 5.text.globl main main:la $t0, Aaddr# $t0 = pointer to array A lw $t1, len# $t1 = length (of array A) sll $t1, $t1, 2# $t1 = 4*length (byte addr.) add $t1, $t1, $t0# $t1 = beyond last elem. A

Increase array elements by 5 Loop:lw $t2, ($t0)# $t2 = A[i] addi $t2, $t2, 5# $t2 = $t2 + 5 sw $t2, ($t0)# A[i] = $t2 addi $t0, $t0, 4# i = i+1 bne $t0, $t1, loop# if $t0<$t1 goto loop li $v0, 10# exit syscall

Increase array elements by 5.data Aaddr:.word 0,2,1,4,5 len:.word 5 Idiosyncratic: Byte addressing => loop in steps of 4 Describe meaning of registers in your documentation!

Conclusion Read Chapter 2 in Patterson, Hennessy, Will have a quiz next class from these set of slides!