CSCI 2321 (Computer Design), Spring 2018 Homework 3

Similar documents
Chapter 3. Instructions:

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

MIPS (SPIM) Assembler Syntax

Chapter 2. Instructions:

Slide Set 4. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

CS61C - Machine Structures. Lecture 6 - Instruction Representation. September 15, 2000 David Patterson.

ECE 30 Introduction to Computer Engineering

Assembly Language Programming. CPSC 252 Computer Organization Ellen Walker, Hiram College

Lecture 9: Disassembly

2) Using the same instruction set for the TinyProc2, convert the following hex values to assembly language: x0f

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

Chapter 2. Instruction Set Architecture (ISA)

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

CS3350B Computer Architecture

MIPS Functions and Instruction Formats

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

CS 110 Computer Architecture MIPS Instruction Formats

CS222: MIPS Instruction Set

1 5. Addressing Modes COMP2611 Fall 2015 Instruction: Language of the Computer

CS3350B Computer Architecture MIPS Introduction

Instructions: MIPS arithmetic. MIPS arithmetic. Chapter 3 : MIPS Downloaded from:

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

We will study the MIPS assembly language as an exemplar of the concept.

Chapter 2A Instructions: Language of the Computer

Chapter 3 MIPS Assembly Language. Ó1998 Morgan Kaufmann Publishers 1

EE 361 University of Hawaii Fall

CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Formats

COMPUTER ORGANIZATION AND DESIGN

Thomas Polzer Institut für Technische Informatik

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

ecture 12 From Code to Program: CALL (Compiling, Assembling, Linking, and Loading) Friedland and Weaver Computer Science 61C Spring 2017

Topic Notes: MIPS Instruction Set Architecture

Programming the processor

bits 5..0 the sub-function of opcode 0, 32 for the add instruction

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

Instructions: MIPS ISA. Chapter 2 Instructions: Language of the Computer 1

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

CS 61C: Great Ideas in Computer Architecture CALL continued ( Linking and Loading)

Lecture 5: Procedure Calls

CSE Lecture In Class Example Handout

Systems Architecture I

CS/COE1541: Introduction to Computer Architecture

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

2. Define Instruction Set Architecture. What are its two main characteristics? Be precise!

CS 61c: Great Ideas in Computer Architecture

CS 61C: Great Ideas in Computer Architecture MIPS Instruction Formats

Architecture II. Computer Systems Laboratory Sungkyunkwan University

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

Character Is a byte quantity (00~FF or 0~255) ASCII (American Standard Code for Information Interchange) Page 91, Fig. 2.21

Review. Disassembly is simple and starts by decoding opcode field. Lecture #13 Compiling, Assembly, Linking, Loader I

ECE 473 Computer Architecture and Organization Project: Design of a Five Stage Pipelined MIPS-like Processor Project Team TWO Objectives

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

Review (1/2) IEEE 754 Floating Point Standard: Kahan pack as much in as could get away with. CS61C - Machine Structures

Chapter 2: Instructions:

ECE 15B COMPUTER ORGANIZATION

Programming with QtSpim: A User s Manual

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

ECE369. Chapter 2 ECE369

From Code to Program: CALL Con'nued (Linking, and Loading)

COMP 303 MIPS Processor Design Project 3: Simple Execution Loop

ECE232: Hardware Organization and Design. Computer Organization - Previously covered

Machine Instructions - II. Hwansoo Han

Instructions: Assembly Language

CS 61C: Great Ideas in Computer Architecture Running a Program - CALL (Compiling, Assembling, Linking, and Loading)

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

CSE Lecture In Class Example Handout

Review. Lecture #9 MIPS Logical & Shift Ops, and Instruction Representation I Logical Operators (1/3) Bitwise Operations

MIPS Instruction Set Architecture (2)

MIPS%Assembly% E155%

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

Assembler. Lecture 8 CS301

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

(Refer Slide Time: 1:40)

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

CSE 2021: Computer Organization

CS2214 COMPUTER ARCHITECTURE & ORGANIZATION SPRING 2014

EN164: Design of Computing Systems Topic 03: Instruction Set Architecture Design

SPIM & MIPS Programming

ENCM 369 Winter 2017 Lab 3 for the Week of January 30

CS61C Machine Structures. Lecture 13 - MIPS Instruction Representation I. 9/26/2007 John Wawrzynek. www-inst.eecs.berkeley.

CS61C Machine Structures. Lecture 18 - Running a Program I. 3/1/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

Instruction Set Architecture

Common Problems on Homework

