Assembly Language Programming Introduction and Addressing Modes CEG

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 D 1, D 2 = D 1 Contents of long word in D 1 is copied into D 2. Destroys contents of D 2! Contents of D 1 do not change! • ADD/SUB/MUL/DIV – – ADD. W D 1, D 2 = D 1 + D 2 Sum of words in D 1 and D 2 is placed into D 2. Destroys contents of D 2! 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 0011 = 3 • 1111 1101 = 3 • EXT. L : 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 D 3, D 2 ÞThe contents of register D 3 are copied to register D 2. ÞA common notation to designate this is: D 2 [D 3] · Brackets around D 3 indicate “contents of” · Left arrow indicates “receives” CEG 320/520: Computer Organization and Assembly Language Programming 9

Register Transfer Notation: Instructions • DEST Result • SUB D 5, D 7 – can be described by: D 7 [D 7] - [D 5] • ADD D 2, $001004 – Here, $001004 is a memory address. The same notation still suffices: $001004 [$001004] + [D 2] CEG 320/520: Computer Organization and Assembly Language Programming 10

Addressing Modes: Register Direct MOVE D 2, D 3 • Register Direct: directly accesses the contents of the indicated register. – RTN: • D 3 [D 2] – 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 D 2 $1234 5678 D 3 $XXXX A 0 $0000 2000 Memory $002000 $1234 $002002 $5678 $002004 $ABCD MOVE. B D 2, D 3 MOVE. W D 2, D 3 Registers D 2 $1234 5678 D 3 $XXXX XX 78 A 0 $0000 2000 Registers D 2 $1234 5678 D 3 $XXXX 5678 A 0 $0000 2000 Registers D 2 $1234 5678 MOVE. L D 2, D 3 CEG 320/520: Computer Organization and Assembly Language Programming D 3 $1234 5678 A 0 $0000 2000 12

Addressing Modes: Absolute Long MOVE $001020, D 2 • Absolute Long: accesses the contents of the indicated memory location. – RTN: • D 2 [$001020] – Motorola 68 k 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, D 2 the indicated memory location. Registers D 2 $XXXX D 3 $XXXX A 0 $0000 2000 Memory $002000 $1234 $002002 $5678 $002004 $ABCD MOVE. B $002000, D 2 Registers D 2 $XXXX 1234 D 3 $XXXX A 0 $0000 2000 Registers D 2 $XXXX XX 12 D 3 $XXXX A 0 $0000 2000 Registers D 2 $1234 5678 MOVE. L $002000, D 2 CEG 320/520: Computer Organization and Assembly Language Programming D 3 $XXXX A 0 $0000 2000 14

Addressing Modes: Immediate MOVE #X, D 2 • Immediate: an actual number X is provided. – RTN: • D 2 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 D 2 $XXXX D 3 $XXXX A 0 $0000 2000 Memory $002000 $1234 $002002 $5678 $002004 $ABCD MOVE. B #12, D 2 MOVE. W #$12, D 2 Registers D 2 $XXXX XX 0 C D 3 $XXXX A 0 $0000 2000 Registers D 2 $XXXX 0012 D 3 $XXXX A 0 $0000 2000 Registers D 2 $0000 000 C MOVE. L #12, D 2 CEG 320/520: Computer Organization and Assembly Language Programming D 3 $XXXX A 0 $0000 2000 16

Addressing Modes: Register Indirect MOVE (A 0), D 2 • Register Indirect: accesses the contents of the memory location in the indicated register. – Effective Address: [A 0] – RTN: • D 2 [[A 0]] – Uses: repeated access to the same memory location – You have a friend (A 0) 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 D 2 $XXXX D 3 $XXXX A 0 $0000 2000 Memory $002000 $1234 $002002 $5678 $002004 $ABCD MOVE. B (A 0), D 2 MOVE. W (A 0), D 2 Registers D 2 $XXXX XX 12 D 3 $XXXX A 0 $0000 2000 Registers D 2 $XXXX 1234 D 3 $XXXX A 0 $0000 2000 Registers D 2 $1234 5678 MOVE. L (A 0), D 2 CEG 320/520: Computer Organization and Assembly Language Programming D 3 $XXXX A 0 $0000 2000 18

