CS 104 PRACTICE Midterm Exam 1

Similar documents
Introduction to MIPS Programming with Mars

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

Figure 1: Graphical example of a mergesort 1.

COMP 303 MIPS Processor Design Project 4: MIPS Processor Due Date: 11 December :59

More MIPS: Recursion. Computer Science 104 Lecture 9

Winter 2002 MID-SESSION TEST Friday, March 1 6:30 to 8:00pm

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

HOMEWORK # 2 SOLUTIO

Introduction to MIPS Assembly Programming

Instruction Set Architecture. or How to talk to computers if you aren t in Star Trek

Dalhousie University CSCI 2132 Software Development Winter 2015 Lab 7, March 11

Chapter 5 Instructor's Manual

Assembly Language Programming

CSI 333 Lecture 1 Number Systems

Solution for Homework 2

Typy danych. Data types: Literals:

Lecture Outline. Stack machines The MIPS assembly language. Code Generation (I)

CP Lab 2: Writing programs for simple arithmetic problems

The programming language C. sws1 1

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

ECE 0142 Computer Organization. Lecture 3 Floating Point Representations

Computer Science 281 Binary and Hexadecimal Review

Numbering Systems. InThisAppendix...

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

Reduced Instruction Set Computer (RISC)

Intel 8086 architecture

1 Description of The Simpletron

Instruction Set Design

CPU Organisation and Operation

MIPS Assembly Code Layout

DNA Data and Program Representation. Alexandre David

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

1. Convert the following base 10 numbers into 8-bit 2 s complement notation 0, -1, -12

Computer Systems Architecture

Instruction Set Architecture

Repetition Using the End of File Condition

Lecture 2. Binary and Hexadecimal Numbers

Arithmetic in MIPS. Objectives. Instruction. Integer arithmetic. After completing this lab you will:

Lab Work 2. MIPS assembly and introduction to PCSpim

Divide: Paper & Pencil. Computer Architecture ALU Design : Division and Floating Point. Divide algorithm. DIVIDE HARDWARE Version 1

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

EE282 Computer Architecture and Organization Midterm Exam February 13, (Total Time = 120 minutes, Total Points = 100)

7.1 Our Current Model

The string of digits in the binary number system represents the quantity

2.2: Bitwise Logical Operations

Instruction Set Architecture (ISA)

CSE 141L Computer Architecture Lab Fall Lecture 2

Sources: On the Web: Slides will be available on:

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Chapter 7D The Java Virtual Machine

Translating C code to MIPS

Stacks. Linear data structures

Binary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.

Data Structure with C

Using the RDTSC Instruction for Performance Monitoring

Summary of the MARIE Assembly Language

BCD (ASCII) Arithmetic. Where and Why is BCD used? Packed BCD, ASCII, Unpacked BCD. BCD Adjustment Instructions AAA. Example

CS61: Systems Programing and Machine Organization

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Chapter 7 Assembly Language

Instruction Set Architecture (ISA) Design. Classification Categories

Number Representation

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

CS201: Architecture and Assembly Language

SECTION C [short essay] [Not to exceed 120 words, Answer any SIX questions. Each question carries FOUR marks] 6 x 4=24 marks

This Unit: Floating Point Arithmetic. CIS 371 Computer Organization and Design. Readings. Floating Point (FP) Numbers

Binary Division. Decimal Division. Hardware for Binary Division. Simple 16-bit Divider Circuit

PROBLEMS (Cap. 4 - Istruzioni macchina)

Useful Number Systems

High level code and machine code

MIPS Assembler and Simulator

Z80 Instruction Set. Z80 Assembly Language

To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Informatica e Sistemi in Tempo Reale

Central Processing Unit Simulation Version v2.5 (July 2005) Charles André University Nice-Sophia Antipolis

Lecture 8: Binary Multiplication & Division

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

Parallelization: Binary Tree Traversal

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

EC 362 Problem Set #2

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

University of Toronto Faculty of Applied Science and Engineering

LC-3 Assembly Language

X86-64 Architecture Guide

İSTANBUL AYDIN UNIVERSITY

Data Storage 3.1. Foundations of Computer Science Cengage Learning

For a 64-bit system. I - Presentation Of The Shellcode

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

CS 61C: Great Ideas in Computer Architecture Finite State Machines. Machine Interpreta4on

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

B.Sc.(Computer Science) and. B.Sc.(IT) Effective From July 2011

This 3-digit ASCII string could also be calculated as n = (Data[2]-0x30) +10*((Data[1]-0x30)+10*(Data[0]-0x30));

Base Conversion written by Cathy Saxton

Two's Complement Adder/Subtractor Lab L03

Transcription:

CS 104 PRACTICE Midterm Exam 1 This is a full length practice midterm exam. If you want to take it at exam pace, give yourself 75 minutes to take the entire test. Just like the real exam, each question has a point value. There are 75 points in the exam, so that you can pace yourself to average 1 point per minute (some parts will be faster, some slower). Questions: 1. Numbers [10 points] 2. Binary Math [10 points] 3. UNIX Commands [10 points] 4. C Programming [15 points] 5. C Tracing [15 points] 6. ISA Concepts [10 points] 7. Asm Programming [15 points] 8. Asm Tracing [15 points] This is the solution set to the practice exam. The solutions appear in blue boxes. 1

Question 1: Numbers [10 pts] 1. Convert the decimal number -67 to 8-bit, signed 2 s complement binary: First we need to convert 67 to binary. 67 = 64 + 2 + 1 = 0100 0011. Next we need to flip the bits (1011 1100) and add 1, to get the answer: 1011 1101. 2. Convert the 8-bit signed 2 s complement hex number 0x3F to decimal: Easiest is to conver to binary: 0011 1111. Then convert to decimal: 32 + 16 + 8 + 4 + 2 + 1 = 63, which is the answer. 3. Convert the 16-bit signed 2 s complement binary number 1001 0001 1111 1010 to hex: Each group of 4 bits converts to 1 hex digit: 1001 = 9 0001 = 1 1111 = F 1010 = A So the answer is 0x91FA 2

4. Convert -35.75 to its hexidecimal representation in IEEE floating point format: First, we need to write down the binary representation of 35.75: 100011.11 Next, we need to put normalize it: 1.0001111 * 2 5 Now we can get the 3 pieces of the number we need: S: 1 (negative) E: 132 (127 + 5) = 1000 0100 M: 000 1111 0000 0000 0000 0000 Now we just put all the bits together: 1100 0010 0000 1111 0000 0000 0000 0000 and convert each group of 4 to a hex digit: 0xC20F0000 5. Convert the hexidecimal IEEE format floating point number 0x40200000 to decimal: First, convert the hex to binary: 0100 0000 0010 0000 0000 0000 0000 0000 Then pull out each of the 3 pieces: S: 0 (positive) E: 1000 0000 = 128. Taking 128-127 = 1 M: 010 0000 0000 0000 0000 0000 So we have 1.01 * 2 1 = 10.1 Then convert that to decimal to get the answer: 2.5 3

Question 2: Binary Math [10 pts] 1. Add the 8-bit 2 s complement numbers 1110 1001 + 1100 0010. State the result of the addition (as a binary value), as well as whether overflow occured if the number were treated as signed, and whether overflow occured if the numbers were treated as unsigned. Show you work. 1 1 1 1 1 0 1 0 0 1 1 1 0 0 0 0 1 0 1 0 1 0 1 0 1 1 Sum: 1010 1011 Signed Overflow: No (CI=CO=1 for the last bit) Unsigned Overflow: Yes (CO = 1) 2. What is 0110 1101 ^ 1100 1001? 1010 0100 4

3. Add the 8-bit 2 s complement numbers 0110 1111 + 0110 1010. State the result of the addition (as a binary value), as well as whether overflow occured if the number were treated as signed, and whether overflow occured if the numbers were treated as unsigned. Show you work. 1 1 1 1 1 0 1 1 0 1 1 1 1 0 1 1 0 1 0 1 0 1 1 0 1 1 0 0 1 Sum: 1101 1001 Signed Overflow: Yes (CI=1, but CO=0 for the last bit) Unsigned Overflow: No (CO = 0) 4. What is 1100 0011 >> 0000 0011 when the operands are treated as signed integers? 1111 1000 5. What is 1100 0011 >> 0000 0011 when the operands are treated as unsigned integers? 0001 1000 5

Question 3: UNIX Commands [10 pts] Match each of the following tasks with the UNIX program must suited to that task: A allfiles 1. Compile a C program E. gcc 2. Copy a file to a different computer M. scp 3. Edit a text file D. emacs 4. View the contents of a file (not to edit it) G. less 5. Show what files are in the current directory H. ls B cp C edit D emacs E gcc F gdb G less H ls I mkdir J nethack K newfldr L rm M scp N ssh O svn P textwzrd 6