Instructions: Language of the Computer

ENCM 369 Winter 2013: Reference Material for Midterm #2 page 1 of 5

QtSPIM and MARS : MIPS Simulators

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

5/17/2012. Recap from Last Time. CSE 2021: Computer Organization. The RISC Philosophy. Levels of Programming. Stored Program Computers

Course Administration

Recap from Last Time. CSE 2021: Computer Organization. Levels of Programming. The RISC Philosophy 5/19/2011

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

SPIM Instruction Set

CS 61C: Great Ideas in Computer Architecture MIPS Instruction Formats

Solutions for Chapter 2 Exercises

Transcription:

CSCI 2321 (Computer Design), Spring 2018 Homework 3 Credit: 50 points. 1 Reading Be sure you have read, or at least skimmed, all assigned sections of Chapter 2 and Appendix A. 2 Honor Code Statement Please include with each part of the assignment the Honor Code pledge or just the word pledged, plus one or more of the following about collaboration and help (as many as apply). 1 Text in italics is explanatory or something for you to fill in. For written assignments, it should go right after your name and the assignment number; for programming assignments, it should go in comments at the start of your program(s). This assignment is entirely my own work. (Here, entirely my own work means that it s your own work except for anything you got from the assignment itself some programming assignments include starter code, for example or from the course Web site. In particular, for programming assignments you can copy freely from anything on the sample programs page.) I worked with names of other students on this assignment. I got help with this assignment from source of help ACM tutoring, another student in the course, the instructor, etc. (Here, help means significant help, beyond a little assistance with tools or compiler errors.) I got help from outside source a book other than the textbook (give title and author), a Web site (give its URL), etc.. (Here too, you only need to mention significant help you don t need to tell me that you looked up an error message on the Web, but if you found an algorithm or a code sketch, tell me about that.) I provided help to names of students on this assignment. (And here too, you only need to tell me about significant help.) 3 Problems Answer the following questions. You may write out your answers by hand or using a word processor or other program, but please submit hard copy, either in class or in one of my mailboxes (outside my office or in the ASO). 1. (5 points) Consider this fragment of MIPS code, intended to be a typical MIPS version of if/else: 1 Credit where credit is due: I based the wording of this list on a posting to a SIGCSE mailing list. SIGCSE is the ACM s Special Interest Group on CS Education. 1

Else: After: slt $t0, $s1, $s2 beq $t0, $zero, Else addi $s3, $s3, 1 addi $s4, $s4, 1 j After addi $s3, $s3, -1 addi $s4, $s4, -1 Translate the beq and j instructions into machine language, assuming that the first instruction is at memory location 0x00400040. (You don t have to translate the other instructions, just those two. As in Homework 2, first list all the fields (e.g., opcode) in binary and then give the 32-bit instruction in hexadecimal.) 2. (5 points) The MIPS assembler supports a number of pseudoinstructions, which look like regular instructions but which assemble into one or more other machine instructions. We ve seen how SPIM assembles the la pseudoinstruction into a combination of lui and ori. As another example, pseudoinstruction ble generates two instructions, a slt and then a bne, using assembly temporary register $at, with ble $t0, $1, There being translated to slt $at, $t1, $t0 bne $at, zero, There If you wanted the assembler to support the following pseudoinstructions, say what code (using real instructions) the assembler should generate for the given examples. As with ble, you should use $at if you need an additional temporary register. bnz with the two operands (register number and target label/address), that branches to the target if the register contents are nonzero. Example: bnz $s0, There swap with two register-number operands, which exchanges the values in the two registers. Example: swap $s0, $s1 3. (15 points) For this problem your mission is to reproduce by hand a little of what an assembler and linker would do with two fairly meaningless 2 pieces of MIPS assembly code. The textbook has an example starting on p. 127 illustrating more or less what I have in mind here, and we reviewed the example in class, but on reflection it doesn t seem that clear to me, so for this assignment I want you to approach the problem a little differently. First, the two files, one containing a main procedure:.text.globl main 2 They don t do anything very interesting, but together they do represent a complete program. 2

