Assembly language Simple, regular instructions building blocks of C, Java & other languages Typically one-to-one mapping to machine language

Similar documents
Assembly language Simple, regular instructions building blocks of C, Java & other languages Typically one-to-one mapping to machine language

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009

CS 351 Exam 2 Wed. 4/5/2017

CS 351 Exam 2, Section 1 Wed. 11/2/2016

We will begin our study of computer architecture From this perspective. Machine Language Control Unit

EE/CSE469 Review Problem 0

Instructions: Language of the Computer

Computer Architecture

Computer Organization MIPS ISA

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Chapter 2A Instructions: Language of the Computer

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

ARM Shift Operations. Shift Type 00 - logical left 01 - logical right 10 - arithmetic right 11 - rotate right. Shift Amount 0-31 bits

Lecture 12: Single-Cycle Control Unit. Spring 2018 Jason Tang

Comparison InstruCtions

CENG3420 Lecture 03 Review

CS3350B Computer Architecture MIPS Instruction Representation

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

Course Administration

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

MIPS Memory Access Instructions

CS222: MIPS Instruction Set

EGC-442 Introduction to Computer Architecture Dr. Izadi Course Design Project (100 Points)

Today s topics. MIPS operations and operands. MIPS arithmetic. CS/COE1541: Introduction to Computer Architecture. A Review of MIPS ISA.

Chapter 2. Instructions: Language of the Computer. HW#1: 1.3 all, 1.4 all, 1.6.1, , , , , and Due date: one week.

Lecture 4: MIPS Instruction Set

COMPUTER ORGANIZATION AND DESIGN. ARM Edition. The Hardware/Software Interface. Chapter 2. Instructions: Language of the Computer

Datapath & Control. Readings: Computer Processor. Control. Input. Datapath. Output

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

CSCE 5610: Computer Architecture

Lecture 4: Instruction Set Architecture

ECE232: Hardware Organization and Design. Computer Organization - Previously covered

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

Dynamic Branch Prediction

Architecture. Digital Computer Design

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

Input. Output. Datapath: System for performing operations on data, plus memory access. Control: Control the datapath in response to instructions.

STEVEN R. BAGLEY ARM: PROCESSING DATA

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls

CENG3420 L03: Instruction Set Architecture

CprE 288 Introduction to Embedded Systems Course Review for Exam 3. Instructors: Dr. Phillip Jones

Programmable Machines

Programmable Machines

CMSC Computer Architecture Lecture 2: ISA. Prof. Yanjing Li Department of Computer Science University of Chicago

CSIS1120A. 10. Instruction Set & Addressing Mode. CSIS1120A 10. Instruction Set & Addressing Mode 1

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

Topic Notes: MIPS Instruction Set Architecture

Instructions: MIPS ISA. Chapter 2 Instructions: Language of the Computer 1

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Chapter 2. Instructions:

Hi Hsiao-Lung Chan, Ph.D. Dept Electrical Engineering Chang Gung University, Taiwan

CprE 288 Introduction to Embedded Systems ARM Assembly Programming: Translating C Control Statements and Function Calls

We will study the MIPS assembly language as an exemplar of the concept.

Instructions: MIPS arithmetic. MIPS arithmetic. Chapter 3 : MIPS Downloaded from:

EN164: Design of Computing Systems Topic 03: Instruction Set Architecture Design

EEC 581 Computer Architecture Lecture 1 Review MIPS

Forecast. Instructions (354 Review) Basics. Basics. Instruction set architecture (ISA) is its vocabulary. Instructions are the words of a computer

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

INSTRUCTION SET COMPARISONS

COMP2121: Microprocessors and Interfacing. Instruction Set Architecture (ISA)

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

The Assembly Language of the Boz 5

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

EEM870 Embedded System and Experiment Lecture 4: ARM Instruction Sets

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

The PAW Architecture Reference Manual

Unsigned Binary Integers

Unsigned Binary Integers

Instruction Set Architecture. "Speaking with the computer"

Processor Status Register(PSR)

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