Addressing Modes: Register Indirect – Indexed Basic MOVE X(A 0), D 2 • 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: • [A 0] + X – RTN: • D 2 [[A 0] + X] – X is a decimal integer index value – Motorola 68 k 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 D 2 $XXXX D 3 $XXXX A 0 $0000 2002 Memory $002000 $1234 $002002 $5678 $002004 $ABCD MOVE. W 2(A 0), D 2 MOVE. W -2(A 0), D 2 Registers D 2 $XXXX ABCD D 3 $XXXX A 0 $0000 2002 Registers D 2 $XXXX 1234 D 3 $XXXX A 0 $0000 2002 Registers D 2 $1234 5678 MOVE. L -2(A 0), D 2 CEG 320/520: Computer Organization and Assembly Language Programming D 3 $XXXX A 0 $0000 2002 20

Addressing Modes: Indexed Basic Example Memory Address $002000 $002002 $002004 Value 95 89 83 MOVE. L #002000, A 0 CLR. L D 1 ADD. W (A 0), D 1 Struct Student { int grade 1; int grade 2; int grade 3; }; Struct Student Joe, Bob, Mary; Avg_Joe = Joe. grade 1 + Joe. grade 2 + Joe. grade 3; Avg_Joe = Avg_Joe / 3; ADD. W 2(A 0), D 1 ADD. W 4(A 0), D 1 DIV. W #3, D 1 CEG 320/520: Computer Organization and Assembly Language Programming 21

Addressing Modes: Register Indirect – Post-increment MOVE (A 0)+, D 2 • Post-increment or Autoincrement: Operand is accessed indirectly, then address register is incremented. – Effective Address: • the contents of A 0 – RTN: • D 2 [[A 0]] • A 0 [A 0] + 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 D 2 $XXXX D 3 $XXXX A 0 $0000 2000 Memory $002000 $1234 $002002 $5678 $002004 $ABCD MOVE. B (A 0)+, D 2 MOVE. W (A 0)+, D 2 Registers D 2 $XXXX XX 12 D 3 $XXXX A 0 $0000 2001 Registers D 2 $XXXX 1234 D 3 $XXXX A 0 $0000 2002 Registers D 2 $1234 5678 MOVE. L (A 0)+, D 2 CEG 320/520: Computer Organization and Assembly Language Programming D 3 $XXXX A 0 $0000 2004 23

Addressing Modes: Register Indirect – Pre-decrement MOVE. W –(A 0), D 2 • Pre-decrement or Autodecrement: Address register is decremented, then operand is accessed indirectly. – Effective Address: • (the contents of A 0) – (number of bytes accessed) – RTN: • A 0 [A 0] – (number of bytes accessed) • D 2 [[A 0]] – 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 -(A 0), D 2 Address register is decremented, then operand is accessed indirectly. Registers D 2 $XXXX D 3 $XXXX A 0 $0000 2004 Memory $002000 $1234 $002002 $5678 $002004 $ABCD MOVE. W –(A 0), D 2 Registers D 2 $XXXX XX 78 D 3 $XXXX A 0 $0000 2003 Registers D 2 $XXXX 5678 D 3 $XXXX A 0 $0000 2002 Registers D 2 $1234 5678 MOVE. L –(A 0), D 2 CEG 320/520: Computer Organization and Assembly Language Programming D 3 $XXXX A 0 $0000 25

Addressing Modes: Post-increment/Pre-decrement • In the 68000, the increment/decrement depends on the operand size – – Suppose A 0 = $00002000 MOVE. B (A 0)+, D 0 A 0 = $00002001 MOVE. W (A 0)+, D 0 A 0 = $00002002 MOVE. L (A 0)+, D 0 A 0 = $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
- Slides: 28