ECE 232 L7.Simul.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 7 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
Lecture 9: MIPS Instruction Set
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
5Z032 Processor Design SPIM, a MIPS simulator Henk Corporaal
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
10/6: Lecture Topics Procedure call Calling conventions The stack
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
SPIM and MIPS programming
Computer Architecture CSCE 350
MIPS Function Continued
MIPS Assembly Language Programming
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
1 Computer Architecture MIPS Simulator and Assembly language.
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
Procedure call frame: Hold values passed to a procedure as arguments
Lecture 8: MIPS Instruction Set
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Spr 2015, Feb 9... ELEC / Lecture 4 1 ELEC / Computer Architecture and Design Spring 2015 Compiling and Executing Programs.
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
OCC - CS/CIS CS116-Ch00-Orientation Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 1CS 116 Fall 2003 Not quite finished Creating.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
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
Orange Coast College Business Division Computer Science Department CS 116- Computer Architecture Instructions: Part the Last.
Lecture 161 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM.
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.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
Lecture 3 Translation.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
Lecture 5: Procedure Calls
Computer Architecture & Operations I
MIPS Assembly Language Programming
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
Instructions - Type and Format
The University of Adelaide, School of Computer Science
COMS 361 Computer Organization
Program and memory layout
Program and memory layout
Computer Architecture
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Week 5 Computers are like Old Testament gods; lots of rules and no mercy. Joseph Campbell.
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

ECE 232 L7.Simul.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 7 MIPS Assembly Language Simulation Maciej Ciesielski

ECE 232 L7.Simul.2 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Outline °Review Sequence: compiler, assembler, linker, loader Procedure calls -Use of stack (pop, push) -Parameter passing, function return °MIPS Assembler Directives, system calls (I/O) Practical examples - read Appendix A: sections A1, A2 °MIPS Simulator Read Appendix A: section A9 °The following manual is put on reserve in library: MIPS Assembly Language Programming, by Robert Britton Read Sections 2 and 4

ECE 232 L7.Simul.3 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Running a program CompilerAssemblerLinker Loader C program Memory Assembly language program Machine language moduleLibrary routines object Machine language program executable

ECE 232 L7.Simul.4 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Memory Layout 0x x x x xFFFFFFFF Reserved Text Segment Data Segment Stack Segment Operating System

ECE 232 L7.Simul.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 L7.Simul.6 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers MIPS Register File Register Naming Convention $0 : Constant Zero $v0, $v1 : Returned values from functions $a0 -$a3 : Arguments passed to functions $t0 - $t7 : Temporary registers (functions) $s0 -$s7 : Saved registers (main program) $sp : Stack Pointer $ra : Return address

ECE 232 L7.Simul.7 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Procedure call °Assume that procedure A computes f($a0,…,$a3) and uses temporary registers $s0 and $t0,$t. It is called by the main program. call procedure A procedure A.… jr $ra pc Memory pc pc  $ra 2. Call procedure A: jal 1200 # $ra  pc+4; j Save registers $so, $t0, $t1 (push on stack) 3. Comute f($a0,..,$a3) and store result in $vo 4. Restore registers $so,$t0,$t1 (pop from stack) 5. Return from procedure: jr $ra # $pc  $ra

