System Programming and Computer Architecture (Fall 2009)

Similar documents
Machine-Level Programming II: Control and Arithmetic

The Hardware/Software Interface CSE351 Spring 2013

Machine Level Programming II: Arithmetic &Control

Machine-Level Programming II: Control Flow

Sungkyunkwan University

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

Process Layout and Function Calls

Chapter 3 Machine-Level Programming II Control Flow

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

Credits to Randy Bryant & Dave O Hallaron

CS61 Section Solutions 3

CS241 Computer Organization Spring Addresses & Pointers

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

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

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

Credits and Disclaimers

CISC 360. Machine-Level Programming II: Control Flow Sep 23, 2008

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

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

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

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

Process Layout, Function Calls, and the Heap

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

Credits and Disclaimers

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

Machine Programming 2: Control flow

CS 3843 Final Exam Fall 2012

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

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

Machine- Level Programming II: Arithme6c & Control

Homework 0: Given: k-bit exponent, n-bit fraction Find: Exponent E, Significand M, Fraction f, Value V, Bit representation

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

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

Machine- Level Programming II: Arithme c & Control

Giving credit where credit is due

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

Machine- Level Programming II: Arithme6c & Control

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

Machine-Level Programming II: Control

Machine-Level Programming II: Control

Second Part of the Course

Machine- level Programming II: Control Flow

Assembly Language: IA-32 Instructions

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

Machine Level Programming: Control

Questions about last homework? (Would more feedback be useful?) New reading assignment up: due next Monday

CPSC W Term 2 Problem Set #3 - Solution

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.

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

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

Credits and Disclaimers

CS241 Computer Organization Spring 2015 IA

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

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

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

Instruction Set Architecture

Instruction Set Architecture

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

Instruction Set Architecture

x86-64 Programming II

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

Instruction Set Architecture

CISC 360 Instruction Set Architecture

CS367. Program Control

CPS104 Recitation: Assembly Programming

Control. Young W. Lim Mon. Young W. Lim Control Mon 1 / 16

Sungkyunkwan University

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

Assembly II: Control Flow

Machine-Level Programming (2)

Assembly Language: Function Calls

Assembly Programming III

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

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

CS 261 Fall Mike Lam, Professor. x86-64 Control Flow

Do not turn the page until 5:10.

Assembly Language: Function Calls" Goals of this Lecture"

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

Assembly Language: Function Calls" Goals of this Lecture"

2/12/2016. Today. Machine-Level Programming II: Control. Condition Codes (Implicit Setting) Processor State (x86-64, Partial)

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

Chapter 4 Processor Architecture: Y86 (Sections 4.1 & 4.3) with material from Dr. Bin Ren, College of William & Mary

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

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

CS429: Computer Organization and Architecture

x86 Programming II CSE 351 Winter

Assembly Language: Part 2

Instructor: Alvin R. Lebeck

CS429: Computer Organization and Architecture

CS213. Machine-Level Programming III: Procedures

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

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

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Machine Language CS 3330 Samira Khan

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2016 Lecture 12

Machine-level Programming (3)

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

The Hardware/Software Interface CSE351 Spring 2015

CS , Fall 2004 Exam 1

Transcription:

System Programming and Computer Architecture (Fall 2009) Recitation 2 October 8 th, 2009 Zaheer Chothia Email: zchothia@student.ethz.ch Web: http://n.ethz.ch/~zchothia/

Topics for Today Classroom Exercise 1 Review: Data Lab (Homework 2) Classroom Exercise & Homework 3 Parameter passing Control structures in assembly - If statements - Switch statements - For loops Reading condition codes with C Due in one week: October 15 2

Assignment 1

Review: Assignment 1 Question 3: Unsigned Two s complement x -x Hex Decimal Decimal Hex 2 2-2 E 0 0 0 0 3 3-3 D 8-8 -8 8 A -6 6 6 F -1 1 1 4

Review: Assignment 1 Question 5: float sum = 255.0; 0 1000 0110 1111 1110 0000 0000 0000 000 E2 = e 127 10 = 134 10 127 10 = 7 10 M2 = 1.f = 1.11111110000000000000000 float delta = 1.0e-5; 0 0110 1110 0100 1111 1000 1011 0101 100 E1 = e 127 10 = 110 10-127 10 = -17 10 M1 = 1.f = 1.01001111100010110101100 5

Review: Assignment 1 Question 5: E = max{e1, E2} = 710 M: 111111110000000000000000 + 101001111100010110101100 111111110000000000000000101001111100010110101100 111111110000000000000001 sum': rounding 0 1000 0110 1111 1110 0000 0000 0000 001 What happens after adding delta 65535 times? 6

Classroom Exercise 1

Question 5: Conditional branches What is the value of %eax, when the last label (.L3 and.l17 respectively) is reached? 5.1 %eax := a, %edx := d 1 cmpl %eax, %edx 2 jle.l2 3 subl %eax, %edx 4 movl %edx, %eax 5 jmp.l3 6.L2: 7 subl %edx, %eax 8.L3: 9... 5.2 %eax := 1, %edx := N 1 testl %ecx, %ecx 2 jle.l17 3 xorl %edx, %edx 4.L18: 5 incl %edx 6 addl %eax, %eax 7 cmpl %edx, %ecx 8 jne.l18 9.L17: 10... 8

