ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.

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

The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 8 Sept 23 Completion of Ch 2 translating procedure into MIPS role of compilers, assemblers, linking, loading etc. pitfalls, conclusions Chapter.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
The University of Adelaide, School of Computer Science
Computer Architecture CSCE 350
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
CSE331 W04.1Irwin&Li 2006 PSU CSE 331 Computer Organization and Design Fall 2006 Week 4 Section 1: Mary Jane Irwin (
The University of Adelaide, School of Computer Science
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
CS3350B Computer Architecture Winter 2015 Lecture 4
Procedure call frame: Hold values passed to a procedure as arguments
Lecture 8: MIPS Instruction Set
ECE 232 L7.Simul.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 7 MIPS Assembly.
CS 536 Spring Code generation I Lecture 20.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
RISC Concepts, MIPS ISA and the Mini–MIPS project
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
ECE 232 L5 Assembl.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 5 MIPS Assembly.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Lecture 4 section 2.4-?? As opposed to CISC – Complicated Instruction Set Architecture (ala the x86)
CDA 3101 Fall 2012 Introduction to Computer Organization Instruction Set Architecture MIPS Instruction Format 04 Sept 2013.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Character Data and 32-bit Constants (Lecture #20) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Lecture 4: MIPS Instruction Set
MicroComputer Engineering IntroLab1 page 1 Introduction Lab1  A crash course in assembler programming  Learn how a processor works!  Decode a coded.
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
© G. Drew Kessler and William M. Pottenger1 Subroutines, Part 2 CSE 262, Spring 2003.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
Lecture 5: Procedure Calls
Computer Architecture & Operations I
Lecture 6: Assembly Programs
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
Instructions - Type and Format
MIPS Instructions.
ECE232: Hardware Organization and Design
The University of Adelaide, School of Computer Science
Instruction encoding The ISA defines Format = Encoding
Lecture 5: Procedure Calls
Computer Instructions
Program and memory layout
Lecture 6: Assembly Programs
Systems Architecture I
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly Instructions, cont’d Maciej Ciesielski

ECE 232 L6.Assemb.2 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Administrative issues °Homework #2 assigned today, due Th., Feb. 21 in class. °Excuses that cannot be tolerated Forgot to buy the textbook Forgot to do the homework Dog ate the homework, etc … (you are pretty creative at those) °Solving the textbook problem Putting one copy of text on reserve in Physical Science Library °Change in late homework policy 25% per day – to allow to post solutions in 3-4 days °Midterm exam I – Th., March 7, 6:30 pm Morrill Science Center (Bldg II) room 131 No excuses (forgot, overslept, overate, etc) Only legitimate reasons (in writing) can be accepted

ECE 232 L6.Assemb.3 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Outline °Review: MIPS instructions, formats MIPS instructions: data transfers, arithmetic, logical Pseudo-instruction example: loading large constant MIPS register organization °Implementing loops °Implementing switch/case statement °Procedures and subroutines Stacks and pointers °Running a program Compiler, Assembler, Linker, Loader

ECE 232 L6.Assemb.4 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers MIPS Addressing Modes/Instruction Formats immedoprsrt Immediate All instructions are 32-bit wide Base+index immedoprsrt register + Memory PC-relative immedoprsrt PC Memory + register Register (direct ) oprsrtrdfnc...

ECE 232 L6.Assemb.5 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers R0$zero constant 0 R1$at reserved for assembler R2$v0 value registers & R3$v1 function results R4$a0 arguments R5$a1 R6$a2 R7$a3 R8$t0 temporary: caller saves... (callee can clobber) R15 $t7 MIPS: Software conventions for registers R16 $s0callee saves... (caller can clobber) R23 $s7 R24 $t8 temporary (cont’d) R25 $t9 R26 $k0 reserved for OS kernel R27 $k1 R28 $gp pointer to global area R29 $spStack pointer R30 $fpFrame pointer R31 $ra return Address

ECE 232 L6.Assemb.6 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers MIPS data transfer instructions Instruction Comment sw 500($r4), $r3Store word sh 502($r2), $r3Store half sb 41($r3), $r2Store byte lw $r1, 30($r2)Load word lh $r1, 40($r3)Load halfword lhu $r1, 40($r3)Load halfword unsigned lb $r1, 40($r3)Load byte lbu $r1, 40($r3)Load byte unsigned lui $r1, 40Load Upper Immediate (16 bits shifted left by 16) 0000 … 0000 LUI $r5 $r5

ECE 232 L6.Assemb.7 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Loading large numbers °Pseudo-instruction li $t0, big: load 32-bit constant lui $t0, upper# $t0 [31:16]  upper ori $t0, $t0, lower# $t0  ($t0 Or [0ext.lower]) $to: upper lower upper lower Or 32-bit constant

ECE 232 L6.Assemb.8 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Loop with variable array index °Compile the following loop, with A[ ] = array with base address in $s5; variables g, h, i, j associated with registers $s1, $s2, $s3, $s4. Loop:g = g + A[i]; i = i + j; if (i  h) go to Loop; MIPS instructions: Loop:add $t1, $s3, $s3 # $t1  i+i = 2i add $t1, $t1, $t1 # $t1  2i+2i = 4i add $t1, $t1, $s5 # $t1  address of A[i] lw $t0, 0 ($t1) # $t0  A[i] add $s1, $s1, $t0 # $s1  g + A[i] add $s3, $s3, $s4 # $s3  i + j bne $s3, $s2, Loop # if (i  h) go to Loop

ECE 232 L6.Assemb.9 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers While loop °Base address of A[i] is in $s6; variables i, j, k are in $s3, $s4, $s5. °Compile the following while loop while (A[i] == k) i = i + j; into MIPS instructions: Loop:add $t1, $s3, $s3 # $t1  i+i = 2i add $t1, $t1, $t1 # $t1  2i+2i = 4i add $t1, $t1, $s6 # $t1  address of A[i] lw $t0, 0 ($t1) # $t0  A[i] bne $t0, $s5, Exit # if (A[I]  k) go to Exit add $s3, $s3, $s4 # $s3  i + j j Loop # go to Loop Exit:

ECE 232 L6.Assemb.10 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Switch/case statement °Variables f - k are in $s0 - $s5. Register $t2 contains constant 4. °Compile the following switch statement into MIPS instructions switch (k) { case 0: f = i + j; break; /* k=0 */ case 1: f = g + h; break; /* k=1 */ case 2: f = g - h; break; /* k=2 */ case 3: f = i - j; break; /* k=3 */ } °Use the switch variable k to index the jump address table. First, test for k, if in correct range (0-3). slt $t3, $s5, $zero # test if k, 0 bne $t3, $zero, Exit # if k < 0, go to Exit slt $t3, $s5, $t2 # test if k < 4 beq $t3, $zero, Exit # if k  4, go to Exit

ECE 232 L6.Assemb.11 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Switch statement, cont’d °Access jump table T [ ] with addresses L0,L1,L2,L3: add $t1, $s5, $s5 # $t1  2k add $t1, $t1, $t1 # $t1  4k add $t1, $t1, $t4 # $t1  address of T [k] lw $t0, 0 ($t1) # $t0  T [k] °Use jump register instruction to jump via $t1 to the right address jr $to # jump based on register $t0 L0:add $s0, $s3, $s4 # k = 0, so f=$s0  i + j j Exit # go to Exit L1:add $s0, $s1, $s2 # k = 1, so f=$s0  g + h j Exit # go to Exit L2:sub $s0, $s1, $s2 # k = 2, so f=$s0  g + h j Exit # go to Exit L3:sub $s0, $s3, $s4 # k = 3, so f=$s0  i - j Exit:

ECE 232 L6.Assemb.12 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Stacks in Procedure Calls Stacking of Subroutine Calls and Returns: A: CALL B CALL C C: RET B: A AB ABC AB A Some machines provide a memory stack as part of the architecture (e.g., VAX) Sometimes stacks are implemented via software convention (e.g., MIPS)

ECE 232 L6.Assemb.13 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Memory Stacks Useful for stacked environments/subroutine call and return Stacks that Grow Up vs. Stacks that Grow Down: a b c 0 Little inf. Big 0 Little inf. Big Memory Addresses SP Next Empty? Last Full? How is empty stack represented? Little --> Big/Last Full POP: Read from Mem(SP) Decrement SP PUSH: Increment SP Write to Mem(SP) grows up grows down Little --> Big/Next Empty POP: Decrement SP Read from Mem(SP) PUSH: Write to Mem(SP) Increment SP

ECE 232 L6.Assemb.14 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Call-Return Linkage: Stack Frames Reference args and local variables at fixed (positive) offset from FP Grows and shrinks during expression evaluation FP Arguments Callee save registers Local variables SP (old FP, RA) °Many variations on stacks possible (up/down, last pushed / next ) °Block structured languages contain link to lexically enclosing frame °Compilers normally keep scalar variables in registers, not memory! High Mem Low Mem

ECE 232 L6.Assemb.15 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Compiling a leaf procedure (not nested) °int leaf_example (int g, int h, int i, int j) {int f; f = (g + h) – (i + j); return f; } °Let parameter variables g, h, i, j, correspond to the argument registers $a0, $a1, $a2, $a3. Will use temp. registers $t0= (g + h) and $t1=(i + j). Function f will be stored in $s0. °Steps: °Save the old values of registers ($s0, $t0, $t1) on stack (push) °Issue a jal sub_address instruction ($ra  ret_addr, j sub_address) °Perform the computation for $t0, $t1, $s0 using argument registers °Copy the value of f into a return value register $v0 °Restore the old values of the saved registers from stack (pop) °Finally, jump back to the calling routine, jr $ra (PC  return_address=PC+4)

ECE 232 L6.Assemb.16 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Compiling a leaf procedure, cont’d Leaf_example:# label of the procedure Save the old values of registers ($s0, $t0, $t1) on stack (push) sub $sp, $sp, 12# adjust stack to make room for 3 items sw $t1, 8 ($sp)# save reg $t1 on stack …….# repeat for $t0, $s0 Perform the computation for $t0, $t1, $s0 using argument registers add $t0, $a0, $a1# $t0  g + h add $t1, $a2, $a3# $t1  i + j sub $s0, $t0, $t1# $s0  (g + h) – (i + j) Copy the value of f into a return value register $v0 add $v0, $s0, $zero# returns f ($v0  $s0 + 0) Restore the old values of the saved registers from stack (pop ) lw $s0, 0 ($sp)# restore reg. $s0 for the caller ……. # repeat for $t0, $t1 … add $sp, $sp, 12# adjust the stack to delete 3 items Finally, jump back to the calling routine (PC  return address) jr $ra# PC  $ra