6809 Arithemetic Shift Left (ASL)

From Davebiz Wiki
Jump to: navigation, search

A prominent Tandy Color Computer 6809 game programmer made a few statements about his belief that the Motorola 6809 processor does not work properly when it comes to the ASL (Arithmetic Shift Left) instruction. His comments are in Episode 73 of CoCoTALK! which can be found here: CoCoTALK Video episodes


Comments begin at around 1:23:00 of episode 73. The slide in the video says, "Does not work for signed math". He then goes on to say, "This is the one I wanna complain about...this does not do an arithmetic shift left...it kinda does it...if this was used for arithmetic shifting of a signed number whatever was in bit 6 would be copied into bit 7 and destroys the sign bit."

Well, it's true that bit 6 does shift into bit 7 but the sign bit is not destroyed! It's shifted into the carry bit.


"It is...literally, physically the same command as logical shift to the left."

Which agrees totally with the Wiki page entitled 'Bitwise Operation' where it is explained that, "In a logical shift, zeros are shifted in to replace the discarded bits. Therefore, the logical and arithmetic left-shifts are exactly the same." Additionally, the Wiki page entitled 'Arithmetic Shift' states that "Processors usually do not offer logical and arithmetic left shift operations with a significant difference, if any."


"I'm kind of upset about this one...they (Motorola) didn't do it right. The engineers messed up here. And I'm sure there are software programmers that are a little pissed at them as I am. That they did not truly put a real arithmetic shift to the left instruction in."

This is the first time I've heard such a complaint from any programmer using the 6809. The 6809 has been with us now for over 40 years and I've never heard this before! I've programmed thousands and thousands of lines of 6809 code and I've never had a problem with ASL.


"Don't expect that your arithmetically shift to the left to operate the way that the arithmetically shift to the right works...it doesn't do what it's supposed to do."

Well, I wouldn't expect it to operate the same way. The shift right is for division where a number just gets smaller. There is no chance for an overflow beyond the bounds of eight bits. Therefore, the sign bit (or bit 7) can be retained from the original, pre-shifted value because performing the shift will never overwrite it. On the other hand, the shift left is for multiplication where the number gets larger and there's a possibility that it will outgrow the boundaries of eight bits. The sign bit (bit 7) must be shifted out of its position to allow for expansion of the number.

You may wonder, then how can you retain the sign bit in bit 7 for results that are within the eight-bit boundaries? The answer is that twos complement signed integers take care of the problem for you. Any twos complement number that when shifted will remain inbounds will have bit 6 set to the same value as the sign bit. That's right! So when you ASL the number the sign will be retained! If, however, the result of the shift will overflow the eight-bit boundaries then the 6809 will inform you by setting the overflow bit (V) in the condition code register. In this case, the sign bit is retrieved from the carry bit and the programmer has the option of acting upon it. This is much the same way that addition works on the 6809. If you perform an addition that overflows the eight-bit boundaries the carry bit will be set alerting the programmer that the least significant eight bits are correct but the number needs to be expanded beyond eight bits.


"Arithemtically shift to the left does not work...just never use that in life...never use that command and you'll be fine."

This is just not true! The arithemtic shift left does work exactly as it is documented. It works on signed as well as unsigned numbers and its action is predictable. By all means, use the ASL instruction as you see fit! It will always give you correct and predictable results. If you plan to use it on twos complement signed 8-bit numbers then brush up a bit on how twos complement numbers and math work.


"Every single reference your gonna find on the 6809 talks about that instruction. It's not there, it doesn't exist."

Sounds like this guy doesn't want us to use the ASL instruction on the 6809. Perhaps he is just ignorant of two things: 1) How twos complement numbers work, 2) How the overflow bit is derived in this operation. Incidentally, the overflow is derived from exclusive ORing bit 6 and bit 7 of the initial pre-shifted value. Remember what I told you about twos complement numbers? A twos complement number that, when shifted left, will not expand beyond the boundaries of eight bits will always have identical bit 6 and 7 values! Go verify it, I'll wait.

So here's the beauty of it. The sign bit (bit 7) will always be retained when the result stays inbounds so you don't have to check the carry bit. But, if the result will be out-of-bounds, guess what? Bit 7 and 6 are always different. That means the overflow bit will be set and, you, the programmer will know that even though bit seven will no longer reflect the sign you can still find the sign in the carry bit. And, you'll be happy to know that the rest of the number which is now using all 8 bits is correct!

Want to see a video which demonstrates the use of ASL with 8-bit signed integers? Check out this little program by Dave Philipsen that steps through ALL possible signed integer values from -128 to +127. It will show the value of the number in decimal then in binary. Then it will show you the result after an arithmetic shift left. The value will be represented in both decimal and binary format. Notice that the binary number on the right has been expanded to nine bits since some results will overflow eight bits. The sign bit is now bit 8. And finally, the status of the overflow (V) bit is indicated after each operation. Everything works properly as documented by Motorola and all results accurately represent the original value multiplied by two.

In fact, if there’s any particular point that programmers need to be aware of regarding the use of ASL and ASR for arithmetic on the 6809, it is this: when using the ASR instruction to divide an odd signed 8-bit number by two, be aware that the result is rounded “down” which in negative numbers is really “up”. An example is -5 divided by 2. If you ASL a +5 the result will be 2. If you ASL -5 the result will be -3 (rounded “up” or, in reality, “down” since it’s a negative number. Get it? This action is well documented in most books dealing with 6809 assembly language.


YouTube Video - ASL demonstrated on the CoCo3 (6809)

Still not convinced? Read what veteran 6809 programmer and Color Computer enthusiast John Linville has to say bout it: Yes, LSL and ASL really are the same!