Computer Organization: A Programmer's Perspective

Similar documents
Computer Architecture I: Outline and Instruction Set Architecture. CENG331 - Computer Organization. Murat Manguoglu

cmovxx ra, rb 2 fn ra rb irmovq V, rb 3 0 F rb V rmmovq ra, D(rB) 4 0 ra rb mrmovq D(rB), ra 5 0 ra rb OPq ra, rb 6 fn ra rb jxx Dest 7 fn Dest

Instruc(on Set Architecture. Computer Architecture Instruc(on Set Architecture Y86. Y86-64 Processor State. Assembly Programmer s View

CSC 252: Computer Organization Spring 2018: Lecture 11

Intel x86-64 and Y86-64 Instruction Set Architecture

CS429: Computer Organization and Architecture

Topics of this Slideset. CS429: Computer Organization and Architecture. Instruction Set Architecture. Why Y86? Instruction Set Architecture

Instruction Set Architecture

Instruction Set Architecture

Instruction Set Architecture

CISC 360 Instruction Set Architecture

Instruction Set Architecture

cmovxx ra, rb 2 fn ra rb irmovl V, rb rb V rmmovl ra, D(rB) 4 0 ra rb D mrmovl D(rB), ra 5 0 ra rb D OPl ra, rb 6 fn ra rb jxx Dest 7 fn Dest

Foundations of Computer Systems

CS429: Computer Organization and Architecture

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

Chapter 4! Processor Architecture!

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

Computer Science 104:! Y86 & Single Cycle Processor Design!

CS 3330 Exam 1 Fall 2017 Computing ID:

Computer Science 104:! Y86 & Single Cycle Processor Design!

last time Exam Review on the homework on office hour locations hardware description language programming language that compiles to circuits

Reading assignment. Chapter 3.1, 3.2 Chapter 4.1, 4.3

Computer Organization Chapter 4. Prof. Qi Tian Fall 2013

Machine Program: Procedure. Zhaoguo Wang

CS 3330: SEQ part September 2016

Machine-Level Programming (2)

CS429: Computer Organization and Architecture

x64 Cheat Sheet Fall 2014

Assembly Language: Function Calls

bitwise (finish) / SEQ part 1

Processor Architecture I. Alexandre David

Machine Language CS 3330 Samira Khan

Changelog. Changes made in this version not seen in first lecture: 13 Feb 2018: add slide on constants and width

CSE351 Spring 2018, Midterm Exam April 27, 2018

CS 3330 Exam 2 Spring 2017 Name: EXAM KEY Computing ID: KEY

Pipelining 3: Hazards/Forwarding/Prediction

SEQ part 3 / HCLRS 1

Lecture 3 CIS 341: COMPILERS

Machine Level Programming: Control

Overview. CS429: Computer Organization and Architecture. Y86 Instruction Set. Building Blocks

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

Processor Architecture

minor HCL update Pipe Hazards (finish) / Caching post-quiz Q1 B post-quiz Q1 D new version of hclrs.tar:

CS 3330 Exam 2 Fall 2017 Computing ID:

Byte. cmovle 7 1. halt 0 0 addq 6 0 nop 1 0 subq 6 1 cmovxx ra, rb 2 fn ra rb andq 6 2 irmovq V, rb 3 0 F rb V xorq 6 3. cmovl 7 2.

Machine- Level Representa2on: Procedure

x86-64 Programming II

Do not turn the page until 5:10.

CS429: Computer Organization and Architecture

CS 3330 Exam 3 Fall 2017 Computing ID:

Areas for growth: I love feedback

CPSC 121: Models of Computation. Module 10: A Working Computer

x86 Programming II CSE 351 Winter

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

Assembly Programming III

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

CPSC 121: Models of Computation. Module 10: A Working Computer

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

Assembly II: Control Flow

x86 Programming I CSE 351 Winter

CS429: Computer Organization and Architecture

Machine-Level Programming II: Control

CSE 401/M501 Compilers

How Software Executes

Machine-Level Programming II: Control

CS 261 Fall Machine and Assembly Code. Data Movement and Arithmetic. Mike Lam, Professor

CSC 252: Computer Organization Spring 2018: Lecture 6

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

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

CS367. Program Control

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

CSCI 2021: x86-64 Control Flow

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

Machine-level Programs Procedure

Machine-Level Programming III: Procedures

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

CSE 351 Midterm - Winter 2015 Solutions

CSE 351 Midterm Exam

CSE 351 Midterm Exam Spring 2016 May 2, 2015

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

The Stack & Procedures

CSE 351 Midterm - Winter 2017

CSE 351 Midterm - Winter 2015

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

Pipelining 4 / Caches 1

Sequential Implementation

x86 64 Programming II

CISC: Stack-intensive procedure linkage. [Early] RISC: Register-intensive procedure linkage.

RISC I from Berkeley. 44k Transistors 1Mhz 77mm^2

Background: Sequential processor design. Computer Architecture and Systems Programming , Herbstsemester 2013 Timothy Roscoe

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

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

SOEN228, Winter Revision 1.2 Date: October 25,

Chapter 2A Instructions: Language of the Computer

layers of abstraction

cmovxx ra, rb 2 fn ra rb irmovq V, rb 3 0 F rb V rmmovq ra, D(rB) 4 0 ra rb D mrmovq D(rB), ra 5 0 ra rb D OPq ra, rb 6 fn ra rb jxx Dest 7 fn Dest

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing

Transcription:

A Programmer's Perspective Instruction Set Architecture Gal A. Kaminka galk@cs.biu.ac.il

Outline: CPU Design Background Instruction sets Logic design Sequential Implementation A simple, but not very fast processor design Pipelining Get more things running simultaneously Pipelined Implementation Make it work A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 2

Instruction Set Architecture Assembly Language View Processor state Registers, memory, Instructions addq, pushq, ret, How instructions are encoded as bytes Layer of Abstraction Above: how to program machine Processor executes instructions in a sequence Below: what needs to be built Use variety of tricks to make it run fast E.g., execute multiple instructions simultaneously Application Program Compiler ISA CPU Design Circuit Design Chip Layout OS A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 3

Y86 64 Processor State %rax %rcx %rsp %rbp RF: Program registers %r8 %r12 %r9 %r13 CC: Condition codes ZF SF OF Stat: Program status DMEM: Memory %rdx %rsi %r10 %r14 PC %rbx %rdi %r11 Program Registers 15 registers (omit %r15). Each 64 bits Condition Codes Single bit flags set by arithmetic or logical instructions» ZF: Zero SF:Negative OF: Overflow Program Counter Indicates address of next instruction Program Status Indicates either normal operation or some error condition Memory Byte addressable storage array Words stored in little endian byte order A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 4

Y86 64 Instruction Set #1 Byte halt 0 0 0 1 2 3 4 5 6 7 8 9 nop 1 0 cmovxx ra, rb 2 fn ra rb irmovq V, rb 3 0 F rb V rmmovq ra, D(rB) 4 0 ra rb D mrmovq D(rB), ra 5 0 ra rb D OPq ra, rb 6 fn ra rb jxx Dest 7 fn Dest call Dest 8 0 Dest ret 9 0 pushq ra A 0 ra F popq ra B 0 ra F A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 5

Y86 64 Instructions Format 1 10 bytes of information read from memory Can determine instruction length from first byte Not as many instruction types, and simpler encoding than with x86 64 Each accesses and modifies some part(s) of the program state A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 6

Y86 64 Instruction Set #2 rrmovq 2 0 Byte 0 1 2 3 4 5 6 7 8 9 cmovle 2 1 halt 0 0 nop 1 0 cmovxx ra, rb 2 fn ra rb irmovq V, rb 3 0 F rb rmmovq ra, D(rB) 4 0 ra rb mrmovq D(rB), ra 5 0 ra rb V D D cmovl 2 2 cmove 2 3 cmovne 2 4 cmovge 2 5 cmovg 2 6 OPq ra, rb 6 fn ra rb jxx Dest 7 fn Dest call Dest 8 0 Dest ret 9 0 pushq ra A 0 ra F popq ra B 0 ra F A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 7

Y86 64 Instruction Set #3 Byte halt 0 0 0 1 2 3 4 5 6 7 8 9 nop 1 0 cmovxx ra, rb 2 fn ra rb irmovq V, rb 3 0 F rb V rmmovq ra, D(rB) 4 0 ra rb D mrmovq D(rB), ra 5 0 ra rb D OPq ra, rb 6 fn ra rb jxx Dest 7 fn Dest call Dest 8 0 Dest addq 6 0 subq 6 1 andq 6 2 xorq 6 3 ret 9 0 pushq ra A 0 ra F popq ra B 0 ra F A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 8

Y86 64 Instruction Set #4 Byte 0 1 2 3 4 5 6 7 8jmp 97 0 halt 0 0 nop 1 0 cmovxx ra, rb 2 fn ra rb irmovq V, rb 3 0 F rb V rmmovq ra, D(rB) 4 0 ra rb D mrmovq D(rB), ra 5 0 ra rb D jle 7 1 jl 7 2 je 7 3 jne 7 4 jge 7 5 jg 7 6 OPq ra, rb 6 fn ra rb jxx Dest 7 fn Dest call Dest 8 0 Dest ret 9 0 pushq ra A 0 ra F popq ra B 0 ra F A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 9

Encoding Registers Each register has 4 bit ID %rax 0 %r8 8 %rcx 1 %r9 9 %rdx 2 %r10 A %rbx 3 %r11 B %rsp 4 %r12 C %rbp 5 %r13 D %rsi 6 %r14 E %rdi 7 No Register F Same encoding as in x86 64 Register ID 15 (0xF) indicates no register Will use this in our hardware design in multiple places A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 10

Instruction Example Addition Instruction Generic Form Encoded Representation addq ra, rb 6 0 ra rb Add value in register ra to that in register rb Store result in register rb Note that Y86 64 only allows addition to be applied to register data Set condition codes based on result e.g., addq %rax,%rsi Encoding: 60 06 Two byte encoding First indicates instruction type Second gives source and destination registers A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 11

Arithmetic and Logical Operations Instruction Code Function Code Add addq ra, rb 6 0 ra rb Subtract (ra from rb) subq ra, rb 6 1 ra rb Refer to generically as OPq Encodings differ only by function code Low order 4 bytes in first instruction word Set condition codes as side effect And andq ra, rb 6 2 ra rb Exclusive Or xorq ra, rb 6 3 ra rb A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 12

Move Operations Register -> Register rrmovq ra, rb 2 0 Immediate -> Register irmovq V, rb 3 0 F rb V Register -> Memory rmmovq ra, D(rB) 4 0 ra rb D Memory -> Register mrmovq D(rB), ra 5 0 ra rb D Like the x86 64 movq instruction Simpler format for memory addresses Give different names to keep them distinct A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 13

Move Instruction Examples X86 64 Y86 64 movq $0xabcd, %rdx Encoding: irmovq $0xabcd, %rdx 30 82 cd ab 00 00 00 00 00 00 movq %rsp, %rbx Encoding: movq -12(%rbp),%rcx Encoding: rrmovq %rsp, %rbx 20 43 mrmovq -12(%rbp),%rcx 50 15 f4 ff ff ff ff ff ff ff movq %rsi,0x41c(%rsp) Encoding: rmmovq %rsi,0x41c(%rsp) 40 64 1c 04 00 00 00 00 00 00 A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 14

Conditional Move Instructions Move Unconditionally rrmovq ra, rb Move When Less or Equal cmovle ra, rb Move When Less cmovl ra, rb Move When Equal cmove ra, rb Move When Not Equal cmovne ra, rb Move When Greater or Equal cmovge ra, rb Move When Greater cmovg ra, rb 2 0 ra rb 2 1 ra rb 2 2 ra rb 2 3 ra rb 2 4 ra rb 2 5 ra rb 2 6 ra rb Refer to generically as cmovxx Encodings differ only by function code Based on values of condition codes Variants of rrmovq instruction (Conditionally) copy value from source to destination register A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 15

Jump Instructions Jump (Conditionally) jxx Dest 7 fn Dest Refer to generically as jxx Encodings differ only by function code fn Based on values of condition codes Same as x86 64 counterparts Encode full destination address Unlike PC relative addressing seen in x86 64 A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 16

Jump Instructions Jump (Conditionally) jxx Dest 7 fn Dest Refer to generically as jxx Encodings differ only by function code fn Based on values of condition codes Same as x86 64 counterparts Encode full destination address Unlike PC relative addressing seen in x86 64 A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 17

Y86 64 Program Stack Stack Bottom Region of memory holding program data Increasing Addresses Used in Y86 64 (and x86 64) for supporting procedure calls Stack top indicated by %rsp Address of top stack element Stack grows toward lower addresses Top element is at highest address in the stack When pushing, must first decrement stack pointer After popping, increment stack pointer Stack Top %rsp A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 18

Stack Operations pushq ra A 0 ra F Decrement %rsp by 8 Store word from ra to memory at %rsp Like x86 64 popq ra B 0 ra F Read word from memory at %rsp Save in ra Increment %rsp by 8 Like x86 64 A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 19

Subroutine Call and Return call Dest 8 0 Dest Push address of next instruction onto stack Start executing instructions at Dest Like x86 64 ret 9 0 Pop value from stack Use as address for next instruction Like x86 64 A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 20

Miscellaneous Instructions nop 1 0 Don t do anything halt 0 0 Stop executing instructions x86 64 has comparable instruction, but can t execute it in user mode We will use it to stop the simulator Encoding ensures that program hitting memory initialized to zero will halt A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 21

Status Conditions Mnemonic Code Normal operation AOK 1 Mnemonic Code Halt instruction encountered HLT 2 Mnemonic Code ADR 3 Bad address (either instruction or data) encountered Invalid instruction encountered Mnemonic Code INS 4 Desired Behavior If AOK, keep going Otherwise, stop program execution A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 22

Just to see a different ISA design: MIPS Registers A RISC design) $0 $1 $2 $3 $4 $5 $6 $7 $0 $at $v0 $v1 $a0 $a1 $a2 $a3 Constant 0 Reserved Temp. Return Values Procedure arguments $16 $17 $18 $19 $20 $21 $22 $23 $s0 $s1 $s2 $s3 $s4 $s5 $s6 $s7 Callee Save Temporaries: May not be overwritten by called procedures $8 $9 $10 $11 $12 $13 $14 $15 $t0 $t1 $t2 $t3 $t4 $t5 $t6 $t7 Caller Save Temporaries: May be overwritten by called procedures $24 $25 $26 $27 $28 $29 $30 $31 $t8 $t9 $k0 $k1 $gp $sp $s8 $ra Caller Save Temp Reserved for Operating Sys Global Pointer Stack Pointer Callee Save Temp Return Address A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 23

MIPS Instruction Examples R R Op Ra Rb Rd 00000 Fn R I addu $3,$2,$1 # Register add: $3 = $2+$1 Op Ra Rb Immediate addu $3,$2, 3145 # Immediate add: $3 = $2+3145 sll $3,$2,2 # Shift left: $3 = $2 << 2 Branch Op Ra Rb Offset beq $3,$2,dest # Branch when $3 = $2 Load/Store Op Ra Rb Offset lw $3,16($2) # Load Word: $3 = M[$2+16] sw $3,16($2) # Store Word: M[$2+16] = $3 A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 24

?שאלות A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 25