Mark Redekopp, All rights reserved. EE 357 Unit 11 MIPS ISA

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

Cortex M3 Programming

ECE 486/586. Computer Architecture. Lecture # 7

All instructions have 3 operands Operand order is fixed (destination first)

Page 1. Structure of von Nuemann machine. Instruction Set - the type of Instructions

Reconfigurable Computing Systems ( L) Fall 2012 Tiny Register Machine (TRM)

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

Writing ARM Assembly. Steven R. Bagley

A Bit of History. Program Mem Data Memory. CPU (Central Processing Unit) I/O (Input/Output) Von Neumann Architecture. CPU (Central Processing Unit)

Processor. Han Wang CS3410, Spring 2012 Computer Science Cornell University. See P&H Chapter , 4.1 4

Computer Architecture

MIPS ISA. 1. Data and Address Size 8-, 16-, 32-, 64-bit 2. Which instructions does the processor support

Processor design - MIPS

COMPUTER ORGANIZATION AND DESIGN

5/17/2012. Recap from Last Time. CSE 2021: Computer Organization. The RISC Philosophy. Levels of Programming. Stored Program Computers

Chapter 3. Instructions:

Recap from Last Time. CSE 2021: Computer Organization. Levels of Programming. The RISC Philosophy 5/19/2011

Flow Control In Assembly

Math 230 Assembly Programming (AKA Computer Organization) Spring MIPS Intro

Computer Systems Architecture

Computer Organization MIPS Architecture. Department of Computer Science Missouri University of Science & Technology

Topic 10: Instruction Representation

Assembly Language Design

This simulated machine consists of four registers that will be represented in your software with four global variables.

A First Look at Microprocessors

ISA and RISCV. CASS 2018 Lavanya Ramapantulu

Transcription:

Assembly Language Readings: 2.1-2.7, 2.9-2.10, 2.14 Green reference card Assembly language Simple, regular instructions building blocks of C, Java & other languages Typically one-to-one mapping to machine language Our goal Understand the basics of assembly language Help figure out what the processor needs to be able to do Not our goal to teach complete assembly/machine language programming Floating point Procedure calls Stacks & local variables 9 Aside: C/C++ Primer struct coord { int x, y; }; /* Declares a type */ struct coord start; /* Object with two slots, x and y */ start.x = 1; /* For objects. accesses a slot */ struct coord *myloc; /* * is a pointer to objects */ myloc = &start; /* & returns thing s location */ myloc->y = 2; /* -> is * plus. */ x y int scores[8]; /* 8 ints, from 0..7 */ scores[1]=5; /* Access locations in array */ int *index = scores; /* Points to scores[0] */ index++; /* Next scores location */ (*index)++; /* * works in arays as well */ index = &(scores[3]); /* Points to scores[3] */ *index = 9; 0 1 2 3 4 5 6 7 10

ARM Assembly Language The basic instructions have four components: Operator name Destination 1 st operand 2 nd operand ADD <dst>, <src1>, <src2> SUB <dst>, <src1>, <src2> // <dst> = <src1> + <src2> // <dst> = <src1> - <src2> Simple format: easy to implement in hardware More complex: A = B + C + D E 11 Operands & Storage For speed, CPU has 32 general-purpose registers for storing most operands For capacity, computer has large memory (multi-gb) Computer Processor Memory Devices Control Datapath GPRs Input Output Load/store operation moves information between registers and main memory All other operations work on registers 12

Registers 32x 64-bit registers for operands Register Function Comment X0-X7 Function arguments/results X8 Result, if a pointer X9-X15 Volatile Temporaries Not saved on call X16-X17 Linker scratch registers Don t use them X18 Platform register Don t use this X19-X27 Temporaries (saved across calls) Saved on call X28 Stack Pointer X29 Frame Pointer X30 Return Address X31 Always 0 No-op on write 13 Basic Operations (Note: just subset of all instructions) Mathematic: ADD, SUB, MUL, SDIV ADD X0, X1, X2 // X0 = X1+X2 Immediate (one input a constant) ADDI X0, X1, #100 // X0 = X1+100 Logical: AND, ORR, EOR AND X0, X1, X2 // X0 = X1&X2 Immediate ANDI X0, X1, #7 // X0 = X1&b0111 Shift: left & right logical (LSL, LSR) LSL X0, X1, #4 // X0 = X1<<4 Example: Take bits 6-4 of X0 and make them bits 2-0 of X1, zeros otherwise: 14

