The University of Nottingham



Similar documents
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

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

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

Typy danych. Data types: Literals:

Reduced Instruction Set Computer (RISC)

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

Introduction to MIPS Assembly Programming

Computer Systems Architecture

Computer Science 281 Binary and Hexadecimal Review

Number Representation

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

Instruction Set Architecture (ISA)

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

Chapter 5 Instructor's Manual

UNIVERSITY OF CALIFORNIA, DAVIS Department of Electrical and Computer Engineering. EEC180B Lab 7: MISP Processor Design Spring 1995

Q. Consider a dynamic instruction execution (an execution trace, in other words) that consists of repeats of code in this pattern:

Assembly Language Programming

Systems I: Computer Organization and Architecture

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

MIPS Assembly Code Layout

Instruction Set Architecture

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

HOMEWORK # 2 SOLUTIO

Chapter 7D The Java Virtual Machine

Solution for Homework 2

EE361: Digital Computer Organization Course Syllabus

LSN 2 Number Systems. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology

Lecture 2. Binary and Hexadecimal Numbers

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

Figure 1: Graphical example of a mergesort 1.

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

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

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

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

Let s put together a Manual Processor

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

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

CSI 333 Lecture 1 Number Systems

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

PROBLEMS #20,R0,R1 #$3A,R2,R4

MIPS Assembler and Simulator

CS352H: Computer Systems Architecture

Intel 8086 architecture

5 MIPS Assembly Language

CPU Organization and Assembly Language

MACHINE INSTRUCTIONS AND PROGRAMS

ECE 0142 Computer Organization. Lecture 3 Floating Point Representations

More MIPS: Recursion. Computer Science 104 Lecture 9

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

(Refer Slide Time: 00:01:16 min)

Design of Pipelined MIPS Processor. Sept. 24 & 26, 1997

Solution: start more than one instruction in the same clock cycle CPI < 1 (or IPC > 1, Instructions per Cycle) Two approaches:

Overview. CISC Developments. RISC Designs. CISC Designs. VAX: Addressing Modes. Digital VAX

CDA 3200 Digital Systems. Instructor: Dr. Janusz Zalewski Developed by: Dr. Dahai Guo Spring 2012

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

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

1 The Java Virtual Machine

2011, The McGraw-Hill Companies, Inc. Chapter 3

Pipeline Hazards. Structure hazard Data hazard. ComputerArchitecture_PipelineHazard1

Numbering Systems. InThisAppendix...

Solutions. Solution The values of the signals are as follows:

CPU Organisation and Operation

what operations can it perform? how does it perform them? on what kind of data? where are instructions and data stored?

EC 362 Problem Set #2

Review: MIPS Addressing Modes/Instruction Formats

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

Data Storage. Chapter 3. Objectives. 3-1 Data Types. Data Inside the Computer. After studying this chapter, students should be able to:

Lab Work 2. MIPS assembly and introduction to PCSpim

A SystemC Transaction Level Model for the MIPS R3000 Processor

Chapter 4 Register Transfer and Microoperations. Section 4.1 Register Transfer Language

Instruction Set Design

Levent EREN A-306 Office Phone: INTRODUCTION TO DIGITAL LOGIC

CS321. Introduction to Numerical Methods

1 Classical Universal Computer 3

CS101 Lecture 26: Low Level Programming. John Magee 30 July 2013 Some material copyright Jones and Bartlett. Overview/Questions

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

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

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

Data Storage 3.1. Foundations of Computer Science Cengage Learning

MICROPROCESSOR AND MICROCOMPUTER BASICS

Computers. Hardware. The Central Processing Unit (CPU) CMPT 125: Lecture 1: Understanding the Computer

Translating C code to MIPS

Computer Architecture Lecture 2: Instruction Set Principles (Appendix A) Chih Wei Liu 劉 志 尉 National Chiao Tung University

Execution Cycle. Pipelining. IF and ID Stages. Simple MIPS Instruction Formats

Lecture 8: Binary Multiplication & Division

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

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

Computer Organization and Components

Administrative Issues

Syscall 5. Erik Jonsson School of Engineering and Computer Science. The University of Texas at Dallas

WAR: Write After Read

Base Conversion written by Cathy Saxton

DNA Data and Program Representation. Alexandre David

INSTRUCTION LEVEL PARALLELISM PART VII: REORDER BUFFER

Chapter 4: Computer Codes

