Part 07 – The SWAP Instruction “People say to me, Would you like to swap your life with me for 24 hours? Your life must be very strange. But of course I have not experienced any other life. It's not strange to me.” ~Prince Andrew

Homework Results

In the last section I gave you a small test to see if you could work out what d2 contained at the end, the answer is 00002201.  Congratulations if you got that one, if you didn’t, then I’ll explain with registers starting at 00000000:

• d0 = 00000000
• d1 = 00000000
• d2 = 00000000

 move.l    #\$11112222,d0

Long-word 11112222 is moved into d0

• d0 = 11112222
• d1 = 00000000
• d2 = 00000000

 move.w    d0,d1

Word of data from d0 is moved into d1

• d0 = 11112222
• d1 = 00002222
• d2 = 00000000

Long-word 01230000 is added to d1

• d0 = 11112222
• d1 = 01232222
• d2 = 00000000

 sub.b     #\$01,d1

Byte 01 is subtracted from d1

• d0 = 11112222
• d1 = 01232221
• d2 = 00000000

 move.w    d0,d2

Word of data from d0 is moved into d2

• d0 = 11112222
• d1 = 01232221
• d2 = 00002222

 sub.b     d1,d2

Byte of data from d1 is subtracted from d2

• d0 = 11112222
• d1 = 01232221
• d2 = 00002201

And there you have it, d2 contains 00002201.

Introduction

SWAP – SWAP register halves

This instruction will swap the upper word with the lower word of a data register.

You saw in the introduction section the phrase “upper word” and “lower word”, and you’re probably wondering what they mean, allow me to explain, here is data register d0 and the data that’s inside it:

 register data inside d0 11112222

The left side of d0 with 1111 in it is the “upper word” of d0, and the right side of d0 with 2222 in it is the “lower word” of d0.

Examples

Now that upper and lower words are explained we can begin to understand the SWAP instruction, as stated in the introduction, this instruction swaps the upper and lower words of a data register around:

 swap.w    d0

• if d0 started with 00001111, then after the instruction d0 will contain 11110000
• if d0 started with 01234567, then after the instruction d0 will contain 45670123
• if d0 started with 22331122, then after the instruction d0 will contain 11222233

And that’s all there is to it for this instruction, please note though, you can only use .w (word), you cannot use .b (byte) or .l (long-word), those are unacceptable/invalid and your assembler will notify you with an error.