ECE 232 L7.Simul.8 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Example: Procedure call (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 °Save the value of f in 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 L7.Simul.9 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

ECE 232 L7.Simul.10 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers SPIM Simulator

ECE 232 L7.Simul.11 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers SPIM Simulator °Console: this is where you enter input data and output is printed also, all system information is displayed (error handling)

ECE 232 L7.Simul.12 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Assembler syntax °Organize memory in segments: data, text °Directives tell assembler how to do it:.data# store in data segment items:.wordi1, i2, …# store integer words array:.space4096# reserve 1k space for arrray prompt:.asciiz str# place the string str in memory.globlmain# must be global.text# instructions go to text segment Main: your program goes here See examples (input data, compute, output data) Note: text in italics is your data: values, strings, names, labels, etc. Optional (if you need data)

ECE 232 L7.Simul.13 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Assembler directives °Help define and allocate space for data variables, arrays, and strings in the data segment of memory °Directs assembler to properly interpret data in memory.align 2# align data on a 4 byte (word) boundary.asciiz str# store the string str in memory.byte b1, …, bn# store the n values in successive memory bytes.word w1, …, wn# store the n words in successive memory words.data # store items in data segment (at address addr).globl symbol# label symbol is global (cannot be ref. by others)

ECE 232 L7.Simul.14 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Assembler directives - examples °C program:int Array[1024] MIPS assembly:.data# store subsequent items in data segment Array:.space 4096 # allocate 4x1024 bytes in Array °C program:int List[4] = {28, 13, 255, 2356} MIPS assembly:.data# can optionally specify base address List:.word 28, 13, 255, 2356# list of constants °To access third element (255) of List and place a copy in $s0: la$a0, list# a0  pointer to base of array List lw$s0, 12($a0)# s0  Mem[a0+12] in bytes Note: la = load address of base of array into register

ECE 232 L7.Simul.15 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers I/O Processing Example.data msg1:.asciiz "\n Please enter an integer: " msg2:.asciiz "\n Thank you ".globl main.text main: li$v0, 4# system call code for print_string la$a0, msg1# load address of msg1 into $a0 syscall# print the string li$v0, 5# system call code for read_string syscall# read the integer li$v0, 4# system call code for print_string la$a0, msg2# load address of msg2 into $a0 syscall# print the string

ECE 232 L7.Simul.16 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Strings How are strings interpreted and stored? The following directive.asciiz "\n Please“ produces the following sequence of bytes in memory (in decimal).byte80, 108, 101, 97, 115, 101 P l e a s e See Appendix A2, and the ASCII Codes in text, Section 3.7,page 142

ECE 232 L7.Simul.17 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers System calls °SPIM provides a small set of operating system-like instructions (syscall instruction) °To request a service, a program loads: system call code into register $v0, and arguments into registers $a0 - $a3 for integers (or $f12 for floating point) °System call returns values put in $v0 (or $f0 for floating point) °Handles I/O: Allows to read in data and print out data °Handles errors, exceptions Prints error messages

ECE 232 L7.Simul.18 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers System services Service System call code ArgumentsResult print_integer1$a0 = integer print_float2$f12 = float print_double3$f12 = double print_string4$a0 = string read_integer5integer (in $v0) read_float6float (in $f0) read_double7double (in $f0) read_string8$a0=buffer $a1=length sbrk9$a0 = amountaddress (in $v0) exit 10

ECE 232 L7.Simul.19 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers A Complete Example sum:li$v0, 0 li$v1, 0# Initialize v0 and v1 to zero loop: blez$a1, retzz# If (a1  0) branch to return addi$a1, $a1, -1# Decrement loop count lw$t0, 0($a0)# Get a value from the array addi$a0, $a0, 4# Increment array pointer to next word bltz$t0, negg# If value is negative branch to negg add$v0, $v0, $t0# Add to the positive sum bloop# Branch around the next two instructions negg: add$v1, $v1, $t0# Add to the negative sum bloop# Branch to loop retzz:jr$ra# Return Given a list of integers, compute and print sum of positive numbers sum of negative numbers Subroutine sum (to be placed at the end of program):

ECE 232 L7.Simul.20 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers A Complete Program Example, cont’d.data array:.word -4, 5, 8, -1 msg1:.asciiz "\n The sum of the positive values = " msg2:.asciiz "\n The sum of the negative values = ".globl main.text main: li$v0, 4# system call code for print_str la$a0, msg1# load address of msg1. into $a0 syscall# print the string la$a0, array# Initialize address Parameter li$a1, 4# Initialize length Parameter jalsum# Call sum move$a0, $v0# move value to be printed to $a0 li$v0, 1# system call code for print_int syscall# print sum of positive values li$v0, 4# system call code for print_str la$a0, msg2# load address of msg2. into $a0 syscall# print the string li$v0, 1# system call code for print_int move$a0, $v1# move value to be printed to $a0 syscall# print sum of negative values li$v0, 10# terminate program run and syscall# return control to system