Assembly Language II: Addressing Modes & Control Flow

Similar documents
Credits and Disclaimers

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

Assembly II: Control Flow

Review addressing modes

Credits and Disclaimers

Machine-Level Programming (2)

CS61 Section Solutions 3

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

Process Layout and Function Calls

Machine Language CS 3330 Samira Khan

The Hardware/Software Interface CSE351 Spring 2013

x86 Programming II CSE 351 Winter

x86-64 Programming II

Machine Level Programming: Control

Areas for growth: I love feedback

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Machine-Level Programming II: Control

Assembly Programming III

CS429: Computer Organization and Architecture

Machine-Level Programming II: Control

CSCI 2021: x86-64 Control Flow

Credits and Disclaimers

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

L09: Assembly Programming III. Assembly Programming III. CSE 351 Spring Guest Lecturer: Justin Hsia. Instructor: Ruth Anderson

CS367. Program Control

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

CSE351 Spring 2018, Midterm Exam April 27, 2018

Computer Systems C S Cynthia Lee

last time Assembly part 2 / C part 1 condition codes reminder: quiz

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

Assembly Language: IA-32 Instructions

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

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

Introduction to 8086 Assembly

CSE 351 Midterm Exam

Control flow (1) Condition codes Conditional and unconditional jumps Loops Conditional moves Switch statements

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

Assembly Language: Part 2

How Software Executes

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Part 2

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 12

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

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

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

Where We Are. Optimizations. Assembly code. generation. Lexical, Syntax, and Semantic Analysis IR Generation. Low-level IR code.

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

x86 64 Programming II

Machine- Level Representa2on: Procedure

x64 Cheat Sheet Fall 2014

Sungkyunkwan University

Do not turn the page until 5:10.

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

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

CSE 351 Midterm - Winter 2015 Solutions

Program Exploitation Intro

Intel x86-64 and Y86-64 Instruction Set Architecture

System Programming and Computer Architecture (Fall 2009)

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

Ex: Write a piece of code that transfers a block of 256 bytes stored at locations starting at 34000H to locations starting at 36000H. Ans.

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Part 1

mith College Computer Science CSC231 Assembly Week #9 Spring 2017 Dominique Thiébaut

Machine- Level Programming II: Arithme6c & Control

Lecture 3 CIS 341: COMPILERS

Intel Instruction Set (gas)

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

Branching and Looping

Changelog. Assembly (part 1) logistics note: lab due times. last time: C hodgepodge

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

Corrections made in this version not seen in first lecture:

CSE 351 Midterm - Winter 2015

Von Neumann Architecture. Machine Languages. Context of this Lecture. High-Level Languages. Assembly Language: Part 1. Princeton University.

BAHAR DÖNEMİ MİKROİŞLEMCİLER LAB4 FÖYÜ

An Introduction to x86 ASM

Credits to Randy Bryant & Dave O Hallaron

Question Points Score Total: 100

5.1. CS356 Unit 5. x86 Control Flow

x86-64 Programming III

Second Part of the Course

Machine Programming 2: Control flow

Computer Architecture..Second Year (Sem.2).Lecture(4) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات

Processor state. Information about currently executing program. %rax, %rdi, ... %r14 %r15. %rsp. %rip CF ZF SF OF. Temporary data

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

Selection and Iteration. Chapter 7 S. Dandamudi

Process Layout, Function Calls, and the Heap

Brief Assembly Refresher

CS 107. Lecture 13: Assembly Part III. Friday, November 10, Stack "bottom".. Earlier Frames. Frame for calling function P. Increasing address

IFE: Course in Low Level Programing. Lecture 6

Conditional Processing

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

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2016 Lecture 12

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

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

Machine-Level Programming II: Control Flow

CS241 Computer Organization Spring Addresses & Pointers

Changelog. Brief Assembly Refresher. a logistics note. last time

Basic Assembly Instructions

Machine-Level Programming II: Control

Transcription:

Assembly Language II: Addressing Modes & Control Flow Learning Objectives Interpret different modes of addressing in x86 assembly Move data seamlessly between registers and memory Map data structures in C to different addressing modes in assembly. Interpret the Intel flag values. Interpret cmp, test, and jump instructions correctly. Map common C control flow constructs into assembly language. Recognize common C control flow patterns in assembly language But first 9/22/2016 CS61 Fall 2016 1

Note 1: Passing structs We know that the first six arguments to a function go in registers: rdi, rsi, rdx, rcx, r8, and r9. At the end of class, someone asked what happens if you pass structs as arguments we re glad you asked! 1. If the struct has fields that all fit in registers, the compiler will pass them in registers. 2. If the struct does not fit in registers, it is placed on the stack. 3. Other parameters are still placed in registers. 9/22/2016 CS61 Fall 2016 2

Note 2: Binary Arithmetic We ve talked about how we represent numbers in a computer in binary: The number decimal 10 can be represented by combining powers of 2: 8 + 2 = 2 3 + 2 2 = 10 So, in binary: 1010 But how do we represent -10? 9/22/2016 CS61 Fall 2016 3

(cont) 2 s Complement Arithmetic We represent n by flipping all the bits in n (~n) and adding 1 to it: -n = (~n) + 1 Example (in 8 bits): N = 5; 5 = 00000101 ~N = 11111010 ~N + 1 = 11111011 = 0xfb 9/22/2016 CS61 Fall 2016 4

(cont) Implications of 2 s Complement Adding signed and unsigned numbers is identical! That s why you could generate some identical assembly last class using signed and unsigned C types. Negative numbers will always have a 1 in the high order bit. Positive numbers will always have a 0 in the high order bit. Confused? Check out: https://mix.office.com/watch/mgjeezhtni45?lcid= 9/22/2016 CS61 Fall 2016 5

Accessing Memory So far we ve seen that you can move data around using mov instructions: mov %rax, %edi Now it s time to dig into that in a bit more detail and find out how to access data in memory. Fundamentally there are three ways to access memory: Via labels (names) Via registers (including %rip-relative addressing) Via constants 9/22/2016 CS61 Fall 2016 6

Register based addressing Recall that simply using %reg (e.g., %rax) accesses the value stored in the register. Indirect addressing: Addressing that uses the contents of a register to produce an address: (%reg) treats the value in reg as an address and refers to the value stored at that address. Example: (%rsp) Refers to the value stored on the stack at the location referenced by the stack pointer. N(%reg) refers to the data at the address produced by adding N to the contents of reg. Example 4(%rsp) refers to the value stored at the address 4 greater than the stack pointer. 9/22/2016 CS61 Fall 2016 7

Sum.c (unoptimized) Screen Capture 9/22/2016 CS61 Fall 2016 8

Indirect Addressing Continued Indirect addressing: Addressing that uses the contents of two registers to produce an address: (%reg1, %reg2) Refers to the value stored at the address formed by adding the contents of reg1 and reg2. Example: (%rax, %rdx): If %rax contains the starting address of an array of chars, and %rdx is the index, this accesses the value at that index. Extended Indirect addressing: (%reg1, %reg2, S) refers to the value stored at the address formed by adding %reg2 * S to %reg1. S must be one of 1, 2, 4, 8 (when missing, 1 is implied). Example: (%rax, %rdx, 4) Now the example above works on an array of integers. 9/22/2016 CS61 Fall 2016 9

Complete form of Indirect Addressing N(%reg1, %reg2, S) Compute address as follows: N + reg1 + (reg2 * S) Another handy instruction: lea[lq] = load effective address: computes an address and places the result in a register. Used to put the pointer to something in a register. Also, often a fast way to perform addition. 9/22/2016 CS61 Fall 2016 10

Screen Capture Sum1.c (optimized) Cindex.c (character array access) 9/22/2016 CS61 Fall 2016 11

Screen Capture Cindex2.c Cindex3.c 9/22/2016 CS61 Fall 2016 12

