ics.p.lodz.plics.p.lodz.pl/~dpuchala/lowlevelprogr/lecture4.pdf · author: dariusz puchała created...

25
Lecture 5 IFE: Course in Low Level Programing

Upload: others

Post on 22-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Lecture 5

IFE: Course in Low Level Programing

Page 2: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

Logical operations on bits

Bitwise operationsNOT – bitwise NOT (complement),AND – bitwise AND ,OR – bitwise OR,XOR – bitwise XOR,TEST – logical comparison.

Shifting operationsSHL – logical shift left,SHR – logical shift right,ROL – cyclic shift left,ROR – cyclic shift right,RCL – cyclic shift left with carry,RCR – cyclic shift right with carry.

String operationsREP/REPE/REPNE – iterative execution of a string operation,MOVS – move block of data,CMPS – compare blocks of data,SCAS – search in block of data,STOS – fill block of data

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

Page 3: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

Bitwise operations

NOT (from not byte, word or double word)

Instruction performs logical negation on each bit (0->1, 1->0).The only argument can be represented by registers or memory locations.Affects no flags.

Available variants:

NOT r/m8 NOT r/m16 NOT r/m32

EXAMPLE:

NOT(00101001)=11010110

(…)a db 10h(…)NOT AXNOT a(…)

Page 4: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

AND (from AND byte, word or double word)

Performs logical bitwise AND operation on its two arguments. Result is hold in the first argument. AND operation gives 1 only for a pair of 1s (00->0, 01->0, 10->0, 11->1).Arguments can be given in the form of memory location, register or immediate value. Affects: OF=0, CF=0, SF, ZF, PF.

Available variants:

AND r,r/m8 AND r,r/m16 AND r,r/m32AND r/m8,r AND r/m16,r AND r/m32,rAND r/m8,imm8 AND r/m16,imm16 AND r/m32,imm32

EXAMPLES(…)a dw 120Ah(...)MOV AX,0010hAND a,AX(…)MOV AL,13hAND AL,01h(…)

Page 5: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

EXAMPLES (cont.)

How to check the state of bit with number n?

1. Prepare mask: mask=1<<n,2. Use AND operation,3. IF ZF==0 THEN Bit equals 0 ELSE Bit equals 1.

00100101 (n=5)00100000 (mask)00100000 (result of AND operation)

How to unset bit with number n?

1. Prepare mask: mask=1<<n,2. Negate mask NOT mask,3. Use AND operation.

00100101 (n=5)00100000 (mask)110111111 (NOT mask)00000101 (result after AND)

Page 6: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

EXAMPLES (cont.)

How to keep bits with numbers n1 to n2 (n1<=n2)?

1. Prepare mask: mask=(1<<(n2-n1+1))-1)<<n1,2. Use AND operation.

00100101 (n1=1, n2=3)00001110 (mask)00000100 (result of AND operation)

Page 7: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

OR (from OR byte, word or double word)

Performs logical bitwise OR operation on its two arguments. Result is hold in the first argument. OR operation gives 0 only for a pair of 0s (00->0, 01->1, 10->1, 11->1).Arguments can be given in the form of memory location, register or immediate value. Affects: OF=0, CF=0, SF, ZF, PF.

Available variants:

OR r,r/m8 OR r,r/m16 OR r,r/m32OR r/m8,r OR r/m16,r OR r/m32,rOR r/m8,imm8 OR r/m16,imm16 OR r/m32,imm32

EXAMPLES(…)a db 1Fh(...)MOV BL,01hOR a,BL(…)MOV CL,13hOR CL,0Fh(…)

Page 8: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

EXAMPLES (cont.)

How to set bit with number n?

1. Prepare mask: mask=1<<n,3. Use OR operation.

00100101 (n=3)00001000 (mask)00101101 (result after OR)

How to set bits with numbers n1 to n2 (n1<=n2)?

1. Prepare mask: mask=(1<<(n2-n1+1))-1)<<n1,2. Use OR operation.

00100101 (n1=1, n2=3)00001110 (mask)00101111 (result of OR operation)

