Home

Logical and Shift Operations

Logical and Shift Operations

 

 

Logical and Shift Operations

This lecture introduces two families of instruction affecting the 68000 CCR: logical operations and shift instructions. You will then learn how ASCII characters can be converted into binary integers.

Learning Outcomes:
On completion of this lecture, you will be able to:

  • Implement 68000 Logical Operations;
  • Implement 68000 Shift Operations;
  • Convert from ASCII to binary integer.

11.1    Logical Operations

The 68000 implements four Boolean operations: AND, OR, EOR and NOT. All logical operation can be applied to longword, word, and byte operands. Logical operations can, with immediate addressing, be applied to the contents of the CCR or Status Register (only under supervisor mode). In general, logical operations are used to modify one or more fields of an operand. Logical operations affect the flag bits if the CCR: the X-bit is unmodified, the V- and C-bits are set to zero, the N- and Z-bits are set according to the result.

  • The NOT operation simply inverts the bits of an operand; eg: if [D0]=11001010, the operation NOT.B  D0   results in [D0]=00110101.
  • The AND operation is applied to a source and destination operand. Each bit of the source operand is ANDed with the corresponding bit of the destination operand and the result is stored in the destination operand. eg: if [D0]=11001010, the operation

AND.B  #11110000,D0   results in [D0]= 11000000.

  • The OR operation sets one or more bits to 1; eg.: if [D0]=11001010, the operation

OR.B  #11110000,D0   results in [D0]= 11111010.

  • The EOR operation is use to toggle one or more bits. EORins a bit with 0 has no effect, and EORing it with 1 inverts it; eg.: if [D0]=11001010, the operation

EOR.B  #11110000,D0   results in [D0]= 00111010.

11.2    Shift Operations

In a shift operation all the bits of the operand are moved one or more places left or right. Shifts are categorized as logical, arithmetic, or circular (also called a rotate). Shift operations act on bytes, words and longwords in data registers, but only words in memory.

11.2.1 Logical Shift
In a logical shift, Fig.12.1, all the bits are moved left or right and zero enters at the input of the shifter. A logical shift left is indicated by LSL and a shift right by LSR. The bit shifted out of one end of the register is placed in the carry flag of the CCR and also the X flag.

e.g.: the following code multiplies the longword contents of register D6 by ten using logic shift operations.
*        MULTIPLY BY TEN   [D6] = x

 MOVE.L         D6,D7           save a copy of D6 in D7
LSL.L            #3,D6          [D6] <- 8*[D6]        ([D6]=8x)
LSL.L            #1,D7          [D7] <- 2*[D7]        ([D7]=2x)
ADD.L          D7,D6           [D6] <- [D7]+[D6]   ([D6]=10x)

Logical shift operations update all bits of the CCR. The N- and Z-bits are set or cleared as you would expect. The V-bit is cleared. The C- and X-bits are set to the state of the last bit shifted out of the operand. If the shift count is zero (i.e., a shift length of zero that shifts no bits), C is cleared and X is unaffected.

      • Arithmetic Shift

An arithmetic shift left, ASL, is almost identical to a logical shift left.
An arithmetic shift right, ASR, causes the most-significant bit, the sign-bit, to be propagated right and, therefore, preserves the correct sign of a two's complement value. For example, if the bytes 00101010=4210 and 10101010=-8610 are shifted one place right by the instruction ASR, the results of the arithmetic shift are 0001010 =2110 and 11010101=-4310 respectively. Arithmetic shifts are intended to be used in conjunction with two's complement arithmetic.

eg: the following code multiplies the word contents of register D6 by ten using arithmetic shift operations.
*        MULTIPLY BY TEN   [D6] = x

 MOVE.W       D6,D7           save a copy of D6 in D7
ASL.W          D6               [D6] <- 2*[D6]        ([D6]=2x)
ASL.W          D6               [D6] <- 2*[D6]        ([D6]=4x)
ADD.W         D7,D6           [D6] <- [D7]+[D6]   ([D6]=5x)
ASL.W          D6               [D6] <- 2*[D6]        ([D6]=10x)

Arithmetic shift operations behave in the same way as logical shift operations, except that the overflow bit is set if the most-significant bit of the operand is changed at any time during the shift operation (i.e., if the number changes sign).
Note that an arithmetic shift left and a logical shift left operation are virtually identical. In each case, all the bits are shifted one place left. The bit shifted out enters the carry bit and extend bit of the CCR and a zero enters the vacated position (the least-significant bit). There is, however, one tiny difference between an ASL and an LSL. Since an arithmetic left shift multiplies a number by two, it is possible for the most-significant bit of the value being shifted to change sign and therefore generate an arithmetic overflow. The V-bit of the CCR is set if this event occurs during an ASL. For example, suppose we shift the 8-bit value 011111112=+12710 one place left arithmetically. The new value is 111111102=-210, the V-bit is set to indicate that arithmetic overflow has taken place. However, since logical operations are applied to strings of bits, a LSL instruction clears the V-bit.

