Instruction Set Architecture CSCI 370 Computer Architecture Instruction

  • Slides: 36
Download presentation
Instruction Set Architecture CSCI 370: Computer Architecture

Instruction Set Architecture CSCI 370: Computer Architecture

Instruction Set Architecture Application Program • Assembly Language View • Processor state • Registers,

Instruction Set Architecture Application Program • Assembly Language View • Processor state • Registers, memory, … Compiler OS • 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 ISA CPU Design Circuit Design Chip Layout

Y 86 -64 Processor State RF: Program registers %rax %rcx %rdx %rbx %rsp %rbp

Y 86 -64 Processor State RF: Program registers %rax %rcx %rdx %rbx %rsp %rbp %rsi %rdi %r 8 %r 9 %r 10 %r 11 %r 12 %r 13 %r 14 CC: Condition codes Stat: Program status ZF SF OF DMEM: Memory PC • Program Registers • 15 registers (omit %r 15). 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

Y 86 -64 Instructions Format • 1– 10 bytes of information read from memory

Y 86 -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 x 86 -64 • Each accesses and modifies some part(s) of the program state

Y 86 -64 Instruction Set #1 Byte 0 halt 0 0 nop 1 0

Y 86 -64 Instruction Set #1 Byte 0 halt 0 0 nop 1 0 cmov. XX r. A, r. B 2 fn r. A r. B irmovq V, r. B 3 0 F rmmovq r. A, D(r. B) 4 0 r. A r. B D mrmovq D(r. B), r. A 5 0 r. A r. B D OPq r. A, r. B 6 fn r. A r. B j. XX Dest 7 fn Dest call Dest 8 0 ret 9 0 pushq r. A A 0 r. A F popq r. A B 0 r. A F 1 2 r. B V Dest 3 4 5 6 7 8 9

Y 86 -64 Instruction Set #2 Byte 0 halt 0 0 nop 1 0

Y 86 -64 Instruction Set #2 Byte 0 halt 0 0 nop 1 0 cmov. XX r. A, r. B 2 fn r. A r. B irmovq V, r. B 3 0 rmmovq r. A, D(r. B) 4 0 mrmovq D(r. B), r. A 5 0 OPq r. A, r. B 6 fn r. A r. B j. XX Dest 7 fn Dest call Dest 8 0 ret 9 0 pushq r. A A 0 r. A F popq r. A B 0 r. A F 1 F 2 r. B V r. A r. B D Dest 3 4 5 6 7 rrmovq 8 cmovle 7 9 7 0 cmovl 7 2 cmove 7 3 cmovne 7 4 cmovge 7 5 cmovg 7 6 1

Y 86 -64 Instruction Set #3 Byte 0 halt 0 0 nop 1 0

Y 86 -64 Instruction Set #3 Byte 0 halt 0 0 nop 1 0 cmov. XX r. A, r. B 2 fn r. A r. B irmovq V, r. B 3 0 F rmmovq r. A, D(r. B) 4 0 r. A r. B D mrmovq D(r. B), r. A OPq r. A, r. B j. XX Dest 5 6 7 1 0 2 3 4 5 6 7 8 9 r. B V addq 6 0 subq 6 1 andq 6 2 xorq 6 3 r. A r. B D fn r. A r. B fn Dest call Dest 8 0 ret 9 0 pushq r. A A 0 r. A F popq r. A B 0 r. A F Dest

Y 86 -64 Instruction Set #4 Byte 0 halt 0 nop 8 jmp 97

Y 86 -64 Instruction Set #4 Byte 0 halt 0 nop 8 jmp 97 0 0 jle 7 1 1 0 jl 7 2 cmov. XX r. A, r. B 2 fn r. A r. B je 7 3 irmovq V, r. B 3 0 F r. B V jne 7 4 rmmovq r. A, D(r. B) 4 0 r. A r. B D jge 7 5 mrmovq D(r. B), r. A 5 0 r. A r. B D jg 7 6 OPq r. A, r. B 6 fn r. A r. B j. XX Dest 7 fn Dest call Dest 8 0 ret 9 0 pushq r. A A 0 r. A F popq r. A B 0 r. A F 1 2 Dest 3 4 5 6 7

Encoding Registers • Each register has 4 -bit ID • Same encoding as in

