Programming Using C Homework 5

Similar documents
1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

Notes: The Marie Simulator

2.2 THE MARIE Instruction Set Architecture

Programming Using C Homework 4

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

ECE 30 Introduction to Computer Engineering

Computer Organization II CMSC 3833 Lecture 33

Introduction to Computer Science. Homework 1

The MARIE Architecture

CS311 Lecture: The Architecture of a Simple Computer

Computer Systems and Networks. ECPE 170 Jeff Shafer University of the Pacific. MARIE Simulator

Part 1 (70% of grade for homework 2): MIPS Programming: Simulating a simple computer

The CPU and Memory. How does a computer work? How does a computer interact with data? How are instructions performed? Recall schematic diagram:

Department of Computer and Mathematical Sciences. Lab 4: Introduction to MARIE

Arithmetic and Bitwise Operations on Binary Data

CSC201, SECTION 002, Fall 2000: Homework Assignment #2

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

Teaching London Computing

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

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

Goals of this Lecture

Binghamton University. CS-140 Fall Pippin

ECE2049 Homework #2 The MSP430 Architecture & Basic Digital IO (DUE Friday 9/8/17 at 4 pm in class)

Brock Wilcox CS470 Homework Assignment 2

Introduction to MiniSim A Simple von Neumann Machine

COMP1917 Computing 1 Written Exam Sample Questions

Course Administration

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Homework #3 CS2255 Fall 2012

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

LC-3 Assembly Language. (Textbook Chapter 7)"

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Add Subtract Multiply Divide

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

CIS 110: Introduction to Computer Programming

Lecture 4: Instruction Set Architecture

CSC 309/404 Section 901/910 Spring 2017 Midterm Exam Due: May 7 (Sun) 2015, 11:59 pm

gcc o driver std=c99 -Wall driver.c bigmesa.c

Syntax of LC-3 assembly: Language elements. Instructions

2010 Summer Answers [OS I]

Five classic components

The C Programming Language Guide for the Robot Course work Module

Introduction to Computer Science Midterm 3 Fall, Points

This simulated machine consists of four registers that will be represented in your software with four global variables.

Due Nov. 6 th, 23:59pm No Late Submissions Accepted

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Binary Representations and Arithmetic

Chapter 4. MARIE: An Introduction to a Simple Computer

CC411: Introduction To Microprocessors

Address Modes effective address

CC312: Computer Organization

CSE/EEE 230 Computer Organization and Assembly Language

Computer Systems Lecture 9

Grading: 3 pts each part. If answer is correct but uses more instructions, 1 pt off. Wrong answer 3pts off.

Representation of Information

CS10001: Programming & Data Structures. Dept. of Computer Science & Engineering

CS61C : Machine Structures

CHAPTER 3 Expressions, Functions, Output

1 The Simpletron Assembly Language

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

Lecture #6 Intro MIPS; Load & Store

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

ET355 Microprocessors Thursday 6:00 pm 10:20 pm

Introduction to Computer Science II (CSI 1101) Final Examination

The Assembly Language of the Boz 5

Lecture 03 Bits, Bytes and Data Types

Arrays, Pointers and Memory Management

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

CS 430 Computer Architecture. C/Assembler Arithmetic and Memory Access William J. Taffe. David Patterson

Microprocessors & Assembly Language Lab 1 (Introduction to 8086 Programming)

syscall takes a service ID (number) in $v0 Read integer $v0=5, after syscall, $v0 holds the integer read from keyboard

Sri Vidya College of Engineering & Technology

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

ME 461 C review Session Fall 2009 S. Keres

ECE331: Hardware Organization and Design

CSC201, SECTION 002, Fall 2000: Homework Assignment #3

11/22/1999 7pm - 9pm. Name: Login Name: Preceptor Name: Precept Number:

McGill University School of Computer Science COMP-202A Introduction to Computing 1

C Programming Language. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

EE 2700 Project 2 Microprocessor Design

ECE2049: Homework 1. Consider the following code to compute the average of your exam grades: #define NUM_EXAMS (3)

Computer Architecture /

Chapter 1 INTRODUCTION

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

2. MACHINE REPRESENTATION OF TYPICAL ARITHMETIC DATA FORMATS (NATURAL AND INTEGER NUMBERS).

Lesson 10..The switch Statement and char

Course PJL. Arithmetic Operations

Repetition Structures

Computer Internals ECE2036. Lecture 2. ECE2036 Computer Internals Spring / 12

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CS401 - Computer Architecture and Assembly Language Programming Glossary By

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Language Reference Manual

EE 361 University of Hawaii Fall

Topics. Structured Computer Organization. Assembly language. IJVM instruction set. Mic-1 simulator programming

Looping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include