Memory Organization Viewed as a large, single-dimension array, with an address. A memory address is an index into the array "Byte addressing" means that the index points to a byte of memory. 0 1 2 3 4 5 6... 8 bits of data 8 bits of data 8 bits of data 8 bits of data 8 bits of data 8 bits of data 8 bits of data 15 Memory Organization (cont.) Bytes are nice, but most data items use larger units. Double-word = 64 bits = 8 bytes Word = 32 bits = 4 bytes 0 8 16 24 64 bits of data 64 bits of data 64 bits of data 64 bits of data Registers hold 64 bits of data 2 64 bytes with byte addresses from 0 to 2 64-1 2 61 double-words with byte addresses 0, 8, 16,... 2 64-8 Double-words and words are aligned i.e., what are the least 3 significant bits of a double-word address? 16

Addressing Objects: Endian and Alignment Doubleword: 2 63..2 56 2 55..2 48 2 47..2 40 2 39..2 32 2 31..2 24 2 23..2 16 2 15..2 8 2 7..2 0 Big Endian 000 001 010 011 100 101 110 111 2 63..2 56 2 55..2 48 2 47..2 40 2 39..2 32 2 31..2 24 2 23..2 16 2 15..2 8 2 7..2 0 0 8 16 24 32 DWord 0 DWord 1 DWord 2 DWord 3 DWord 4 Little Endian 000 001 010 011 100 101 110 111 2 7..2 0 2 15..2 8 2 23..2 16 2 31..2 24 2 39..2 32 2 47..2 40 2 55..2 48 2 63..2 56 Big Endian: address of most significant byte = doubleword address Motorola 68k, MIPS, IBM 360/370, Xilinx Microblaze, Sparc Little Endian: address of least significant byte = doubleword address Intel x86, DEC Vax, Altera Nios II, Z80 ARM: can do either this class assumes Little-Endian. 17 Data Storage Characters: 8 bits (byte) Integers: 64 bits (word) Array: Sequence of locations Pointer: Address (64 bits) // G = ASCII 71 char a = G ; int x = 258; char *b; int *y; b = new char[4]; y = new int[10]; 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1046 1045 1047 (Note: real compilers place local variables (the stack ) from beginng of memory, new ed structures (the heap ) from end. We ignore that here for simplicity) 18

