Upload
dinhhanh
View
218
Download
3
Embed Size (px)
Citation preview
System Programming withSystem Programming with Assembly Language: Part IXy g g
First Semester 2012Department of Computer Science
Faculty of ScienceChiang Mai University
OutlineOutline
• Logic Instructions• Shift InstructionsShift Instructions• Rotate Instructions
204231: Computer Organization and Architecture 2
AND OR and XOR InstructionsAND, OR, and XOR Instructions
AND destination, sourceOR destination, sourceOR destination, sourceXOR destination, source
Effect on flags:SF ZF PF fl t th ltSF, ZF, PF reflect the resultAF is undefinedCF, OF = 0
204231: Computer Organization and Architecture 3
AND OR and XOR InstructionsAND, OR, and XOR Instructions
• The destination must be a register or memory location.
• The source may be a constant, register, or memory locationmemory location.
• The memory‐to‐memory operations are not allowed.
204231: Computer Organization and Architecture 4
ANDAND
• can be used to clear specific destination bits while preserving the others.p g
• A 0 mask bit clears the corresponding destination bitdestination bit.
• A 1 mask bit preserves the corresponding destination bit.
204231: Computer Organization and Architecture 5
OROR
• can be used to set specific destination bits while preserving the others.p g
• A 1 mask bit sets the corresponding destination bitdestination bit.
• A 0 mask bit preserves the corresponding destination bit.
204231: Computer Organization and Architecture 6
XORXOR
• can be used to complement specific destination bits while preserving the others.p g
• A 1 mask bit complements the corresponding destination bitdestination bit.
• A 0 mask bit preserves the corresponding destination bit.
204231: Computer Organization and Architecture 7
Clear the sign bit of AL while leaving the other bits unchanged.Use AND with 0111 1111b = 7Fh as the mask. Thus,AND AL, 7Fh
204231: Computer Organization and Architecture 8
Set msb and lsb of AL while preserving the other bits.Use OR with 1000 0001b = 81h as the mask. Thus,OR AL, 81h
204231: Computer Organization and Architecture 9
Change the sign bit of DXChange the sign bit of DX.
Use XOR with a mask of 8000h. Thus,XOR DX, 8000hXOR DX, 8000h
204231: Computer Organization and Architecture 10
Converting an ASCII Digit to a NumberConverting an ASCII Digit to a Number
If the “5” key is pressed, AL gets 35h instead of 5. To get 5 in AL, we could do this:gSUB AL, 30h
Another method is to use AND to clear the high nibble (high four bits) of AL:nibble (high four bits) of AL:AND AL, 0FH
Because the codes “0” to “9” are 30h to 39h.
204231: Computer Organization and Architecture 11
Convertinga Lowercase Letter to Upper Case
The ASCII codes of “a” to “z” range from 61h to 7Ah; the codes “A” to “Z” go from 41h to 5Ah.g
Thus for example, if DL contains the code of a lowercase letter, we could convert to upper case by executingSUB DL, 20h
204231: Computer Organization and Architecture 12
Convertinga Lowercase Letter to Upper Case
Character Code Character Codea 0110 0001 A 0100 0001a 0110 0001 A 0100 0001b 0110 0010 B 0100 0010. . . .. . . .. . . .z 0111 1010 Z 0101 1010
204231: Computer Organization and Architecture 13
Convertinga Lowercase Letter to Upper Case
We need only clear bit 5 by using AND with 1101 1111b or 0DFh.
So if the lowercase character to be converted is in DL, executeAND DL, 0DFhAND DL, 0DFh
204231: Computer Organization and Architecture 14
Clearing a RegisterClearing a Register
To clear AX, we could executeMOV AX, 0MOV AX, 0SUB AX, AX
Using the fact that 1 XOR 1 = 0 and 0 XOR 0 = 0, a third way isa third way isXOR AX, AX
204231: Computer Organization and Architecture 15
Testing a Register for ZeroTesting a Register for Zero
OR CX, CXBecause 1 OR 1 = 1 and 0 OR 0 = 0, it leaves theBecause 1 OR 1 1 and 0 OR 0 0, it leaves the content of CX unchanged; however, it affects ZF and SF and in particular if CX contains 0ZF and SF, and in particular if CX contains 0 then ZF = 1.
So it can be used as an alternative toCMP CX 0CMP CX, 0
204231: Computer Organization and Architecture 16
NOT InstructionNOT Instruction
NOT destination
204231: Computer Organization and Architecture 17
Complement the bits in AXComplement the bits in AX.
NOT AX
204231: Computer Organization and Architecture 18
TEST InstructionTEST Instruction
TEST destination, source
Effect on flags:Effect on flags:SF, ZF, PF reflect the resultAF is undefinedCF OF 0CF, OF = 0
204231: Computer Organization and Architecture 19
TEST InstructionTEST Instruction
• performs an AND operation but does not change the destination content.g
• The purpose of TEST is to set the status flags.
204231: Computer Organization and Architecture 20
Examining BitsExamining Bits
TEST destination, maskBecause 1 AND b = b and 0 AND b = 0, the resultBecause 1 AND b b and 0 AND b 0, the result will have 1’s in the tested bit positions if and only if the destination has 1’s in theseonly if the destination has 1 s in these positions; it will have 0’s elsewhere.
If destination has 0’s in all the tested position, the result will be 0 and so ZF = 1the result will be 0 and so ZF = 1.
204231: Computer Organization and Architecture 21
Jump to label BELOWif AL contains an even numberEven numbers have a 0 in bit 0. Thus, the mask is 0000 0001b = 1.
TEST AL 1 i AL ?TEST AL, 1 ; is AL even?JZ BELOW ; yes, go to BELOW; y , g
204231: Computer Organization and Architecture 22
Shift and Rotate InstructionsShift and Rotate Instructions
For a single shift or rotate, the form isOpcode destination, 1Opcode destination, 1
For a shift or rotate of N positions, the form isOpcode destination, CL
Wh CL t i NWhere CL contains N.
In both cases destination is an 8‐ or 16‐bitIn both cases, destination is an 8 or 16 bit register or memory location.
204231: Computer Organization and Architecture 23
Shift LeftShift Left
SHL shifts the bits in the destination to the left.
Effect on flags:SF, ZF, PF reflect the resultAF is undefinedAF is undefinedCF = last bit shifted outOF = 1 if result changes sign on last shift
204231: Computer Organization and Architecture 24
SHL and SALSHL and SAL
204231: Computer Organization and Architecture 25
Multiplication by Left ShiftMultiplication by Left Shift
A left shift on a binary number multiplies it by 2.
Suppose that AL contains 5 = 00000101b. A left hif i 00001010b 10d h d bli ishift gives 00001010b = 10d, thus doubling its value.
If AX i FFFFh ( 1) th hifti th ti illIf AX is FFFFh (–1), then shifting three times will yield AX = FFF8h (–8).
204231: Computer Organization and Architecture 26
Shift Arithmetic LeftShift Arithmetic Left
SAL is often used in instances where numeric multiplication is intended.p
Both instructions generate the same machine codecode.
204231: Computer Organization and Architecture 27
OverflowOverflow
h fl fl l bl d fThe overflow flags are not reliable indicators for a multiple left shift because it is really a series p yof single shifts, and CF, OF only reflect the result of the last shiftresult of the last shift.
204231: Computer Organization and Architecture 28
OverflowOverflow
If BL contains 80h, CL contains 2and we execute SHL BL, CL, then CF = OF = 0 even though both gsigned and unsigned overflow occur.
204231: Computer Organization and Architecture 29
Write some code to multiply the value of AX by 8. Assume that overflow will not occur.
MOV CL, 3 ; number of shifts to doSAL AX, CL ; multiply by 8SAL AX, CL ; multiply by 8
204231: Computer Organization and Architecture 30
Shift RightShift Right
SHR performs right shifts on the destination operand.p
A 0 is shifted into the msb position, and the rightmost bit is shifted into CFrightmost bit is shifted into CF.
204231: Computer Organization and Architecture 31
Shift Arithmetic RightShift Arithmetic Right
SAR operates like SHR, with one difference: the msb retains its original value.g
204231: Computer Organization and Architecture 32
SHRSHR
204231: Computer Organization and Architecture 33
SARSAR
204231: Computer Organization and Architecture 34
Division by Right ShiftDivision by Right Shift
For even numbers, a right shift divides the destination’s value by 2.y
For odd numbers, a right shift halves the destination’s value and round down to thedestination s value and round down to the nearest integer.
If BL contains 00000101b = 5 then after a rightIf BL contains 00000101b = 5, then after a right shift BL will contain 00000010b = 2.
204231: Computer Organization and Architecture 35
Signed and Unsigned DivisionSigned and Unsigned Division
If an unsigned interpretation is being given, SHR should be used.
For a signed interpretation, SAR must be used, because it preserves the signbecause it preserves the sign.
204231: Computer Organization and Architecture 36
Rotate LeftRotate Left
ROL shifts bits to the left.The msb is shifted into the rightmost bit.The msb is shifted into the rightmost bit.The CF also gets the bit shifted out of the msb.
204231: Computer Organization and Architecture 37
Rotate RightRotate Right
ROR works just like ROL, except that the bits are rotated to the right.g
204231: Computer Organization and Architecture 38
ROLROL
204231: Computer Organization and Architecture 39
RORROR
204231: Computer Organization and Architecture 40
Use ROL to count the number of 1 bits in BX, without changing BX. Put the answer in AX.
biXOR AX, AX ; AX counts bitsMOV CX, 16 ; loop counterp
TOP:ROL BX 1 ; CF = bit rotated outROL BX, 1 ; CF = bit rotated outJNC NEXT ; 0 bitINC AX ; 1 bit, increment total
NEXT:NEXT:LOOP TOP ; loop until done
204231: Computer Organization and Architecture 41
Use ROL to count the number of 1 bits in BX, without changing BX. Put the answer in AX.
In this example, we used JNC (jump if no carry), which causes a jump if CF = 0.j p
204231: Computer Organization and Architecture 42
Rotate Carry LeftRotate Carry Left
RCL shifts bits of the destination to the left.The msb is shifted into the CF, and the previousThe msb is shifted into the CF, and the previous value of CF is shifted into the rightmost bit.
204231: Computer Organization and Architecture 43
Rotate Carry RightRotate Carry Right
RCR works just like RCL, except that the bits are rotated to the right.g
204231: Computer Organization and Architecture 44
RCLRCL
204231: Computer Organization and Architecture 45
RCRRCR
204231: Computer Organization and Architecture 46
An application: Reversing a Bit PatternAn application: Reversing a Bit Pattern
MOV CX, 8 ; number of operations to do
REVERSE:REVERSE:SHL AL, 1 ; get a bit into CFRCR BL, 1 ; rotate it to BLLOOP REVERSE ; loop until doneLOOP REVERSE ; loop until doneMOVAL, BL ; AL gets reversed pattern
204231: Computer Organization and Architecture 47
ReferenceReference
• Ytha Yu and Charles Marut, Assembly Language Programming and Organization of g g g g gthe IBM PC. New York: McGraw‐Hill, 1992.
204231: Computer Organization and Architecture 48