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:
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.
AND.B #11110000,D0 results in [D0]= 11000000.
OR.B #11110000,D0 results in [D0]= 11111010.
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
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.
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
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.
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.
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
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…
REFERENCES
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.
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