54
Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr. Rozier (UM)

Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Embed Size (px)

Citation preview

Page 1: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Lecture 2: Basic Instructions

EEN 312: Processors: Hardware, Software, and Interfacing

Department of Electrical and Computer Engineering

Spring 2014, Dr. Rozier (UM)

Page 2: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

COURSE PLAN FOR TODAY

1.Lab 1 Introduction2.Introduction to debugging with GDB3.Introduction to Instructions4.Lab 1 GDB demo

Page 3: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

LAB 1

Page 4: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Lab 1: Binary Bomb

• Quick change to the syllabus– Due dates will now be the Monday before the next

lab goes out. Gives everyone an automatic extension for all labs.

• Lab 1 is out today• Due Monday, February 3rd at 11:59pm.

Page 5: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Lab 1: Binary Bomb

• Your task is to solve a series of six stages by finding the password.

• You will get a unique compiled binary.

• You will need to disassemble this binary to find the passwords.

Page 6: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Lab 1: Binary Bomb

• If you enter a wrong password, the bomb will “explode” and notify us.

• The bomb has tamper proof protections. Do not try to run it on a non-lab computer, or it will notify us and abort!

• Each explosion deducts half apoint from your lab score.

Page 7: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Lab 1: Binary Bomb

Assignment Part Points

Phase 1 10

Phase 2 10

Phase 3 10

Phase 4 10

Phase 5 15

Phase 6 15

Write up 10

Explosions -0.5 each

80 pts maximum score

Page 8: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Lab 1: Binary Bomb

• If you do things right, your bomb should never blow up! Think carefully!

• Use the debugger gdb to step through and analyze the program.– Figure out what the code is doing to check for a

correct result, and how to pass the checks.

Page 9: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Lab 1: Binary Bomb

• Start early! The lab will not be easy!

• Bomb download site:– http://een312.performalumni.org:15213/

• Bomb scoreboard:– http://een312.performalumni.org:15213/scoreboa

rd

Page 10: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

GDB: THE GNU DEBUGGER

Page 11: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

gdb: The GNU Debugger

• Standard and portable debugger for Unix and Unix-like systems.– Originally written in 1986– Very active tool. Three software releases in 2013.– Still the gold-standard for debugging

• Enables users to trace, alter, and execute computer programs in a controlled environment.

Page 12: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

gdb: The GNU Debugger

• Most useful features– Step through program execution, line by line, or

instruction by instruction.– Examine the values of variables and registers.– Trap system signals.– Set breakpoints to halt execution at any point.– Watch variables to see when they change.

Page 13: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

gdb: The GNU Debugger

• Some commands– run – executes the program– break <NAME> - sets a breakpoint at label

<NAME>– break *<ADDRESS> - sets a breakpoint at the

address <ADDRESS>– print <REGISTER> - prints the register’s value– stepi – step through one assembly instruction

Page 14: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

gdb: The GNU Debugger

• Some commands– disas <NAME> - disassemble the code at label <NAME>.– continue – continue execution after halting at a

breakpoint.– info [break|<REGISTER>] - give information about

breakpoints or registers– info r – display the value of all registers– x/<FMT> <ADDRESS|REGISTER> - display the value

stored at <ADDRESS|REGISTER> in the format specified by <FMT>

Page 15: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

gdb: The GNU Debugger

We will show an example towards the end of class of the debugger in action.

Page 16: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

BASIC INSTRUCTIONS

Page 17: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Instruction Set

• The repertoire of instructions of a computer• Different computers have different instruction

sets– But with many aspects in common

• Early computers had very simple instruction sets– Simplified implementation

• Many modern computers also have simple instruction sets

Page 18: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

MIPS vs ARMv6

• The book uses the MIPS instruction set.• We will be using ARMv6 in our labs.

• Both are RISC (reduced instruction set computer) architectures.– Many similarities.

Page 19: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

MIPS

• Used in many embedded systems– Routers, gateways– Playstation 2 and PSP