Loads & Stores Loads & Stores move data between memory and registers All operations on registers, but too small to hold all data LDUR X0, [X1, #14] STUR X2, [X3, #20] // X0 = Memory[X1+14] // Memory[X3+20] = X2 General Purpose Registers X0: X1: X2: X3: 130 723 4 Load Store Memory 24: 144: 66 Note: LDURB & STURB load & store bytes 19 Addressing Example The address of the start of a character array is stored in X0. Write assembly to load the following characters X2 = Array[0] X3 = Array[1] X4 = Array[2] X5 = Array[k] // Assume the value of k is in X1 20

Array Example /* Swap the kth and (k+1)th element of an array */ swap(int v[], int k) { int temp = v[k]; v[k] = v[k+1]; X0: GPRs 928 } v[k+1] = temp; X1: 10 X2: X3: // Assume v in X0, k in X1 X4: Load Store Memory 1000 0A12170D34BC2DE1 1008 1111111111111111 1016 0000000000000000 1024 0F0F0F0F0F0F0F0F 1032 FFFFFFFFFFFFFFFF 1040 FFFFFFFFFFFFFFFF 21 Execution Cycle Example PC: Program Counter IR: Instruction Register Note: Word addresses Instructions are 32b General Purpose Registers X0: X1: X2: X3: X4: PC: IR: 928 10 Load Store Memory 0000 D3600C22 0004 8B020002 0008 F8400043 0012 F8408044 0016 F8400044 0020 F8408043 1000 1008 1016 1024 1032 1040 0A12170D34BC2DE1 1111111111111111 0000000000000000 0F0F0F0F0F0F0F0F FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF Instruction Fetch Instruction Decode Operand Fetch Execute Result Store Next Instruction 22

Flags/Condition Codes Flag register holds information about result of recent math operation Negative: was result a negative number? Zero: was result 0? Overflow: was result magnitude too big to fit into 64-bit register? Carry: was the carry-out true? Operations that set the flag register contents: ADDS, ADDIS, ANDS, ANDIS, SUBS, SUBIS, some floating point. Most commonly used are subtracts, so we have a synonym: CMP CMP X0, X1 same as SUBS X31, X0, X1 CMP X0, #15 same as SUBIS X31, X0, #15 23 Control Flow Unconditional Branch GOTO different next instruction B START // go to instruction labeled with START tag BR X30 // go to address in X30: PC = value of X30 Conditional Branches GOTO different next instruction if condition is true 1 register: CBZ (==0), CBNZ (!= 0) CBZ X0, FOO // if X0 == 0 GOTO FOO: PC = FOO 2 register: B.LT (<), B.LE(<=), B.GE (>=), B.GT(>), B.EQ(==), B.NE(!=) first compare (CMP X0, X1, CMPI X0, #12), then b.cond instruction CMP X0, X1 // compare X0 with X1 same as SUBS X31, X0, X1 B.EQ FOO // if X0 == X1 GOTO FOO: PC = FOO if (a == b) a = a + 3; else b = b + 7; c = a + b; // X0 = a, X1 = b, X2 = c CMP X0, X1 // set flags B.NE ELSEIF // branch if a!=b ADDI X0, X0, 3 // a = a + 3 B DONE // avoid else ELSEIF: ADDI X1, X1, 7 // b = b + 7 DONE: ADD, X2, X0, X1 // c = a + b 24

Loop Example Compute the sum of the values 0 N-1 int sum = 0; for (int I = 0; I!= N; I++) { sum += I; } // X0 = N, X1 = sum, X2 = I 25 String toupper Convert a string to all upper case char *index = string; while (*index!= 0) { /* C strings end in 0 */ if (*index >= a && *index <= z ) *index = *index +( A - a ); index++; } // string is a pointer held at Memory[80]. // X0=index, A = 65, a = 97, z = 122 26

Machine Language vs. Assembly Language Assembly Language mnemonics for easy reading labels instead of fixed addresses Easier for programmers Almost 1-to-1 with machine language Machine language Completely numeric representation format CPU actually uses SWAP: LSL X9, X1, #3 ADD X9, X0, X9 // Compute address of v[k] LDUR X10, [X9, #0] // get v[k] LDUR X11, [X9, #8] // get v[k+1] STUR X11, [X9, #0] // save new value to v[k] STUR X10, [X9, #8] // save new value to v[k+1] BR X30 // return from subroutine 11010011011 00000 000011 00001 01001 10001011000 01001 000000 00000 01001 11111000010 000000000 00 01001 01010 11111000010 000001000 00 01001 01011 11111000000 000000000 00 01001 01011 11111000000 000001000 00 01001 01010 11010110000 00000 000000 00000 11110 27 Labels Labels specify the address of the corresponding instruction Programmer doesn t have to count line numbers Insertion of instructions doesn t require changing entire code // X0 = N, X1 = sum, X2 = I ADD X1, X31, X31 // sum = 0 ADD X2, X31, X31 // I = 0 TOP: CMP X2, X0 // Check I vs N B.GE END // end when!(i<n) ADD X1, X1, X2 // sum += I ADDI X2, X2, #1 // I++ B TOP // next iteration END: Notes: Branches are PC-relative PC = PC + 4*(BranchOffset) BranchOffset positive -> branch downward. Negative -> branch upward. 28

Labels Example Compute the value of the labels in the code below. Branches: PC = PC + 4*(BranchOffset) // Program starts at address 100 LDUR X0, [X31, #100] LOOP: LDURB X1, [X0, #0] CBZ X1, END CMPI X1, #97 B.LT NEXT CMPI X1, #122 B.GT NEXT SUBI X1, X1, #32 STURB X1, [X0, #0] NEXT: ADDI X0, X0, 1 B LOOP END: 29 Instruction Types Can group instructions by # of operands 3-register 2-register 1-register 0-register ADD X0, X1, X2 ADDI X0, X1, #100 AND X0, X1, X2 ANDI X0, X1, #7 LSL X0, X1, #4 LSR X0, X1, #2 LDUR X0, [X1, #14] LDURB X0, [X1, #14] STUR X0, [X1, #14] STURB X0, [X1, #14] B START BR X30 CBZ X0, FOO B.EQ DEST 30

Instruction Formats All instructions encoded in 32 bits (operation + operands/immediates) Branch (B-Type) Instr[31:21] = 0A0-0BF Opcode Conditional Branch (CB-Type) Instr[31:21] = 2A0-2A7, 5A0-5AF Register (R-Type) Instr[31:21] = 450-458, 4D6-558, 650-658, 69A-758 Immediate (I-Type) Instr[31:21] = 488-491, 588-591, 688-691, 788-791 Memory (D-Type) BrAddr26 Opcode CondAddr19 Rd Opcode Rm SHAMT Rn Rd Opcode ALU_Imm12 Rn Rd Instr[31:21] = 1C0-1C2, 7C0-7C2 Opcode DT_Address9 00 Rn Rd 31 B-Type Used for unconditional branches 0 0 0 1 0 1 BrAddr26 0x05: B B -3 // PC = PC + 4*-3 32

CB-Type Used for conditional branches Opcode CondAddr19 Rd 0x54: B.cond 0xB4: CBZ 0xB5: CBNZ Condition Codes 0x00: EQ (==) 0x01: NE (!=) 0x0A: GE (>=) 0x0B: LT (<) 0x0C: GT (>) 0x0D: LE (<=) Reg or Cond. Code CBZ X12, -3 // if(x12==0) PC = PC + 4*-3 B.LT -5 // if (lessthan) PC = PC + 4*-5 33 R-Type Used for 3 register ALU operations and shift Opcode Rm SHAMT Rn Rd 0x450: AND 0x458: ADD 0x4D6: SDIV, shamt=02 0x4D8: MUL, shamt=1f 0x550: ORR 0x558: ADDS 0x650: EOR 0x658: SUB 0x69A: LSR 0x69B: LSL 0x6B0: BR, rest all 0 s but Rd 0x750: ANDS 0x758: SUBS Op2 (0 for shift) Shift amount (0 for non-shift) Op1 Dest ADD X3, X5, X6 // X3 = X5+X6 LSL X10, X4, #6 // X10 = X4<<6 34

I-Type Used for 2 register & 1 constant ALU operations Opcode ALU_Imm12 Rn Rd 0x244: ADDI 0x248: ANDI 0x164: ADDIS 0x168: ORRI 0x344: SUBI 0x348: EORI 0x2C4: SUBIS 0x2C8: ANDIS Constant - Op2 Op1 Dest ADDI X8, X3, #35 // X8 = X3+35 35 D-Type Used for memory accesses Opcode DAddr9 00 Rn Rd 0x1C0: STURB 0x1C2: LDURB 0x7C0: STUR 0x7C2: LDUR Address Constant Address Reg Target Reg LDUR X6, [X15, #12] // X6 = Memory[X15+12] 36

Conversion example Compute the sum of the values 0 N-1 ADD X1, X31, X31 ADD X2, X31, X31 B TEST TOP: ADD X1, X1, X2 ADDI X2, X2, #1 TEST: SUBS X31, X2, X0 B.LT TOP END: 37 Assembly & Machine Language Assembly Machine Language 38