Encoding Registers • Each register has 4 -bit ID • Same encoding as in x 86 -64 • Register ID 15 (0 x. F) special! • indicates “no register” • Will use this in our design %rax 0 %r 8 8 %rcx 1 %r 9 9 %rdx 2 %r 10 A %rbx 3 %r 11 B %rsp 4 %r 12 C %rbp 5 %r 13 D %rsi 6 %r 14 E %rdi 7 No Register F

Instruction Example • Addition Instruction Generic Form Encoded Representation addq r. A, r. B

Instruction Example • Addition Instruction Generic Form Encoded Representation addq r. A, r. B 6 0 r. A r. B • Add value in register r. A to that in register r. B • Store result in register r. B • Note that Y 86 -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

Arithmetic and Logical Operations Instruction Code Add addq r. A, r. B Function Code

Arithmetic and Logical Operations Instruction Code Add addq r. A, r. B Function Code 6 0 r. A r. B • Encodings differ only by “function code” Subtract (r. A from r. B) subq r. A, r. B • Low-order 4 bytes in first instruction word 6 1 r. A r. B And andq r. A, r. B 6 2 r. A r. B Exclusive-Or xorq r. A, r. B • Refer to generically as “OPq” 6 3 r. A r. B • Set condition codes as side effect

Move Operations Register rrmovq r. A, r. B 2 0 Immediate Register irmovq V,

Move Operations Register rrmovq r. A, r. B 2 0 Immediate Register irmovq V, r. B 3 0 F r. B V Register Memory rmmovq r. A, D(r. B) 4 0 r. A r. B D Memory Register mrmovq D(r. B), r. A 5 0 r. A r. B D • Like the x 86 -64 movq instruction • Simpler format for memory addresses • Give different names to keep them distinct

Move Instruction Examples X 86 -64 Y 86 -64 movq $0 xabcd, %rdx irmovq

Move Instruction Examples X 86 -64 Y 86 -64 movq $0 xabcd, %rdx irmovq $0 xabcd, %rdx Encoding: movq %rsp, %rbx 30 82 cd ab 00 00 00 rrmovq %rsp, %rbx Encoding: movq -12(%rbp), %rcx Encoding: movq %rsi, 0 x 41 c(%rsp) Encoding: 20 43 mrmovq -12(%rbp), %rcx 50 15 f 4 ff ff rmmovq %rsi, 0 x 41 c(%rsp) 40 64 1 c 04 00 00 00

Conditional Move Instructions Move Unconditionally rrmovq r. A, r. B 2 0 r. A

Conditional Move Instructions Move Unconditionally rrmovq r. A, r. B 2 0 r. A r. B • Refer to generically as “cmov. XX” 2 1 r. A r. B • Encodings differ only by “function code” Move When Less or Equal cmovle r. A, r. B Move When Less cmovl r. A, r. B 2 2 r. A r. B • Variants of rrmovq instruction Move When Equal cmove r. A, r. B 2 3 r. A r. B Move When Not Equal cmovne r. A, r. B 2 4 r. A r. B Move When Greater or Equal cmovge r. A, r. B 2 5 r. A r. B Move When Greater cmovg r. A, r. B • Based on values of condition codes 2 6 r. A r. B • (Conditionally) copy value from source to destination register

Jump Instructions Jump (Conditionally) j. XX Dest • • • 7 fn Dest Refer

Jump Instructions Jump (Conditionally) j. XX Dest • • • 7 fn Dest Refer to generically as “j. XX” Encodings differ only by “function code” fn Based on values of condition codes Same as x 86 -64 counterparts Encode full destination address • Unlike PC-relative addressing seen in x 86 -64

Jump Instructions Jump Unconditionally jmp Dest 7 0 Dest Jump When Less or Equal

Jump Instructions Jump Unconditionally jmp Dest 7 0 Dest Jump When Less or Equal jle Dest 7 1 Dest Jump When Less jl Dest 7 2 Dest Jump When Equal je Dest 7 3 Dest Jump When Not Equal jne Dest 7 4 Dest Jump When Greater or Equal jge Dest 7 5 Dest Jump When Greater jg Dest 7 6 Dest

Y 86 -64 Program Stack “Bottom” • Region of memory holding program data •

Y 86 -64 Program Stack “Bottom” • Region of memory holding program data • Used in Y 86 -64 (and x 86 -64) for supporting procedure calls • Stack top indicated by %rsp • Increasing Addresses • 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 %rsp Stack “Top”

Stack Operations pushq r. A popq r. A A 0 r. A F B

Stack Operations pushq r. A popq r. A A 0 r. A F B 0 r. A F • Decrement %rsp by 8 • Store word from r. A to memory at %rsp • Like x 86 -64 • • Read word from memory at %rsp Save in r. A Increment %rsp by 8 Like x 86 -64