• Invented by Prof John Hennessy at Stanford, the first RISC architecture.

Page 20: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

ARM

• Introduced in 1985• Focused on low-power friendly operation.• Since 2005, over 98% of all mobile phones had

at least one ARM processor.• Over 37 billion ARM processors in use in 2013.

• Rapidly becoming the dominant processor architecture in the world.

Page 21: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Instructions

• C code:– f = (g + h) – (i + j);

• Compile ARM code:– add r0, r3, r4 # temp t0 = g + h– add r1, r5, r6 # temp t1 = i + j– sub r2, r0, r1 # f = t0 – t1

Page 22: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Register Operands

• Instructions use registers for operands.• Registers are extremely fast SRAM locations

that are directly accessible by the processor.– Very fast, but very expensive, so very small.

Page 23: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Registers

• Each register holds a word (4 bytes).• Registers r0-r12 are general purpose.

Name Function Name Function

r0 General Purpose r8 General Purpose

r1 General Purpose r9 General Purpose

r2 General Purpose r10 General Purpose

r3 General Purpose r11 General Purpose

r4 General Purpose r12 General Purpose

r5 General Purpose r13 Stack Pointer

r6 General Purpose r14 Link Register

r7 General Purpose r15 Program Counter

Page 24: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Registers

• Registers r13 – r15 have special purposes• The PC, r15, is very dangerous.

Name Function Name Function

r0 General Purpose r8 General Purpose

r1 General Purpose r9 General Purpose

r2 General Purpose r10 General Purpose

r3 General Purpose r11 General Purpose

r4 General Purpose r12 General Purpose

r5 General Purpose r13 Stack Pointer

r6 General Purpose r14 Link Register

r7 General Purpose r15 Program Counter

Page 25: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Registers

• The register r13 holds the stack pointer– Also called sp– Points to a special part of memory called the

stack.– More about this later.

Page 26: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Registers

• The register r14 holds the link register– Also called lr– Holds the value of a return address that allows for

fast and efficient implementation of subroutines.

Page 27: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Registers

• The register r15 holds the program counter– Also called pc– Holds an address of an instruction. Keeps track of

where your program is in its execution of machine code.

– PC holds the address of the instruction to be fetched next.

Page 28: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Registers

• One additional register, the “current program status register”

• Four most significant bits hold flags which indicate the presence or absence of certain conditions.

31 30 29 28 27…8 7 6 5 4…0

N Z C V Reserved I F T MODE

Page 29: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Registers

• N – negative flag• Z – zero flag• C – carry flag• V – overflow flag

31 30 29 28 27…8 7 6 5 4…0

N Z C V Reserved I F T MODE

Page 30: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Registers

• N – set by an instruction if the result is negative (set equal to the two’s complement sign bit)

• N – negative flag• Z – zero flag• C – carry flag• V – overflow flag

31 30 29 28 27…8 7 6 5 4…0

N Z C V Reserved I F T MODE

Page 31: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Registers

• Z – set by an instruction if the result of the instruction is zero.

• N – negative flag• Z – zero flag• C – carry flag• V – overflow flag

31 30 29 28 27…8 7 6 5 4…0

N Z C V Reserved I F T MODE

Page 32: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Registers

• C – set by an instruction if the result of an unsigned operation overflows the 32-bit register. Can be used for 64-bit arithmetic

• N – negative flag• Z – zero flag• C – carry flag• V – overflow flag

31 30 29 28 27…8 7 6 5 4…0

N Z C V Reserved I F T MODE

Page 33: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Registers

• V – works the same as the C flag, but for signed operations.

• N – negative flag• Z – zero flag• C – carry flag• V – overflow flag

31 30 29 28 27…8 7 6 5 4…0

N Z C V Reserved I F T MODE

Page 34: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

MORE ABOUT THESE LATER…

Page 35: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

The Memory Hierarchy

Page 36: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Load-Store Architecture

• RISC architectures, like ARM and MIPS utilize a load-store architecture.

