0% found this document useful (0 votes)
27 views45 pages

Sunu4 Arithmetic, Logic and Control Instructions2012

The document provides an overview of arithmetic, logic, and control instructions in Intel Assembly, detailing operations such as addition, subtraction, multiplication, and logical operations like AND, OR, and XOR. It explains how these instructions affect the FLAGS register and includes examples of their usage in various addressing modes. Additionally, it covers string instructions for manipulating blocks of data in memory, including moving, comparing, and scanning strings.

Uploaded by

admiralakbar6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views45 pages

Sunu4 Arithmetic, Logic and Control Instructions2012

The document provides an overview of arithmetic, logic, and control instructions in Intel Assembly, detailing operations such as addition, subtraction, multiplication, and logical operations like AND, OR, and XOR. It explains how these instructions affect the FLAGS register and includes examples of their usage in various addressing modes. Additionally, it covers string instructions for manipulating blocks of data in memory, including moving, comparing, and scanning strings.

Uploaded by

admiralakbar6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Arithmetic, Logic and

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

xor ah, ch ;ah=ah XOR ch


xor si, bx
xor ah, 0eeh
xor di, 0ddh
xor dx, [si]
xor dates[di+2], al

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.

or cx, 0600h ;set bits 9 and 10


and cx, 0ffch; clear bits 0 and 1
xor cx, 1000h; invert bit 12
TEST: Operates like the AND but doesn't affect the destination.
Sets the Z flag to the complement of the bit being tested:
test al, 4;Tests bit 2 in al -- 00000100
jz LABEL ;Jump to LABEL if bit 2 is zero.
jz (jump if zero) and jnz (jump if not zero)
Test dl, dh, dl anded with dh
Test cx, bx; cx anded with bx

NOT (logical one's complement)


NEG (arithmetic two's complement - sign of number inverted)
not ebx,
neg TEMP
Neg ax
Not byte ptr [bx]
l Not and neg can use any addressing mode except segment register
addressing!!!
Shift and rotate instructions manipulate binary
numbers at the binary bit level . They are used
to shift or rotate any memory data or register.
Shift:
l moves numbers to the left or right within a
register or memory location.
l They also perform simple arithmetic such as
multiplication and divison.
l The arithmetic and logical left shifts are identical.
l The arithmetic and logical right shifts are
different. (Logical shifts insert 0, arithmetic right
shifts insert sign bit.)
l Logical shifts function with unsigned numbers,
and arithmetic shifts function with signed
numbers.
shl eax, 1 ; eax is logically shifted left 1 bit pos.
shr bx, 12;
sal data1, cl; cl holds the shift count
sar si, 2
l Updates cf,of,pf,zf and sf, and leaves af undefined
l Example
Shl dx,14
Or
Mov cl,14
Shl dx, cl

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

lThe JMP instruc. has the general form:


JMP target
l Target can be short or near, or far
l Direct or indirect
l Direct jumps are 2 bytes long with the SHORT
label, 3 bytes long with the NEAR label and 5
bytes long with the FAR label
l e.g. JMP THERE
3 byte instr. if THERE is in the same segment
5 byte instr. if THERE is in the different segment
Short jump: PC-relative using two bytes if
the label lies within +127/-128 bytes of the
JMP.
l (PC-relative: constant added to eip).
NEXT: add ax, bx
jmp short NEXT
It executes in the same amount of time as
JMP NEXT; But takes up one less byte in
memory.
Near jump:
Within segment (+/- 32K).
jmp near eax ;Jump to address given by eax.
jmp [eax] ;Jump to address given by [eax].
Far jump:
2 bytes give the offset and two bytes give a new
segment address.
jmp far LABEL ;Jump to address given by LABEL.
l EB Disp
l E9 Displow DispHigh
l EA Iplow IpHigh Cslow CsHigh
l Conditional Jumps:
l Let the microproc. make an execution decision based on
some prescribed condition. If the condition is satisfied
proccesor makes the jump
l Test flag bits S, Z, C, P and O.
l The general form
Jx short_label
where x a modifier.The target (short_label) must be no
more than -128 or 127 bytes away from the instr.
l Cont. transfer inst.s occupy 2 bytes in memory. The First
byte holds opcode and the second one holds the relative
displacement.
l For unsigned numbers:
ja ;Jump if above (Z=0 and C=0)
jbe ;Jump if below or equal (Z=1 or C=1)
l For signed numbers
jl ;Jump if < (S not=O)
jge ;Jump if >= (S=O)
l For either signed or unsigned:
jne ;Jump if != (Z=0)
je or jz ;Jump if ==; or jump if zero (Z=1)
jc ;Jump if carry set (C=1)

Test cx instead of flags:


jcxz ;Jump if cx==0
jecxz ;Jump if ecx==0
Iteration Control Instructions
l These are conditional transfers used to set
up repetitive loops.
l The count register (CX) is the counter for
loops.
l Each of them decrements cx by 1, then
makes a jump/no-jump decision based on the
result.
LOOP Instruction:
l Loop until count complete
Combination of decrement ecx and jnz
conditional jump.
loop LABEL ;Jump if ecx != 0
Decrement ecx
If ecx != 0, jump to label
else fall through.
Example:
MOV cx, 100
Start:……
Loop start ; if cx is not zero , jump to
; start otherwise , continue
; here
l Many applications involve loops that
terminates before cx reaches zero.
l loope ;Jump if (Z = 1 AND ecx != 0)
has the alternate form loop if zero(loopz)
l loopne ;Jump if (Z = 0 AND ecx != 0)
has the alternate form loop if not zero(loopnz)

You might also like