1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 1 In-Class Lab Session (Lab 2)

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

Goal: Write Programs in Assembly
Lecture 5: MIPS Instruction Set
CS/COE0447 Computer Organization & Assembly Language
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 5 MIPS ISA & Assembly Language Programming.
Chapter 2 Instructions: Language of the Computer
CS 61C L13Introduction to MIPS: Instruction Representation I (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Chapter 2 Instructions: Language of the Computer Part III.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 08 MIPS Instruction Representation I Lecturer SOE Dan Garcia
CS61C L10 : MIPS Instruction Representation II (1) Pearce, Summer 2010 © UCB inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 10 MIPS.
Assembly Language II CPSC 321 Andreas Klappenecker.
CS 61C L09 Instruction Format (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #9: MIPS Instruction.
CS61C L13 MIPS Instruction Representation I (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.
Instruction Representation II (1) Fall 2007 Lecture 10: Instruction Representation II.
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
CS61C L13 Introduction to MIPS: Instruction Representation I (1) Garcia © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
1 Lecture 2: MIPS Instruction Set Today’s topic:  MIPS instructions Reminder: sign up for the mailing list cs3810 Reminder: set up your CADE accounts.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
Instruction Representation II (1) Fall 2005 Lecture 10: Instruction Representation II.
Instruction Representation I (1) Fall 2005 Lecture 05: INSTRUCTION REPRESENTATION.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 13 MIPS Instruction Representation I The National Science Foundation (NSF)
CS 300 – Lecture 6 Intro to Computer Architecture / Assembly Language Instructions.
ISA-2 CSCE430/830 MIPS: Case Study of Instruction Set Architecture CSCE430/830 Computer Architecture Instructor: Hong Jiang Courtesy of Prof. Yifeng Zhu.
Foundation of Systems Xiang Lian The University of Texas-Pan American.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 3.
Lecture Objectives: 1)Define the terms least significant bit and most significant bit. 2)Explain how unsigned integer numbers are represented in memory.
Fall EE 333 Lillevik 333f06-l5 University of Portland School of Engineering Computer Organization Lecture 5 MIPS Instructions Loops Machine Instructions.
Lecture 4: MIPS Instruction Set
Computer Organization and Architecture Instructions: Language of the Machine Hennessy Patterson 2/E chapter 3. Notes are available with photocopier 24.
IFT 201: Unit 1 Lecture 1.3: Processor Architecture-3
MIPS coding. Review Shifting – Shift Left Logical (sll) – Shift Right Logical (srl) – Moves all of the bits to the left/right and fills in gap with 0’s.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 2.
9/29: Lecture Topics Conditional branch instructions
CENG 311 Instruction Representation
EET 4250 Instruction Representation & Formats Acknowledgements: Some slides and lecture notes for this course adapted from Prof. Mary Jane Penn.
Chapter 2 — Instructions: Language of the Computer — 1 Memory Operands Main memory used for composite data – Arrays, structures, dynamic data To apply.
Computer Organization Instructions Language of The Computer (MIPS) 2.
CS Computer Organization Numbers and Instructions Dr. Stephen P. Carl.
10/1: Lecture Topics Conditional branch instructions –slides from 9/29 set Unconditional jump instructions Instruction encoding.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Yaohang Li.
CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 1 In-Class Lab Session (Lab 2) Answer Key.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Instructor: Dr. Mike Turi Department of Computer Science and Computer Engineering Pacific Lutheran University Lecture slides adapted from Part 8, EE 334.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 08 MIPS Instruction Representation I Guest Lecturer Alan Christopher
MIPS Instruction Set Advantages
COMPUTER ARCHITECTURE & OPERATIONS I
Morgan Kaufmann Publishers
MIPS Coding Continued.
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
CS170 Computer Organization and Architecture I
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
CS/COE0447 Computer Organization & Assembly Language
Computer Architecture & Operations I
MIPS Instruction Encoding
The University of Adelaide, School of Computer Science
ECE232: Hardware Organization and Design
MIPS Instruction Encoding
Instruction encoding The ISA defines Format = Encoding
COSC 2021: Computer Organization Instructor: Dr. Amir Asif
Instruction encoding The ISA defines Format = Encoding
COMP541 Datapaths I Montek Singh Mar 18, 2010.
COMS 361 Computer Organization
Instruction encoding The ISA defines Format = Encoding
Instruction encoding The ISA defines Format = Encoding
MIPS Coding Continued.
CS/COE0447 Computer Organization & Assembly Language
Presentation transcript:

1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 1 In-Class Lab Session (Lab 2)

2 To get credit, attend class 1/13 and 1/20, and work with your partner to answer all the questions Don’t skim! Work through the examples, even if there is no question If a slide has questions to answer (or is labeled exercise), answer the question and then check your answers on the Answer Key. Before moving on, make sure you understand the answers. Ask if you have questions. For this lab, there is nothing to hand in.

3 Topics MIPS operations and operands MIPS registers Instruction encoding Understanding the technical documentation for instructions (green card) Arithmetic operations Logic operations Memory transfer operations Labels Example MIPS program

4 MIPS Operations/Operands “Operation” (instruction) – Produces a value from one or more input values “Operand” -Input or Output values for an operation MIPS operations –Arithmetic operations (integer/floating-point) (add, sub,…) –Logical operations (and, or,…) –Shift operations (shift a certain number of bits to the left or right) –Compare operations (do something if one operand is less than another,…) –Load/stores to transfer data from/to memory –Branch/jump operations –System control operations/coprocessor operations Question: Check on the green card. What is the mnemonic for a shift logical left instruction? ________

5 MIPS Operations/Operands MIPS operands –General-purpose registers: Question: name 2 of these____________ –Fixed registers, e.g., HI/LO registers –Memory location –Immediate value –Question: we saw this instruction in lecture: addi $t3,$zero,4. –$t3 is the destination. –addi is the mnemonic for the instruction. –$zero is an operand, and 4 is an operand. –What type of operand is $zero? (pick from the above list):_______ What type of operand is 4?__________ What do you think “i” stands for in “addi”?_________

6 MIPS Arithmetic Arithmetic Type Instruction: – All arithmetic instructions have 3 operands –Operand order is fixed: destination first –32 registers (page 2 of green card) –Question: What number is $t3?_____ What number is $s3?_____ Examples –add $t0, $s0, $s2# $t0 = $s0 + $s2 –sub $s0, $t0, $t1# $s0 = $t0 – $t1 –Question: In the sub instruction, which register is rd?_____ Which register is rs?_______ Which register is rt?_____ rd rs rt Operands

7 General-Purpose Registers GPR: all can be used as operands in instructions Still, conventions and limitations exist to keep GPRs from being used arbitrarily –r0, termed $zero, always has a value “0” –r31, termed $ra (return address), is reserved for subroutine call/return –Etc. (we’ll see other conventions/limitations later) –Register usage and related software conventions are summarized in “application binary interface” (ABI), which is important when writing system software such as an assembler and a compiler Question: Check the green card: what is the calling convention for $t0-$t7?_________ Note that these conventions are part of the ABI mentioned above. What does ABI stand for?________________

8 Instruction Encoding Instructions are encoded in binary numbers –Assembler translates assembly programs into binary numbers –Machine decodes binary numbers to figure out what the instruction is, and then executes the instruction –MIPS has “fixed” 32-bit instruction encoding MIPS has several instruction formats –R-format: arithmetic instructions –I-format: transfer/branch/immediate format –J-format: jump instruction format –(FI/FR-format: floating-point instruction format)(later chapter)

9 MIPS Instruction Formats NameFieldsComments 6 bits5 bits 6 bitsAll MIPS instructions 32 bits R-formatoprsrtrdshamtfunctArithmetic/logic instruction format I-formatoprsrtaddress/immediateTransfer, branch, immediate format J-formatoptarget addressJump instruction format

10 R-Format Instructions Define “fields” of the following number of bits each: = opcodersrtrdfunctshamt For simplicity, each field has a name: So, the opcode is 6 bits, rs, rt, rd, shamt are each 5 bits, and funct is 6 bits. For shift instructions: “shift amount”. 0 if the instruction is not a shift.

11 Question: R-Format Example add $8,$9,$10 Translate the above instruction.Specifically: Look up “add” on green card. As you can see, “add” is R format. Look up the R format and the opcode/funct values for add. Then, fill in the tables and the underline below. Binary number per field representation (use the right # of bits!): Decimal number per field representation: hex representation: _________________________ (group the bits by 4 from the *right*)

12 M I P S Reference Data: CORE INSTRUCTION SET NAMEMNE- MON-IC FOR- MAT OPERATION (in Verilog) OPCODE/ FUNCT (hex) AddaddRR[rd] = R[rs] + R[rt] (1)0 / 20 hex Add ImmediateaddiIR[rt] = R[rs] + SignExtImm (1)(2) 8 hex Branch On Equal beqIif(R[rs]==R[rt]) PC=PC+4+ BranchAddr (4) 4 hex (1) May cause overflow exception (2) SignExtImm = { 16{immediate[15]}, immediate } (3) ZeroExtImm = { 16{1b’0}, immediate } (4) BranchAddr = { 14{immediate[15]}, immediate, 2’b0} Later  NOW  make sure you understand everything but the OPERATION Column.

13 R-Format Instructions (REMINDER) Define “fields” of the following number of bits each: = opcodersrtrdfunctshamt For simplicity, each field has a name:

14 I-Format Instructions Define “fields” of the following number of bits each: = opcodersrtimmediate For simplicity, each field has a name: So, the opcode is 6 bits, rs and rt are each 5 bits, and immediate is 16 bits Let’s do an example using addi

15 M I P S Reference Data: CORE INSTRUCTION SET NAMEMNE- MON-IC FOR- MAT OPERATION (in Verilog) OPCODE/ FUNCT (hex) AddaddRR[rd] = R[rs] + R[rt] (1)0 / 20 hex Add ImmediateaddiIR[rt] = R[rs] + SignExtImm (1)(2) 8 hex Branch On Equal beqIif(R[rs]==R[rt]) PC=PC+4+ BranchAddr (4) 4 hex (1) May cause overflow exception (2) SignExtImm = { 16{immediate[15]}, immediate } (3) ZeroExtImm = { 16{1b’0}, immediate } (4) BranchAddr = { 14{immediate[15]}, immediate, 2’b0} Look at addi – note that its opcode is 8. We’ll look at the operation column later. Later 

16 Question: I-Format Example MIPS Instruction: addi $8,$9,7 $8 is rt; $9 is rs. This instruction adds 7 to the contents of $9 and stores it in $8. Binary number per field representation: Decimal number per field representation: Hex representation:_________________________________ Translate the instruction above to fill in the following tables:

17 M I P S Reference Data: CORE INSTRUCTION SET NAMEMNE- MON-IC FOR- MAT OPERATION (in Verilog) OPCODE/ FUNCT (hex) AddaddRR[rd] = R[rs] + R[rt] (1)0 / 20 hex Add ImmediateaddiIR[rt] = R[rs] + SignExtImm (1)(2) 8 hex Branch On Equal beqIif(R[rs]==R[rt]) PC=PC+4+ BranchAddr (4) 4 hex (1) May cause overflow exception (2) SignExtImm = { 16{immediate[15]}, immediate } (3) ZeroExtImm = { 16{1b’0}, immediate } (4) BranchAddr = { 14{immediate[15]}, immediate, 2’b0} NOW

18 Verilog – language used to describe functions of hardware Example: (2) SignExtImm = { 16{immediate[15]}, immediate } Rules: - {x,y,z} means xyz (the three concatenated together) - {3{a}} means “a” repeated 3 times: aaa - Immediate[15] means bit 15 of the immediate field of the instruction Question: What does the Verilog statement labeled (2) above mean? What specific binary value is SignExtImm for the command addi $8,$9,7?

19 addi $8,$9,7 Executing the addi instruction $8 $90x Suppose  Immediate = 0x0007 (16 bits; 4 hex digits) SignExtImm = 0x (32 bits; 8 hex digits) 0x x x A

20 addi $8,$9,7 RECAP We saw that the machine code is 0x –See answer slide 5 –This is the numerical version of the instruction, which the assembler creates –‘immediate’ refers to the right-most 4 hex digits (16 bits) of the machine code  the 0x0007 [see previous slide] On the green card, the OPERATION column tells us what happens when this instruction is executed –The immediate value 0x0007 is sign-extended to 0x –Then 0x is added to the value stored in $9 and the result is placed in $8 We looked at the funny Verilog stuff so we can understand the technical documentation, e.g., (2) SignExtImm = {16{immediate[15]}, immediate}

21 Exercise Which instruction has same representation as 35 ten ? A. add $0, $0, $0 B. subu $s0,$s0,$s0 C. lw $0, 0($0) D. addi $0, $0, 35 E. subu $0, $0, $0 F. Trick question! Instructions are not numbers. Use Green Card to Answer.

22 Logic Instructions Bit-wise logic operations Examples –and $t0, $s0, $s2# $t0 = $s0 ^ $s2 –or $s0, $t0, $t1# $s0 = $t0 | $t1 –nor $s0, $t0, $t1# $s0 = ~($t0 | $t1) NameFieldsComments R-formatoprsrtrdshamtfunctLogic instruction format

23 Bitwise and, or A and B: 1 if both are 1; 0, otherwise X or Y: 0 if both are 0; 1, otherwise Let’s take bitwise AND of the following 8-bit binary #s: because both bits above are 1.

24 Bitwise and, or A and B: 1 if both are 1; 0, otherwise X or Y: 0 if both are 0; 1, otherwise Now, let’s take bitwise OR of the following 8-bit binary #s: because both bits above are 0.

25 Logic Instructions: Example addi $t0,$0,0x32 addi $t1,$0,0x777 and $t2,$t1,$t0 $t0 $t1 $t

26 Logic Instructions: Example addi $t0,$0,0x32 addi $t1,$0,0x777 or $t2,$t1,$t0 $t0 $t1 $t

27 Exercise What values are placed into $t0, $t1, $t5 and $t4 by the following pieces of code? addi $t0,$0,0xA23 addi $t1,$0,0x80001B42 and $t4,$t0,$t1 or $t5,$t0,$t1

28 Memory Transfer Instructions To load/store a word from/to memory: –LOAD: move data from memory to register lw $t3, 4($t2)# $t3  M[$t2 + 4] –STORE: move data from register to memory sw $t4, 16($t1)# M[$t1 + 16]  $t4 Suppose $t2 = 0x Suppose Memory[0x ] = 0x55 lw $t3,4($t2) loads 0x55 into $t3: 1.Adds 4 to contents of $t2 to get 0x Gets the contents of memory location 0x and puts it into $t3 –sw $t3,8($t2) stores 0x55 into 0x Adds 8 to contents of $t2 to get 0x Puts contents of $t3 (0x55) into location 0x

29 Loading addresses into registers la $t2,0x loads 0x into $t2 li $t3,55 loads 55 decimal into $t3 (so, 0x37) [We’ll see later that these actually require 2 instructions each. But the assembler takes care of this for you; you can use la and li in your programs]

30 Labels (note: # is for comments) The assembler makes it easy to give names to memory addresses. So, you don’t need to use actual numbers in your code..data #In the memory segment, starting with 0x word Label1: 0x55 Label2: 0x66 Label3: 16 0x is stored at the 4 bytes starting with 0x x is stored at the 4 bytes starting with 0x x is stored at the 4 bytes starting with 0x Label1 stands for 0x Label2 stands for 0x Label3 stands for 0x

31 Sample Program Now we can look at a sample program If there is time in class, we will go through it We’ll work with this and other code in recitation

32.data # sample0.asm.word c: 3 k: 5.text la $t0,c # address of c la $t1,k # address of k lw $s0,0($t0) # load the contents of c lw $s1,0($t1) # load the contents of k slt $s3,$s0,$s1 # if $s0 < $s1 then $s3 = 1; else $s3 = 0 beq $s3,$0,notless #if $s3 == 0: go to notless; o/w just go to the next instruction sw $s0,0($t1) #store contents of c into k sw $s1,0($t0) #store the contents of k into c notless: # the end of the code; we just stop here #So, what did we do? If c < k then we swapped their values. If not, we just left them alone.