Instructions moving data

Instructions moving data – do not change flags. Syntax: mov destination, source (move data) Source:  constant  general-purpose register  segment r...
12 downloads 4 Views 44KB Size
Instructions moving data – do not change flags. Syntax: mov destination, source (move data)

Source:  constant  general-purpose register  segment register  memory location Destination:  general-purpose register  segment register  memory location If one operand is a segment register, the other must be a general-purpose register or a memory location. mov ds,es mov ds,@data

Assembly Language 5/ 1

mov al,Value; 56h → al mov bx,offset Value; 1234h → bx

AL 1234h

56h

56h

address of variable Value (offset in the data segment) Moving data to and from memory, register EAX is usually used, because: Number DW 0 mov bx,Number → 8B1E0000 mov ax,Number → A10000

better!

Assembly Language 5/ 2

Operator offset cannot be used to get the address of a memory location pointed to by an indirect addressing mode (using a base or index register). Example Store the address of variable Value increased by DI to BX. mov bx,offset Value[di] will be compiled as mov bx,Value[di]

lea register, memory (load effective address) – load the effective address (offset) of a memory location to a general-purpose register. lea bx,Value is the same as mov bx,offset Value lea bx,Value[di]; store the address of variable Value increased by DI to BX – can be used for fast addition and multiplication: lea ecx,[eax + ebx]; ecx := eax + ebx lea eax,[eax + eax*4]; eax := 5*eax

Assembly Language 5/ 3

movzx register, register/memory (move with zero-extend) – copy the right operand into the lower half of the left operand and set the upper half to 0. .386 ; instruction is from the extended instruction set movzx bx,dl the same as

mov bl,dl mov bh,0

movsx register, register/memory (move with sign-extend) – copy the right operand into the lower half of the left operand and sign extend the right operand to the upper half of the left operand (i.e. duplicate the highest order bit of the right operand throughout the upper bits of the left operand). Instruction is from the extended instruction set. xchg register/memory, register/memory (exchange) – exchange the contents of the operands.

Assembly Language 5/ 4

lds register, memory (load full pointer into DS) The right operand contains a complete address (a base address of a segment and offset). The instruction copies the base address from the right operand to register DS and the offset to the given register. Value DB 0 Address DD Value; offset and segment of variable Value lds bx,Address; the same as mov bx,offset Value mov ax,seg Value mov ds,ax

les register, memory (load full pointer into ES) lfs register, memory (load full pointer into FS) lgs register, memory (load full pointer into GS)

Assembly Language 5/ 5

Arithmetic instructions – set ZF, CF, OF, AF, PF, SF add register/memory, register/memory/constant (add) – add the operands, store the result to the left operand. sub register/memory, register/memory/constant (subtract) – subtract the right operand from the left, store the result to the left operand. adc register/memory, register/memory/constant (add with carry) – add the operands and CF, store the result to the left operand; useful when adding operands longer than a generalpurpose register. A DQ 0FFFFFFFFh ; FF FF FF FF 00 00 00 00 B DQ 1 ; 01 00 00 00 00 00 00 00 C DQ ?; C = A + B .386 mov eax,dword add eax,dword mov dword ptr mov eax,dword adc eax,dword mov dword ptr

ptr A ptr B C,eax ptr A+4 ptr B+4 C+4,eax Assembly Language 5/ 6

sbb register/memory, register/memory/constant (subtract with borrow) – subtract the right operand and CF from the left operand, store the result to the left operand; useful when subtracting operands longer than a general-purpose register. inc register/memory (increment) – increment the operand by 1. It does not affect CF! add bx,1 → 83C301 inc bx → 43

better! dec register/memory (decrement) – decrement the operand by 1. It does not affect CF! cmp register/memory, register/memory/constant (compare) – compare the operands: subtract the right operand from the left one, set flags according to the result (do not store the result itself). Assembly Language 5/ 7

mul register/memory (unsigned multiply)

Operand type Multiplied by

Result

byte

AL

AX

word

AX

DX:AX

dword

EAX

EDX:EAX

If the upper half of the result is 0, OF a CF are set to 0. Otherwise OF and CF are set to 1. imul register/memory (signed multiply) – implicit operands as mul imul register, register/memory/constant – left operand := left * right The operands must be of the same type (word or dword): imul dx,word ptr [di] imul register, register/memory, constant – left operand := intermediate * right The leftmost and intermediate operands must be of the same type (word or dword). If the upper half of the result is a sign extension of the lower half, OF and CF are set to 0, otherwise they are set to 1. Assembly Language 5/ 8

div register/memory (unsigned divide) – unsigned integer division Dividend

Type of the operand Quotient Reminder (divider)

AX

byte

AL

AH

DX:AX

word

