Computer Systems Organization V Fall 2009

Similar documents
Sample Exam I PAC II ANSWERS

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

CS , Fall 2001 Exam 1

CSE P 501 Compilers. x86 Lite for Compiler Writers Hal Perkins Autumn /25/ Hal Perkins & UW CSE J-1

UW CSE 351, Winter 2013 Midterm Exam

CSE351 Spring 2018, Midterm Exam April 27, 2018

CS , Spring 2004 Exam 1

Binghamton University. CS-220 Spring x86 Assembler. Computer Systems: Sections

CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009

CSC 8400: Computer Systems. Machine-Level Representation of Programs

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

CSE P 501 Exam 8/5/04 Sample Solution. 1. (10 points) Write a regular expression or regular expressions that generate the following sets of strings.

CS , Fall 2002 Exam 1

Basic Pentium Instructions. October 18

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

EECS 213 Fall 2007 Midterm Exam

The x86 Instruction Set

CMSC 313 Lecture 12 [draft] How C functions pass parameters

16.317: Microprocessor Systems Design I Fall 2014

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

CPS104 Recitation: Assembly Programming

complement) Multiply Unsigned: MUL (all operands are nonnegative) AX = BH * AL IMUL BH IMUL CX (DX,AX) = CX * AX Arithmetic MUL DWORD PTR [0x10]

CSE 582 Autumn 2002 Exam Sample Solution

Machine Programming 1: Introduction

15-213/18-243, Spring 2011 Exam 1

22 Assembly Language for Intel-Based Computers, 4th Edition. 3. Each edge is a transition from one state to another, caused by some input.

Practical Malware Analysis

SOEN228, Winter Revision 1.2 Date: October 25,

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

CSE 351 Midterm Exam

CS61, Fall 2012 Midterm Review Section

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

Computer Architecture and Assembly Language. Practical Session 3

CS241 Computer Organization Spring 2015 IA

Second Part of the Course

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

CSCI 2121 Computer Organization and Assembly Language PRACTICE QUESTION BANK

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

Name: CMSC 313 Fall 2001 Computer Organization & Assembly Language Programming Exam 1. Question Points I. /34 II. /30 III.

Towards the Hardware"

CAS CS Computer Systems Spring 2015 Solutions to Problem Set #2 (Intel Instructions) Due: Friday, March 20, 1:00 pm

Assembly I: Basic Operations. Jo, Heeseung

Subprograms: Arguments

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2016 Lecture 12

Page 1. Condition Codes CISC 360. Machine-Level Programming II: Control Flow Sep 17, Setting Condition Codes (cont.)

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

Computer Architecture and System Programming Laboratory. TA Session 3

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Faculty of Engineering Student Number:

Chapter 3 Machine-Level Programming II Control Flow

CISC 360. Machine-Level Programming II: Control Flow Sep 17, class06

Do not turn the page until 5:10.

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

COMPUTER ENGINEERING DEPARTMENT

Digital Forensics Lecture 3 - Reverse Engineering

Reverse Engineering Low Level Software. CS5375 Software Reverse Engineering Dr. Jaime C. Acosta

16.317: Microprocessor Systems Design I Spring 2015

It is possible to define a number using a character or multiple numbers (see instruction DB) by using a string.

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

Assembly Language: IA-32 Instructions

Process Layout and Function Calls

Program Exploitation Intro

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

CS241 Computer Organization Spring Addresses & Pointers

CS , Fall 2004 Exam 1

Outline. x86 Architecture

Defining and Using Simple Data Types

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

Step 1: Add the least significant digits together: produces with carry 1.

Machine-Level Programming II: Control Flow

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

Compiler construction. x86 architecture. This lecture. Lecture 6: Code generation for x86. x86: assembly for a real machine.

CS 3843 Final Exam Fall 2012

CSE 351 Midterm Exam Spring 2016 May 2, 2015

15-213, Fall 2007 Midterm Exam

ECOM Computer Organization and Assembly Language. Computer Engineering Department CHAPTER 7. Integer Arithmetic

Do not turn the page until 11:30.

16.317: Microprocessor Systems Design I Fall 2015

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

Overview REWARDS TIE HOWARD Summary CS 6V Data Structure Reverse Engineering. Zhiqiang Lin

CF Carry Flag SF Sign Flag ZF Zero Flag OF Overflow Flag. ! CF set if carry out from most significant bit. "Used to detect unsigned overflow

W4118: PC Hardware and x86. Junfeng Yang

Assembly Language: Function Calls" Goals of this Lecture"