• Memory cannot be part of arithmetic operations.– Only registers can do this

• Access memory is through loads and stores.

Page 37: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Register Memory Architecture

• Featured on many CISC architectures, like x86

• Allows direct access to memory by instructions.

Page 38: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Load Store and ARM

• Register space is pretty cramped!!!• LoaD to a Register with LDR• SToRe to memory with STR• ldr <register>, [<base>{,<offset>}]

– Loads a byte from <base>+<offset> into <register>

• str <register>, [<base>{,<offset>}]– Stores a byte from <register> into <base>+<offset>

Page 39: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Load Store and ARM

• Example– ldr r0, [r1,r2]

• Load data from location r1+r2 into r0.

– ldr r0, =string• Load data from label string into r0.

• Special cases exist, see ARM manual– Example: ldrb loads a single byte, padded with

zeros.

Page 40: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Constants or Immediates

• Operands can contain registers, or immediate values.– An immediate is like a constant– Represent immediates as follows:

• #20• add r0, r1, #20 – adds 20 to the value of r1 and stores it

in r0.

Page 41: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Arithmetic Instructions

• Addition– add, adc, adds, etc

• Subtraction– sub, sbc, rsb, subs, etc

• Multiply– mul, mla, etc

Page 42: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Move Instruction

• mov <destination>, <operand>– mov r0, r1 – copy the contents of r1 into r0.– mov r0, #20 – copy an immediate value of 20 into

r0.

• mvn <destination>, <operand>– Move negative, negates operand before copying it.

Page 43: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Compare Instructions

• cmp <operand1>, <operand2>• cmn <operand1>, <operand2>

• Don’t change the operands, update special status register flags.

• cmp – subtracts operand2 from operand1 and discards the result.

• cmn – adds operand2 to operand1 and discards the result.

Page 44: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Status Register Flags

• Compare instructions and the special “S” versions of instructions (adds, subs, movs) set the status register flags.

• Can be used with conditional suffixes to make conditionally executed instructions.

Page 45: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Conditional Execution

• Just as the special “S” suffix can be added to set status flags, other suffixes can be added to act on status flags.

Page 46: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

EQ: Equal Z=1

• Using the EQ suffix on an instruction will cause it to only be executed if the zero flag is set.

cmp r0, r1 @ Set flags based on r0-r1

adds r0, r1, r2 @ Set flags based on r0 = r1 + r2

movs r0, r1 @ Set flags based on r0 = r1

Page 47: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

EQ: Equal Z=1

• Using the EQ suffix on an instruction will cause it to only be executed if the zero flag is set.

Examplecmp r0, r1 @ Set flags based on r0-r1addeq r2, r0, r1 @ Conditional addition

Page 48: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

NE: Equal Z=0

• Using the NE suffix on an instruction will cause it to only be executed if the zero flag is not set.

Page 49: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Other conditional suffixes

• VS – overflow set, V=1• VC – overflow clear, V=0• MI – minus set, N=1• PL – minus clear, N=0• CS – carry set, C=1• CC – carry clear, C=0• AL – always, unconditional• NV – never, unconditional

Page 50: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Multiple Conditional Suffixes

• HI – higher (unsigned), C=1 and Z=0– Unsigned greater than

• LS – lower (unsigned), C=0 and Z=1– Unsigned less than

• GE – greater or equal (signed), N=1, V=1 OR N=0, V=0– Signed greater than or equal to

• LT – less than (signed), N=1, V=0, OR N=0,V=1– Signed less than

Page 51: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

Multiple Conditional Suffixes

• GT – greater than (signed), N=1, V=1, OR N=0, V=0 AND Z=0– Signed greater than

• LE – less than or equal (signed), N=1, V=0, OR N=0, V=1, OR Z=1– Signed less than or equal to

Page 52: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

LAB 1 GDB DEMO

Page 53: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

WRAP UP

Page 54: Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr

For next time

• Read Chapter 2, Sections 2.6 – 2.8

• Learn about control, branch, loops, etc.