Question 5: Conditional branches Solution 5.1 %eax := (a - d) if (d > a) { t = d - a; } else { t = a - d; } 5.2 %eax := 2 N t = 1; for (i = 0; i < N; i++) { t = t * 2; } 9

Review: Data Lab (Homework 2)

Surprising Results Almost half of the students beat the professor Really? Actually, there was a bug in the data lab Written in good old 32 bit times Use of LONG_MIN from limits.h to get T min But: int and long have different size on 64 bit machines Overflow in test code Accepted incorrect solutions To check if your solution is really correct: Pass -m32 to gcc in the Makefile 11

Example: islessorequal int islessorequal(int x, int y) { int x_neg = x>>31; int y_neg = y>>31; return!((!x_neg & y_neg) (!(x_neg ^ y_neg) & (y+~x+1)>>31)); } 12

Example: islessorequal int islessorequal(int x, int y) { int x_neg = x>>31; int y_neg = y>>31; return!((!x_neg & y_neg) (!(x_neg ^ y_neg) & (y+~x+1)>>31)); } Are x and y negative? 13

Example: islessorequal int islessorequal(int x, int y) { int x_neg = x>>31; int y_neg = y>>31; return!((!x_neg & y_neg) (!(x_neg ^ y_neg) & (y+~x+1)>>31)); } Are x and y negative? one is negative and the other is positive 14

Example: islessorequal int islessorequal(int x, int y) { int x_neg = x>>31; int y_neg = y>>31; return!((!x_neg & y_neg) (!(x_neg ^ y_neg) & (y+~x+1)>>31)); } Are x and y negative? both have the same sign recall: invert + increment = negate 15

Homework 3

Assembler: Warm-up exercise 1 movl $0x40, %eax 2 movl (,%eax,4), %ebx 3 leal 0x88(,%eax,2), %ecx 4 movl (%ecx), %ecx 5 cmpl %ecx, %ebx 6 jg L1 7 <Fragment A> 8 jmp L2 9 L1: 10 <Fragment B> 11 L2: 12... Memory Address Value 0x100 0xff 0x104 0xab 0x108 0x01 Which code fragment would be executed? 17

Assembler: Warm-up exercise 1 movl $0x40, %eax 2 movl (,%eax,4), %ebx 3 leal 0x88(,%eax,2), %ecx 4 movl (%ecx), %ecx 5 cmpl %ecx, %ebx 6 jg L1 7 <Fragment A> 8 jmp L2 9 L1: 10 <Fragment B> 11 L2: 12... Memory Address Value 0x100 0xff 0x104 0xab 0x108 0x01 Jump Condition %ebx - %ecx > 0 %ebx > %ecx Which code fragment would be executed? 18

IA32 Assembler Cheat Sheet Condition Codes Jump Instruction 1 (unconditional) jmp label ZF je label ~ZF jne label SF js label ~SF jns label ~(SF^OF) & ~ZF jg label ~(SF^OF) jge label SF^OF jl label (SF^OF) ZF jle label ~CF & ~ZF ja label CF jb label 19

IA32 Assembler Cheat Sheet Condition Codes CF addl Src,Dest cmpl Src2,Src1 testl Src2,Src1 addl a, b t = a + b cmpl a, b Set CCs based on b a Carry or borrow generated out of MSB (unsigned equivalent of overflow flag) testl a, b Set CCs based on a & b ZF t == 0 a == b a & b == 0 SF t < 0 (b-a) < 0 a & b < 0 OF 0 = b (a > 0 && b > 0 && t < 0) (a < 0 && b < 0 && t > 0) 0 20

Stack Layout Revisited 21

Stack vs. Heap Stack is the place for function parameters and local variables Recursion possible No need for memory management Heap is the place for global variables and dynamically managed memory Explicit memory management Globally accessible 22