Condition Codes. Lecture 4B Machine-Level Programming II: Control Flow. Setting Condition Codes (cont.) Setting Condition Codes (cont.

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

Do not turn the page until 12:30.

Basic Assembly Instructions

Condition Codes The course that gives CMU its Zip! Machine-Level Programming II Control Flow Sept. 13, 2001 Topics

Assembly Language Programming: Procedures. EECE416 uc. Charles Kim Howard University. Fall

Transcription:

Computer Systems Organization V22.0201 Fall 2009 Sample Midterm Exam ANSWERS 1. True/False. Circle the appropriate choice. (a) T (b) F At most one operand of an x86 assembly instruction can be an memory address At most one operand of an x86 assembly instruction can be a register. (c) F A program written directly in machine language (binary) would run faster than the same program written in assembly code and then assembled. (d) T The jne operation depends on the zero flag. (e) T In C, given the declaration int num[10], the use of the name num in the program corresponds to an address. (f) T C has no built-in boolean type, rather the value 0 is used to represent false and all other values represent true. (g) F EBP is a caller-saved register. (h) T In two s complement representation of binary numbers, a negative number will always have a 1 in the leftmost bit. (i) F In x86 assembly using the calling convention discussed in class, the first parameter to a procedure can be found at the address computed by adding the value of the ebp register and 12. (j) T The instruction pop ebp (or pop %ebp ) is equivalent to mov ebp,[esp] followed by add esp,4 (or mov (%esp),%ebp followed by add $4,%esp ) 2. Short Answer. Write the answers in the space provided. (a) What does the following C code print? int main() int *p = (int *) 100; char *c = (char *) p; p++; c++; printf("p = %d, c = %d\n", p, c); p = 104, c = 101 (b) What value ends up in EAX afer the following code is executed? #Intel Syntax mov eax,0xff xor eax,0xf0 shr eax,2 shl eax,4 #AT&T Syntax mov $0xff,%eax xor $0xf0,%eax shr $2,%eax shl $4,%eax xor is the bitwise exclusive-or operator. 48 (decimal) or 110000 (binary) or 0x30 (hex) 1

3. Write your answer on this page. Suppose you are writing x86 code that conforms to the calling convention discussed in class. Suppose also that you have already written the following code..intel syntax #AT&T syntax.globl foo.globl foo push ebp push %ebp mov ebp,esp mov %esp,%ebp mov ebx,10 mov $10,%ebx mov ecx,5 mov $5,%ecx mov edx,0 mov $0,%edx cmp ecx,0 cmp $0,%ecx imul ebx,[ebp+8] imul 8(%ebp),%ebx push ebx push %ebx call bar call bar add esp,4 add $4,%esp add edx,eax add %eax,%edx dec ecx dec %ecx mov eax,edx mov %edx,%eax pop ebp pop %ebp Your program doesn t work however, and you suspect that it has to do with your not saving registers. Mark the above code, indicating what instructions you would add to the code, and where, to save registers in a manner consistent with the calling convention. 2

The code, with the appropriate instructions inserted (shown underlined) would be the following:.intel syntax #AT&T syntax.globl foo.globl foo push ebp push %ebp mov ebp,esp mov %esp,%ebp push ebx push %ebx mov ebx,10 mov $10,%ebx mov ecx,5 mov $5,%ecx mov edx,0 mov $0,%edx cmp ecx,0 cmp $0,%ecx push ecx push %ecx push edx push %edx imul ebx,[ebp+8] imul 8(%ebp),%ebx push ebx push %ebx call bar call bar add esp,4 add $4,%esp pop edx pop %edx pop ecx pop %ecx add edx,eax add %eax,%edx dec ecx dec %ecx mov eax,edx mov %edx,%eax pop ebx pop %ebx pop ebp pop %ebp 4. Put your answers in the blue book. (a) Translate the following C code into x86 assembly code. Try to make it fairly efficient (e.g. avoiding imul and idiv). int foo(int x, int y) int i; //a local variable, not a global variable i = (x*8) + y; // computing urn i / 4; //integer division 3

.intel syntax.text.text.globl foo.globl foo push ebp push %ebp mov ebp,esp mov %esp,%ebp sub esp,4 sub $4,%esp #make room for i mov eax,[ebp+8] mov 8(%ebp),%eax #load x into eax shl eax,3 shl $3,%eax #muliply by 8 (shift) add eax,[ebp+12] add 12(%ebp),%eax #add y mov [ebp-4],eax mov %eax, -4(%ebp) #store into i mov eax,[ebp-4] mov -4(%ebp),%eax #load i (not necessary) shr eax,2 shr $2,%eax #divide by 4, leave in eax add esp,4 add $4,%esp #remove i pop ebp pop %ebp (b) Draw the state of the stack right after the assembly code corresponding to the statement that has the comment computing is executed. Label each item on the stack in a way that makes it clear what that item is and be sure to show the stack pointer and the base pointer. 5. Put your answers in the blue book. As you know from 102, a binary tree is a structure that looks like this: 4

(a) Define in C a type NODE that can serve as the node of a binary tree, where each node contains string called name and a integer called age. typedef struct node char name[80]; int age; struct node *left, *right; NODE; (b) Write a C procedure with the signature NODE *create_node(char *name, int age); that creates a node that has the specified name and age. This node will have no children yet. NODE *create_node(char *name, int age) NODE *n = (NODE *) malloc(sizeof(node)); int i; for(i=0;name[i]!=0;i++) n->name[i] = name[i]; n->name[i] = 0; n->age = age; n->left = NULL; n->right = NULL; urn n; (c) Write a C procedure with the signature void insert_left_child(node *n, NODE *root); that inserts the node n into the tree as the left child of the root node. The existing left child of the root should become the left child of n. In other words, the tree should change as follows. 5

void insert_left_child(node *n, NODE *root) n->left = root->left; root->left = n; 6. Put your answers in the blue book. (a) Write in x86 assembly a two instruction sequence that would place the value of 2 x in the EAX register, for any integer x between 0 and 31 (note: assume x is a constant, not a variable). mov eax,1 shl eax,x mov $1,%eax shl $x,%eax (b) Given a value y in the EAX register, write some assembly code that would place log 2 y in the ECX register (Hint: Consider the relationship between x and the result placed in the EAX register in the previous question). mov ecx,0 mov $0,%ecx cmp eax,1 comp $1,%eax #eax cannot be 0 je done je done shr eax,1 shr $1,%eax inc ecx inc %ecx 6