CPSC W Term 2 Problem Set #3 - Solution

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

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

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

You may work with a partner on this quiz; both of you must submit your answers.

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

Assembly Language: Function Calls" Goals of this Lecture"

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

Machine-level Programming (3)

Assembly Language: Function Calls

Assembly Language: Function Calls" Goals of this Lecture"

COMP 210 Example Question Exam 2 (Solutions at the bottom)

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

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

System Programming and Computer Architecture (Fall 2009)

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

CS213. Machine-Level Programming III: Procedures

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

CPS104 Recitation: Assembly Programming

CIT Week13 Lecture

Sungkyunkwan University

Machine-Level Programming III: Procedures

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

SYSTEMS PROGRAMMING AND COMPUTER ARCHITECTURE Assignment 5: Assembly and C

Introduction to Computer Systems. Exam 1. February 22, This is an open-book exam. Notes are permitted, but not computers.

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

X86 Stack Calling Function POV

Introduction to Computer Systems. Exam 1. February 22, Model Solution fp

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

CS241 Computer Organization Spring 2015 IA

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

CPEG421/621 Tutorial

Sungkyunkwan University

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

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

Process Layout and Function Calls

CS241 Computer Organization Spring Loops & Arrays

Machine-Level Programming II: Control and Arithmetic

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

IA32 Stack. Stack BoDom. Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address

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

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element

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

Instruction Set Architecture

X86 Assembly -Procedure II:1

An Elegant Weapon for a More Civilized Age

Machine Programming 3: Procedures

Machine-Level Programming II: Arithmetic & Control. Complete Memory Addressing Modes

Lab 10: Introduction to x86 Assembly

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Data Representa/ons: IA32 + x86-64

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

Register Allocation, iii. Bringing in functions & using spilling & coalescing

Instructor: Alvin R. Lebeck

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

Assignment 11: functions, calling conventions, and the stack

Machine Level Programming II: Arithmetic &Control

Machine Program: Procedure. Zhaoguo Wang

IA32 Stack. Lecture 5 Machine-Level Programming III: Procedures. IA32 Stack Popping. IA32 Stack Pushing. Topics. Pushing. Popping

Systems I. Machine-Level Programming V: Procedures

THEORY OF COMPILATION

IA32 Stack The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, IA32 Stack Popping. IA32 Stack Pushing

Control flow. Condition codes Conditional and unconditional jumps Loops Switch statements

Page 1. IA32 Stack CISC 360. Machine-Level Programming III: Procedures Sept. 22, IA32 Stack Popping Stack Bottom. IA32 Stack Pushing

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

Machine-Level Programming II: Arithmetic & Control /18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012

Machine- Level Programming III: Switch Statements and IA32 Procedures

Giving credit where credit is due

CS61, Fall 2012 Midterm Review Section

Process Layout, Function Calls, and the Heap

Machine- Level Programming III: Switch Statements and IA32 Procedures

Compiler Construction D7011E

The Hardware/Software Interface CSE351 Spring 2013

EECS 213 Fall 2007 Midterm Exam

x86 assembly CS449 Fall 2017

Code Generation. Lecture 30

CS 3843 Final Exam Fall 2012

CS61 Section Solutions 3

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

Final exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Dec 20, Student's name: Student ID:

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Credits to Randy Bryant & Dave O Hallaron

Instruction Set Architecture

Instruction Set Architecture

Intel assembly language using gcc

University of Washington

This is a medical robot, guided by a skilled surgeon and designed to get to places doctors are unable to reach without opening a pacent up.

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

CS642: Computer Security

CS241 Computer Organization Spring Addresses & Pointers

Assembly Language Programming - III

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

Machine Programming 2: Control flow

hnp://

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

Credits and Disclaimers

Transcription:

1. (a) int gcd(int a, int b) { if (a == b) urn a; else if (a > b) urn gcd(a - b, b); else urn gcd(a, b - a); CPSC 313 06W Term 2 Problem Set #3 - Solution.file "gcdrec.c".globl gcd.type gcd, @function gcd: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jne.l9 jmp.l3.l11: subl %eax, %edx cmpl %eax, %edx je.l3.l9: cmpl %eax, %edx.p2align 4,,5 jg.l11 subl %edx, %eax cmpl %eax, %edx jne.l9.l3: popl %ebp.p2align 4,,4.size gcd,.-gcd.ident "GCC: (GNU) 4.1.0 (SUSE Linux)".section.note.GNU-stack,"",@progbits 1

int gcd(int a, int b) { for (; a!= b; a > b? (a -= b) : (b -= a)) ; urn a;.file "gcdfor.c".globl gcd.type gcd, @function gcd: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax movl 12(%ebp), %edx cmpl %eax, %edx jne.l8 jmp.l2.l11: subl %edx, %eax je.l2.l8:.p2align 4,,5 jg.l11 subl %eax, %edx jne.l8.l2: popl %ebp.p2align 4,,4.size gcd,.-gcd.ident "GCC: (GNU) 4.1.0 (SUSE Linux)".section.note.GNU-stack,"",@progbits 2

int gcd(int a, int b) { while (a!= b) { if (a > b) { a -= b; else { b -= a; urn a;.file "gcdwhile.c".globl gcd.type gcd, @function gcd: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax movl 12(%ebp), %edx cmpl %eax, %edx jne.l8 jmp.l2.l11: subl %edx, %eax je.l2.l8:.p2align 4,,5 jg.l11 subl %eax, %edx jne.l8.l2: popl %ebp.p2align 4,,4.size gcd,.-gcd.ident "GCC: (GNU) 4.1.0 (SUSE Linux)".section.note.GNU-stack,"",@progbits 3

int gcd(int a, int b) { do { if (a > b) { a -= b; else if (b > a) { b -= a; while (a!= b); urn a;.file "gcddo.c".globl gcd.type gcd, @function gcd: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax movl 12(%ebp), %edx jmp.l3.l14: subl %edx, %eax.l6: je.l13.l3: jg.l14.p2align 4,,5 jge.l6 subl %eax, %edx.p2align 4,,5 jne.l3.l13: popl %ebp.p2align 4,,6.size gcd,.-gcd.ident "GCC: (GNU) 4.1.0 (SUSE Linux)".section.note.GNU-stack,"",@progbits 4

(b) In my solution, the recursive, for and while versions generate essentially identical code. The do version is somewhat different, but still uses the same number of registers, and (within one at least) the same number of branches. 2..file "isfib.c".globl isfib.type isfib, @function isfib: pushl %ebp movl %esp, %ebp xorl %eax, %eax movl 8(%ebp), %ecx cmpl $100, %ecx ja.l2 jmp *.L4(,%ecx,4).section.rodata.align 4.L4: 5

6

.L3:.L2: movl $1, %eax popl %ebp.size isfib,.-isfib.ident "GCC: (GNU) 4.1.0 (SUSE Linux)".section.note.GNU-stack,"",@progbits 7

Test program: #include <stdio.h> extern int isfib(int n); struct problem { int n; int ans; problems[] = { -23222, 0, -1, 0, 0, 0, 1, 1, 2, 1, 3, 1, 5, 1, 8, 1, 13, 1, 21, 1, 34, 1, 55, 1, 89, 1, 100, 0, 454243, 0, ; int main(int c, char **v) { struct problem *p, *limit = &problems[sizeof(problems) / sizeof(struct problem)]; for (p = problems; p < limit; ++p) { int ans = isfib(p->n); if (ans!= p->ans) { printf("isfib failed on %d, expected %d, but found %d\n", p->n, p->ans, ans); else { printf("isfib correct on %d\n", p->n); 8

3. (a).file "power.c".globl power.type power, @function power: pushl %ebp # prologue movl %esp, %ebp pushl %ebx # save callee save %ebx subl $4, %esp # allocate space for local variable movl 8(%ebp), %eax # np in %eax movl 12(%ebp), %ebx # base in %ebx movl (%eax), %eax # *np in %eax testl %eax, %eax # check for 0 jne.l2 # if not branch around movl $1, -8(%ebp) # n = 1 jmp.l3 # jump to epilogue.l2: subl $1, %eax # compute *np - 1 movl %eax, -8(%ebp) # n = *np - 1 pushl %ebx # push second arg (base) first leal -8(%ebp), %eax # compute &n pushl %eax # push first arg (&n) last call power # make the call addl $8, %esp # pop the arguments imull %ebx, %eax # multiply by base movl %eax, -8(%ebp) # set n.l3: movl -8(%ebp), %eax # urn n addl $4, %esp # deallocate space for n popl %ebx # restore callee save reg %ebx popl %ebp # epilogue.size power,.-power (b) There are two options here. One is to keep track of the stack growth and adjust the offsets to find the arguments to power and its local variable (n) each time the size of the stack changes. The other is to allocate all the space necessary all at once and then the offsets of everything are fixed just like they were from the frame pointer, but they are bigger since esp points to the other end of the stack frame. 9

The first one:.file "power.c".globl power.type power, @function power: # base offset of 1st argument is 4 pushl %ebx # -4 subl $4, %esp # -4 movl 12(%esp), %eax movl 16(%esp), %ebx movl (%eax), %eax testl %eax, %eax jne.l2 movl $1, (%esp) jmp.l3.l2: subl $1, %eax movl %eax, (%esp) pushl %ebx # -4 leal 4(%esp), %eax pushl %eax # -4 call power addl $8, %esp # +8 imull %ebx, %eax movl %eax, (%esp).l3: movl (%esp), %eax addl $4, %esp # +4 popl %ebx # +4.size power,.-power 10

The second one:.file "power.c".globl power.type power, @function power: pushl %ebx subl $12, %esp # 4 for n, 8 for args # esp never changes again # n is at 8(%esp) # saved ebx is at 12(%esp) # urn addr is at 16(%esp) # my arg1 is at 20(%esp) # my arg2 is at 24(%esp) # when I call myself, arg1 is at 4(%esp) # when I call myself, arg2 is at (%esp) movl 20(%esp), %eax movl 24(%esp), %ebx movl (%eax), %eax testl %eax, %eax jne.l2 movl $1, 8(%esp) jmp.l3.l2: subl $1, %eax movl %eax, 8(%esp) movl %ebx, 4(%esp) leal 8(%esp), %eax movl %eax, (%esp) call power imull %ebx, %eax movl %eax, 8(%esp).L3: movl 8(%esp), %eax addl $12, %esp popl %ebx.size power,.-power 11

4. (a) i. If A puts x in a caller-save register then A needs to save it each time A calls B, so 10 times. ii. If A puts x in a callee-save register, then A saves it only on entry, and since B never uses that register it never saves it, so 1 time. (b) i. If C puts x in a caller-save register and D puts y in (the same) caller-save register, then C needs to save/restore it each time C calls D (10 times) and for each time D is called, it needs to save/restore it when D calls E, (10 x 20 times), or 210 times. ii. If C puts x in a caller-save register and D puts y in a callee-save register, then C needs to save/restore it each time C calls D (10 times) and for each time D is called, it needs to save/restore it once, (10 x 1 times), or 20 times. iii. If C puts x in a callee-save register and D puts y in a caller-save register, then C needs to save/restore it once and for each time D is called, it needs to save/restore it when D calls E, (10 x 20 times), or 201 times. iv. If C puts x in a callee-save register and D puts y in (the same) callee-save register, then C needs to save/restore it once and for each time D is called, it needs to save/restore it once (10 x 1 times), or 11 times. 12