48
System Programming with System Programming with Assembly Language: Part IX First Semester 2012 Department of Computer Science Faculty of Science Chiang Mai University

Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

Embed Size (px)

Citation preview

Page 1: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

System Programming withSystem Programming with Assembly Language: Part IXy g g

First Semester 2012Department of Computer Science

Faculty of ScienceChiang Mai University

Page 2: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

OutlineOutline

• Logic Instructions• Shift InstructionsShift Instructions• Rotate Instructions

204231: Computer Organization and Architecture 2

Page 3: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 4: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 5: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 6: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 7: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 8: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 9: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 10: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 11: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 12: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 13: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 14: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 15: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 16: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 17: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

NOT InstructionNOT Instruction

NOT destination

204231: Computer Organization and Architecture 17

Page 18: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

Complement the bits in AXComplement the bits in AX.

NOT AX

204231: Computer Organization and Architecture 18

Page 19: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 20: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 21: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 22: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 23: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 24: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 25: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

SHL and SALSHL and SAL

204231: Computer Organization and Architecture 25

Page 26: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 27: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 28: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 29: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 30: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 31: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 32: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 33: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

SHRSHR

204231: Computer Organization and Architecture 33

Page 34: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

SARSAR

204231: Computer Organization and Architecture 34

Page 35: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 36: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 37: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 38: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

Rotate RightRotate Right

ROR works just like ROL, except that the bits are rotated to the right.g

204231: Computer Organization and Architecture 38

Page 39: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

ROLROL

204231: Computer Organization and Architecture 39

Page 40: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

RORROR

204231: Computer Organization and Architecture 40

Page 41: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 42: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 43: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 44: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 45: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

RCLRCL

204231: Computer Organization and Architecture 45

Page 46: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

RCRRCR

204231: Computer Organization and Architecture 46

Page 47: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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

Page 48: Assembly - Chiang Mai University 9.pdfOpcode destination, 1 For a shift or rotate of N positions, the form is Opcode destination, CL Where CL contitains N. In both cases, destination

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