NUMBER SYSTEMS. 1.1 Introduction

CS:APP Chapter 4 Computer Architecture. Wrap-Up. William J. Taffe Plymouth State University. using the slides of

l C-Programming l A real computer language l Data Representation l Everything goes down to bits and bytes l Machine representation Language

1 Description of The Simpletron

CS201: Architecture and Assembly Language

Transcription:

The University of Nottingham School of Computer Science A Level 1 Module, Autumn Semester 2007-2008 Computer Systems Architecture (G51CSA) Time Allowed: TWO Hours Candidates must NOT start writing their answers until told to do so Answer QUESTION ONE and THREE other questions No calculators are permitted in this examination Dictionaries are not allowed with one exception. Those whose first language is not English may use a standard translation dictionary to translate between that language and English provided that neither language is the subject of this examination. Subject-specific translation dictionaries are not permitted. No electronic devices capable of storing and retrieving text, including electronic dictionaries, may be used. DO NOT turn examination paper over until instructed to do so G51CSA-E1 1 Turn Over

2 G51CSA-E1 Question 1: (Compulsory) The following questions are multiple choice. There is at least one correct choice but there may be several. For each of the questions list all the roman numerals corresponding to correct answers but none of the incorrect ones. Questions are marked as follows: no errors 5 points 1 error 3 points 2 errors 1 point 3 errors 0 points where an error is a correct answer left out, or an incorrect one included. no error 5 points 1 error 2 points 2 or more errors 0 points where an error is a correct answer left out or an incorrect one included. a. Which of the following statements is correct? (i) A word is always two bytes. (ii) On MIPS an aligned half-word can only start at even addresses. (iii) A Unicode character can be stored in 8 bits (iv) On a little endian processor, the least significant byte of a word can be found at the same address as the word (v) One byte can be represented by two hexadecimal digits On MIPS an aligned half-word can only start at even addresses. On a little endian processor, the least significant byte of a word can be found at the same address as the word One byte can be represented by two hexadecimal digits b. What are the typical features of the von Neumann architecture? (i) Programs cannot be modified while the processor is running (ii) Data and programs share the same address space (iii) The processor has a special register called the program counter (PC). G51CSA-E1 2 (5)

3 G51CSA-E1 (iv) Each machine instruction contains the address of the next instruction (v) Programs have to be written in assembly language. The processor has a special register called the program counter (PC). Data and programs share the same address space c. Consider the following program fragment in MIPS32 assembly code: li $s0, -1 srl $v0, $s0, 1 addiu $a0, $v0, 1 Which of the following statements are correct? (i) The fragment will occupy 128 bits in memory (ii) Register $a0 will contain the largest positive representable signed number after executing the fragment (iii) Register $a0 will contain the least negative representable signed number after executing the fragment (iv) The fragment will raise an overflow exception (v) Register $v0 will contain 1 after executing the fragment The fragment will occupy 128 bits in memory Register $a0 will contain the least negative representable signed number after executing the fragment d. Which of the following statements about 8-bit two s complement are correct? (i) The most significant bit has a weight of 2 6 (ii) The representable range is 128 to +127 (iii) The representable range is 127 to +128 (iv) To calculate x from x, we flip all the bits and add 1 (v) To calculate x from x, we flip all the bits and subtract 1 G51CSA-E1 3 Turn Over (5) (5)

4 G51CSA-E1 The representable range is 128 to +127 To calculate x from x, we flip all the bits and add 1 e. Which of the following statements about IEEE 754 single precision floating point numbers are correct? (i) The exponent is represented in two s complement. (ii) The word 0000 0001 16 is the largest denormalised number (iii) The word 8000 0000 16 represents the number 0 (iv) = 0 (v) The number 1.111 10 10 2 can be represented exactly The word 8000 0000 16 represents the number 0 (5) (5) G51CSA-E1 4

5 G51CSA-E1 Question 2 a. Implement a MIPS assembly fragment which, given the values of x and y in $s0 and $s1, calculates the expression x 3 + 6x 2 y + 12xy 2 + 8y 3, saving the result in $s2. You don t have to check for overflows. Hint: use high-school algebra to make it easier. (8) The hard way mul $t0, $s0, $s0 mul $t1, $s1, $s1 mul $a0, $t0, $s0 li $a0, 6 mul $a0, $a0, $t0 mul $a0, $a0, $s1 add $s2, $s2, $a0 li $a0, 12 mul $a0, $a0, $s0 mul $a0, $a0, $t1 add $s2, $s2, $a0 mul $a0, $t1, $s1 sll $a0, $a0, 3 add $s2, $s2, $a0 # $t0 = x^2 # $t1 = y^2 # $a0 = x^3 # $a0 = 6x^2y # $s2 = x^3 + 6x^2y # $a0 = 12xy^2 # $s2 = x^3 + 6x^2y + 12xy^2 # $a0 = y^3 # $a0 = 8y^3 # $s2 = x^3 + 6x^2y + 12xy^2 + 8y^3 The easy way: calculate (x + 2y) 3 sll $t0, $s1, 1 add $t0, $t0, $s0 mul $s2, $t0, $t0 mul $s2, $s2, $t0 # $t0 = 2y # $t0 = x + 2y # $s2 = (x + 2y)^2 # $s2 = (x + 2y)^3 b. Consider the following program in MIPS32 assembler: main: start:.text li $v0,5 syscall move $s0,$v0 li $s1,0 # read integer G51CSA-E1 5 Turn Over

6 G51CSA-E1 loop: test: out: j test andi $t0,$s0,1 xor $s1,$s1,$t0 srl $s0,$s0,1 bne $s0,$zero,loop move $a0,$s1 li $v0,1 syscall li $v0,10 syscall # print integer # exit (i) Simulate the program by hand for the inputs: 15, 64, 1. In each case, how many times does the program loop, and what will it output? (6) For 15 the program loops 4 times and then prints 0. For 64 the program loops 6 times and then prints 1. For 1 the program loops 32 times and then prints 0. (ii) Summarise the action the program performs in one sentence. (2) The program prints 1 if the binary representation of the input contains an odd number of 1s and 0 otherwise. c. Implement a loop in MIPS assembly which given a positive integer n in register $s0, calculates the nth fibonacci number, i.e. the nth element in the series 1, 1, 2, 3, 8, 11... - each fibonacci number is the sum of the two previous ones. For example, if $s0 contains 5 then the code should store 8 in $s1. (9) Three points for style: naming, layout, comments &c.; six points for functionality. Does it work for 0? li $s1, 0 li $t1, 1 li $t0, 0 j fib_cond fib_loop: add $t2, $s1, $t1 move $t1, $s1 move $s1, $t2 addi $t0, $t0, 1 G51CSA-E1 6

7 G51CSA-E1 fib_cond: blt $t0, $s0, sum_loop G51CSA-E1 7 Turn Over

8 G51CSA-E1 Question 3 a. Calculate the 8-bit two s complement representation of the following decimal numbers: a = 114 b = 17 c = 73 (3) a = 114 10 = 0111 0010 2 b = 17 10 = 0001 0001 2 c = 73 10 = 0100 1001 2 = 1011 0111 2 b. Showing details of your working, calculate using signed 8-bit two s complement the following sums: a + b b + c a + c Which case results in an overflow? (4) 0 1 1 1 0 0 1 0 a = 114 + 0 0 0 1 0 0 0 1 b = 17 1 0 0 0 0 0 1 1 a + b = 131 = 125 Overflow! 0 0 0 1 0 0 0 1 b = 17 + 1 0 1 1 0 1 1 1 c = 73 1 1 0 0 1 0 0 0 b + c = 56 0 1 1 1 0 0 1 0 a = 114 + 1 0 1 1 0 1 1 1 c = 73 0 0 1 0 1 0 0 1 b + c = 41 c. Calculate in detail using signed 8 bit arithmetic, the 16 bit result of a b and b c. Give the final product in both decimal and binary. Take care with sign extension. (7) 0 1 1 1 0 0 1 0 a = 114 0 0 0 1 0 0 0 1 b = 17 0 1 1 1 0 0 1 0 + 0 1 1 1 0 0 1 0 0 0 0 0 0 1 1 1 1 0 0 1 0 0 1 0 a b = 1938 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 c = 73 0 0 0 1 0 0 0 1 b = 17 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 + 1 1 1 1 1 0 1 1 0 1 1 1 Negate 1 1 1 1 1 0 1 1 0 0 1 0 0 1 1 1 b c = 1241 0 0 0 0 0 1 0 0 1 1 0 1 1 0 0 1 b c = 1241 G51CSA-E1 8

9 G51CSA-E1 d. Calculate in detail using unsigned 8-bit arithmetic, the 8 bit quotient and remainder of a b. (4) 0 0 0 0 0 1 1 0 a b = 6 1 0 0 0 1 0 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 0 0 1 0 1 1 1 0 1 0 0 0 1 1 0 0 0 0 1 1 0 0 Remainder: 12 1 0 0 1 0 0 e. If we perform addu $s0, $s1, $s2 the MIPS processor will ignore overflows. Implement in MIPS32 assembly a test following the above instruction which jumps to a label named overflow, if an overflow has indeed occured. (7) xor $t0, $s1, $s2 blt $t0, $zero, no overflow # sign($s1)!= sign($s2) xor $t0, $s0, $s1 blt $t0, $zero, overflow # sign($s0)!= sign($s1) no overflow: continue normal processing overflow: error handler G51CSA-E1 9 Turn Over

10 G51CSA-E1 Question 4 a. The following questions are about the binary representation of single-precision (32-bit) IEEE 754 floating-point numbers? (10 total) (i) Show, using a diagram like the one below, how many bits are used for each of the sign, exponent and mantissa fields, and their ordering in a 32-bit word. field field field x bits y bits z bits (3) sign exponent mantissa 1 bit 8 bits 23 bits (ii) How is the exponent encoded? How would you represent an exponent of 42? (2) Exponent in excess-127 encoding. An exponent of 42 is represented by 42 + 127 = 169 = 1010 1001 2. (iii) In general, floating-point numbers are normalised before being encoded. What does this mean for the most significant bit of the mantissa? (1) The MSB is always 1, so we do not need to store it explicitly for normalised numbers. (iv) Give the 32-bit IEEE 754 representation of the following floating point numbers as 8 hexadecimal digits: 1.125, 385 (4) 1.125 = 1.0010... 2 2 0 Exponent: 0 + 127 = 0111 1111 2 ; Drop MSB of mantissa: 0010... 2 So 1.125 = 1 01111111 001 2... and 20 trailing 0s... = BF900000 16 385 = 1 1000 0001 2 = 1.1000 0001... 2 2 8 Exponent: 8 + 127 = 1000 0111 2 Drop MSB of mantissa: 1000 0001... 2 So 385 = 0 10000111 10000001000 2... and 12 trailing 0s... = 43C08000 16 b. Suppose there exists a 12-bit IEEE 754 floating point format, with 1 sign bit, 6 exponent bits, and 5 mantissa bits. (15 total) (i) How would be represented in this 12-bit format? And the smallest positive normalised number? Give the value in G51CSA-E1 10

11 G51CSA-E1 decimal of the second number, and show both either as 12 bits or as 3 hexadecimal digits. (4) Value Binary Hex 1 111111 00000 2 FE0 16 2 30 0 000001 00000 2 020 16 Two points for each representation; one point for 2 30. (ii) Give the nearest representation n of 5.612 in this format. (3) 0.612. 1.224 1 0.448 0 0.896 0 1.692 1 1.284 1 5.612 10 = 101.10011... 2 101.101 2 = 1.01101 2 2 2 Excess-31 exponent is 100001 2, so 0 100001 01101 2. Drop MSB of mantissa: 01101 2 One mark for evidence of working out, one for rounding to the nearest answer and one for giving the full 12 bits. (iii) What is the actual value of n? Hence, work out its relative error r, to 3 significant digits. You may use the fact that a/5.612 a 0.1782. (2) 101.101 2 = 5 + 1/2 + 1/8 = 5.625 Relative error r = (5.625 5.612)/5.612 0.013 0.1782 = 13 10 3 1782 10 4 = 23166 10 7 2.32 10 3 (iv) Calculate n 2 using binary floating point multiplication. Show rounding, normalisation and where you might check for overflow. Give the result as a 12-bit IEEE 754 number. (6) 1.0 1 1 0 1 2 2 1.0 1 1 0 1 2 2 1.0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 + 1 0 1 1 0 1 Round 1.1 1 1 1 1 0 1 0 0 1 2 4 Result 1.1 1 1 1 1 2 4 No normalisation necessary; 6-bit excess-31 exponent can accommodate 4 = 100011 2, so no overflow. Result: 0 100011 11111 2 = 11111.1 2 = 31.5 10 G51CSA-E1 11 Turn Over

12 G51CSA-E1 Question 5 a. Following standard MIPS procedure calling conventions, implement a procedure strrchr (string reversed character) which receives the starting address of a NUL-terminated string in $a0, an ASCII character code in $a1, and returns the position of the last occurrence of the character in the string. If the character is NUL, you are to return the position of the terminating byte. If the character cannot be found, return 1. The first character of the string is position 0. (13) strrchr(str, chr) r = -1; i = 0; while(true) if(str[i] == chr) r = i; if(str[i] == \0 ) break; i++; return r; strrchr: # $a0: haystack starting address, $a1: needle li $v0, -1 move $t0, $zero j strrchr_load strrchr_loop: addi $t0, $t0, 1 strrchr_load: lbu $t0, 0($a0) addi $a0, $a0, 1 bne $t0, $a1, strrchr_skip move $v0, $t0 strrchr_skip: bne $t0, $zero, strrchr_loop j $ra b. The following program reads in a string from the user and prints out a number, but is missing some code in the middle: G51CSA-E1 12

13 G51CSA-E1.data buffer:.space 1024.text.globl main main: la $a0, buffer li $a1, 1024 # size of buffer li $v0, 8 # read_string syscall # insert your code here li $v0, 1 syscall li $v0, 10 syscall # print_int/$a0 # exit Provide the missing code in the above program, calling the strrchr function from the previous part, such that it calculates and prints the length of the user input to the console. (4) la $a0, buffer move $a1, $zero jal strrchr move $a0, $v0 c. Describe the procedure calling conventions for MIPS32: (8 total) (i) How are arguments passed? (2) First four in registers $a0 $a3, with the remaining pushed onto the stack. (ii) How are results returned? (1) In registers $v0 and $v1. (iii) Which of the user registers (ignoring $at, $k0 and $k1) have to be saved by the caller? (Assuming the caller needs them preserved.) Which ones are always saved by the callee? (4) Caller: $t0 $t9, $a0 $a3, $v0 and $v1. Callee: $s0 $s7, $sp, $fp and $ra. (iv) Where are local variables stored? (1) On the stack. G51CSA-E1 13 Turn Over

14 G51CSA-E1 Question 6 a. How does the technique of pipelining increase performance? Explain the increased instruction throughput, compared with a multicycle non-pipelined processor. Does pipelining reduce the execution time for individual instructions? Why? (4) Pipelining splits the execution of instructions into several simpler stages, each of which is guaranteed to complete within one clock cycle. Instructions are fed through the pipeline overlapped, increasing the total throughput. Each instruction will still take several cycles to pass through the pipeline completely, so the individual instruction execution time is not reduced. b. Below is an idealised schematic for the 5-stage MIPS pipeline. pgflastimage What do each of the abbreviations IF/ID/EX/MEM/WB stand for? Give a short description of what happens at each stage. (5) IF Instruction Fetch machine code retrieved from memory ID Instruction Decode set control signals, load source registers EX Execute Perform ALU operation MEM Memory Read/write from/to memory WB Write-Back Result written back into register file c. Give four features of MIPS instruction set architecture which make it especially suitable for a pipelined implementation. (3) Fixed width 32-bit instructions simplifies instruction fetch Only three instruction formats; with operand fields in the same positions, operands can be fetched while the instruction is being decoded no need for a separate register read stage Memory access only with load/store instructions, which means the register + offset address can be calculated during the execute stage by the ALU avoids structural hazards G51CSA-E1 14

15 G51CSA-E1 Multi-cycle instructions such as mul write to dedicated registers, so it doesn t need to finish in the one cycle imposed by the EX stage Memory accesses must be aligned, so the data for each load/store can be transferred in one cycle (Not taught) Branch delay slot / delayed branches compensates for the cost of flushing the pipeline d. What is a data hazard? Name and describe a hardware technique that overcomes most data hazards. What happens during the execution of the following sequence? lw $t0, 0($a0) add $s0, $s0, $t0 (4) Exercise 8. e. What is a control hazard? How can we avoid stalling the pipeline some of the time? What happens if this technique goes wrong?(4) Exercise 8 f. Identify the data hazards in the following code fragment and reorder the instructions to avoid any pipeline stalls. lw $t0, ($sp) lw $t1, ($t0) addi $t1, $t1, 4 lw $t2, ($t0) add $t3, $t2, $t1 sw $t3, 4($sp) sw $zero, ($sp) (5) Exercise 8 G51CSA-E1 15 END