Question 4: C Programming [15 pts] Given the following linked list node definition: struct ll_node { int data; struct ll_node * next; ; typedef struct ll_node node; Fill in the function below which finds the largest element in the list. If the list is empty, this function should return 0 (although, a non-empty list may contain only negative numbers, in which case, your code should function correctly). int findmax(node * head) { if (head == NULL) { return 0; if (head->next == NULL) { return head->data; int temp = findmax(head->next); if (head->data > temp) { return head->data; return temp; 7

Question 5: C Tracing [15 pts] What is the output when the following C program is run? #include <stdio.h> void f (int x, int * p) { *p = x; x = 33; int main(void) { int a = 4; int b = 2; int * p = &a; *p = 55; printf("1: a = %d, b=%d\n", a, b); f(a,&b); printf("2: a = %d, b=%d\n", a, b); int ** q = & p; *q = & b; *p = 42; printf("3: a = %d, b=%d\n", a, b); return 0; 1: a = 55, b=2 2: a = 55, b=55 3: a = 55, b=42 8

Question 6: ISA Concepts [10 pts] Briefly answer each of the following: 1. ISA stands for Instruction Set Architecture, but what does it mean? The ISA is the contract between the software and the hardware. It specifies how instructions are encoded, and what functional behavior they have. 2. Describe the defining characteristics of an accumulator-style ISA: An accumulator ISA has one specific register (the accumulator) which is implicitly one source and the destination of all operations. For example, an accumulator ISA s add instruction might look like add X which means accumulator = accumulator + X 3. What is an advantage of a 3-operand ISA over a 2-operand ISA? (Hint: you might give an example assembly sequence) 3-operand ISAs allow the compiler more flexibility in where the result is written: it does not have to implicitly be one of the source registers. This is especially useful when the source registers s values are needed for future computations. Consider the following 3-operand assembly sequence: add r1 = r2 + r3 sub r4 = r2 - r3 A 2-operand ISA would have required a register copy to preserve the value of either r2 or r3 for the second instruction, as one of them would need to be the destination of the first instruction. 9

Question 7: Asm Programming [15 pts] Write MIPS assembly for the following C function. int sumarray(int * ptr, int count) { int total = 0; while (count > 0) { total += *ptr; ptr ++; count--; return count;.globl sumarray.ent sumarray.text sumarray: addi $t0, $r0, 0 blez $a1, end loop: lw $t1, 0($a0) add $t0, $t0, $t1 addi $a0, $a0, 4 addi $a1, $a1, -1 bgtz $a1, loop end: addi $v0, $t0, 0 jr $ra.end sumarray 10

Question 8: Asm Tracing [15 pts] Trace the following MIPS assembly (note the code is split into two columsn to fit the page, but the top of the second column is sequentially after the bottom of the first): start: # 1000 lw $t0, 0($sp) li $t1, 3 mul $t0, $t0, $t1 a: # 1010 bne $a0, $a1, c b: # 1014 sw $a0, 0($sp) c: # 1018 li $a0, 42 li $a1, 5 sw $ra, 4($sp) jal xyz d: # 1028 lw $ra, 4($sp) e: # 102C jr $ra f: # 1030 li $v0, 10 syscall # exit xyz: # 1038 add $v0, $a0, $a1 addi $v0, $v0, 2 jr $ra The initial state (location/registers/memory) is given in the table below. As you execute the above assembly, whenever you encounter a label (i.e., immediately before you execute the instruction right after the label), write down the label s name, and the current register and memory values. You may not encounter all labels. The instruction addresses for each label are given in a comment on the same line as each label, if you find you need them. All values shown are in hex and you should also write hex values. Registers Memory Label $v0 $a0 $a1 $t0 $t1 $sp $ra FFE0 FFE4 start 0 1142 7FE5 FFFF 9999 FFE0 1234 3 42 a 0 1142 7FE5 9 3 FFE0 1234 3 42 c 0 1142 7FE5 9 3 FFE0 1234 3 42 xyz 0 42 5 9 3 FFE0 1028 3 1234 d 49 42 5 9 3 FFE0 1028 3 1234 e 49 42 5 9 3 FFE0 1234 3 1234 11