CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.

Slides:



Advertisements
Similar presentations
CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.
Advertisements

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.
Goal: Write Programs in Assembly
Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
Lecture 5: MIPS Instruction Set
CS/COE0447 Computer Organization & Assembly Language
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.
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.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 5 MIPS ISA & Assembly Language Programming.
ELEN 468 Advanced Logic Design
CS3350B Computer Architecture Winter 2015 Lecture 4
Chapter 2 Instructions: Language of the Computer
CMPT 334 Computer Organization Chapter 2 Instructions: Language of the Computer [Adapted from Computer Organization and Design 5 th Edition, Patterson.
CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.
CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.
331 Week 3. 1Spring 2005 Review: MIPS Organization Processor Memory 32 bits 2 30 words read/write addr read data write data word address (binary) 0…0000.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
331 W02.1Spring 05 Announcements  HW1 is due on this Friday  Appendix A (on CD) is very helpful to HW1.
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
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.
331 W02.1Fall 2003 Announcements  HW1 is due on this Friday (Sept 12 th )  Appendix A is very helpful to HW1. Check out system calls on Page A-48. 
331 Week 3. 1Fall 2003 Head’s Up  This week’s material l MIPS control flow operations (for, while, if-the-else, …) -Reading assignment - PH 3.5  Reminders.
331 Practice Exam.1Fall 2003 Naming Conventions for Registers 0$zero constant 0 (Hdware) 1$atreserved for assembler 2$v0expression evaluation & 3$v1function.
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
CSE331 W02.1Irwin Fall 2001 PSU Computer Architecture Discussion Lecture # 2 MIPS Programming.
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
1 Computer Architecture COSC 3430 Lecture 3: Instructions.
COSC 2021: Computer Organization Instructor: Dr. Amir Asif Department of Computer Science York University Handout # 3: MIPS Instruction Set I Topics: 1.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
CS Computer Architecture Spring 2006 Week 4 Paul Durand ( Course url:
CSIE30300 Computer Architecture Unit 02: MIPS ISA Review Hsin-Chou Chi [Adapted from material by and
6.S078 - Computer Architecture: A Constructive Approach Introduction to SMIPS Li-Shiuan Peh Computer Science & Artificial Intelligence Lab. Massachusetts.
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.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 7, 8 Instruction Set Architecture.
Lecture 2: Instruction Set Architecture part 1 (Introduction) Mehran Rezaei.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations.
MIPS Assembly.
CS 230: Computer Organization and Assembly Language
CS2100 Computer Organisation
Instructions: Language of the Computer
Lecture 4: MIPS Instruction Set
ELEN 468 Advanced Logic Design
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Computer Architecture (CS 207 D) Instruction Set Architecture ISA
CS170 Computer Organization and Architecture I
The University of Adelaide, School of Computer Science
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
CS/COE0447 Computer Organization & Assembly Language
The University of Adelaide, School of Computer Science
ECE232: Hardware Organization and Design
September 24 Test 1 review More programming
Chapter 2 Instructions: Language of the Computer
Lecture 5: Procedure Calls
The University of Adelaide, School of Computer Science
Computer Architecture
COMS 361 Computer Organization
UCSD ECE 111 Prof. Farinaz Koushanfar Fall 2018
COMS 361 Computer Organization
COMS 361 Computer Organization
Computer Architecture
CS352H Computer Systems Architecture
CS/COE0447 Computer Organization & Assembly Language
9/27: Lecture Topics Memory Data transfer instructions
Presentation transcript:

CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics Arizona State University Slides courtesy: Prof. Yann Hang Lee, ASU, Prof. Mary Jane Irwin, PSU, Ande Carle, UCB

CML CMLQuestion What is the difference between a calculator and a computer A desk calculator is a “fixed program computer”. –Cannot change the program of such a machine –You have to re-wire, re-structure, or even re-design the machine Computers are “stored program computers” –The program is a software –You can feed in new set of instructions and you have a completely different functionality BIG IDEA – Stored Program Concept Chief Instrument of change –Instruction Set Architecture

CML CML Stored Program Concept Everything is represented and stored as a “bit sequence” –The program, compiler, editor, Operating System, input interface, GUI, etc… Provides amazing degree of flexibility –Computers can be used to simulate “anything that is simulatable” –Compare this with a calculator

CML CMLAnnouncements Quiz 1 on Thursday, Sept 10, 2009 –Open Book, Open notes, open internet –Chapter 2, ( ) Arithmetic, Load Store and Branch instructions –Just no function calls Project 0 –Start exploring MARS MIPS simulator Install it on your machine Write some assembly language programs, e.g., add two numbers –DO NOT SUBMIT Project 1 –Will be online on Thursday –About writing some assembly language programs –Due in 1 week, end of Wednesday

CML CML What you need to know So far – –Should be able to write assembly programs for Arithmetic operations Given memory state, and some load store operations, you should be able to tell what is the final state of the memory Convert assembly instructions to binary forms –R and I format instructions Today – –Should be able to write assembly programs for Control flow - Branches, loops Except function calls

CML CML  High-level language program (in C) swap (int v[], int k)...  Assembly language program (for MIPS) swap: sll $2, $5, 2 add $2, $4, $2 lw $15, 0($2) lw $16, 4($2) sw $16, 0($2) sw $15, 4($2) jr $31  Machine (object) code (for MIPS) Below the Program C - Compiler Assembler

CML 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

CML CML MIPS R3000 ISA Instruction Categories –Arithmetic –Load/Store –Jump and Branch –Floating Point coprocessor –Memory Management –Special R0 - R31 PC HI LO OP rsrt rdsafunct rs rtimmediate jump target Registers R Format I Format 6 bits5 bits 6 bits 3 Instruction Formats: all 32 bits wide 6 bits5 bits 6 bits

CML CML 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 (Hardware) 0$zero constant 0 (Hardware) 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 Convention for Registers

CML CML 32 registers ($zero - $ra) MIPS Organization so far Arithmetic instructions – to/from the register file Load/store instructions - to/from memory read data src1 addr src2 addr dst addr write data 32 bits 32 Processor Memory 32 bits 2 30 words read/write addr write data word address (binary) 0…0000 0…0100 0…1000 0…1100 1…1100 Register File src1 data src2 data 32 ALU byte address (big Endian)

CML CML Which end do you break your egg? Gulliver’s Travel –Lilliput: Sharp-end(Little-endian) –Blefuscu: Rounded-end(Big-endian) Suppose, we have these two values in registers We store these values in memory $s1 = $s2 = lw $s1 4($t0) lw $s2 8($t0) $t0= 0x4080

CML CML What does the memory look like? 4/15/ Increasing Address $s1 = $s2 = a 408b Increasing Address a 408b $t0= 0x4080 lw $s1 4($t0) lw $s2 8($t0)

CML CML Memory Addressing What is the word at address 4080? 4/15/ Memory Address What is the byte at address 4086? Memory Address

CML CML Memory Addressing What is the word at address 4080? 4/15/ Little Endian Memory Address What is the byte at address 4086? Big Endian Memory Address Leftmost byte is word address e.g., Motorola 68k, MIPS, Sparc, HP PA Rightmost byte is word address e.g., Intel 80x86, DEC Vax, DEC Alpha

CML CML Endian-ness and Alignment Big Endian: leftmost byte is at word address Little Endian: rightmost byte is at word address 4/15/ 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

CML CML Loading and Storing Bytes MIPS provides special instructions to move bytes lb $t0, 1($s3) #load byte from memory sb $t0, 6($s3) #store byte to memory What 8 bits get loaded and stored? –load byte places the byte from memory in the rightmost 8 bits of the destination register what happens to the other bits in the register? –store byte takes the byte from the rightmost 8 bits of a register and writes it to a byte in memory 4/15/ op rs rt 16 bit number

CML CML Example of Loading and Storing Bytes Given following code sequence and memory state (contents are given in hexadecimal), what is the state of the memory after executing the code? add$s3, $zero, $zero lb$t0, 1($s3) sb$t0, 6($s3) 4/15/ Memory A 0 Data Word Address (Decimal) F F F F  What value is left in $t0?  What if the machine was little Endian? $t0 = 0x mem(4) = 0xFFFF90FF mem(4) = 0xFF12FFFF $t0 = 0x

CML CML Instructions for Making Decisions Decision making instructions –alter the control flow –i.e., change the "next" instruction to be executed MIPS conditional branch instructions: bne $s0, $s1, Label#go to Label if $s0  $s1 beq $s0, $s1, Label#go to Label if $s0=$s1 Example: if (i==j) h = i + j; bne $s0, $s1, Lab1 add $s3, $s0, $s1 Lab1:...

CML CML op rs rt 16 bit number Assembling Branches Instructions: bne $s0, $s1, Label#go to Label if $s0  $s1 beq $s0, $s1, Label#go to Label if $s0=$s1 Machine Formats: I format ???? ???? How is the branch destination address specified?

CML CML bne $s0,$s1,Lab1 add $s3,$s0,$s1 Specifying Branch Destinations Could use a register (like lw and sw) and add to it the 16-bit offset –which register? Instruction Address Register –PC = program counter its use is automatically implied by instruction PC gets updated (PC+4) during the fetch cycle so that it holds the address of the next instruction –limits the branch distance to to instructions from the (instruction after the) branch instruction, but most branches are local anyway (principle of locality)... Lab1: PC  Could specify the memory address –But that would require 32 bit field

CML CML Disassembling Branch Destinations The contents of the updated PC (PC+4) is added to the low order 16 bits of the branch instruction which is converted into a 32 bit value by –concatenated two low-order zeros to create an 18 bit number –sign-extending those 18 bits The result is written into the PC if the branch condition is true prior to the next Fetch cycle PC Add 32 offset sign-extend from the low order 16 bits of the branch instruction branch dst address ? Add 4 32

CML CML op rs rt 16 bit offset Assembling Branches Example Assembly code bne $s0, $s1, Lab1 add $s3, $s0, $s1 Lab1:... Machine Format of bne : I format 1 Remember –After the bne instruction is fetched, the PC is updated to address the add instruction (PC = PC + 4). –Two low-order zeros are concatenated to the offset number and that value sign-extended is added to the (updated) PC

CML CML Another Instruction for Changing Flow MIPS also has an unconditional branch instruction or jump instruction: j label#go to label Example: if (i!=j) h=i+j; else h=i-j; beq$s0, $s1, Lab1 add$s3, $s0, $s1 jLab2 Lab1:sub$s3, $s0, $s1 Lab2:...

CML CML op 26-bit address Assembling Jumps Instruction: j label#go to label Machine Format: How is the jump destination address specified? –As an absolute address formed by concatenating the upper 4 bits of the current PC (now PC+4) to the 26-bit address and concatenating 00 as the 2 low-order bits J format 2 ????

CML CML Disassembling Jump Destinations The low order 26 bits of the jump instruction is converted into a 32 bit jump instruction destination address by –concatenating two low-order zeros to create an 28 bit (word) address –concatenating the upper 4 bits of the current PC (now PC+4) to create a 32 bit instruction address that is placed into the PC prior to the next Fetch cycle PC from the low order 26 bits of the jump instruction

CML CML Assembling Branches and Jumps Assemble the MIPS machine code (in decimal is fine) for the following code sequence. Assume that the address of the beq instruction is 0x (hex address) beq$s0, $s1, Lab1 add$s3, $s0, $s1 jLab2 Lab1:sub$s3, $s0, $s1 Lab2:... 0x x x x c x jmp dst = (0x0) 0x (00 2 ) = 0x

CML CML Compiling While Loops Compile the assembly code for the C while loop where i is in $s0, j is in $s1, and k is in $s2 while (i!=k) i=i+j; Loop:beq$s0, $s2, Exit add$s0, $s0, $s1 jLoop Exit:...

CML CML op rs rt rd funct = 0x2a More Instructions for Making Decisions We have beq, bne, but what about branch-if-less-than? New instruction: slt $t0, $s0, $s1 # if $s0 < $s1 # then # $t0 = 1 # else # $t0 = 0 Machine format: 2

CML CML Other Branch Instructions Can use slt, beq, bne, and the fixed value of 0 in register $zero to create all relative conditions –less than blt $s1, $s2, Label –less than or equal to ble $s1, $s2, Label –greater than bgt $s1, $s2, Label –great than or equal to bge $s1, $s2, Label Example - slt $t0, $s1, $s2#$t0 set to 1 if bne $t0, $zero, Label# $s1 < $s2

CML CML op rs funct = 0x08 Another Instruction for Changing Flow Most higher level languages have case or switch statements allowing the code to select one of many alternatives depending on a single value. Instruction: jr $t1#go to address in $t1 Machine format: 2

CML CML Compiling a Case (Switch) Statement switch (k) { case 0: h=i+j; break; /*k=0*/ case 1: h=i+h; break; /*k=1*/ case 2: h=i-j; break; /*k=2*/ Assuming three sequential words in memory starting at the address in $t4 have the addresses of the labels L0, L1, and L2 and k is in $s2 add$t1, $s2, $s2#$t1 = 2*k add$t1, $t1, $t1#$t1 = 4*k add$t1, $t1, $t4#$t1 = addr of JT[k] lw$t0, 0($t1)#$t0 = JT[k] jr$t0#jump based on $t0 L0:add$s3, $s0, $s1#k=0 so h=i+j jExit L1:add$s3, $s0, $s3#k=1 so h=i+h jExit L2:sub$s3, $s0, $s1#k=2 so h=i-j Exit:... $t4  L0 L1 L2 Memory

CML 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 load byte32lb $s1, 101($s2)$s1 = Memory($s2+101) store byte40sb $s1, 101($s2)Memory($s2+101) = $s1 Cond. Branch br on equal4beq $s1, $s2, Lif ($s1==$s2) go to L br on not equal5bne $s1, $s2, Lif ($s1 !=$s2) go to L set on less than0 and 42slt $s1, $s2, $s3 if ($s2<$s3) $s1=1 else $s1=0 Uncond. Jump jump2j 2500go to jump register0 and 8jr $t1go to $t1

CML CML 32 read data MIPS Organization Processor Memory 32 bits 2 30 words read/write addr 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) 32 PC ALU byte address (big Endian) Fetch PC = PC+4 DecodeExec Add 4 br offset

CML CML MIPS R3000 ISA Instruction Categories –Arithmetic –Load/Store –Jump and Branch –Floating Point coprocessor –Memory Management –Special R0 - R31 PC HI LO OP rsrt rdsafunct rs rtimmediate 26-bit jump target Registers R Format I Format 6 bits5 bits 6 bits 3 Instruction Formats: all 32 bits wide 6 bits5 bits 16 bits J Format 6 bits26 bits

CML CML Yoda says… Luke: What's in there? Yoda: Only what you take with you