11.2.3 Circular Shift
In a circular shift, Fig. 12.3, the bit shifted out is moved to the position of the bit shifted in. The bits are shifted left by ROL, rotate left, and right by ROR, rotate right. No bit is lost during a circular shift. The bit copied from one end of the register to the other is also copied into the carry bit.

 
Fig. 11.3 Rotation Left (ROL) and Rotation Right (ROR)

Rotate operations affect the CCR like logical shifts, except that the X-bit is not affected (i.e., it does not change state).

11.2.4 Rotate through extend
The rotate through extend instructions, ROXL and ROXR, behave rather like the ROL/ROR pair, except that the rotate includes the X-bit. That is, the shift takes place over 9 bits for a .B operation, over 17 bits for a .W operation, and over 33 bits for a .L operation. As you can see from Figure 12.5, the old value of the X-bit is shifted into the register (or memory location) and the bit shifted out is shifted into both the X-bit and the C-bit.

 
Fig. 11.4 Rotation through extend instructions: ROXL and ROXR

 

The rotate through extend instructions enable you to perform shifts over words longer than 32 bits. Suppose you have a 64-bit quadword in the register pair D1, D0 with the most-significant 32 bits in D1, and wish to perform a logical shift left over the entire 64-bit quadword.
*        64-bit logical shift level over D1, D0

 LSL.L  #1,D0                   Shift low-order longword one place left
ROXL.L #1,D1         Shift high-order longword one place left

Executing LSL.L #1,D0 shifts the lower longword one place left and the bit shifted out of the left-hand end is copied into the X-bit. If we now execute ROXL.L #1,D1 , the most-significant longword is shifted left and the bit shifted out of DO is shifted into the least-significant bit of D1. Rotate through extend instructions are largely used to facilitate 64-bit arithmetic.

 

 

11.3    Example: Conversion ASCII to binary integer

When a user enters a number to a computer, it is almost always in the form of a string of characters representing the decimal form of the number. Internally, as you know, the number can be represented in binary.
Write a program that 'reads' a NUL-terminated character string that represents a decimal number and produces an unsigned binary word representation of the magnitude of that number.
For example, if the number is 124, the sequence of characters will be '1','2','4',NUL. What you want to end up is the unsigned binary word representation of the number, 01111100.

         ORG              $1FFC
RSLT   DS.L             1                          
ORG              $2000                   
num    DC.B             '124',$0                 

         ORG              $4000
LEA               num,A0                  load number’s effective address   
CLR              D1                         clear accumulator

loop    MOVE.B         (A0)+,D0                copy ascii char in D0
BEQ              exit                       if char is 0 then exit
SUB.B           #$30,D0                 else subtract $30 from ascii char code
MOVE.L         D1,D2                    copy D1 in D2
LSL.L            #3,D1                    multiply contents of D1 by 8
LSL.L            #1,D2                    multiply contents of D2 by 2
ADD.L           D2,D1                    add [D1] and [D2] ( product of by 10 mult)
ADD.L           D0,D1                    add [D0] and [D1]
BRA              loop                       goto loop

exit     MOVE.L         D1,RSLT                 else save [D1] to RSLT
TRAP             #0                        return control to Monitor
END   

11.4    Conclusion
So far…

  • We know how to write regular code to specify data and operations like add, subtract, compare, etc. sufficient for any purpose.
  • We know how to modify and control the flow of program execution, conditionally and unconditionally.
  • We know how to map high-level flow control constructs like while…, for…, if… to assembly language
  • We don’t yet know how to implement functions, procedures or methods at assembly language level.

REFERENCES

  • A. Clements; The 68000’s Instruction Set, In: 68000 Family Assembly Language; pp.196-203; PWS Publishing Company; 1994.
  • Dr. Mike Brady, Microprocessor Systems 1, dept of Computer Science, Trinity College Dublin: http://www.tcd.ie/Engineering/Courses/BAI/JS_Subjects/3D1/.
  • Look on the Web at http://www.mee.tcd.ie/~assambc/3D1.

Source: http://www.mee.tcd.ie/~assambc/3d1_l11n.doc

Web site to visit: http://www.mee.tcd.ie

Author of the text: indicated on the source document of the above text

If you are the author of the text above and you not agree to share your knowledge for teaching, research, scholarship (for fair use as indicated in the United States copyrigh low) please send us an e-mail and we will remove your text quickly. Fair use is a limitation and exception to the exclusive right granted by copyright law to the author of a creative work. In United States copyright law, fair use is a doctrine that permits limited use of copyrighted material without acquiring permission from the rights holders. Examples of fair use include commentary, search engines, criticism, news reporting, research, teaching, library archiving and scholarship. It provides for the legal, unlicensed citation or incorporation of copyrighted material in another author's work under a four-factor balancing test. (source: http://en.wikipedia.org/wiki/Fair_use)

The information of medicine and health contained in the site are of a general nature and purpose which is purely informative and for this reason may not replace in any case, the council of a doctor or a qualified entity legally to the profession.

 

Logical and Shift Operations

 

The texts are the property of their respective authors and we thank them for giving us the opportunity to share for free to students, teachers and users of the Web their texts will used only for illustrative educational and scientific purposes only.

All the information in our site are given for nonprofit educational purposes

 

Logical and Shift Operations

 

 

Topics and Home
Contacts
Term of use, cookies e privacy

 

Logical and Shift Operations