Assembly Language Programming Introduction and Addressing Modes CEG 320/520: Computer Organization and Assembly Language Programming 1 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 CEG 320/520: Computer Organization and Assembly Language Programming 2 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. CEG 320/520: Computer Organization and Assembly Language Programming 3 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. CEG 320/520: Computer Organization and Assembly Language Programming 4 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. CEG 320/520: Computer Organization and Assembly Language Programming 5 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! CEG 320/520: Computer Organization and Assembly Language Programming 6 Assembly: Common Instructions - EXT 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 • 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 CEG 320/520: Computer Organization and Assembly Language Programming 7 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 CEG 320/520: Computer Organization and Assembly Language Programming 8 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” CEG 320/520: Computer Organization and Assembly Language Programming 9 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] CEG 320/520: Computer Organization and Assembly Language Programming 10 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. CEG 320/520: Computer Organization and Assembly Language Programming 11 Addressing Modes: Register Direct Examples •Register Direct: directly accesses the contents of the indicated register. Registers D2 $1234 5678 D3 $XXXX XXXX A0 $0000 2000 Memory $002000 $1234 $002002 $5678 $002004 $ABCD MOVE.B D2, D3 MOVE.W D2, D3 Registers D2 $1234 5678 D3 $XXXX XX78 A0 $0000 2000 Registers D2 $1234 5678 D3 $XXXX 5678 A0 $0000 2000 Registers D2 $1234 5678 MOVE.L D2, D3 CEG 320/520: Computer Organization and Assembly Language Programming D3 $1234 5678 A0 $0000 2000 12 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. CEG 320/520: Computer Organization and Assembly Language Programming 13 Addressing Modes: Absolute Long Examples •Absolute Long: accesses the contents of MOVE.W $002000, D2 the indicated memory location. Registers D2 $XXXX XXXX D3 $XXXX XXXX A0 $0000 2000 Memory $002000 $1234 $002002 $5678 $002004 $ABCD MOVE.B $002000, D2 Registers D2 $XXXX 1234 D3 $XXXX XXXX A0 $0000 2000 Registers D2 $XXXX XX12 D3 $XXXX XXXX A0 $0000 2000 Registers D2 $1234 5678 MOVE.L $002000, D2 CEG 320/520: Computer Organization and Assembly Language Programming D3 $XXXX XXXX A0 $0000 2000 14 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 CEG 320/520: Computer Organization and Assembly Language Programming 15 Addressing Modes: Immediate Examples •Immediate: an actual number X is provided. Registers D2 $XXXX XXXX D3 $XXXX XXXX A0 $0000 2000 Memory $002000 $1234 $002002 $5678 $002004 $ABCD MOVE.B #12, D2 MOVE.W #$12, D2 Registers D2 $XXXX XX0C D3 $XXXX XXXX A0 $0000 2000 Registers D2 $XXXX 0012 D3 $XXXX XXXX A0 $0000 2000 Registers D2 $0000 000C MOVE.L #12, D2 CEG 320/520: Computer Organization and Assembly Language Programming D3 $XXXX XXXX A0 $0000 2000 16 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. CEG 320/520: Computer Organization and Assembly Language Programming 17 Addressing Modes: Register Indirect Examples •Register Indirect: accesses the contents of the memory location in the indicated register. Registers D2 $XXXX XXXX D3 $XXXX XXXX A0 $0000 2000 Memory $002000 $1234 $002002 $5678 $002004 $ABCD MOVE.B (A0), D2 MOVE.W (A0), D2 Registers D2 $XXXX XX12 D3 $XXXX XXXX A0 $0000 2000 Registers D2 $XXXX 1234 D3 $XXXX XXXX A0 $0000 2000 Registers D2 $1234 5678 MOVE.L (A0), D2 CEG 320/520: Computer Organization and Assembly Language Programming D3 $XXXX XXXX A0 $0000 2000 18 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. CEG 320/520: Computer Organization and Assembly Language Programming 19 Addressing Modes: Indexed Basic Examples •Indexed Basic: An index value is added to the memory address to form the effective address. Registers D2 $XXXX XXXX D3 $XXXX XXXX A0 $0000 2002 Memory $002000 $1234 $002002 $5678 $002004 $ABCD MOVE.W 2(A0), D2 MOVE.W -2(A0), D2 Registers D2 $XXXX ABCD D3 $XXXX XXXX A0 $0000 2002 Registers D2 $XXXX 1234 D3 $XXXX XXXX A0 $0000 2002 Registers D2 $1234 5678 MOVE.L -2(A0), D2 CEG 320/520: Computer Organization and Assembly Language Programming D3 $XXXX XXXX A0 $0000 2002 20 Addressing Modes: Indexed Basic Example Memory Address $002000 $002002 $002004 Value 95 89 83 MOVE.L #002000, A0 CLR.L D1 ADD.W (A0), D1 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; ADD.W 2(A0), D1 ADD.W 4(A0), D1 DIV.W #3, D1 CEG 320/520: Computer Organization and Assembly Language Programming 21 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 CEG 320/520: Computer Organization and Assembly Language Programming 22 Addressing Modes: Post-increment Examples •Post-increment: Operand is accessed indirectly, then address register is incremented. Registers D2 $XXXX XXXX D3 $XXXX XXXX A0 $0000 2000 Memory $002000 $1234 $002002 $5678 $002004 $ABCD MOVE.B (A0)+, D2 MOVE.W (A0)+, D2 Registers D2 $XXXX XX12 D3 $XXXX XXXX A0 $0000 2001 Registers D2 $XXXX 1234 D3 $XXXX XXXX A0 $0000 2002 Registers D2 $1234 5678 MOVE.L (A0)+, D2 CEG 320/520: Computer Organization and Assembly Language Programming D3 $XXXX XXXX A0 $0000 2004 23 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 CEG 320/520: Computer Organization and Assembly Language Programming 24 Addressing Modes: Pre-decrement Examples •Pre-decrement: MOVE.B -(A0), D2 Address register is decremented, then operand is accessed indirectly. Registers D2 $XXXX XXXX D3 $XXXX XXXX A0 $0000 2004 Memory $002000 $1234 $002002 $5678 $002004 $ABCD MOVE.W –(A0), D2 Registers D2 $XXXX XX78 D3 $XXXX XXXX A0 $0000 2003 Registers D2 $XXXX 5678 D3 $XXXX XXXX A0 $0000 2002 Registers D2 $1234 5678 MOVE.L –(A0), D2 CEG 320/520: Computer Organization and Assembly Language Programming D3 $XXXX XXXX A0 $0000 2000 25 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 CEG 320/520: Computer Organization and Assembly Language Programming 26 Assembly Language: In-Class Exercises • Assembly Language and Addressing Modes – Exercise • Big Endian (Memory) vs Little Endian (Registers) – Exercise CEG 320/520: Computer Organization and Assembly Language Programming 27 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 CEG 320/520: Computer Organization and Assembly Language Programming 28