3D1-Microprocessor Systems 1 Lecture 11: 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 Vand C-bits are set to zero, the N- and Z-bits are set according to the result. 11.2 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. 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. LSL C Operand LSR ← 0 0 Operand → X C X Fig. 11.1 Logical Shift Left (LSL) and Logical Shift Right (LSR) 11-1 3D1-Microprocessor Systems 1 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. 11.2.2 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. ASL C Operand ASR ← 0 M S B Operand → X C X Fig. 11.2 Arithmetic Shift Left (ASL) and Arithmetic Shift Right (ASR) eg: the following code multiplies the word contents of shift operations. * MULTIPLY BY TEN [D6] = x MOVE.W D6,D7 save a copy of D6 in ASL.W D6 [D6] <- 2*[D6] ASL.W D6 [D6] <- 2*[D6] ADD.W D7,D6 [D6] <- [D7]+[D6] ASL.W D6 [D6] <- 2*[D6] register D6 by ten using arithmetic D7 ([D6]=2x) ([D6]=4x) ([D6]=5x) ([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 11111110 2=-210, the V- 11-2 3D1-Microprocessor Systems 1 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. ROL C Operand ROR ← Operand C → 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. ROXL C Operand ← ROXR X X Operand C → 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 mostsignificant 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 left-hand end is copied into the X-bit. If we now execute ROXL.L #1,D1 , the significant longword is shifted left and the bit shifted out of DO is shifted into the significant bit of D1. Rotate through extend instructions are largely used to facilitate arithmetic. of the mostleast64-bit 11-3 3D1-Microprocessor Systems 1 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 DS.L ORG DC.B $1FFC 1 $2000 '124',$0 ORG LEA CLR $4000 num,A0 D1 load number’s effective address clear accumulator loop MOVE.B BEQ SUB.B MOVE.L LSL.L LSL.L ADD.L ADD.L BRA (A0)+,D0 exit #$30,D0 D1,D2 #3,D1 #1,D2 D2,D1 D0,D1 loop copy ascii char in D0 if char is 0 then exit else subtract $30 from ascii char code copy D1 in D2 multiply contents of D1 by 8 multiply contents of D2 by 2 add [D1] and [D2] ( product of by 10 mult) add [D0] and [D1] goto loop exit MOVE.L TRAP END D1,RSLT #0 else save [D1] to RSLT return control to Monitor RSLT num 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. 11-4