AX

DX

EDX:EAX

dword

EAX

EDX

Example Divide 4001 in AX by 10: mov dx,0 mov bx,10 div bx; 400 → ax, 1 → dx If a division by zero occurs or the quotient does not fit into a given register, a type 0 interrupt is generated. idiv register/memory (signed divide) – signed integer division – implicit operands as div

Assembly Language 5/ 9

Sign extension If a signed dividend is of the same size than a divider, then it must sign extend before division. cbw (convert byte to word) – all bits of register AH are filled with a value of the highest bit of AL. Example Divide -15 in BH by 2 in BL: mov al,bh cbw; ax := FFF1h = -15 idiv bl; al := F9h = -7, ah := FFh = -1 cwd (convert word to doubleword) – convert AX to DX:AX. cwde (convert word to doubleword extended) – convert AX to EAX. cdq (convert doubleword to quadword) – convert EAX to EDX:EAX. Instructions cbw, ... do not affect flags. Instructions cwde a cdq are from the extended instruction set. Assembly Language 5/ 10

neg register/memory (two’s complement negation) – switch the sign of the operand (replace the operand by its two’s complement) mov neg mov neg

al,-15 al bl,al bl

; ; ; ;

al al bl bl

:= := := :=

-15 = 11110001b 15 = 00001111b 15 -15

Assembly Language 5/ 11

Logical instructions – perform logical operations on the corresponding bits of the operands. The result is stored to the left operand. – set flags:  CF and OF to 0  ZF, PF, SF according to the result of the operation  AF – undefined and register/memory, register/memory/constant – is useful when a single bit (several bits) of the left operand are to be set to 0. The right operand is a mask having 0 in given bits and 1 in the other bits. Examples Set the 3rd bit of register BL to zero: and bl,11110111b Convert the ASCII code for a digit to the number: ‘0’ = 00110000b, ‘1’ = 00110001b, ... mov al,’9’ and al,00001111b; ’9’ → 9

Assembly Language 5/ 12

or register/memory, register/memory/constant – is useful when a single bit (several bits) of the left operand are to be set to 1. The right operand is a mask having 1 in given bits and 0 in the other bits. Examples Set the 3rd bit of register BL to 1: or bl,00001000b Convert a number to the ASCII code: mov al,9 or al,110000b; 9 → ’9’ xor register/memory, register/memory/constant – is useful when a single bit (several bits) of the left operand are to be inverted. 1 in the mask inverts the corresponding bit, 0 leaves the bit unchanged. Example Invert the upper 4 bits of register BL: mov bl,01010101b xor bl,11110000b ; bl := 10100101b Set register CX to 0: mov cx,0

→ B90000

xor cx,cx → 33C9

better!

Assembly Language 5/ 13

test register/memory, register/memory/constant (logical compare) – AND without saving the result – is useful when we want to branch the execution according to the value of a chosen bit of the left operand. Example Branch according to the 3rd bit of register BL: test bl,00001000b jz Zero; the 3rd bit is 0 One: ... not register/memory (one’s complement negation) – invert all bits of the operand – does not change flags

Assembly Language 5/ 14

Shift and rotate instructions The left operand is a register or memory location (byte, word or doubleword), whose bits are shifted or rotated to the left or right. The right operand is a constant or register CL. The right operand determines the number of places to shift. Instructions with a constant value above 1 are from the extended instruction set (directive .386 must be in the program before the instruction is used).

Shift instructions They set flags: – CF, OF, ZF, PF, SF according to the result of the operation – AF – undefined shl register/memory, constant/CL (shift logical left) sal register/memory, constant/CL (shift arithmetic left)

CF

… …

0

– are used for fast doubling (or multiplying by 4, 8 or 16) unsigned numbers: shl dx,4; dx := 16*dx Assembly Language 5/ 15

shr register/memory, constant/CL (shift logical right) … …

0

CF

– halving (or dividing by 4, 8 or 16) unsigned numbers sar register/memory, constant/CL (shift arithmetic right) … …

CF

– halving (or dividing by 4, 8 or 16) signed numbers: mov bl,-4 ; bl := -4 = 11111100b sar bl,1 ; bl := -2 = 11111110b

Rotate instructions – change CF a OF rol register/memory, constant/CL (rotate left)

CF

… …

Assembly Language 5/ 16

ror register/memory, constant/CL (rotate right)

… …

CF

rcl register/memory, constant/CL (rotate left through carry)

CF

… …

– useful when doubling operands longer than a generalpurpose register. Example Double variable BigNumber of type qword: shl dword ptr [BigNumber],1 rcl dword ptr [BigNumber + 4],1

rcr register/memory, constant/CL (rotate right through carry)

… …

CF

Assembly Language 5/ 17