Subroutine Call and Return call Dest 8 0 Dest • Push address of next

Subroutine Call and Return call Dest 8 0 Dest • Push address of next instruction onto stack • Start executing instructions at Dest • Like x 86 -64 ret 9 0 • Pop value from stack • Use as address for next instruction • Like x 86 -64

Miscellaneous Instructions nop 1 0 • Don’t do anything halt • • 0 0

Miscellaneous Instructions nop 1 0 • Don’t do anything halt • • 0 0 Stop executing instructions x 86 -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

Status Conditions Mnemonic Code AOK 1 Mnemonic Code HLT 2 Mnemonic Code ADR 3

Status Conditions Mnemonic Code AOK 1 Mnemonic Code HLT 2 Mnemonic Code ADR 3 Mnemonic Code INS 4 • Normal operation • Halt instruction encountered • Bad address (either instruction or data) encountered • Invalid instruction encountered • Desired Behavior • If AOK, keep going • Otherwise, stop program execution

Writing Y 86 -64 Code • Try to Use C Compiler as Much as

Writing Y 86 -64 Code • Try to Use C Compiler as Much as Possible • • Write code in C Compile for x 86 -64 with gcc –Og –S Transliterate into Y 86 -64 Modern compilers make this more difficult • Coding Example • Find number of elements in null-terminated list int len 1(int a[]); a 5043 6125 7395 0 3

Y 86 -64 Code Generation Example • First Try • Write typical array code

Y 86 -64 Code Generation Example • First Try • Write typical array code /* Find number of elements in null-terminated list */ long len(long a[]) { long len; for (len = 0; a[len]; len++) ; return len; } • Compile with gcc -Og -S Problem! • Hard to do array indexing on Y 86 -64 • Since don’t have scaled addressing L 3: modes addq $1, %rax cmpq $0, (%rdi, %rax, 8) jne L 3

Y 86 -64 Code Generation Example #2 • Second Try • Write C code

Y 86 -64 Code Generation Example #2 • Second Try • Write C code that mimics expected Y 86 -64 code long len 2(long *a) { long ip = (long) a; long val = *(long *) ip; long len = 0; while (val) { ip += sizeof(long); len++; val = *(long *) ip; } return len; } • Result • Compiler generates exact same code as before! • Compiler converts both versions into same intermediate form

Y 86 -64 Code Generation Example #3 len: irmovq $1, %r 8 irmovq $8,

Y 86 -64 Code Generation Example #3 len: irmovq $1, %r 8 irmovq $8, %r 9 irmovq $0, %rax mrmovq (%rdi), %rdx andq %rdx, %rdx je Done Loop: addq %r 8, %rax addq %r 9, %rdi mrmovq (%rdi), %rdx andq %rdx, %rdx jne Loop Done: ret # # # Constant 1 Constant 8 len = 0 val = *a Test val If zero, goto Done # # # len++ a++ val = *a Test val If !0, goto Loop Register Use %rdi a %rax len %rdx val %r 8 1 %r 9 8

Y 86 -64 Sample Program Structure #1 init: . . . call Main halt

Y 86 -64 Sample Program Structure #1 init: . . . call Main halt # Initialization . align 8 array: . . . # Program data Main: . . . call len # Main function • Program starts at address 0 • Must set up stack • Where located • Pointer values • Make sure don’t overwrite code! • Must initialize data . . . len: . . . # Length function . pos 0 x 100 Stack: # Placement of stack

Y 86 -64 Program Structure #2 init: # Set up stack pointer irmovq Stack,

Y 86 -64 Program Structure #2 init: # Set up stack pointer irmovq Stack, %rsp # Execute main program call Main # Terminate halt # Array of 4 elements + terminating 0. align 8 Array: . quad 0 x 000 d. quad 0 x 00 c 000 c 0. quad 0 x 0 b 000 b 00. quad 0 xa 000 a 000. quad 0 • • Program starts at address 0 Must set up stack Must initialize data Can use symbolic names

Y 86 -64 Program Structure #3 Main: irmovq array, %rdi # call len(array) call

Y 86 -64 Program Structure #3 Main: irmovq array, %rdi # call len(array) call len ret • Set up call to len • Follow x 86 -64 procedure conventions • Push array address as argument

Assembling Y 86 -64 Program unix> yas len. ys • Generates “object code” file

Assembling Y 86 -64 Program unix> yas len. ys • Generates “object code” file len. yo • Actually looks like disassembler output 0 x 054: 0 x 05 e: 0 x 068: 0 x 072: 0 x 07 c: 0 x 07 e: 0 x 087: 0 x 089: 0 x 08 b: 0 x 095: 0 x 097: 0 x 0 a 0: 30 f 8010000000 30 f 9080000000 30 f 000000000 502700000000 6222 73 a 00000000 6080 6097 502700000000 6222 74870000000 90 | len: | irmovq $1, %r 8 | irmovq $8, %r 9 | irmovq $0, %rax | mrmovq (%rdi), %rdx | andq %rdx, %rdx | je Done | Loop: | addq %r 8, %rax | addq %r 9, %rdi | mrmovq (%rdi), %rdx | andq %rdx, %rdx | jne Loop | Done: | ret # # # Constant 1 Constant 8 len = 0 val = *a Test val If zero, goto Done # # # len++ a++ val = *a Test val If !0, goto Loop

Simulating Y 86 -64 Program unix> yis len. yo • Instruction set simulator •

Simulating Y 86 -64 Program unix> yis len. yo • Instruction set simulator • Computes effect of each instruction on processor state • Prints changes in state from original Stopped Changes %rax: %rsp: %rdi: %r 8: %r 9: in 33 steps at PC = 0 x 13. Status 'HLT', CC Z=1 S=0 O=0 to registers: 0 x 000000004 0 x 00000000 0 x 0000000100 0 x 0000000000000038 0 x 000000001 0 x 000000008 Changes to memory: 0 x 00 f 0: 0 x 00000000 0 x 00 f 8: 0 x 00000000 0 x 000000053 0 x 000000013

CISC Instruction Sets • Complex Instruction Set Computer • IA 32 is example •

CISC Instruction Sets • Complex Instruction Set Computer • IA 32 is example • Stack-oriented instruction set • Use stack to pass arguments, save program counter • Explicit push and pop instructions • Arithmetic instructions can access memory • addq %rax, 12(%rbx, %rcx, 8) • requires memory read and write • Complex address calculation • Condition codes • Set as side effect of arithmetic and logical instructions • Philosophy • Add instructions to perform “typical” programming tasks

RISC Instruction Sets • Reduced Instruction Set Computer • Internal project at IBM, later

RISC Instruction Sets • Reduced Instruction Set Computer • Internal project at IBM, later popularized by Hennessy (Stanford) and Patterson (Berkeley) • Fewer, simpler instructions • Might take more to get given task done • Can execute them with small and fast hardware • Register-oriented instruction set • Many more (typically 32) registers • Use for arguments, return pointer, temporaries • Only load and store instructions can access memory • Similar to Y 86 -64 mrmovq and rmmovq • No Condition codes • Test instructions return 0/1 in register

MIPS Registers

MIPS Registers

MIPS Instruction Examples R-R Op Ra Rb addu $3, $2, $1 R-I Op Ra

MIPS Instruction Examples R-R Op Ra Rb addu $3, $2, $1 R-I Op Ra Rb Ra Fn Immediate # Immediate add: $3 = $2+3145 # Shift left: $3 = $2 << 2 Rb beq $3, $2, dest Load/Store Op 00000 # Register add: $3 = $2+$1 addu $3, $2, 3145 sll $3, $2, 2 Branch Op Ra Rd Offset # Branch when $3 = $2 Rb Offset lw $3, 16($2) # Load Word: $3 = M[$2+16] sw $3, 16($2) # Store Word: M[$2+16] = $3

CISC vs. RISC • Original Debate • Strong opinions! • CISC proponents---easy for compiler,

CISC vs. RISC • Original Debate • Strong opinions! • CISC proponents---easy for compiler, fewer code bytes • RISC proponents---better for optimizing compilers, can make run fast with simple chip design • Current Status • For desktop processors, choice of ISA not a technical issue • With enough hardware, can make anything run fast • Code compatibility more important • x 86 -64 adopted many RISC features • More registers; use them for argument passing • For embedded processors, RISC makes sense • Smaller, cheaper, less power • Most cell phones use ARM processor

Summary • Y 86 -64 Instruction Set Architecture • Similar state and instructions as

Summary • Y 86 -64 Instruction Set Architecture • Similar state and instructions as x 86 -64 • Simpler encodings • Somewhere between CISC and RISC • How Important is ISA Design? • Less now than before • With enough hardware, can make almost anything go fast