280 likes | 299 Views
Learn about basic instructions, operand size indicators, register transfer notation, and addressing modes in assembly language programming. Understand when to use different modes with examples and common instructions.
E N D
Assembly Language Programming Introduction and Addressing Modes
Intro to Assembly Language: Goals • Introduction to Assembly language • Basic instructions: MOVE, ADD, EXT, etc. • Operand size (B, W, L) • Register Transfer Notation (RTN) • Addressing Modes • Register direct, immediate, absolute long, register indirect, indexed basic, autoincrement, autodecrement • When to use the various modes. • Assigned Reading • HVZ Chapter 2.4, 2.5, 2.6, 3.8, 3.9, 3.10, 3.11 • Reference: HVZ Appendix C
Introduction: Basic Instructions • Instructions begin with a mnemonic which represents the operation to be performed. • MOVE, ADD, SUB, CLR, etc. • The mnemonic is followed by a character representing the length of the data to be operated on. • Byte (B), Word (W), and Long Word (L) • In most instructions, the instruction is followed by one or more operands. • How an operand is interpreted depends on the information provided and the addressing mode used for that operand.
Introduction: A Few Example Instructions • ADD/SUB/MUL/DIV/AND/OR source, dest • Performs operation on dest and source and stores result in dest. • NEG location • Computes 2’s complement of value in location, then stores it into location. • NOT location • Computes complement of value in location, then stores it into location. • CLR location • Sets value of byte/word/long at location to 0. • MOVE source, dest • Moves value of source to dest location.
Assembly: Operand size • Because the 68000 is capable of performing operations on bytes, words and long words: • In 68000 assembly language, a size indicator is appended to the instruction mnemonic (W=word, B=byte, L=long word): • MOVE.W, MOVE.B, MOVE.L, ADD.W, ADD.B, and ADD.L are examples of this. • If the size indicator is omitted, a WORD operation is assumed.
Assembly: Common Instructions - Examples • MOVE • MOVE.L D1, D2 • D2 = D1 • Contents of long word in D1 is copied into D2. • Destroys contents of D2! • Contents of D1 do not change! • ADD/SUB/MUL/DIV • ADD.W D1, D2 • D2 = D1 + D2 • Sum of words in D1 and D2 is placed into D2. • Destroys contents of D2!
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Assembly: Common Instructions - EXT • EXT.W extends a byte to a word • Bit 7 is copied into bits 8-15. • EXT.L extends a word into a long word • Bit 15 is copied into bits 16-31
Assembly: Common Instructions - EXT • Sign extension does not change the value of positive or negative 2’s complement numbers! • 0000 0011 = 3 • EXT.L : 0000 0000 0000 0011 = 3 • 1111 1101 = 3 • EXT.L : 1111 1111 1111 1101 = 3
Register Transfer Notation: Introduction • Symbolic and precise way to describe the effect of an instruction. • Example: MOVE D3,D2 • The contents of register D3 are copied to register D2. • A common notation to designate this is: D2 [D3] • Brackets around D3 indicate “contents of” • Left arrow indicates “receives”
Register Transfer Notation: Instructions • DEST Result • SUB D5, D7 • can be described by: D7 [D7] - [D5] • ADD D2, $001004 • Here, $001004 is a memory address. The same notation still suffices: $001004 [$001004] + [D2]
Addressing Modes: Register Direct MOVE D2, D3 • Register Direct: directly accesses the contents of the indicated register. • RTN: • D3 [D2] • Most efficient addressing mode because it requires no memory access to fetch the operand. • Uses: loop counters, accumulators, operation results of all kinds. • The data is already in your mailbox, you just need to get it.
Addressing Modes: Register Direct Examples • Register Direct: directly accesses the contents of the indicated register. MOVE.B D2, D3 MOVE.W D2, D3 MOVE.L D2, D3
Addressing Modes: Absolute Long MOVE$001020, D2 • Absolute Long: accesses the contents of the indicated memory location. • RTN: • D2 [$001020] • Motorola 68k also provides an Absolute Short addressing mode, but we will not be using it. • Uses: moving stored variables from memory into registers for processing, storing results back to memory. • You know the actual address ($001020) of the data, so you need to get it from there.
Addressing Modes: Absolute Long Examples • Absolute Long: accesses the contents of the indicated memory location. MOVE.W $002000, D2 MOVE.B $002000, D2 MOVE.L $002000, D2
Addressing Modes: Immediate MOVE #X, D2 • Immediate: an actual number X is provided. • RTN: • D2 X • Immediate value is assumed to be decimal unless indicated otherwise (ie by $ for hexadecimal or @ for octal). • Uses: incrementing loop counters, working with immediate values. • You know the actual value of the data
Addressing Modes: Immediate Examples • Immediate: an actual number X is provided. MOVE.B #12, D2 MOVE.W #$12, D2 MOVE.L #12, D2
Addressing Modes: Register Indirect MOVE (A0), D2 • Register Indirect: accesses the contents of the memory location in the indicated register. • Effective Address: [A0] • RTN: • D2 [[A0]] • Uses: repeated access to the same memory location • You have a friend (A0) who knows the address of the data. You can ask her where it is in memory, then get it from that location in memory.
Addressing Modes: Register Indirect Examples • Register Indirect: accesses the contents of the memory location in the indicated register. MOVE.B (A0), D2 MOVE.W (A0), D2 MOVE.L (A0), D2
Addressing Modes: Register Indirect – Indexed Basic MOVE X(A0), D2 • Indexed Basic: An index value X is added to the memory address in the indicated register to form the effective address, then the contents of the effective address are accessed. • Effective Address: • [A0] + X • RTN: • D2 [[A0] + X] • X is a decimal integer index value • Motorola 68k also provides an Indexed Full addressing mode, but we will not be using it.
Addressing Modes: Indexed Basic Examples • Indexed Basic: An index value is added to the memory address to form the effective address. MOVE.W 2(A0), D2 MOVE.W -2(A0), D2 MOVE.L -2(A0), D2
Addressing Modes: Indexed Basic Example Struct Student { int grade1; int grade2; int grade3; }; Struct Student Joe, Bob, Mary; Avg_Joe = Joe.grade1 + Joe.grade2 + Joe.grade3; Avg_Joe = Avg_Joe / 3; Memory Address Value $002000 95 $002002 89 $002004 83 MOVE.L #002000, A0 CLR.L D1 ADD.W (A0), D1 ADD.W 2(A0), D1 ADD.W 4(A0), D1 DIV.W #3, D1
Addressing Modes: Register Indirect – Post-increment MOVE (A0)+, D2 • Post-increment or Autoincrement: Operand is accessed indirectly, then address register is incremented. • Effective Address: • the contents of A0 • RTN: • D2 [[A0]] • A0 [A0] + the number of bytes accessed • Increment size: BYTE = 1, WORD = 2, LONG = 4 • Uses: moving through an array, popping from stack
Addressing Modes: Post-increment Examples • Post-increment: Operand is accessed indirectly, then address register is incremented. MOVE.B (A0)+, D2 MOVE.W (A0)+, D2 MOVE.L (A0)+, D2
Addressing Modes: Register Indirect – Pre-decrement MOVE.W –(A0), D2 • Pre-decrement or Autodecrement: Address register is decremented, then operand is accessed indirectly. • Effective Address: • (the contents of A0) – (number of bytes accessed) • RTN: • A0 [A0] – (number of bytes accessed) • D2 [[A0]] • Decrement size: BYTE = 1, WORD = 2, LONG = 4 • Uses: moving through an array, pushing onto stack
Addressing Modes: Pre-decrement Examples • Pre-decrement: Address register is decremented, then operand is accessed indirectly. MOVE.B -(A0), D2 MOVE.W –(A0), D2 MOVE.L –(A0), D2
Addressing Modes: Post-increment/Pre-decrement • In the 68000, the increment/decrement depends on the operand size • Suppose A0 = $00002000 • MOVE.B (A0)+,D0 A0 = $00002001 • MOVE.W (A0)+,D0 A0 = $00002002 • MOVE.L (A0)+,D0 A0 = $00002004
Assembly Language: In-Class Exercises • Assembly Language and Addressing Modes • Exercise • Big Endian (Memory) vs Little Endian (Registers) • Exercise
Intro to Assembly Language: You Should Know… • Introduction to Assembly language • Basic instructions: MOVE, ADD, EXT, etc. • Operand size (B, W, L) • Register Transfer Notation • Addressing Modes • Register direct, immediate, absolute long, register indirect, indexed basic, autoincrement, autodecrement • When to use the various modes • Assigned Reading • HVZ Chapter 2.4, 2.5, 2.6, 3.8, 3.9, 3.10, 3.11 • Reference: HVZ Appendix C