Sunu4 Arithmetic, Logic and Control Instructions2012
Sunu4 Arithmetic, Logic and Control Instructions2012
Control Instructions
Intel Assembly
Arithmetic Operations:
Addition
Subtraction
Multiplication
Division
Comparison
Negation
Increment
Decrement
Logic Operations:
AND
OR
XOR
NOT
shift
rotate
compare (test)
Arithmetic Operations
l Addition (8,16, and 32 bit), Increment, Add-with-carry and
Exchange-and-add:
l Contents of the rightmost 8 bits of the FLAGS register can change
(+Overflow) for arithmetic and logic instructions.
l Flags include: Z ,C, A, S ,P ,O
l Examples:
add al, bl
add cl, 44h
add cl, [bp]
add [bx],al
add [bx+di], dl
add byte ptr [dı],3
add al, [ARRAY + esi]
xadd ecx, ebx ;ecx=ecx+ebx, ebx=original ecx.
l The adressing modes include almost all those mentioned in Chap 3.
The only types of addition not allowed are memory-to-memory and
segment register.
l The following example uses register addition…
; a procedure that sums ax,bx,cd and dx the result is returned in ax
adds proc near
add ax, bx
add ax, cx
add ax, dx
ret
adds endp
l In the following example , immediate addition is employed.
mov dl,12h
add dl, 33h
l Memory to register addition
;a procedure that sums data in locations numb and numb+1
Sums proc near
Mov di, offset numb
Mov al,0
Add al, [di]
Add al, [di+1]
Ret
Sums endp
l Increment addition (INC): adds 1 to a register or
the content of a memory location, except a
segment register.
l Examples:
inc bl ; bl=bl+1
inc sp ;sp=sp+1
inc byte ptr [bx] ; adds 1 to the byte contents of the data segment memory loc.
Addressed by bx
inc word ptr [si]
inc data1
inc byte [edi] ;Adds 1 to any reg/mem except seg
l Increment is used to point to the next memory element in a byte-sized array
of data only.
l For word-sized data it is better to use an add di, 2
l Increment inst. Do not affect the carry flag.
l Addition-with-carry: adds the bit in the carry flag to the
operand data. (Mainly appears in software that adds
numbers that are wider than 16 bits or 32 bits)
l It affects the flags after addition.
l Examples
adc al, ah ;al=al+ah+carry
adc cx, bx ; cx=cx+bx+carry
adc dh, [bx]
adc bx, [bp+2]
adc ecx, ebx ;Adds registers + Carry flag.
l ; a procedure that sums bx-ax and dx-cx; the result
; is returned in bx-ax (written for 8086-80286)
Sum32 proc near
add ax, cx
adc bx, dx
Ret
Sum32 endp
Arithmetic Operations
Subtraction, Decrement and Subtract-with-borrow.
Subtraction:
l Forms of subtraction use any addressing mode with
8,16, and 32 bit data.
l Memory-to-memory and segment register subtractions
are not allowed.
l It affects the flag bits.
l Examples
sub eax, ebx ; eax=eax-ebx
sub ax,sp ; ax=ax-sp
sub dh, 6fh; dh=dh-6fh
sub [di], ch ;
sub ch, [bp]
Register subtraction:
l Example: This example subtracts the 16-bit contents of
registers cx and dx from the contents of register bx.
sub bx,cx
sub bx, dx
Immediate Subtraction: The micropr. allows
immediate operands for subtraction.
l Example: Subtract a 44h from a 22h.
mov ch, 22h
sub ch, 44h
Decrement subtraction:
l It subtracts a 1 from a register or the contents of a
memory location.
l The decrement indirect memory data instructions require
byte ptr, word ptr or dword ptr.( e.g. dec byte ptr [si] )
l Examples
Dec bh; bh=bh-1
Dec cx ; cx=cx-1
Dec byte ptr [di] ; data segment
Dec word ptr [bp]; stack segment
l Subtraction-with-borrow: it functions as a regular
subtraction, except that the carry flag subtracts from the
difference. It is for subtractions that are wider than16 bits
in (8086-80286 )or 32 bits in 80386 and above .
sbb ah, al ; ah=ah-al-carry
sbb ax, bx
sbb cl,2; cl=cl-2-carry
sbb byte ptr [di],3
sbb ecx, ebx ;Subs registers - borrow.
Comparison (cmp):
l A subtraction
l Changes only the flag bits. Destination never changes.
l Useful for checking the entire contents of a register or a
memory location against another value.
l It is normally followed by a conditional jump instruction,
which tests the condition of the flag bits.
l Disallowed forms are comparison of memory –to
memory and segment registers
l Examples
cmp al, 10H ; al-10
cmp ax, sp ; ax-sp
cmp [di],ch
cmp cl, [bp]
jae LABEL1 ;Jump if equal or above.
jbe LABEL2 ;Jump if equal or below.
Multiplication and Division:
Multiplication:
l performed on bytes, words or doublewords.
l Only the 80386 –P4 multiply 32 bit doublewords.
l imul/idiv: Signed integer multiplication/division.
mul/div: Unsigned.
l The product after a multiplication is always a double-
width product.
l C and O bits are cleared if most significant 8 bits of the
16-bit product are zero
l al always holds the multiplicand (or ax or eax).
Result is placed in ax (or dx and ax or edx or eax).
l mul bl;ax=al*bl (unsigned)
l imul bx;dx|ax=ax*bx (signed) dx gets the most
significant byte.
l imul cx, dx, 12H ;Special, cx=dx*12H (signed only)
l mul ecx ;edx|eax=eax*ecx
l imul byte ptr[bx]
l Example: dx=blxcl
mov bl, 5
mov cl,10,
mov al, cl
mul bl
mov dx,ax
Division:
l İdiv for signed integers, div for unsigned integers
l The dividend (bölünen) is always a double –width dividend
l There is no immediate division
l None of the flags change predictably for a division
l Division by zero and overflow generate errors.Overflow occurs when
a small number divides a large dividend.
l Examples:
div cl; ah|al=ax/cl, unsigned quotient (bölüm) in al, remainder in
ah. (8-bit division)
idiv cx ;dx|ax=(dx|ax)/cx (16-bit signed division. dx-ax 32-bit dividend.
quotient in ax, remainder in dx)
div byte pointer [bp](8-bit unsigned division)
l Example:
Mov ax, -100
Mov cx, 9
Cwd; sign-extend (cbw for “convert word to doubleword)
İdiv cx
LOGIC INSRUCTIONS
Allow bits to be set, cleared and complemented.
Commonly used to control I/O devices.
Logic operations always clear the carry and overflow flags, changes
PF, SF, ZF.
AND:
l Commonly used with a MASK to clear bits:
XXXX XXXX Operand
0000 1111 Mask
0000 XXXX Result
l Examples
and al, bl ;al=al AND bl
and cl, 33H
and ax, [di]
and array[si], al
l The and inst. Uses any addressing modes except memory-memory
and segment register adressing.
OR:
XXXX XXXX Operand
0000 1111 Mask
XXXX 1111 Result
l examples
or eax, 10 ; eax=eax OR 0000000AH
or ah,bl
or si, dx
or sp, 990dh
or dx, [bx]
or dates[di+2], al
l The OR instruction uses any addressing modes except
segment register addressing and memory-to-memory.
Logic Operations
XOR: Commonly used with a MASK to complement bits:
XXXX XXXX Operand
0000 1111 Mask
XXXX X’X’X’X’ Result
l A common use for XOR is to clear a register zero. (XOR CH,CH inst. Of
mov ch,00h)
l Example: clear bits 0 and 1 of cx, set bits 9 and 10 of cx , and invers bit 12
of cx.
Rotate:
l Rotates bits from one end to the other or through the carry flag.
l Commonly used to operate on numbers wider than 32-bits in the
386-p4 or wider than 16 bits in the 8086-80286. A rotate count can
be immediate or located in register CL.
rol si, 14 ;si rotated left by 14 places.
rcr bl, cl; bl rotated right cl places through carry.
rcl dx, 1 ;Rotate carry bit from previous rcl in dx.
ror word ptr [bp], 2; word contents of the memory location addressed by bp in the
stack segment rotated right 2 places.
l Affects only cf and of
String Instructions
l String Insts. let us operate on blocks of bytes
or words in memory.
l These blocks may consist of numeric or
alphanumeric values.
l Provides five basic operations: move,
compare, scan, load, and
store.
l These operations may have three forms.
e.g: movs dest, source
movsb
Scas dest
l The 8086 assumes the destination string is in
the extra segment and the source string is in
the data segment.
l The processor addesses the destination
string with DI
l The processor addesses the source string
with SI
l E.g. MOVS
l String instructions automatically update the
pointer to address the next element in the
string.
l Direction Flag
l CLD and STD control the state of DF.
l Repeat prefixes are modifiers that cause the
8086 hardware to repeat an intsruction.
l E.g:
mov cx, 500
rep movs dest, source
l REPE, REPZ
e.g: mov cx, 100
Repe cmps dest, source
l E.g: mov cx, 100
repne cmps dest, source
Move-String Instructions
l MOVS copies a byte or word from one
portion of memory to another.
movs dest-string, source-string
l source-string in the data segment (possible
to override SI)
l dest-string in the extra segment
l To move a string by applying rep prefix
l Clear DF or set DF
l Load the offset of the source string into SI
l Load the offset of the dest string into DI
l Load the element count into Cx
l Execute the MOVS inst. With a REP prefix.
CLD ; set DF to 0 to move forward
LEA SI, source
LEA DI, ES:dest
mov cx, 100
Rep movs dest, source
l Move Byte String (MOVSB) and Move Word
String (MOVSW)
l Are preferable to MOVS, they save the assembler
from looking up the size of operands.
l Have no operands.
l E.g.: CLD ; set DF to 0 to move forward
LEA SI, source
LEA DI, ES:dest
mov cx, 100
Rep movsb
l SI normally addresses tha data segment, you can use a different
segment.
l LEA SI, ES:HERE
LEA DI, ES:THERE
MOVSB
l Copies a byte from here to there, where both strings are in the
extra segment.
l We can not override DI. The destination must be in
ES???
l Copy 100 bytes from a string in DS to a string in DS:
MOV SI,DS
MOV ES,SI
CLD
LEA SI, HERE
LEA DI, THERE
MOV CX,100
REP MOVSB
Compare Strings (CMPS)
l Compares a source operand to a
destination operand and return the results
in the flags. It affects neither operand.
l The General form of CMPS:
CMPS des-string, source-string
l Compares an element in the data segment
with an element in the extra segment.
l To compare more than one element, a
repeat prefix is needed.
l Prefix ith either REPE(REPZ) or
REPNE(REPNZ)
l E.g:
cld
Mov cx,100
Repe cmps dest, source
l Cld
Mov cx,100
Repne cmps dest, source
l CMPSB and CMPSW are size-specific, no
operand form of CMPS.
l CMPS CMPSB and CMPSW change SF,
ZF, AF, PF, CF, and OF.
SCAN STRING INSTRUCTIONS
l Let you search a string in the extra
segment for a specific value.
l The search value must be in AL or AX.
l The general form of Scan String(SCAS)
SCAS dest-string
l To operate on more than one string
element, apply the repeat prefix REPE
(REPZ) or REPNE(REPNZ)
l E.g.:
CLD
LEA DI, ES:B-STRING
MOV AL,10
MOV CX, 100
REPE SCAS B-STRING
l This sequence searches up to 100 elements of the B_STRING, trying to find an
element doec not contain 10. ıf such an element is found the offset of the next
element is returned DI and ZF is zero
l Scan Byte String (SCASB) and Scan Word String (SCASW) are
size-specific, no operand form of SCAS.
l SCASW, SCASB and SCas change SF, ZF, AF, PF, CF, and OF.
Laod String and Store String
Instructions
l LODS transfer a source string operand
addressed by SI to accumulator AL or AX
then adjusts the SI to point to the next
element
l STOS tranfers the value in AL or AX to to
the destination string operand in the extra
segment addressed by DI then adjust DI
to point the next element.
String Comparisons:
String Scan Instructions:
scasb/w/d compares the al/ax/eax register
with a byte block of memory and sets the
flags.
Often used with repe and repne
cmpsb/w/d compares 2 sections of memory
data.
Program Control Instructions
l Transfer program execution from
one part of memory to another.
l Conditional tranfer, unconditional
transfer, and iteration control.
l They do not affect the flags.
l Jumps, Calls, Returns, Interrupts
Unconditional Jumps