CSc 256 Midterm 1 Spring 2011

Similar documents
CSc 256 Midterm 1 Fall 2011

CSc 256 Midterm (green) Fall 2018

CSc 256 Final Spring 2011

CSc 256 Final Fall 2016

CSc 256 Midterm 2 Spring 2012

CSc 256 Midterm 2 Fall 2011

MIPS Functions and the Runtime Stack

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

ECE 30 Introduction to Computer Engineering

Compiling Techniques

Lecture 6 Decision + Shift + I/O

MIPS function continued

Field 6-Bit Op Code rs Field rt Field 16-bit Immediate field

CSE Lecture In Class Example Handout

QtSPIM and MARS : MIPS Simulators

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

Lecture 5: Procedure Calls

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

MIPS Assembly Language Programming

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

MIPS Assembly Language Programming

MIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8

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

Lecture 7: Examples, MARS, Arithmetic

4.2: MIPS function calls

ECE232: Hardware Organization and Design

CDA3100 Midterm Exam, Summer 2013

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

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

Control Instructions

SPIM Instruction Set

Chapter 2. Instructions:

ECE 30 Introduction to Computer Engineering

Chapter 2: Instructions:

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

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

MIPS Functions and Instruction Formats

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

MIPS Assembly Language Guide

MIPS Assembly (Functions)

Common Problems on Homework

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

instructions aligned is little-endian

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

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

Instruction Set Architectures (4)

2B 52 AB CA 3E A1 +29 A B C. CS120 Fall 2018 Final Prep and super secret quiz 9

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Midterm. CS64 Spring Midterm Exam

Computer Systems and Networks

Procedure Calling. Procedure Calling. Register Usage. 25 September CSE2021 Computer Organization

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

MIPS Reference Guide

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

ECE331: Hardware Organization and Design

Computer Organization MIPS ISA

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

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.

Assignment 1: Pipelining Implementation at SPIM Simulator

Lecture 6: Assembly Programs

CS61C : Machine Structures

Lecture 7: MIPS Functions Part 2. Nested Function Calls. Lecture 7: Character and String Operations. SPIM Syscalls. Recursive Functions

MIPS Instruction Set Architecture (2)

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

MIPS Coding Snippets. Prof. James L. Frankel Harvard University. Version of 9:32 PM 14-Feb-2016 Copyright 2016 James L. Frankel. All rights reserved.

Procedure Call and Return Procedure call

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

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

Lecture 5: Procedure Calls

ECE Exam I - Solutions February 19 th, :00 pm 4:25pm

Problem 3: Theoretical Questions: Complete the midterm exam taken from a previous year attached at the end of the assignment.

Function Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read

ECE Exam I February 19 th, :00 pm 4:25pm

MIPS%Assembly% E155%

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

Course Administration

Points available Your marks Total 100

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

Kernel Registers 0 1. Global Data Pointer. Stack Pointer. Frame Pointer. Return Address.

Basic Computer Operations and Assembly Language Programming

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

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

Numbers: positional notation. CS61C Machine Structures. Faux Midterm Review Jaein Jeong Cheng Tien Ee. www-inst.eecs.berkeley.

CENG3420 Lecture 03 Review

Course Administration

Computer Hardware Engineering

1/26/2014. Previously. CSE 2021: Computer Organization. The Load/Store Family (1) Memory Organization. The Load/Store Family (2)

MIPS and QTSPIM Recap. MIPS Architecture. Cptr280. Dr Curtis Nelson. Cptr280, Autumn

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

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how.

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

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

Today s topics. MIPS operations and operands. MIPS arithmetic. CS/COE1541: Introduction to Computer Architecture. A Review of MIPS ISA.

CS3350B Computer Architecture MIPS Introduction

Mark Redekopp, All rights reserved. EE 352 Unit 6. Stack Frames Recursive Routines

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

Assembler. Lecture 8 CS301

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

Transcription:

