More decisions and logic (1) Fall 2010 Lecture 4: Loads, Logic, Loops.

Slides:



Advertisements
Similar presentations
Lecturer PSOE Dan Garcia
Advertisements

CENG 311 Decisions in C/Assembly Language
Goal: Write Programs in Assembly
4.
Lecture 5: MIPS Instruction Set
CS/COE0447 Computer Organization & Assembly Language
Branches Two branch instructions:
Deeper Assembly: Addressing, Conditions, Branching, and Loops
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
CMPT 334 Computer Organization Chapter 2 Instructions: Language of the Computer [Adapted from Computer Organization and Design 5 th Edition, Patterson.
cs61c L5 Decisions 9/13/00 1 CS61C - Machine Structures Lecture 5 - Decisions in C/Assembly Language September 13, 2000 David Patterson
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 6 – Introduction to MIPS Data Transfer & Decisions I Pieter Abbeel’s recent.
Chap.2: Instructions: Language of the computer Jen-Chang Liu, Spring 2006 Adapted from
CS61C L09 Introduction to MIPS: Data Transfer & Decisions I (1) Garcia © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS61CL L03 MIPS I: Registers, Memory, Decisions (1) Huddleston, Summer 2009 © UCB Jeremy Huddleston inst.eecs.berkeley.edu/~cs61c CS61CL : Machine Structures.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 10 – Introduction to MIPS Decisions II In what may be regarded as the most.
CS61C L10 Introduction to MIPS : Decisions II (1) Garcia, Fall 2006 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS 61C L09 Introduction to MIPS: Data Transfer & Decisions I (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 10 Introduction to MIPS : Decisions II People are wondering what the next.
Lecture 4: Loads, Logic, Loops. Review Memory is byte-addressable, but lw and sw access one word at a time. These instructions transfer the contents of.
CS61C L7 MIPS Decisions (1) Garcia, Fall 2005 © UCB Lecturer PSOE, new dad Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS61C L10 Introduction to MIPS : Decisions II (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Decisions in C/Assembly Language Jen-Chang Liu, Spring 2006 Adapted from
CPSC CPSC 3615 Computer Architecture Chapter 3 Instructions and Addressing (Text Book: Chapter 5)
CS61C L7 MIPS: Load & Store, Decisions (1) Chae, Summer 2008 © UCB Albert Chae, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture.
CS61C L7 MIPS Decisions (1) Beamer, Summer 2007 © UCB Scott Beamer, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #7 – MIPS.
CS 61C L07 MIPS Memory (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #7: MIPS Memory & Decisions.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
EEM 486 EEM 486: Computer Architecture Lecture 2 MIPS Instruction Set Architecture.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 07 Introduction to MIPS : Decisions II Researchers at Microsoft and UW.
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
Lecture 5: Procedures. Function call book-keeping in C main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);... } /* really dumb mult function */
CS61C L09 Introduction to MIPS : Data Transfer and Decisions (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS 61C L10 Introduction to MIPS: Decisions II (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS 61C L10 Introduction to MIPS: Decisions II (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
Intel has developed a new technique to generate random numbers that is suitable for integration directly into the CPU! This circuit can turn out 2.4 billion.
CS 61C: Great Ideas in Computer Architecture Lecture 5: Intro to Assembly Language, MIPS Intro Instructor: Sagar Karandikar
CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro Instructors: Krste Asanovic & Vladimir Stojanovic
IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education.
Registers and MAL Lecture 12. The MAL Architecture MAL is a load/store architecture. MAL supports only those addressing modes supported by the MIPS RISC.
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
IT 251 Computer Organization and Architecture More MIPS Control Instructions Chia-Chi Teng.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
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.
Chapter 2 Decision-Making Instructions (Instructions: Language of the Computer Part V)
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 07 Introduction to MIPS : Decisions II Guest Lecturer Alan Christopher.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
CDA 3101 Spring 2016 Introduction to Computer Organization
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations.
1. CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions 2 Instructors: Vladimir Stojanovic & Nicholas Weaver
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.
MIPS: Load & Store, Decisions. Memory management review Three 3’s Picking blocks off free list  best-, first-, next-fit Attempts to solve external fragmentation.
Decisions (cont), Procedures. Review Memory transfer: lw, sw, lb, sb A Decision allows us to decide what to execute at run-time rather than compile-time.
CS 102 Computer Architecture Lecture 4: Intro to Assembly Language, MIPS Intro Instructor: Sören Schwertfeger School of Information.
Rocky K. C. Chang Version 0.3, 14 September 2017
CS2100 Computer Organisation
ECE232: Hardware Organization and Design
The University of Adelaide, School of Computer Science
Part II Instruction-Set Architecture
3.
COMS 361 Computer Organization
MIPS Assembly.
MIPS assembly.
MIPS instructions.
Control Flow and Arrays
Presentation transcript:

More decisions and logic (1) Fall 2010 Lecture 4: Loads, Logic, Loops

More decisions and logic (2) Fall 2010 Review Memory is byte-addressable, but lw and sw access one word at a time. These instructions transfer the contents of memory to/from a register. lw $s3, X Load address instruction: la loads the memory address of X. A memory address is a pointer. -la $s2, X A pointer (used by lw and sw ) is just a memory address, so we can do this: (assume $s2 has address of X) Base/displacement: lw $s3, 32($s2) Indexing by adding: addi $s2,$s2,32 lw $s3,0($s2)

More decisions and logic (3) Fall 2010 Review A Decision allows us to decide what to execute at run-time rather than compile-time. C Decisions are made using conditional statements within if, while, do while, for. MIPS Decision-making instructions are the conditional branches: native MIPS instructions beq and bne pseudo instructions blt, ble, bgt, bge. MIPS unconditional branch: j (jump)

More decisions and logic (4) Fall 2010 Lecture 4: Load, Logic, Loops Loading bytes and halfwords A little more about arithmetic Two logical operations (shift left, shift right) Loops

More decisions and logic (5) Fall 2010 Loading, Storing bytes 1/2 In addition to word data transfers ( lw, sw ), MIPS has byte data transfers: load byte: lb store byte: sb same format as lw, sw lb moves one byte of data into a register which holds one word.

More decisions and logic (6) Fall 2010 x Loading, Storing bytes 2/2 What to do with the other 24 bits in the 32 bit register? lb : sign extends to fill upper 24 bits byte loaded …is copied to “sign-extend” This bit xxxx xxxx xxxx zzz zzzz Normally don't want to sign extend chars MIPS instruction that doesn’t sign extend when loading bytes: load byte unsigned: lbu

More decisions and logic (7) Fall 2010 Overflow in Arithmetic (1/2) Reminder: Overflow occurs when there is a mistake in arithmetic due to the limited precision in computers. Example (4-bit unsigned numbers): But we don’t have room for 5-bit solution, so the solution would be 0010, which is +2, and wrong.

More decisions and logic (8) Fall 2010 Overflow in Arithmetic (2/2) Some languages detect overflow (Ada), some don’t (C) MIPS provides 2 kinds of arithmetic instructions to recognize 2 choices: add ( add ), add immediate ( addi ) and subtract ( sub ) cause overflow to be detected add unsigned ( addu ), add immediate unsigned ( addiu ) and subtract unsigned ( subu ) do not cause overflow detection Compiler selects appropriate arithmetic MIPS C compilers produce addu, addiu, subu

More decisions and logic (9) Fall 2010 Two Logic Instructions Shift Left: sll $s1,$s2,2 #s1=s2<<2 Store in $s1 the value from $s2 shifted 2 bits to the left, inserting 0’s on right; << in C Before: hex two After: hex two What arithmetic effect does shift left have? Shift Right: srl is opposite shift; >>

More decisions and logic (10) Fall 2010 Loops in C/Assembly (1/3) Simple loop in C; A[] is an array of int s do{ g = g + A[i]; i = i + j; } while (i != h); Rewrite this as: Loop:g = g + A[i]; i = i + j; if (i != h) goto Loop; Use this mapping: g, h, i, j, base of A $s1, $s2, $s3, $s4, $s5

More decisions and logic (11) Fall 2010 Loops in C/Assembly (2/3) (This code uses a trick to multiply by 4 using logical shift. Just accept this trick for now.) Loop: sll $t1,$s3,2 #$t1= 4*I add $t1,$t1,$s5 #$t1=addr A[i] lw $t1,0($t1) #$t1=A[i] add $s1,$s1,$t1 #g=g+A[i] add $s3,$s3,$s4 #i=i+j bne $s3,$s2,Loop #goto Loop if # (i!=h) Original code: Loop:g = g + A[i]; i = i + j; if (i != h) goto Loop;

More decisions and logic (12) Fall 2010 A more efficient version lw $t1, $t2($t3) not allowed (sadly). The offset needs to be a constant. sll $t3,$s2,2 #$t3=4*h(Compare to i) sll $t2,$s4,2 #$t2=4*j (1 st i offset) sll $t1,$s3,2 #$t1=4*i (1 st j offset) add $t1,$t1,$s5 #$t1=addr of A[i] add $t3,$t3,$s5 #$t3=addr of A[h] L: lw $t7,0($t1) #$t7=A[i] add $s1,$s1,$t7 #g=g+A[i] add $t1,$t1,$t2 #i=i+j (address) bne $t3,$t1,L #goto Loop if i!=h Note the difference in the loop section: Original: 1 sll, 3 add, 1 lw, 1 bne Improved: 2 add, 1 lw, 1 bne

More decisions and logic (13) Fall 2010 Loops in C/Assembly (3/3) There are three types of loops in C: while do … while for Each can be rewritten as either of the other two, so the method used in the previous example can be applied to while and for loops as well. Key Concept: Though there are multiple ways of writing a loop in MIPS, the key to decision making is conditional branch

More decisions and logic (14) Fall 2010 MIPS Loop Examples These are excerpts of code from the examples posted on the class website. Example 1: a simple loop Example 2: print out Fibbonacci numbers Example 3: array indexing version 1 Example 4: array indexing version 2

More decisions and logic (15) Fall 2010 Loops Example 1: print integers 1 to 10 # c code would be: for (i=1; i<= 10; i++) printf(" %d",i) li $s0,1 # $s0 holds index of loop # print this element loop: move $a0,$s0 # load value to print with syscall li $v0,1 # load code for print integer syscall # print it # set up for next iteration addi $s0,$s0,1 # get next in list ble $s0,10,loop # finished whole list? # if not, go back around # done li $v0,10 syscall

More decisions and logic (16) Fall 2010 Loops Example 2: Print Fibbonaci #s # t1 is required number of iterations # t2 is number of iterations so far # s1 holds current Fibbonacci number # s2 holds next Fibbonacci number # v0 has the user’s input (how many Fibbonaccis to print out) # print desired number of Fibbonacci numbers # initialize loop move $t1,$v0 # save required number of iterations in t1 li $t2,0 # number of this iteration li $s1,1 # 1 st Fibonacci number li $s2,1 # 2 nd Fibonacci number

More decisions and logic (17) Fall 2010 Loops Example 2: (Fibbonaci - cont.) #Check for more to print? LOOP: bge $t2,$t1,DONE # not done, print next one (code for printing has been omitted) blah, blah, blah, … #increment for next iteration add $s0,$s1,$s2 #get next Fibbonacci number move $s1,$s2 #update s1 and s2 move $s2,$s0 addi $t2,$t2,1 #increment iteration count j LOOP DONE: # end program Modify it to fill up an array with Fibbonaci numbers.

More decisions and logic (18) Fall 2010 Loops Example 3: array indexing 1 # start with array values already there to shorten example List:.word 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 # initialize for loop la $s0, List # $s0 holds current address in array addi $s1, $s0, 36 # $s1 holds address of final word li $s2, 0 # initialize sum loop: bgt $s0, $s1, out # if summed entire array, done # else get current element lw $t0, 0($s0) # load element value add $s2, $s2, $t0 # add into sum # set up for next iteration add $s0, $s0, 4 # get address of next element j loop # next iteration out: # sum completed, print it (code omitted)

More decisions and logic (19) Fall 2010 Loops Example 4: array indexing 2 # start with array values already there to shorten example List:.word 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 # initialize for loop li $s0,0 # $s0 holds current offset in array li $s1,36 # $s1 holds last offset in array li $s2,0 # initialize sum loop: bgt $s0,$s1,out # summed entire array? # no, get this element lw $t0, List($s0) # load elem.value—note arg. order! add $s2,$s2,$t0 # add into sum # set up for next iteration add $s0,$s0,4 # get address of next element j loop # go back around # sum completed, print it out:

More decisions and logic (20) Fall 2010 Inequalities in MIPS (1/3) Pseudo MIPS inequality instructions: blt, bgt, ble, bge Native MIPS inequality instructions: “Set on Less Than” Syntax: slt reg1,reg2,reg3 Meaning: if (reg2 < reg3) reg1 = 1; else reg1 = 0; In computereeze, “set” means “set to 1”, “reset” means “set to 0”. reg1 = (reg2 < reg3); Same thing…

More decisions and logic (21) Fall 2010 Inequalities in MIPS (2/3) How do we use this? Compile by hand: if (g < h) goto Less; #g:$s0, h:$s1 Answer: compiled MIPS code… slt $t0,$s0,$s1 # $t0 = 1 if g<h bne $t0,$0,Less Branch if $t0!=0  ( g < h ) Register $0 always contains the value 0, so bne and beq often use it for comparison after an slt instruction. A slt  bne pair means if(… < …)goto…

More decisions and logic (22) Fall 2010 Inequalities in MIPS (3/3) Now, we can implement, ≤ and ≥ ? We could add 3 more instructions, but: MIPS goal: Simpler is Better Can we implement ≤ in one or more instructions using just slt and the branches? What about > ? What about ≥ ?

More decisions and logic (23) Fall 2010 Immediates in Inequalities There is also an immediate version of slt to test against constants: slti Helpful in for loops if (g >= 1) goto Loop Loop:... slti $t0,$s0,1 # $t0=1 if # $s0 =1)) C MIPSMIPS A slt  beq pair means if(… ≥ …)goto…

More decisions and logic (24) Fall 2010 “And in conclusion…” In order to help the conditional branches make decisions concerning inequalities, we introduce a single instruction: “Set on Less Than”called slt, slti One can store and load (signed and unsigned) bytes as well as words Unsigned add/sub don’t cause overflow New MIPS Instructions: sll, srl slt, slti addu, addiu, subu

More decisions and logic (25) Fall 2010 “And in conclusion…” You have all the basics to write loops and to manipulate arrays of data.