main: addi $sp, $sp, -4 sw $ra, 0($sp) jal subpgm lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra.end main.data.globl datax local:.word 0 datax:.word 1, 2 and another a procedure it calls:.text.globl subpgm subpgm: addi $sp, $sp, -4 sw $ra, 0($sp) # copy data (two "words") from datax to datay la $s0, datax la $s1, datay lw $t0, 0($s0) sw $t0, 0($s1) lw $t0, 4($s0) sw $t0, 4($s1) lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra.end subpgm.data.globl datay datay:.space 8 For the assembly phase, I don t want you to actually translate the instructions into machine language, but I do want you to construct for each file a table with information as listed below. Note that you will need to expand the two la pseudoinstructions. The example in the textbook doesn t really show how to do this; they instead show how to deal with lw and sw referencing a symbol and assembled into something using the $gp register. 3 Instead I want you to expand these instructions in the way SPIM does: each as a lui followed by a ori. (You can see examples of this by loading any of the sample programs that use la into SPIM and looking at what it shows for code.) (Hint: Before going further, you ll probably find it useful to write down, for each of the two files, what s in its text segment (a list of instructions and their offsets, remembering to expand 3 I m not quite sure how they get this from MIPS assembly source; SPIM will accept load/store instructions referencing a label, but it turns them into lui/ori pairs in the same way it does for la. 3

any pseudoinstructions), and what s in its data segment (a list of variables/labels and their offsets and sizes).) Then produce, for each of the two source files, a table with the following. (Use hexadecimal to represent addresses and offsets.) Text (code) and data sizes, in hexadecimal. Relocation information : For each instruction that involves an absolute address (jumps and the instructions corresponding to a la pseudoinstruction): Its offset in the text segment. The instruction type (as in the textbook example). The symbol referenced ( dependency in the textbook example). A symbol table listing all symbols, showing for each: Its name. Which segment it s in (text or data) and its offset into that segment. For example. the first symbol in the first file is main, at offset 0 into the text segment. (A real assembler would probably try to resolve references to local symbols at this point, but for simplicity I want you to just resolve them all in the next step.) Next, link these two files to produce information for an executable for the SPIM simulator. Since programs in this simulator always have their text segments at 0x00400024 and their data segments at 0x10010000, absolute addresses into either segment can be based on these values. (Normally an executable file might include relocation information for any instructions containing absolute addresses that would need to be changed when the program is loaded into memory, but we ll skip that.) (Hint: Notice that the text segment of the executable is just the text segment for the first file followed by the one for the second file, and similarly for the data segment. So you ll probably find it useful to come up with a list of what s in each segment, similar to what you did in the first step, but with addresses rather than offsets.) The information I want is this: Text (code) and data size, in hexadecimal. A symbol table showing locations of all symbols and their addresses (e.g., main is at 0x00400024). (Really I think this should just be the global symbols, but to patch the unresolved references you ll need some non-global labels, and this is the simplest way to achieve that.) Patched versions of the instructions from the object files relocation information sections, in the form of another table, one entry per instruction, with: The instruction s address (or you can specify offset into combined text segment that s what I asked for originally, but address is both better and easier). The patched instruction, in a form that looks like source code but doesn t reference labels so for example a j main would become j 0x00400024. (Use hexadecimal for the constant/immediate values here.) 4

4 Programming Problems Do the following programming problems. You will end up with at least one code file per problem. Submit your program source(and any other needed files) by sending mail to bmassing@cs.trinity.edu with each file as an attachment. Please use a subject line that mentions the course and the assignment (e.g., csci 2321 hw 3 or computer design hw 3 ). You can develop your programs on any system that provides the needed functionality, but I will test them on one of the department s Linux machines, so you should probably make sure they work in that environment before turning them in. 1. (10 points) Add code to your solution 4 to textbook problem 2.27 from Homework 2 to make it a complete program that, run from SPIM, prompts for values for a and b and prints the ending values of the elements of D. I.e., for output the program should do the equivalent of the C code for (k=0; k<4*b; k++) printf("%d\n", D[k]); Programs echo.s and echoint.s on the sample programs page show how to input and output text and integer values. (I think this is also a good opportunity to tweak your solution so it uses registers $s3 and $s4 (rather than $t0 and $t1) for variables i and j, but if you can make the program work without doing that, okay.) 2. (15 points) Problem 2.31 from the textbook asks you to write a MIPS implementation of a recursive function fib to compute elements of the Fibonacci sequence. For this problem, your mission is to write this MIPS function and incorporate it into a complete program that, run from SPIM, prompts for an integer value N, calls fib to compute the N-th element of the sequence, and prints the result. Program factorial.s on the sample programs page may be helpful, since it shows how to do the needed input/output and also contains an example of a recursive procedure written in MIPS. To get full credit, your program must use recursion, and any functions/procedures you define must follow the conventions described in the textbook and in class for passing arguments and saving/restoring registers. 4 Or you can start from my sample solution, shared with you via Google Drive. 5