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:

 

 

          move.l    #$11112222,d0

 

Long-word 11112222 is moved into d0

 

 

          move.w    d0,d1

 

Word of data from d0 is moved into d1

 

 

          add.l     #$01230000,d1

 

Long-word 01230000 is added to d1

 

 

          sub.b     #$01,d1

 

Byte 01 is subtracted from d1

 

 

          move.w    d0,d2

 

Word of data from d0 is moved into d2

 

 

          sub.b     d1,d2

 

Byte of data from d1 is subtracted from d2

 

 

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.

 

 

 

 

About Upper And Lower Words

 

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

 

 

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.

 

 

 

 

Previous Part

Main Menu

Next Part