Page 9: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

XOR (from exclusive OR byte, word or double word)

Performs logical bitwise XOR operation on its two arguments. Result is hold in the first argument. Exclusive OR operation produces 1 only for a pair of opposite bits (00->0, 01->1, 10->1, 11->0).Arguments can be given in the form of memory location, register or immediate value. Affects: OF=0, CF=0, SF, ZF, PF.

Available variants:

XOR r,r/m8 XOR r,r/m16 XOR r,r/m32XOR r/m8,r XOR r/m16,r XOR r/m32,rXOR r/m8,imm8 XOR r/m16,imm16 XOR r/m32,imm32

EXAMPLES(…)a db 1Fh(...)MOV AH,01hXOR a,AH(…)XOR AX,AX(…)

Page 10: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

EXAMPLES (cont.)

It should be noted that XOR is reversible.

0010010110101010 (mask)10001111 (result after XOR)00100101 (result after one more XOR with the same mask)

How to clear contents of byte?

1. Use XOR using one value as arguments.

001001010010010100000000 (result after XOR operation)

Page 11: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

TEST (from test or logical compare byte, word or double word)

Instruction performs logical AND operation but in the contrary to AND instruction the result is volatile and only flags are affected.Arguments can be given in the form of memory location, register or immediate value. Affects: OF=0, CF=0, SF, ZF, PF.

Available variants:

TEST r,r/m8 TEST r,r/m16 TEST r,r/m32TEST r/m8,r TEST r/m16,r TEST r/m32,rTEST r/m8,imm8 TEST r/m16,imm16 TEST r/m32,imm32

EXAMPLES(…)MOV AX,0131hTEST AX,0001h(…)

Page 12: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

Shifting operations

SHL (from shift logical left byte, word or double word)

Instruction shifts left the contents of the first argument by the number of bits indicated by the second argument. With the number of bits greater than 1 this instruction takes CL as the second argument. First argument can be given in the form of register or memory location.Affects: OF, SF, ZF, PF, CF and AF is unpredictable.

Available variants:

SHL r/m8,1 SHL r/m16,1 SHL r/m32,1SHL r/m8,CL SHL r/m16,CL SHL r/m32,CL

EXAMPLE:

(…)a db 16 (…)SHL a,1(…)

Page 13: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

SHR (from shift logical right byte, word or double word)

Instruction in opposition to SHL shifts right the contents of its first argument.

0

n-1 0CF

SHL instruction

0

n-1 0 CF

SHR instruction

Page 14: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

EXAMPLES (cont.)

How to conveniently multiply integer number by the power of 2?

1. Write into CL the power indicator.2. Use SHL number, CL instruction

00000101 (integer number 5)CL<-2 (multiplication by 4)00010100 (result after SHL number,CL, in decimal system: 20)

How to conveniently divide integer number by the power of 2?

1. Write into CL the power indicator.2. Use SHR number, CL instruction

00110000 (integer number 48)CL<-2 (division by 4)00001100 (result after SHR number,CL, in decimal system: 12)

Page 15: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

ROL (from rotate left byte, word or double word)

Instruction performs cyclic shift left of the contents of the first argument by the number of bits hold in the second argument. When the number of bits is greater than 1 it should be provided with CL register taken as the second argument. First argument can be given in the form of register or memory location.Affects: OF, CF.

Available variants:

ROL r/m8,1 ROL r/m16,1 ROL r/m32,1ROL r/m8,CL ROL r/m16,CL ROL r/m32,CL

EXAMPLE:

(…)MOV AX,1234hROL AX,1(…)

Page 16: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

ROR (from rotate right byte, word or double word)

ROR instruction cyclically shift right the first argument. For other information see ROL instruction.

n-1 0CF

ROL instruction

1n-2

ROR instruction

n-1 0 CF1n-2

Page 17: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

EXAMPLES (cont.)

How to count the number of 1s in byte?

1. Clear the counter value.2. Use ROL byte,1.3. ADC counter,1.4. Execute steps 2 and 3 eight times.