Assignment 3: Distance COP3330 Fall 2017

Transcription:

Programming Using C Homework 5 1. In this problem you will simulate the execution of a computer s CPU. The computer that we will simulate has 32 bytes of addressable memory (each byte is uniquely addressed). The CPU of this machine consists of the ALU (Arithmetic and Logic Unit) that performs all operations, the Inst (Instruction) Register that holds the current instruction being executed, the PC (Program Counter) that keeps track of the location of the instruction to be executed, and the Accumulator that stores the results of the arithmetic calculations. Each memory location contains a value or an instruction to execute. If a memory location contains an instruction the first three bits contain the OPCode (operation code) and the remaining 5 bits contain the argument of the instruction. Since three bits are used to describe the instruction, our machine can execute eight unique instructions: Each of these instructions has a single argument that is 5 bits in length. These instructions, in detail, are: (a) Halt. Halt the execution of the computer. Execute no more instructions. This instruction ignores the argument. (b) JANZ. Jump if the Accumulator is not zero. Examine the accumulators value. If it is not zero, jump to the address given as the argument 1

(set the PC to that value). (c) StAM. Store the Accumulator in Memory. The argument of this command is a memory address. Make a copy of the value in the Accumulator and store it in this memory location. (d) LdAM. Load the Accumulator with Memory value. The argument of this command is a memory address. Make a copy of the value stored in this location and place it in the Accumulator. (e) AddM. Add Memory value to Accumulator. The argument of this command is a memory address. Make a copy of the value stored in this location and add it to the Accumulators value. (f) AddI. Add Immediate. The argument of this command is a value. Since the argument is only 5 bits long, the 3 bits missing in front should be considered to be 0 and this value should be added to the value currently in the Accumulator replacing the value that is currently there. (g) SubM. Subtract Memory value from Accumulator. The argument of this command is a memory address. The value in this location should be subtracted from the Accumulators value. (h) SubI. Subtract Immediate. The argument of this command is a value. Since the argument is only 5 bits long, the 3 bits missing in front should be considered to be 0 and this value should be subtracted from the value in the Accumulator. The result should be placed in the Accumulator. Each execution cycle of the CPU involves performing the following steps in the order given: (a) The value in the PC is examined to find the next instruction to execute. (b) This instruction is loaded into the Instruction Register. (c) The PC is incremented. 2

(d) The instruction is interpreted and its specified action(s) is performed. For example, suppose that the first eight bytes of memory contain the following instructions: Given this information in memory and starting execution at 00000, the following occurs: See if you can figure out what is in each memory position when the program ends. Your program should represent the memory as an array of null-terminated character strings of length 8. It should contain the following macros, global declarations and functions: #define MEM_WIDTH 8 #define MEM_SIZE 32 #define OPCODE_SIZE 3 int pc = 0; // program counter char ir[mem_width+1]; // instruction register char ac[mem_width+1]; //accumulator char memory[mem_size][mem_width+1]; //memory // sets the memory to some default content void set_memory(); 3

// reads the memory from keyboard void read_memory(); // prints the memory on screen void print_memory(); // converts an integer from decimal to binary, // represented as a null-terminated string // the result is always represented on MEM_WIDTH bits void dec2bin(int dec, char *bin); // converts an integer from binary, represented as a null-terminated string, // to decimal // the string can have any length (up to MEM_WIDTH), // depending on the position of the null character int bin2dec(char *bin); // executes the program in memory void execute(); The program should take one argument. If no argument is given it should print an informative message about the expected argument and terminate. If the argument is 0, the program uses the function set memory to set the memory to the content given below. If the argument is anything else, it should use read memory to read the memory from keyboard. If the user enters a byte 00000000 the program should stop reading input and assign 00000000 to the rest of the memory locations. The program then executes the program in memory, starting at adress 0, and then prints the content of the memory and the value of the acumulator after execution. 4

2. Consider the code of the following macro: // This macro calculates the SIGMA function, from bottom to top, // and places the result in sum. #define sigma(func, bottom, top, sum) \ do \ { \ int i; \ sum =0; \ for(i=bottom; i<=top; i++) \ sum+=func(i); \ } \ while (0) Write a program with the necessary functions and statements to calculate each of the following: (a) 100 i i=1 5

(b) (c) (d) 7 i= 3 25 i=20 15 i 3 i 2 i i=1 (e) 15 i=10 sin i Submission instructions: Submit 2 archived source code files on Sakai by July 16 11:59 PM. The files should be named cpu.c and sums.c and saved in a folder named homework5. Grading scale Comments: 5% Use of descriptive identifiers and symbolic constants: 5% Spacing and indentation: 5% Following submission instructions: 5% Problem1: 60% Problem2: 20% 6