SYSTEMS PROGRAMMING AND COMPUTER ARCHITECTURE Assignment 5: Assembly and C

Similar documents
CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here

CS356: Discussion #15 Review for Final Exam. Marco Paolieri Illustrations from CS:APP3e textbook

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

CPSC W Term 2 Problem Set #3 - Solution

Process Layout and Function Calls

Machine Programming 3: Procedures

Machine/Assembler Language Putting It All Together

Setting & Examining Condition Codes. int gt(long x, long y) { return x > y; } int has_nonzero_masked(long x, long mask) { return!!

Function Calls and Stack

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

Credits and Disclaimers

Credits and Disclaimers

CS 33. Machine Programming (2) CS33 Intro to Computer Systems XI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

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

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

Machine Program: Procedure. Zhaoguo Wang

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

CS356 Unit 5. Translation to Assembly. Translating HLL to Assembly ASSEMBLY TRANSLATION EXAMPLE. x86 Control Flow

The Hardware/Software Interface CSE351 Spring 2013

System Programming and Computer Architecture (Fall 2009)

Credits to Randy Bryant & Dave O Hallaron

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

5.1. CS356 Unit 5. x86 Control Flow

Machine- Level Programming II: Arithme6c & Control

Machine-Level Programming II: Control and Arithmetic

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

C to Assembly SPEED LIMIT LECTURE Performance Engineering of Software Systems. I-Ting Angelina Lee. September 13, 2012

Do not turn the page until 5:10.

Data Representa/ons: IA32 + x86-64

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

Systems Programming and Computer Architecture ( )

Machine Level Programming II: Arithmetic &Control

Question Points Score Total: 100

Sungkyunkwan University

EECS 213 Fall 2007 Midterm Exam

Controlling Program Flow

Sungkyunkwan University

CS 33. Machine Programming (2) CS33 Intro to Computer Systems XII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CS241 Computer Organization Spring Loops & Arrays

Process Layout, Function Calls, and the Heap

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

University of Washington

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

How Software Executes

Lecture 1: Introduction

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

Procedures and the Call Stack

Machine- Level Programming II: Arithme6c & Control

Machine- Level Programming II: Arithme c & Control

Do not turn the page until 5:10.

The von Neumann Machine

The Hardware/Software Interface CSE351 Spring 2015

Meet & Greet! Come hang out with your TAs and Fellow Students (& eat free insomnia cookies) When : TODAY!! 5-6 pm Where : 3rd Floor Atrium, CIT

Assembly I: Basic Operations. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS241 Computer Organization Spring Addresses & Pointers

Question Points Score Total: 100

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

CSE351 Spring 2018, Midterm Exam April 27, 2018

Credits and Disclaimers

Sungkyunkwan University

CS , Spring 2008 Final Exam

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-64 assembly code is produced:

1 Number Representation(10 points)

EXAMINATIONS 2014 TRIMESTER 1 SWEN 430. Compiler Engineering. This examination will be marked out of 180 marks.

Assembly IV: Complex Data Types. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

The von Neumann Machine

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

Compiling C Programs Into X86-64 Assembly Programs

CSE 351 Midterm Exam

Assembly I: Basic Operations. Jo, Heeseung

Machine Level Programming: Arrays, Structures and More

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

UW CSE 351, Winter 2013 Midterm Exam

%r8 %r8d. %r9 %r9d. %r10 %r10d. %r11 %r11d. %r12 %r12d. %r13 %r13d. %r14 %r14d %rbp. %r15 %r15d. Sean Barker

Assembly IV: Complex Data Types

Machine Programming 2: Control flow

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

Optimization part 1 1

Credits and Disclaimers

Do not turn the page until 12:30.

The Hardware/Software Interface CSE351 Spring 2015

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

CS527 Software Security

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

Do not turn the page until 5:10.

Areas for growth: I love feedback

CS356: Discussion #7 Buffer Overflows. Marco Paolieri

Arrays. CSE 351 Autumn Instructor: Justin Hsia

Chapter 3 Machine-Level Programming II Control Flow

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

Arrays. CSE 351 Autumn Instructor: Justin Hsia

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 6

Compiler Construction D7011E

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

CS , Fall 2004 Exam 1

Stack Frame Components. Using the Stack (4) Stack Structure. Updated Stack Structure. Caller Frame Arguments 7+ Return Addr Old %rbp

Transcription:

Fall Term 2016 SYSTEMS PROGRAMMING AND COMPUTER ARCHITECTURE Assignment 5: Assembly and C Assigned on: 20th Oct 2016 Due by: 27th Oct 2016 Pen & Paper exercise Assembly Code Fragments Consider the following pairs of C functions and assembly code. Fill in the missing instructions in the assembly code fragments (one instruction per blank). Your answers should be correct x86 64 assembly code. a) int f1(int a, int b) f1: pushq %rbp %rsp, %rbp urn a - b; %rbp, %rsp popq %rbp b) int f2(int a) f2: pushq %rbp %rsp, %rbp leal %rbp, %rsp urn a*5; popq %rbp c) int f3(int a) f3: pushq %rbp %rsp, %rbp

