ECE/CS 314 Fall 2003 Homework 2 Solutions. Question 1

Similar documents
Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

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

Lecture 5: Procedure Calls

ECE 30 Introduction to Computer Engineering

Lecture 5: Procedure Calls

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

ECE260: Fundamentals of Computer Engineering

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam Two 11 March Your Name (please print) total

CS 61c: Great Ideas in Computer Architecture

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

ECE 2035 Programming HW/SW Systems Fall problems, 7 pages Exam Two 23 October 2013

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

Architecture II. Computer Systems Laboratory Sungkyunkwan University

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

MIPS Functions and the Runtime Stack

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

Reduced Instruction Set Computer (RISC)

Lecture 6: Assembly Programs

EE 361 University of Hawaii Fall

ECE232: Hardware Organization and Design

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

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

Control Instructions

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

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

Compiling Techniques

Reduced Instruction Set Computer (RISC)

University of California at Santa Barbara. ECE 154A Introduction to Computer Architecture. Quiz #1. October 30 th, Name (Last, First)

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 23 October Your Name (please print clearly) Signed.

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

ECE 2035 A Programming Hw/Sw Systems Fall problems, 8 pages Final Exam 8 December 2014

ECE 331 Hardware Organization and Design. Professor Jay Taneja UMass ECE - Discussion 3 2/8/2018

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

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009

Thomas Polzer Institut für Technische Informatik

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

Procedure Call and Return Procedure call

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

Q1: /30 Q2: /25 Q3: /45. Total: /100

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 21 October 2016

Code Genera*on for Control Flow Constructs

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

ECE 473 Computer Architecture and Organization Lab 4: MIPS Assembly Programming Due: Wednesday, Oct. 19, 2011 (30 points)

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

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

MIPS Instruction Set Architecture (2)

2. dead code elimination (declaration and initialization of z) 3. common subexpression elimination (temp1 = j + g + h)

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

ECE 2035 A Programming Hw/Sw Systems Spring problems, 8 pages Final Exam 29 April 2015

S Computer Architecture. Computer architecture assignment E1 A subroutine, which searches for given byte pattern from an byte array.

Computer Architecture

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Lecture 7: Procedures

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

Lec 10: Assembler. Announcements

Computer Organization MIPS Architecture. Department of Computer Science Missouri University of Science & Technology

MIPS Reference Guide

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam One 19 September 2012

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

COMPUTER ORGANIZATION AND DESIGN

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

Course Administration

Review of Activation Frames. FP of caller Y X Return value A B C

COE608: Computer Organization and Architecture

The MIPS Instruction Set Architecture

CSE Lecture In Class Example Handout

ICS DEPARTMENT ICS 233 COMPUTER ARCHITECTURE & ASSEMBLY LANGUAGE. Midterm Exam. First Semester (141) Time: 1:00-3:30 PM. Student Name : _KEY

ECE 15B Computer Organization Spring 2010

Week 10: Assembly Programming

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

EE 109 Unit 15 Subroutines and Stacks

Machine Instructions - II. Hwansoo Han

Question 0. Do not turn this page until you have received the signal to start. (Please fill out the identification section above) Good Luck!

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

CS 61C: Great Ideas in Computer Architecture. More MIPS, MIPS Functions

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

ECE331: Hardware Organization and Design

MIPS Functions and Instruction Formats

University of California College of Engineering Computer Science Division -EECS. CS 152 Midterm I

CS61C : Machine Structures

Chapter 2. Instructions:

MIPS%Assembly% E155%

ECE 2035 Programming Hw/Sw Systems Fall problems, 10 pages Final Exam 9 December 2013

CS 316: Procedure Calls/Pipelining

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

CS61C : Machine Structures

SPIM Instruction Set

Inequalities in MIPS (2/4) Inequalities in MIPS (1/4) Inequalities in MIPS (4/4) Inequalities in MIPS (3/4) UCB CS61C : Machine Structures

CS61C : Machine Structures

Computer Architecture. The Language of the Machine

ECE 2035 Programming HW/SW Systems Spring problems, 7 pages Exam One Solutions 4 February 2013

MIPS Assembly Language. Today s Lecture

UCB CS61C : Machine Structures

ECE260: Fundamentals of Computer Engineering

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam One 4 February Your Name (please print clearly)

Today s Lecture. MIPS Assembly Language. Review: What Must be Specified? Review: A Program. Review: MIPS Instruction Formats

Transcription:

ECE/CS 314 Fall 2003 Homework 2 Solutions Question 1 /* Equivalent C code with no loops (goto instead) char* strchr(char* pcstring, char csearchchr) strchr_loop: if (!*pcstring) return 0; if (*pcstring == csearchchr) return pcstring; pcstring++; goto strchr_loop; /* Assembly of strchr Note: leaf function Also, s used very conservatively strchr: 5 points if they remember to check first condition that if the input string is NULL, the function must return NULL lbu $8, 0($4) load the first character of pcstring bne $8, $0, $L2 branch if not null move $2, $0 null, so set return value to 0 jr $31 and return delay branch $L2: 10 points if they remember to check end condition bne $8, $5, $L3 If they are equal, we found it move $2, $4 set $2 to $4 and return jr $31 $L3: 10 points if they remember to increment addu $2,$4,1 we want to increment $4 move $4,$2 j strchr

Question 2 /* Equivalent C code with no loops (goto instead) int strccnt(char* pcstring, char csearchchr) int ncounter = 0; strcnnt_loop: pcstring = strchr(pcstring, csearchchr); if (pcstring) ncounter++; pcstring++; goto strcnnt_loop; return ncounter; /* Assembly of strccnt Notes: Use of fp is optional. In fact,the following code uses fp trivially. Do not take points off is fp is not used but the code is otherwise functional Also, s used very conservatively strccnt: Setting up stack frame and initializing variable - 5 points 2 points for allocating space (32 for fp version, 24 for non fp) 2 points for saving ra 1 point for initialize counter to 0 subu $sp,$sp,32 allocate space 32 because: 4 for ra 4 for old fp 4 for var ncounter 16 for additional args double word aligh sw $31, 28($sp) save ra sw $fp, 24($sp) save old fp move $fp, $sp move current sp into fp

$L6: $L7: sw $0, 20($fp) set counter to 0 Calling strchr - 8 points 2 points for allocating space and restoring space on stack 3 points for for saving and restoring register 3 points for the branch test subu $sp, $sp, 16 allocate space to save arguments sw $4, 0($sp) save A0 and A1 sw $5, 4($sp) jal strchr lw $4, 0($sp) Restore A0 and A1 lw $5, 4($sp) addu $sp, $sp, 16 restore stack beq $2, $0, $L7 If strchr returns 0, jump to label which sets return value from counter and returns Incrementing variables - 8 points 2 points for loading counter 2 points for incrementing counter 2 points for restoring counter 2 points for incrementing pcstring lw $3, 20($fp) load counter into $3 addu $3, $3, 1 add 1 to counter sw $3, 20($fp) and store it back into mem addu $4, $2, 1 increment pcstring, and store it into $4 (A0) j $L6 goto L6 to restart loop Restoring stack 4 points 2 points for restoring stack 2 points for restoring ra lw $2,16($fp) Set return value to counter move $sp,$fp sp not trusted here lw $31,28($sp) restore ra lw $fp,24($sp) restore old fp addu $sp,$sp,32 restore stack jr $31 exit

Question 3 int timesten(int ninvalue) /* Multiple by 8 (shift by 3) + Multiple by 2 (shift by 2) return ((ninvalue << 3) + ( ninvalue << 1)); Assume initial param value in $4 The question doesn't specify that it is a function, so just a sequence of 3 instructions is fine 20 points if shift used but used incorrectly 15 points if some type of loop is used sll $2, $4, 3 $2 has the value to be multiplied sll $1, $4, 1 $1 is $at addu $2, $2, $1 Alternate solution for Question 3: ninvalue = (ninvalue << 2 + ninvalue) << 1; sll $2,$4,2 ninvalue = ninvalue * 4 + ninvalue add $1,$2,$4 sll $2,$1,1 ninvalue * 2 Question 4 /* C version of atoi with no loops (using gotos) Note: Horner's rule example 123 = (((1 * 10) + 2) * 10) + 3 ASCII numbers from 0 to 9 are 48 to 57 int atoi(char* pcstring)

int nreturnvalue = 0; atoi_loop: if (*pcstring >= 48 && *pcstring <= 57) nreturnvalue = nreturnvalue * 10 + *pcstring - 48; pcstring++; goto atoi_loop; else if (*pcstring!= 0) return -1; return nreturnvalue; /* Assembly version of atoi Note, leaf function with data atoi: 2 points for allocating space on stack subu $sp,$sp,8 sw $0,4($sp) nreturnvalue = 0 $L2: 10 points for implementing horners rule although other creative solution might be okay as well, use discretion. Note that multiple can be used instead of the shift and add solution. 5 points to recognize ASCII range of 0 to 9 lbu $8, 0($4) Load first character of string sltu $2, $8, 48 Test if it is less than 48 bne $2, $0, $L3 If it is, go to else if sltu $2, $8, 58 Test if it is less than 58 beq $2, $0, $L3 If false, go to else if lw $2, 4($sp) Load nreturnvalue sll $3, $2, 2 nreturnvalue = nreturnvalue * 4 + nreturnvalue addu $3, $3, $2 sll $2, $3, 1 nreturnvalue = nreturnvalue * 2 addu $2, $2, $8 nreturnvalue = nreturnvalue + *pcstring addu $3, $2, -48 nreturnvalue = nreturnvalue - 48 sw $3, 4($sp) Store back into memory

$L3: $L5: $L1: addu $4, $4, 1 pcstring++ j $L2 Loop back and test again 3 points for checking NULL end case beq $8, $0, $L5 If *pcstring is 0, we've hit the NULL, and we can return the value 3 points for checking the invalid case li $2, -1 Load -1 into return value as j $L1 we have hit the invalid case lw $2, 4($sp) Load nreturnvalue into return register j $L1 2 points for restoring stack addu $sp, $sp, 8 Restore stack jr $31 Return