IA32 Stack Frame Structure Caller Stack Frame Arguments for callee -Pushed from right-to-left ( instruction Return address (call Caller's Frame Arguments Return Addr 23

IA32 Stack Frame Structure Caller Stack Frame Arguments for callee -Pushed from right-to-left ( instruction Return address (call Current Stack Frame Caller's Frame Pointer Frame Arguments Return Addr Old %ebp +8 +4 Old frame pointer Saved register context -Callee safe registers ( spilling ) Local variables Current Stack Frame Saved Registers + Local Variables Parameters for next function call Stack Pointer Arguments 24

Parameter passing Parameters and return value in assembly int sub(int arg1, int arg2) { return arg1-arg2; } sub: pushl %ebp movl %esp, %ebp movl 12(%ebp), %edx movl 8(%ebp), %eax subl %edx, %eax leave ret 25

Control Structures in Assembly - Revisited 26

For loops int forloop(int x) { int a, s = x; (++ a for (a=0; a<=8; s += s; return s; } 27

For loops int forloop(int x) { int a, s = x; (++ a for (a=0; a<=8; s += s; return s; } int forloop(int x) { int a = 0, s = x; ( 8 > (a if goto done; do { s += s; a++; } while (a <= 8); done: return s; } 28

For loops int forloop(int x) { int a, s = x; (++ a for (a=0; a<=8; s += s; return s; } int forloop(int x) { int a = 0, s = x; ( 8 > (a if goto done; do { s += s; a++; } while (a <= 8); done: return s; } forloop: pushl movl subl movl movl movl.test: cmpl jle jmp.body: movl addl leal incl jmp.done: movl leave ret %ebp %esp, %ebp $8, %esp ( 8(%ebp -,$0 8(%ebp), %eax ( 4(%ebp - %eax, ( 8(%ebp -,$8.body.done -4(%ebp), %eax ( 4(%ebp - %eax, -8(%ebp), %eax ( eax %).test -4(%ebp), %eax 29

Switch statements Two implementation possibilities: Jump tables vs. nested if-else Question: Advantages and disadvantages? 30

Switch statements Two implementation possibilities: Jump tables vs. nested if-else Question: Advantages and disadvantages? Jump tables: constant time If-else: time complexity depends on nb. of cases GCC: Use jump tables when > 4 cases and reasonable range of values 31

Example switch (a) { case 100: a+=1; break; case 102: a+=2; case 103: a+=3; break; case 104: case 106: a+=6; default: a+=7; } Jump table: Fall through! Array with addresses of code segments Index = switch index (here: a) Normalize switch index (here: 0...6 instead of 100...106) 32

Example switch (a) { case 100: a+=1; break; case 102: a+=2; case 103: a+=3; break; case 104: case 106: a+=6; default: a+=7; }.L0: leal -100(%eax), %edx cmpl $6, %edx ja.l8 jmp ( L9(,%edx,4.* normalize and jump to right case jump table.section.rodata.align 4.L9:.long.L3 # a==100.long.l8 # a==101.long.l4 # a==102.long.l5 # a==103.long.l7 # a==104.long.l8 # a==105.text.l3:.l4:.l5:.l7:.l8:.l2:.long incl %eax jmp.l2.l7 # a==106 addl $2, %eax # fall through addl $3, %eax jmp.l2 addl $6, %eax # fall through addl $7, %eax 33

setx instructions int lessthan(x, y) { return x < y; } movl 12(%ebp), %eax cmpl ( 8(%ebp %eax, setl %al # %al = x < y movzbl %al, %eax # fill rest with 0 ret Integer registers: %eax %ah %ax %al Other setx instructions: sete (ZF), setne (~ZF), sets (SF), setns (~SF), 34

Reading condition codes with C EFLAGS register (32 bits): 31 11 7 6 0??? See Intel Architecture Software Developer's Manual (link on course website) 35

Quiz 1. Which of the following C expressions has a result value of 0x01? (a)!0x2a (b)!!0x00 (c)!(~0) (d) 0x0102 & (0xff << 8) (e) 0x0102 && (0xff << 8) 36

Quiz 1. Which of the following C expressions has a result value of 0x01? (a)!0x2a (b)!!0x00 (c)!(~0) (d) 0x0102 & (0xff << 8) (e) 0x0102 && (0xff << 8) 37

Quiz 2. Assuming: 16-bit two s complement integers, Which has the greatest absolute value? (a) 0xffff (b) 0x00ff (c) 0xc000 (d) 0xe000 38

Quiz 2. Assuming: 16-bit two s complement integers, Which has the greatest absolute value? (a) 0xffff (b) 0x00ff (c) 0xc000 (d) 0xe000 39

Quiz 3. Tiny floating-point: sign bit, 2 bit exponent, 2 bit fraction Which bit pattern corresponds to +1 10? (a) 0 00 00 (b) 0 00 01 (c) 0 01 00 (d) 0 01 10 (e) 1 01 00 40

Quiz 3. Tiny floating-point: sign bit, 2 bit exponent, 2 bit fraction Which bit pattern corresponds to +1 10? (a) 0 00 00 (b) 0 00 01 (c) 0 01 00 (d) 0 01 10 (e) 1 01 00 41

Quiz 4. Single-precision floating-point: sign bit, 8 bit exponent, 23 bit fraction Which has the greatest value? (a) 0x00000000 (b) 0x00000001 (c) 0x50000000 (d) 0x60000000 (e) 0xc0000000 42

Quiz 4. Single-precision floating-point: sign bit, 8 bit exponent, 23 bit fraction Which has the greatest value? (a) 0x00000000 (b) 0x00000001 (c) 0x50000000 (d) 0x60000000 (e) 0xc0000000 43

Quiz 5. In assembly code corresponding to a C function, a reference to an operand at memory location -8(%ebp) most likely corresponds to: (a) a local variable declared within that function (b) a global variable declared within that function (c) an incoming parameter value for that function (d) a dynamically allocated variable from the heap 44

Quiz 5. In assembly code corresponding to a C function, a reference to an operand at memory location -8(%ebp) most likely corresponds to: (a) a local variable declared within that function (b) a global variable declared within that function (c) an incoming parameter value for that function (d) a dynamically allocated variable from the heap 45

That's all for today! Questions? 46