CEG 320/520: Computer Organization and Assembly Language Programming1 Assembly Language Programming Introduction and Addressing Modes
CEG 320/520: Computer Organization and Assembly Language Programming2 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 Programming3 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 Programming4 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 2s 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 Programming5 Assembly: Operand size Because the is capable of performing operations on bytes, words and long words: –In 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 Programming6 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 Programming7 Assembly: Common Instructions - EXT EXT.W extends a byte to a word –Bit 7 is copied into bits EXT.L extends a word into a long word –Bit 15 is copied into bits
CEG 320/520: Computer Organization and Assembly Language Programming8 Assembly: Common Instructions - EXT Sign extension does not change the value of positive or negative 2s complement numbers! = 3 EXT.L : = = 3 EXT.L : = 3
CEG 320/520: Computer Organization and Assembly Language Programming9 Register Transfer Notation: Introduction Symbolic and precise way to describe the effect of an instruction. Example: MOVED3,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 Programming10 Register Transfer Notation: Instructions DEST Result SUBD5, D7 –can be described by: D7 [D7] - [D5] ADDD2, $ –Here, $ is a memory address. The same notation still suffices: $ [$001004] + [D2]
CEG 320/520: Computer Organization and Assembly Language Programming11 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 Programming12 Addressing Modes: Register Direct Examples Memory $002000$1234 $002002$5678 $002004$ABCD Registers D2$ D3$XXXX XXXX A0$ MOVE.B D2, D3 Registers D2$ D3$XXXX XX78 A0$ MOVE.W D2, D3 Registers D2$ D3$XXXX 5678 A0$ Registers D2$ D3$ A0$ MOVE.L D2, D3 Register Direct: directly accesses the contents of the indicated register.
CEG 320/520: Computer Organization and Assembly Language Programming13 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 Programming14 Addressing Modes: Absolute Long Examples Memory $002000$1234 $002002$5678 $002004$ABCD Registers D2$XXXX XXXX D3$XXXX XXXX A0$ MOVE.W $002000, D2 Registers D2$XXXX 1234 D3$XXXX XXXX A0$ MOVE.B $002000, D2 Registers D2$XXXX XX12 D3$XXXX XXXX A0$ Registers D2$ D3$XXXX XXXX A0$ MOVE.L $002000, D2 Absolute Long: accesses the contents of the indicated memory location.
CEG 320/520: Computer Organization and Assembly Language Programming15 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 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 Programming16 Addressing Modes: Immediate Examples Memory $002000$1234 $002002$5678 $002004$ABCD Registers D2$XXXX XXXX D3$XXXX XXXX A0$ MOVE.B #12, D2 Registers D2$XXXX XX0C D3$XXXX XXXX A0$ MOVE.W #$12, D2 Registers D2$XXXX 0012 D3$XXXX XXXX A0$ Registers D2$ C D3$XXXX XXXX A0$ MOVE.L #12, D2 Immediate: an actual number X is provided.
CEG 320/520: Computer Organization and Assembly Language Programming17 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 Programming18 Addressing Modes: Register Indirect Examples Memory $002000$1234 $002002$5678 $002004$ABCD Registers D2$XXXX XXXX D3$XXXX XXXX A0$ MOVE.B (A0), D2 Registers D2$XXXX XX12 D3$XXXX XXXX A0$ MOVE.W (A0), D2 Registers D2$XXXX 1234 D3$XXXX XXXX A0$ Registers D2$ D3$XXXX XXXX A0$ MOVE.L (A0), D2 Register Indirect: accesses the contents of the memory location in the indicated register.
CEG 320/520: Computer Organization and Assembly Language Programming19 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 Programming20 Addressing Modes: Indexed Basic Examples Memory $002000$1234 $002002$5678 $002004$ABCD Registers D2$XXXX XXXX D3$XXXX XXXX A0$ MOVE.W 2(A0), D2 Registers D2$XXXX ABCD D3$XXXX XXXX A0$ MOVE.W -2(A0), D2 Registers D2$XXXX 1234 D3$XXXX XXXX A0$ Registers D2$ D3$XXXX XXXX A0$ MOVE.L -2(A0), D2 Indexed Basic: An index value is added to the memory address to form the effective address.
CEG 320/520: Computer Organization and Assembly Language Programming21 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 AddressValue $ $ $ MOVE.L #002000, A0 CLR.LD1 ADD.W (A0), D1 ADD.W 2(A0), D1 ADD.W 4(A0), D1 DIV.W #3, D1
CEG 320/520: Computer Organization and Assembly Language Programming22 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 Programming23 Addressing Modes: Post-increment Examples Memory $002000$1234 $002002$5678 $002004$ABCD Registers D2$XXXX XXXX D3$XXXX XXXX A0$ MOVE.B (A0)+, D2 Registers D2$XXXX XX12 D3$XXXX XXXX A0$ MOVE.W (A0)+, D2 Registers D2$XXXX 1234 D3$XXXX XXXX A0$ Registers D2$ D3$XXXX XXXX A0$ MOVE.L (A0)+, D2 Post-increment: Operand is accessed indirectly, then address register is incremented.
CEG 320/520: Computer Organization and Assembly Language Programming24 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 Programming25 Addressing Modes: Pre-decrement Examples Memory $002000$1234 $002002$5678 $002004$ABCD Registers D2$XXXX XXXX D3$XXXX XXXX A0$ MOVE.B -(A0), D2 Registers D2$XXXX XX78 D3$XXXX XXXX A0$ MOVE.W –(A0), D2 Registers D2$XXXX 5678 D3$XXXX XXXX A0$ Registers D2$ D3$XXXX XXXX A0$ MOVE.L –(A0), D2 Pre-decrement: Address register is decremented, then operand is accessed indirectly.
CEG 320/520: Computer Organization and Assembly Language Programming26 Addressing Modes: Post-increment/Pre-decrement In the 68000, the increment/decrement depends on the operand size –Suppose A0 = $ –MOVE.B(A0)+,D0 A0 = $ –MOVE.W(A0)+,D0 A0 = $ –MOVE.L(A0)+,D0 A0 = $
CEG 320/520: Computer Organization and Assembly Language Programming27 Assembly Language: In-Class Exercises Assembly Language and Addressing Modes –ExerciseExercise Big Endian (Memory) vs Little Endian (Registers) –ExerciseExercise
CEG 320/520: Computer Organization and Assembly Language Programming28 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