CSc 256 Midterm 1 Spring 2011 NAME: Problem1a: GiventheC++functionprototypeandvariabledeclarations: intfunc(intarg0,int*arg1,int*arg2); int*ptr,n,arr[10]; whichofthefollowingstatementswillcausesyntaxerrorsorwarningsabouttype mismatch/missingcast?foreachcase,writeeitherbad(errororwarning),orok (noerrororwarning),andcirclealso*all*theargument/variablesthathavetype mismatches.nopointswillbeawardedforastatementifthereisasyntaxerrorand youcirclethewrongargument/variable.(6points) arr[*ptr]=func(*ptr,&arr[*ptr],&n); *ptr=func(arr[ptr],&arr[n],ptr); withpointer) OK BAD(1 st argumentarr[]indexed

Problem1b:SupposewearetracingaC/C++programusingadebuggersuchasgdb. Thecodeshowingallfunctioncallslookslikethis: main(){ F1(5); F1(2); } F1(intarg){ F2(arg*2); F2(arg/2); } F2(intarg){ F3(arg); intx=4; //somecodenotshown } F3(intarg){ //somecodenotshown } Supposewesetabreakpointatthelineintx=4(inF2()).Weruntheprogram,stop atthebreakpointforthefirsttime,andcheckthestackframesonthestackusinga commandsimilartogdb sbacktrace.whatarethestackframesthatyouseeatthis time?foreachstackframethatyousee,indicateclearwhatfunctionitbelongsto, theargumentsforthatfunctioncall,andtheorder(whichoneisatthebottom, whichisaboveit,etc).(4points) Bottomisstackframeformain AboveitisstackframeforF1(5) AboveitisstackframeforF2(10) Or: [stackframeforf2(10)] [stackframeforf1(5)] [stackframeformain]

Problem2: Translate the C++ function moro() into MIPS assembly language. (You don't have to write a main program, or the gnew() function.) Your solution must compile and run correctly in spim, and follow all MIPS register use conventions. You may use $s? or $t? as temporaries, but follow the specifications below. For moro:: void moro(int *arg0, int *arg1, int arg2) arg0 a0 arg1 a1 arg2 a2 For gnew: int gnew(int *arg0, int arg1) arg0 a0 arg1 a1 result returned in $v0 Make sure your loops are efficient (i.e., they should not have unnecessary branches). Points will be deducted for obvious inefficiencies. (45 points)

void moro(int *arg0, int *arg1, int arg2) { int *ptr, sum = 5; ptr = arg0; if (arg2 < sum *ptr > 0) { sum = sum + arg1[arg2];; } else { for (int i=arg2; i>0; i--) { ptr++; sum = sum + gnew(ptr, arg2); } } *ptr = sum; }

Solution: # $s0 sum # $s1 ptr # $s2 copy of arg2 # $s3 i moro: sw $31, -4($sp) sw $s0, -8($sp) sw $s1, -12($sp) sw $s2, -16($sp) addi $s0, $s0, -16 move $s2, $a2 # int *ptr, sum = 5; li $s0, 5 move $s1, $a0 # ptr = arg0; blt $a2, $s0, if# if (arg2 < sum *ptr > 0) { lw $t0, ($s1) ble $t0, $0, else if: sll $t0, $s2, 2 # sum = sum + arg1[arg2]; add $t0, $t0, $a1 lw $t1, ($t0) add $s0, $s0, $t1 j exit # } else: # else { move $s3, $s2 # for (int i=arg2; i>0; i--) { ble $s3, $0, exit for: addi $s1, $s1, 4 # ptr++; move $a0, $s1 # sum = sum + gnew(ptr, arg2); move $a1, $s2 jal gnew add $s0, $s0, $v0 addi $s3, $s3, -1 # } bgt $s3, $0, for # } exit: sw $s0, ($s1) # *ptr = sum; addi $s0, $s0, 16 lw $31, -4($sp) lw $s0, -8($sp) lw $s1, -12($sp) lw $s2, -16($sp) jr $31

Problem3: Show what is printed on the screen after this C++ program is executed. (20 points) ANSWER: obsvrvaqory #include <iostream> using std::cout; int main() { char str[] = "observatory"; char *ptr0, *ptr1; } ptr0 = &str[7]; ptr1 = ptr0-5; *ptr0 = (*ptr1) - 2; ptr1 = ptr1 + 1; ptr0 = ptr0-2; *ptr1 = *ptr0; cout << str; [Work:] ptr1 = ptr0 5 = &str[2] *ptr0 = (*ptr1) 2 // str[7 ] = str[2] 2 = q ptr1 = ptr1 + 1 = &str[3] ptr0 = ptr0 2 = &str[5] *ptr1 = *ptr0 // str[3] = str[5] = v

Problem 4: The following MIPS program is executed. Show the contents of registers $t0, $t1 and $t2, when the label "end" is reached. Assume the initial contents of registers as shown. All integers are in hex. Show each step clearly for partial credit. (25 points) Initially: $t0 0x10010008 $t1 0xffffff6a $t2 0x918120b0 label address contents here: 0x10010004 0x39383736 there: 0x10010008 0x2a2b2c2d.text main:sra $t1,$t1,3 sb $t1,-3($t0) or $t0, $t1, 0x00000099 lw $t2,here end: li $v0,10 syscall obsvrvaqory CONTENTS of $t0 (in hex): 0xfffffffd CONTENTS of $t1 (in hex): 0xffffffed CONTENTS of $t2 (in hex): 0x39ed3736 $t1 = 1111 1111 1111 1111 1111 1111 0110 1010 sra $t1, $t1, 3 $t1 = 1111 1111 1111 1111 1111 1111 1110 1101 = 0xffff ffed sb or $t1,-3($t0) ADDR = $t0 3 = 0x1001 0005 MEM[0x10010004] = 0x39ed3736 $t0, $t1, 0x00000099 $t1 = 1111 1111 1111 1111 1111 1111 1110 1101 0000 0000 0000 0000 0000 0000 1001 1001 $t0 = 1111 1111 1111 1111 1111 1111 1111 1101

= 0xffff fffd lw $t2,here $t2 = 0x39ed3736

MIPSinstructions op1,op2areregisters,op3isregisterorconstant cont[op1]meanscontentsofop1 moveop1,op2 cont[op1]=cont[op2] addop1,op2,op3 cont[op1]=cont[op2]+cont[op3] subop1,op2,op3 cont[op1]=cont[op2] cont[op3] mulop1,op2,op3 cont[op1]=cont[op2]*cont[op3] divop1,op2,op3 cont[op1]=cont[op2]/cont[op3] remop1,op2,op3 cont[op1]=cont[op2]%cont[op3] notop1,op2 cont[op1]=notcont[op2](bitwise) andop1,op2,op3 cont[op1]=cont[op2]andcont[op3](bitwise) orop1,op2,op3 cont[op1]=cont[op2]orcont[op3](bitwise) nandop1,op2,op3 cont[op1]=cont[op2]nandcont[op3](bitwise) norop1,op2,op3 cont[op1]=cont[op2]norcont[op3](bitwise) xorop1,op2,op3 cont[op1]=cont[op2]xorcont[op3](bitwise) sllop1,op2,amt cont[op1]=cont[op2]shiftleftlogical byamtbits srlop1,op2,amt cont[op1]=cont[op2]shiftrightlogical byamtbits sraop1,op2,amt cont[op1]=cont[op2]shiftrightarithmetic byamtbits rolop1,op2,amt cont[op1]=cont[op2]rotateleftbyamtbits rorop1,op2,amt cont[op1]=cont[op2]rotaterightbyamtbits blabel gotolabel jlabel gotolabel beqop1,op2,label if(cont[op1]==cont[op2])gotolabel bneop1,op2,label if(cont[op1]!=cont[op2])gotolabel bgtop1,op2,label if(cont[op1]>cont[op2])gotolabel bgeop1,op2,label if(cont[op1]>=cont[op2])gotolabel bltop1,op2,label if(cont[op1]<cont[op2])gotolabel bleop1,op2,label if(cont[op1]<=cont[op2])gotolabel beqzop1,label if(cont[op1]==0)gotolabel bnezop1,label if(cont[op1]!=0)gotolabel bgtzop1,label if(cont[op1]>0)gotolabel bgezop1,label if(cont[op1]>=0)gotolabel bltzop1,label if(cont[op1]<0)gotolabel blezop1,label if(cont[op1]<=0)gotolabel lar,label cont[r]=addressoflabel lir,constant cont[r]=constant lwr,?? cont[r]=m[addr]

lbr,?? cont[r]=m[addr],sign extended lbur,?? cont[r]=m[addr],zero extended swr,?? M[ADDR]=cont[R] sbr,?? m[addr]=low8 bitsofcont[r] if??isalabel,addr=addressoflabel if??is(r),addr=cont[r] if??isconstant(r),addr=cont[r]+constant if??islabel(r),addr=cont[r]+addressoflabel mtc0op1,op2 contentsofcoprocessor0registerop1= contentsofmipsregisterop2 mfc0op1,op2 contentsofmipsregisterop1= contentsofcoprocessor0registerop2 Syscallusage: printanint $v0=1,$a0=inttobeprinted printastring $v0=4,$a0=addressofstringtobeprinted readanint $v0=5,inputintappearsin$v0 exit $v0=10 MIPSregisternames: $0 $1 $2,$3 $v0,$v1 $4 $7 $a0 $a3 $8 $15 $t0 $t7 $16 $23 $s0 $s7 $24 $25 $t8 $t9 $26 $27 $k0 $k1 $28 $gp $29 $sp $30 $s8 $31 $ra