if (a <= 0) %edi, %eax urn -a; jle.l11 else.l8: %rbp, %rsp urn a; popq %rbp.l11: negl %eax jmp.l8 Conditional branches What is the value of %eax, when the last label (respectively.l3 and.l17) is reached? First, annotate the assembly code and then, write the corresponding C-statements! i) Assume %eax := a, %edx := d. ii) Assume %eax := 1, %ecx := N..L2:.L3: cmpl %eax, %edx jle.l2 subl %eax, %edx %edx, %eax jmp.l3 subl %edx, %eax.l18:.l17: testl %ecx, %ecx jle.l17 xorl %edx, %edx incl %edx addl %eax, %eax cmpl %edx, %ecx jne.l18 For Loop This problem tests your understanding of how for loops in C relate to machine code. Consider the following x86 64 assembly code for a procedure dog(). Based on the assembly code, fill in the blanks in its corresponding C source code. (Note: you may only use symbolic variables x, y, i, and result from the source code in your expressions below. Do not use register names.) dog:.l1:.l2: cmpl %esi, %edi jge.l2 imull %edi, %eax addl $2, %edi cmpl %esi, %edi jl.l1 q int dog(int x, int y) int i, result; result = ; for (i = ; ; ) result = ; urn result; 2

Switch Statement Consider the following C function and assembly code fragments. Which of the assembly code fragments matches the C function shown? C Code int woohoo(int a) int = 0; switch(a) case 11: = 4; case 22: case 55: = 7; case 33: case 44: = 11; default: = 1; urn ; Fragment 1 woohoo: $0, %ecx cmpl $11, %edi jne.l2 $4, %ecx jmp.l3.l2: cmpl $22, %edi jne.l3 $7, %ecx.l3: cmpl $55, %edi jne.l5 $7, %ecx.l5: cmpl $33, %edi sete %al cmpl $44, %edi sete %dl orl %edi, %eax testb $1, %al je.l6 $11, %ecx.l6: %ecx, %eax Fragment 2 woohoo: subl $1, %edi cmpl $4, %edi ja.l2 jmp *.L9(,%edi,4).section.rodata.align 4.L9:.long.L3.long.L5.long.L7.long.L7.long.L5.text.L3: $4, %eax jmp.l2.l5: $7, %eax jmp.l2.l7: $11, %eax.l2: Fragment 3 woohoo: je.l6 je.l7 je.l8 je.l8 je.l7 jmp.l9.l6: $4, %eax.l7: $7, %eax.l8: $11, %eax.l9:.l4: 3

Arrays in Assembly Suppose the start address of a short array A and size t index i are stored in registers %rdi and %rsi, respectively. For each of the following C expressions, give its type, a formula for its value, and an assembly code implementation. The result should be stored in register %rax if it is a pointer and in register element %ax if the result is a short integer. Note that each C expression can be implemented with one single assembly instruction by choosing a suitable addressing mode. A+3 A[5] &A[i] A[4*i+2] A+2*i-7 Structs Consider the following structure declaration: struct line char *id; short rgb[3]; int x; int y; int width; int height; char f; l; a) Draw the memory layout of the structure including the address offset of each field. b) How many total bytes does the structure require? c) Optimize the structure in terms of memory consumption and state the new size. d) The following procedure operates on the structure: void obfuscatedoperation(struct line *lp) = ; = ; Using the original struct from above, the compiler generated the following assembly code for the body of the procedure: sall sall $1, 24(%rdi) $1, 28(%rdi) From this assembly code, fill in the missing lines in obfuscatedoperation() and provide a more meaningful name for the procedure. 4

Assembly to C Express the operations of the following assembly language sequence as a C program. foo: pushq incl popq %rbp %rsp, %rbp %rdi, %rdx %rdi, %rax (%rax), %eax %eax %eax, (%rdx) $0, %eax %rbp, %rsp %rbp Give an example how the function foo can be called (provide type declarations for all parameters). Parameters in Assembly What parameters are expected by function huh and what does it do? huh: pushq %rbp %rsp, %rbp movslq %esi, %rsi leaq 0(,%rsi,4), %rdx $999, (%rdi,%rdx) %rbp, %rsp popq %rbp Hand-in instructions You have 1 week for this assignment. This is a paper exercise. If you want your solution to be revised please hand it in during your exercise class on the due date or send it to your assistent via mail. 5