Section 04 Part 04 – The EXT Instruction “I invented the cordless extension cord.” ~Steven Wright

Introduction

EXT – sign EXTend a data register

This instruction will extend the most significant bit of a byte of the destination operand to word, or the most significant bit of a word of the destination operand to long-word.

Examples

You should by now, know about signed numbers and what is positive or negative.  To demonstrate the purpose of the EXT instruction, we’ll start with d0 containing 000000FC.

If we read d0 as a byte, we get FC.  This is negative 04.  However, if we read d0 as a word, we get 00FC, which is in fact positive instead.

The EXT instruction is designed to extend the polarity from byte to word, or from word to long-word.  For example:

 ext.w     d0

After this instruction, d0 will now contain 0000FFFC.

So now, if we read d0 as a word, we get FFFC, which is now negative 0004.  However, once again, if we read d0 as a long-word, we get 0000FFFC, which is positive.  Once again, using the same instruction but .l for long-word size:

 ext.l     d0

And now d0 contains FFFFFFFC.  So if we read d0 as a long-word, we get FFFFFFFC, which is once again negative 00000004.

We’ll have a final example with positive numbers.  d0 contains 7FC0E024.

• If you read d0 as a byte, you get 24 (positive)
• If you read d0 as a word, you get E024 (negative)
• If you read d0 as a long-word, you get 7FC0E024 (positive)

 ext.w     d0

Positive is extended as word; d0 now contains 7FC00024, where a word is now positive 0024.  A long-word is 7FC00024, which is positive, but it isn’t +24.  And so:

 ext.l     d0

Extending from word to long-word, now d0 contains 00000024, positive 24 once again.

This instruction can ONLY be used on data registers.

The process

This instruction works by reading the MSB.  For example, if d0 contains FEDCBA14, and we perform:

 ext.w     d0

What happens is, the byte 14 of d0 is read, and we’ll take a look at it in binary:

 0001 0100

As you can see, the MSB is 0 (clear).  This bit is extended to the left, so if we take a look at the word of d0 (BA14) in binary:

 1011 1010 0001 0100

After the ext.w instruction, you’ll be left with:

 0000 0000 0001 0100

As you can see, the MSB has extended and effectively “copied” itself out to a word.  Now d0 contains FEDC0014.

If d0 contains C1208489, and we perform:

 ext.l     d0

The word 8489 is read, in binary:

 1000 0100 1000 1001

The MSB is 1 (set).  That bit is then extended to long-word, so looking at d0 as a long-word:

 1100 0001 0010 0000 1000 0100 1000 1001

After ext.l you’ll be left with:

 1111 1111 1111 1111 1000 0100 1000 1001

The MSB of word has extended out to long-word.  Now d0 contains FFFF8489.