MOV AL, 1234hXOR CL,CLROL AL,1ADC CL,0ROL AL,1ADC CL,0ROL AL,1ADC CL,0ROL AL,1ADC CL,0ROL AL,1ADC CL,0ROL AL,1ADC CL,0ROL AL,1ADC CL,0ROL AL,1ADC CL,0

Page 18: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

RCL (from rotate through carry left byte, word or double word)

Instruction cyclically shifts left the contents of the first argument through CF flag by the number of bits provided by the second argument. If the number of bits is greater than 1 it should be given as the argument in CL register. First argument can be given in the form of register or memory location.Affects: OF, CF.

Available variants:

RCL r/m8,1 RCL r/m16,1 RCL r/m32,1RCL r/m8,CL RCL r/m16,CL RCL r/m32,CL

EXAMPLE:

(…)a db 0A10h(…)MOV CL,3RCL a,CL(…)

Page 19: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

RCR (from rotate through carry right byte, word or double word)

Instruction cyclically shifts right the contents of the first argument through CF flag by the number of bits provided by the second argument. For further information see RCL instruction.

n-2n-1 0CF

RCL instruction

1

n-1 0 CF1n-2

RCR instruction

Page 20: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

EXAMPLES (cont.)

How to obtain bits in reversed order in byte?

1. Clear the result value.2. Use ROL byte,1.3. Use RCR result,1.4. Execute steps 2 and 3 eight times.

MOV AL, 1234hXOR CL,CLROL AL,1RCR CL,1ROL AL,1RCR CL,1ROL AL,1RCR CL,1ROL AL,1RCR CL,1ROL AL,1RCR CL,1ROL AL,1RCR CL,1ROL AL,1RCR CL,1ROL AL,1RCR CL,1

Page 21: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

REP/REPE/REPNE (from repeat, repeat while equal, repeat while not equal string operation)

1. checks CX and if CX equals 0 it goes to step 5,2. executes string operation3. decrements CX by 1,4. REPE and REPNE additionally check ZF flag. If ZF=1 for REPE or ZF=0 for REPNE then go to step 1. REP always goes to step 1,5. finish.

Affects no flags.Available variants:

REP/REPE/REPNE string_instruction

Page 22: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

MOVSB/MOVSW/MOVSD (from move string of bytes, words or double words)

1. sends byte, word or double word from address DS:SI to ES:DI,2. If DF=0 then SI=SI+1{2,4}, DI=DI+1{2,4}, otherwise SI=SI-1{2,4}, DI=DI-1{2,4},

Affects no flags.

EXAMPLE:

Page 23: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

CMPSB/CMPSW/CMPSD (from compare string of bytes, words or double words)

1. compares two bytes, words or double words at locations DS:SI to ES:DI and sets flags (see CMP instruction),2. If DF=0 then SI=SI+1{2,4}, DI=DI+1{2,4}, otherwise SI=SI-1{2,4}, DI=DI-1{2,4},

Affects like CMP instruction.

EXAMPLE:

Page 24: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

SCASB/SCASW/SCASD (from scan string of bytes, words or double words)

1. it subtracts from AL{AX,EAX}register byte, word or double word. The result is not kept but flags are affected,2. if DF=0 then SI=SI+1{2,4}, DI=DI+1{2,4}, otherwise SI=SI-1{2,4}, DI=DI-1{2,4},

Affects flags like SUB instruction.

EXAMPLE:

Page 25: ics.p.lodz.plics.p.lodz.pl/~dpuchala/LowLevelProgr/Lecture4.pdf · Author: Dariusz Puchała Created Date: 11/18/2010 11:54:18 AM

Instruction Set of Intel x86 Microprocessors

IFE: Course in Low Level Programing

STOSB/STOSW/STOSD (from store byte, word or double word in a string of data)

1. it stores AL{AX,EAX} register at location ES:DI,2. if DF=0 then SI=SI+1{2,4}, DI=DI+1{2,4}, otherwise SI=SI-1{2,4}, DI=DI-1{2,4},

Affects no flags.

EXAMPLE: