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

Similar documents
16.317: Microprocessor Systems Design I Fall 2015

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

16.317: Microprocessor Systems Design I Spring 2015

16.317: Microprocessor Systems Design I Fall 2013

16.317: Microprocessor Systems Design I Fall 2014

Procedure-Calling Conventions October 30

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

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 10. Advanced Procedures

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

CSCI 334: Principles of Programming Languages. Computer Architecture (a really really fast introduction) Lecture 11: Control Structures II

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

Program Exploitation Intro

Computer Systems Lecture 9

Subprograms: Arguments

EECE.3170: Microprocessor Systems Design I Summer 2017

Subprograms: Local Variables

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

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

/ 28 HLL assembly Q4: Conditional instructions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

SOEN228, Winter Revision 1.2 Date: October 25,

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

CS61 Section Solutions 3

An Elegant Weapon for a More Civilized Age

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

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

Basic Assembly Instructions

Assignment 11: functions, calling conventions, and the stack

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

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

The x86 Architecture

The x86 Instruction Set

CSC 2400: Computer Systems. Using the Stack for Function Calls

Microprocessors ( ) Fall 2010/2011 Lecture Notes # 15. Stack Operations. 10 top

Computer Systems Organization V Fall 2009

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.

CPS104 Recitation: Assembly Programming

Assembly Language: Function Calls" Goals of this Lecture"

CSC 8400: Computer Systems. Using the Stack for Function Calls

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

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

Towards the Hardware"

printf("this program adds the value 10 to a given integer number.\n\n");

An Introduction to x86 ASM

COMPUTER ENGINEERING DEPARTMENT

Assembler Programming. Lecture 10

Inline Assembler. Willi-Hans Steeb and Yorick Hardy. International School for Scientific Computing

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

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

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

Assembly Language Lab # 9

CSC 2400: Computer Systems. Using the Stack for Function Calls

Second Part of the Course

Machine-level Programming (3)

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 8. Conditional Processing

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

UW CSE 351, Winter 2013 Midterm Exam

Assembly Language: Function Calls

Practical Malware Analysis

COMPUTER ENGINEERING DEPARTMENT

Computer Architecture and Assembly Language. Practical Session 3

Assembly Language: Function Calls" Goals of this Lecture"

Marking Scheme. Examination Paper. Module: Microprocessors (630313)

Lab 4: Basic Instructions and Addressing Modes

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

Introduction to Reverse Engineering. Alan Padilla, Ricardo Alanis, Stephen Ballenger, Luke Castro, Jake Rawlins

Marking Scheme. Examination Paper. Module: Microprocessors (630313)

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Sample Exam I PAC II ANSWERS

Module 3 Instruction Set Architecture (ISA)

Lab 3. The Art of Assembly Language (II)

Millions of instructions per second [MIPS] executed by a single chip microprocessor

Function Call Convention

Lecture 10 Return-oriented programming. Stephen Checkoway University of Illinois at Chicago Based on slides by Bailey, Brumley, and Miller

CPSC W Term 2 Problem Set #3 - Solution

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

mith College Computer Science CSC231 Assembly Week #11 Fall 2017 Dominique Thiébaut

Process Layout and Function Calls

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

238P: Operating Systems. Lecture 3: Calling conventions. Anton Burtsev October, 2018

Scott M. Lewandowski CS295-2: Advanced Topics in Debugging September 21, 1998

15-213/18-243, Summer 2011 Exam 1 Tuesday, June 28, 2011

Computer Science Final Examination Wednesday December 13 th 2006

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

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

Assembly Language for Intel-Based Computers, 4 th Edition. Lecture 25: Interface With High-Level Language

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

UNIVERSITY OF CALIFORNIA, RIVERSIDE

Review Questions. 1 The DRAM problem [5 points] Suggest a solution. 2 Big versus Little Endian Addressing [5 points]

Lecture 4 CIS 341: COMPILERS

Systems I. Machine-Level Programming V: Procedures

Chapter 3: Addressing Modes

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Intel assembly language using gcc

6/20/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

Assembly III: Procedures. Jo, Heeseung

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

Transcription:

1. (40 points) Write the following subroutine in x86 assembly: Recall that: int f(int v1, int v2, int v3) { int x = v1 + v2; urn (x + v3) * (x v3); Subroutine arguments are passed on the stack, and can be accessed within the body of the subroutine starting at address EBP+8. At the start of each subroutine: i. Save EBP on the stack ii. iii. iv. Copy the current value of the stack pointer (ESP) to EBP Create space within the stack for each local variable by subtracting the appropriate value from ESP. For example, if your function uses four integer local variables, each of which contains four bytes, subtract 16 from ESP. Local variables can then be accessed starting at the address EBP-4. Save any registers the function uses other than EAX, ECX, and EDX. A subroutine s urn value is typically stored in EAX. See Lectures 14 and 16-18 for more details on subroutines, the x86 architecture, and the conversion from high-level concepts to low-level assembly. Solution: Solution is shown on the next page; note that many different solutions are possible. The key points are: Setting up the stack frame appropriately (save base pointer; point base pointer to appropriate location; create space for local variable(s); save any overwritten registers except eax). Adding v1 + v2 while appropriately accessing different memory locations (only one memory operand per instruction; accessing arguments at right addresses relative to ebp) Computing urn value while appropriately accessing different memory locations Cleaning up stack frame (restoring saved registers; clearing space for local variable(s); restoring base pointer)

f PROC ; Start of function f sub esp, 4 ; Create space on the ; stack for x push ebx ; Save ebx on the stack push edx ; Save edx on the stack mov add mov ebx, DWORD PTR 8[ebp] ; ebx = v1 ebx, DWORD PTR 12[ebp] ; ebx = v1 + v2 DWORD PTR -4[ebp], ebx ; x = ebx = v1 + v2 mov eax, ebx ; eax = ebx = x add sub eax, DWORD PTR 16[ebp] ; eax = eax + v3 = x + v3 ebx, DWORD PTR 16[ebp] ; ebx = ebx v3 = x v3 imul ebx ; (edx,eax) = eax * ebx ; = (x + v3) * (x v3) pop edx ; Restore edx mov esp, ebp ; Clear x f ENDP 2

2. (60 points) Write the following subroutine in x86 assembly: int fib(int n) Given a single integer argument, n, urn the nth value of the Fibonacci sequence a sequence in which each value is the sum of the previous two values. The first 15 values are shown below note that the first value is urned if n is 0, not 1. n 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 fib(n) 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 Solution: How you implement the low-level code for this version of the Fibonacci function depends on the algorithm you use. What follows is both C code and assembly for the algorithm implemented either with or without recursion. int fib(int n) { int i; int first, sec; int cur; // FIBONACCI WITHOUT RECURSION // Loop index // Two previous Fibonacci values // Value from current iteration // For n == 0 or n == 1, fib(n) == n if (n <= 1) urn n; // Use loop to calculate fib(n)--at each step, // current value is sum of previous two values else { first = 0; sec = 1; for (i = 2; i <= n; i++) { cur = first + sec; first = sec; sec = cur; urn cur; 3

fib PROC ; Start of subroutine sub esp, 8 ; Create space for first, ; sec (cur, if needed, ; will be in eax) push ebx ; Save ebx and ecx (both push ecx ; (overwritten in fn) ; CODE FOR: if (n <= 1) urn n cmp DWORD PTR 8[ebp], 1 ; Compare n to 1 jg L1 ; If n isn t <= 1, jump ; to else case mov eax, DWORD PTR 8[ebp] ; eax = n (eax holds ; urn value) jmp L3 ; Jump to end of function ; CODE FOR: first = 0; sec = 1 L1: mov DWORD PTR -4[ebp], 0 ; first = 0 mov DWORD PTR -8[ebp], 1 ; sec = 1 ; CODE FOR: loop initialization ; Note that the loop will execute n 1 iterations, so we ; can initialize ECX to n - 1 and use loop instructions mov ecx, DWORD PTR 8[ebp] ; cx = n dec ecx ; cx = cx 1 = n 1 ; CODE FOR: cur = first + sec; first = sec; sec = cur L2: mov eax, DWORD PTR -4[ebp] ; cur = eax = first add eax, DWORD PTR -8[ebp] ; cur = first + sec mov ebx, DWORD PTR -8[ebp] ; ebx = sec mov DWORD PTR -4[ebp], ebx ; first = ebx = sec mov DWORD PTR -8[ebp], eax ; sec = eax = cur ; CODE FOR: decrement loop counter & go to start of loop loop L2 ; CLEANUP (NOTE: No additional code needed for urn cur ; in else case, since cur is already stored in eax) L3: pop ecx ; Restore ecx mov esp, ebp ; Clear first, sec fib ENDP 4

int fib(int n) { // FIBONACCI WITH RECURSION // For n == 0 or n == 1, fib(n) == n if (n <= 1) urn n; // Otherwise, value is sum of two previous steps else urn fib(n-1) + fib(n-2); fib PROC ; Start of subroutine push ebx ; Save ebx (overwritten ; in function) ; CODE FOR: if (n <= 1) urn n cmp DWORD PTR 8[ebp], 1 ; Compare n to 1 jg L1 ; If n isn t <= 1, jump ; to else case mov eax, DWORD PTR 8[ebp] ; eax = n (eax holds ; urn value) jmp L2 ; Jump to end of function ; CODE FOR: calling fib(n-1) L1: mov ebx, DWORD PTR 8[ebp] ; Copy n to ebx dec ebx ; ebx = n 1 push ebx ; Push n 1 to pass it ; as argument call fib ; Call fib(n-1) ; Return value in eax ; CODE FOR: calling fib(n-2) ; NOTE: We can take advantage of the fact that n-1 is still ; on the stack--decrement that value, and we ll have the ; value n-2 to pass to our next function call mov ebx, eax ; ebx = eax = fib(n-1) dec DWORD PTR [esp] ; Value at top of stack = ; (n-1) 1 = n-2 call fib ; Call fib(n-2) ; Return value in eax ; CODE FOR: urn fib(n-1) + fib(n-2) add eax, ebx ; eax = fib(n-1)+fib(n-2) ; CLEANUP L2: add esp, 4 ; Clear argument passed to ; fib(n-2) fib ENDP 5