Screen Capture Iindex.c 9/22/2016 CS61 Fall 2016 13

Other modes: Immediate Immediate: The syntax $NNN refers to the number NNN. You ll see it used in (at least) two ways: movl $63, %eax # Moves the value 63 into a register movl $Label, %eax # Move the address to which Label # corresponds into a register. Note: When you use labels as the destination of a jumpclass instruction, you do not use the $, e.g., jmp Label 9/22/2016 CS61 Fall 2016 14

Other modes: RIP-relative RIP-relative: Recall that %rip is the instruction pointer. RIP-relative addressing is a special case of indirect addressing, using the instruction pointer as the register. Example: you will frequently see global symbols referenced in this manner: #include <stdio.h> extern long a; void func(void) { printf("%lu\n", a); } func:.lfb23: subq movq movl movl $8, %rsp a(%rip), %rdx $.LC0, %esi $1, %edi 9/22/2016 CS61 Fall 2016 15

Control Flow When we write: while (variable!= 0) { } <do something> <do something that might change variable> What do we really mean? 9/22/2016 CS61 Fall 2016 16

Control Flow When we write: while (variable!= 0) { } <do something> <do something that might change variable> What do we really mean? loop: if (variable == 0) goto end <do something> end: <do something that might change variable> go to loop; 9/22/2016 CS61 Fall 2016 17

Control Flow Overview Unconditional: Directs the processor to execute at an address other than the next sequential address. Examples: jmp (jump) callq (call a function) Conditional: Paired instructions: 1. An instruction that sets condition flags (e.g., arithmetic, logical, cmp, test) 2. A conditional jump instruction that changes the sequential execution of a program depending on the state of the condition flags. 9/22/16 CS61 Fall 2016 18

Condition Flags Special bits stored in a special register: EFLAGS SF: Sign Flag The most recent operation yielded a negative value. Equal to MSbit of result; which indicates the sign of a two's complement integer. 0 means result was positive, 1 means negative. CF: Carry Flag The most recent operation generated a carry bit out of the MSbit. Indicates overflow when performing unsigned integer arithmetic. OF: Overflow Flag The most recent operation caused a 2 s complement overflow (either positive or negative). Indicates an overflow when performing signed integer arithmetic. ZF: Zero Flag The most recent operation yielded a zero. Condition flags are set implicitly by every arithmetic instruction Condition flags are set explicitly by comparison and test instructions 9/22/16 CS61 Fall 2016 19

Comparison Instructions cmp[bwlq] src1, src2 Compares value of src1 and src2 src1, src2 can be registers, immediate values, or contents of memory. Computes (src2 src1) without modifying either operand like subl src1, src2 without changing src2 But, sets the condition flags based on the result of the subtraction. test[bwlq] src1, src2 Like cmpl, but computes (src1 & src2) instead of subtracting them. 9/22/16 CS61 Fall 2016 20

Conditional Jump Instructions Used for signed or unsigned operations JE: jump if equal (ZF=1) JNE: jump if not equal (ZF=0) Used for signed operations JS: jump if signed/negative (SF = 1) JNS: Jump if not signed/positive (SF = 0) JL: jump if less (SF!= OF) JLE: jump if less than or equal to (ZF = 1 or SF!= OF) JG: jump if greater than (ZF = 0 and SF = OF) JGE: jump if greater than or equal to (SF = OF) Used for unsigned operations JA: jump above (CF = 0 and ZF = 0) JAE: jump above or equal (CF = 0) JB: jump below (CF = 1) JBE: jump below or equal (CF = 1 or ZF = 1) 9/22/16 CS61 Fall 2016 21

CS Faculty Coffee Chats CS will pay for CS faculty and undergrads to go out for coffee (or other beverage). Open to all undergrads, not just CS concentrators One or more students can join each coffee chat Check my calendar and suggest a time! beverage_t beverage = malloc(sizeof(beverage_t)) fill(beverage); while (!is_empty(beverage)) drink (beverage); 9/22/2016 CS61 Fall 2016 22