Chapter 4
Chapter 4
Chapter 4
1
Chapter Overview
Data Transfer Instructions
Addition and Subtraction
Data-Related Operators and Directives
Indirect Addressing
JMP and LOOP Instructions
Shift and Rotate Operation
Extended Addition and Subtraction
Multiplication and Division
ASCII and Unpacked Decimal Arithmetic
Packed Decimal Arithmetic
2
Data Transfer Instructions
Operand Types
Instruction Operand Notation
Direct Memory Operands
MOV Instruction
Zero & Sign Extension
XCHG Instruction
Direct-Offset Instructions
3
Operand Types
Immediate – a constant integer (8, 16, or 32 bits)
value is encoded within the instruction
Register – the name of a register
register name is converted to a number and encoded
within the instruction
Memory – reference to a location in memory
memory address is encoded within the instruction, or a
register holds the address of a memory location
4
Instruction Operand Notation
5
Direct Memory Operands
A direct memory operand is a named reference to
storage in memory
The named reference (label) is automatically
dereferenced by the assembler
.data
var1 BYTE 10h
.code
mov al,var1 ; AL = 10h
mov al,[var1] ; AL = 10h
alternate format
6
MOV Instruction
• Move from source to destination. Syntax:
MOV destination,source
• No more than one memory operand permitted
• CS, EIP, and IP cannot be the destination
• No immediate to segment moves
.data
count BYTE 100
wVal WORD 2
.code
mov bl,count
mov ax,wVal
mov count,al
mov al,wVal ; error
mov ax,count ; error
mov eax,count ; error
7
Zero Extension
When you copy a smaller value into a larger destination, the MOVZX instruction fills
(extends) the upper half of the destination with zeros.
0 10001111 Source
mov bl,10001111b
movzx ax,bl ; zero-extension
8
Sign Extension
The MOVSX instruction fills the upper half of the destination with a copy of the
source operand's sign bit.
10001111 Source
mov bl,10001111b
movsx ax,bl ; sign extension
9
XCHG Instruction
XCHG exchanges the values of two operands. At least one operand must be a
register. No immediate operands are permitted.
.data
var1 WORD 1000h
var2 WORD 2000h
.code
xchg ax,bx ; exchange 16-bit regs
xchg ah,al ; exchange 8-bit regs
xchg var1,bx ; exchange mem, reg
xchg eax,ebx ; exchange 32-bit regs
10
Direct-Offset Operands
A constant offset is added to a data label to produce an effective address (EA).
The address is dereferenced to get the value inside its memory location.
.data
arrayB BYTE 10h,20h,30h,40h
.code
mov al,arrayB+1 ; AL = 20h
mov al,[arrayB+1] ; alternative notation
11
Direct-Offset Operands (cont)
A constant offset is added to a data label to produce an effective address (EA).
The address is dereferenced to get the value inside its memory location.
.data
arrayW WORD 1000h,2000h,3000h
arrayD DWORD 1,2,3,4
.code
mov ax,[arrayW+2] ; AX = 2000h
mov ax,[arrayW+4] ; AX = 3000h
mov eax,[arrayD+4] ; EAX = 00000002h
12
Addition and Subtraction
INC and DEC Instructions
ADD and SUB Instructions
NEG Instruction
Implementing Arithmetic Expressions
Flags Affected by Arithmetic
Zero
Sign
Carry
Overflow
13
INC and DEC Instructions
Add 1, subtract 1 from destination operand
operand may be register or memory
INC destination
Logic: destination destination + 1
DEC destination
Logic: destination destination – 1
14
INC and DEC Examples
.data
myWord WORD 1000h
myDword DWORD 10000000h
.code
inc myWord ; 1001h
dec myWord ; 1000h
inc myDword ; 10000001h
mov ax,00FFh
inc ax ; AX = 0100h
mov ax,00FFh
inc al ; AX = 0000h
15
ADD and SUB Instructions
• ADD destination, source
• Logic: destination destination + source
• SUB destination, source
• Logic: destination destination – source
• Same operand rules as for the MOV instruction
16
ADD and SUB Examples
.data
var1 DWORD 10000h
var2 DWORD 20000h
.code ; ---EAX---
mov eax,var1 ; 00010000h
add eax,var2 ; 00030000h
add ax,0FFFFh ; 0003FFFFh
add eax,1 ; 00040000h
sub ax,1 ; 0004FFFFh
17
NEG (negate) Instruction
Reverses the sign of an operand. Operand can be a register or memory operand.
.data
valB BYTE -1
valW WORD +32767
.code
mov al,valB ; AL = -1
neg al ; AL = +1
neg valW ; valW = -32767
Suppose AX contains –32,768 and we apply NEG to it. Will the result be valid?
18
NEG Instruction and the Flags
The processor implements NEG using the following internal operation:
SUB 0,operand
Any nonzero operand causes the Carry flag to be set.
.data
valB BYTE 1,0
valC SBYTE -128
.code
neg valB ; CF = 1, OF = 0
neg [valB + 1] ; CF = 0, OF = 0
neg valC ; CF = 1, OF = 1
19
Implementing Arithmetic Expressions
HLL compilers translate mathematical expressions into assembly language. You
can do it also. For example:
Rval = -Xval + (Yval – Zval)
Rval DWORD ?
Xval DWORD 26
Yval DWORD 30
Zval DWORD 40
.code
mov eax,Xval
neg eax ; EAX = -26
mov ebx,Yval
sub ebx,Zval ; EBX = -10
add eax,ebx
mov Rval,eax ; -36
20
Flags Affected by Arithmetic
The ALU has a number of status flags that reflect the
outcome of arithmetic (and bitwise) operations
based on the contents of the destination operand
Essential flags:
Zero flag – set when destination equals zero
Sign flag – set when destination is negative
Carry flag – set when unsigned value is out of range
Overflow flag – set when signed value is out of range
The MOV instruction never affects the flags.
21
Concept Map CPU
part of executes
executes
ALU
conditional jumps
arithmetic & bitwise
operations attached to used by provide
affect
status flags
branching logic
You can use diagrams such as these to express the relationships between assembly language
concepts.
22
Zero Flag (ZF)
The Zero flag is set when the result of an operation produces zero in the
destination operand.
mov cx,1
sub cx,1 ; CX = 0, ZF = 1
mov ax,0FFFFh
inc ax ; AX = 0, ZF = 1
inc ax ; AX = 1, ZF = 0
Remember...
• A flag is set when it equals 1.
• A flag is clear when it equals 0.
23
Sign Flag (SF)
The Sign flag is set when the destination operand is negative. The flag is clear
when the destination is positive.
mov cx,0
sub cx,1 ; CX = -1, SF = 1
add cx,2 ; CX = 1, SF = 0
mov al,0
sub al,1 ; AL = 11111111b, SF = 1
add al,2 ; AL = 00000001b, SF = 0
24
Signed and Unsigned Integers
A Hardware Viewpoint
25
Overflow and Carry Flags
A Hardware Viewpoint
How the ADD instruction affects OF and CF:
CF = (carry out of the MSB)
OF = CF XOR MSB
How the SUB instruction affects OF and CF:
CF = INVERT (carry out of the MSB)
negate the source and add it to the destination
OF = CF XOR MSB
26
Carry Flag (CF)
The Carry flag is set when the result of an operation generates an
unsigned value that is out of range (too big or too small for the destination
operand).
mov al,0FFh
add al,1 ; CF = 1, AL = 00
mov al,0
sub al,1 ; CF = 1, AL = FF
27
Overflow Flag (OF)
The Overflow flag is set when the signed result of an operation is invalid
or out of range.
; Example 1
mov al,+127
add al,1 ; OF = 1, AL = ??
; Example 2
mov al,7Fh ; OF = 1, AL = 80h
add al,1
The two examples are identical at the binary level because 7Fh equals +127. To
determine the value of the destination operand, it is often easier to calculate in
hexadecimal.
28
A Rule of Thumb
When adding two integers, remember that the
Overflow flag is only set when . . .
Two positive operands are added and their sum is
negative
Two negative operands are added and their sum is
positive
What will be the values of the Overflow flag?
mov al,80h
add al,92h ; OF = 1
mov al,-2
add al,+127 ; OF = 0
29
Data-Related Operators and Directives
OFFSET Operator
PTR Operator
TYPE Operator
LENGTHOF Operator
SIZEOF Operator
LABEL Directive
30
OFFSET Operator
OFFSET returns the distance in bytes, of a label from the beginning
of its enclosing segment
Protected mode: 32 bits
Real mode: 16 bits
offset
data segment:
myByte
31
OFFSET Examples
Let's assume that the data segment begins at 00404000h:
.data
bVal BYTE ?
wVal WORD ?
dVal DWORD ?
dVal2 DWORD ?
.code
mov esi,OFFSET bVal ; ESI = 00404000
mov esi,OFFSET wVal ; ESI = 00404001
mov esi,OFFSET dVal ; ESI = 00404003
mov esi,OFFSET dVal2 ; ESI = 00404007
32
Relating to C/C++
The value returned by OFFSET is a pointer. Compare the following code written
for both C++ and assembly language:
33
PTR Operator
Overrides the default type of a label (variable). Provides the flexibility to access
part of a variable.
.data
myDouble DWORD 12345678h
.code
mov ax,myDouble ; error – why?
Little endian order is used when storing data in memory (see Section 3.4.9).
34
PTR
.data
Operator Examples
myDouble DWORD 12345678h
56 0001 myDouble + 1
12 0003 myDouble + 3
35
PTR Operator (cont)
PTR can also be used to combine elements of a smaller data type and move
them into a larger operand. The CPU will automatically reverse the bytes.
.data
myBytes BYTE 12h,34h,56h,78h
.code
mov ax,WORD PTR [myBytes] ; AX = 3412h
mov ax,WORD PTR [myBytes+2] ; AX = 7856h
mov eax,DWORD PTR myBytes ; EAX = 78563412h
36
TYPE Operator
The TYPE operator returns the size, in bytes, of a single
element of a data declaration.
.data
var1 BYTE ?
var2 WORD ?
var3 DWORD ?
var4 QWORD ?
.code
mov eax,TYPE var1 ; 1
mov eax,TYPE var2 ; 2
mov eax,TYPE var3 ; 4
mov eax,TYPE var4 ; 8
37
LENGTHOF Operator
The LENGTHOF operator counts the number of
elements in a single data declaration.
.data LENGTHOF
byte1 BYTE 10,20,30 ; 3
array1 WORD 30 DUP(?),0,0 ; 32
array2 WORD 5 DUP(3 DUP(?)) ; 15
array3 DWORD 1,2,3,4 ; 4
digitStr BYTE "12345678",0 ; 9
.code
mov ecx,LENGTHOF array1 ; 32
38
SIZEOF Operator
The SIZEOF operator returns a value that is equivalent to multiplying
LENGTHOF by TYPE.
.data SIZEOF
byte1 BYTE 10,20,30 ; 3
array1 WORD 30 DUP(?),0,0 ; 64
array2 WORD 5 DUP(3 DUP(?)) ; 30
array3 DWORD 1,2,3,4 ; 16
digitStr BYTE "12345678",0 ; 9
.code
mov ecx,SIZEOF array1 ; 64
39
Spanning Multiple Lines (1 of 2)
A data declaration spans multiple lines if each line (except the last) ends with a
comma. The LENGTHOF and SIZEOF operators include all lines belonging to
the declaration:
.data
array WORD 10,20,
30,40,
50,60
.code
mov eax,LENGTHOF array ; 6
mov ebx,SIZEOF array ; 12
40
Spanning Multiple Lines (2 of 2)
In the following example, array identifies only the first WORD declaration.
Compare the values returned by LENGTHOF and SIZEOF here to those in
the previous slide:
.data
array WORD 10,20
WORD 30,40
WORD 50,60
.code
mov eax,LENGTHOF array ; 2
mov ebx,SIZEOF array ; 4
41
LABEL Directive
Assigns an alternate label name and type to an existing
storage location
LABEL does not allocate any storage of its own
Removes the need for the PTR operator
.data
dwList LABEL DWORD
wordList LABEL WORD
intList BYTE 00h,10h,00h,20h
.code
mov eax,dwList ; 20001000h
mov cx,wordList ; 1000h
mov dl,intList ; 00h
42
Indirect Addressing
Indirect Operands
Array Sum Example
Indexed Operands
Pointers
43
Indirect Operands (1 of 2)
An indirect operand holds the address of a variable, usually an array or string. It
can be dereferenced (just like a pointer).
.data
val1 BYTE 10h,20h,30h
.code
mov esi,OFFSET val1
mov al,[esi] ; dereference ESI (AL = 10h)
inc esi
mov al,[esi] ; AL = 20h
inc esi
mov al,[esi] ; AL = 30h
44
Indirect Operands (2 of 2)
Use PTR to clarify the size attribute of a memory operand.
.data
myCount WORD 0
.code
mov esi,OFFSET myCount
inc [esi] ; error: ambiguous
inc WORD PTR [esi] ; ok
45
Array Sum Example
Indirect operands are ideal for traversing an array. Note that the register in
brackets must be incremented by a value that matches the array type.
.data
arrayW WORD 1000h,2000h,3000h
.code
mov esi,OFFSET arrayW
mov ax,[esi]
add esi,2 ; or: add esi,TYPE
arrayW
add ax,[esi]
add esi,2
add ax,[esi] ; AX = sum of the array
46
Indexed Operands
An indexed operand adds a constant to a register to generate an effective address.
There are two notational forms:
[label + reg] label[reg]
.data
arrayW WORD 1000h,2000h,3000h
.code
mov esi,0
mov ax,[arrayW + esi] ; AX = 1000h
mov ax,arrayW[esi] ; alternate format
add esi,2
add ax,[arrayW + esi]
etc.
47
Pointers
You can declare a pointer variable that contains the offset of another variable.
.data
arrayW WORD 1000h,2000h,3000h
ptrW DWORD arrayW
.code
mov esi,ptrW
mov ax,[esi] ; AX = 1000h
Alternate format:
48
JMP and LOOP Instructions
JMP Instruction
LOOP Instruction
LOOP Example
Summing an Integer Array
Copying a String
49
JMP Instruction
• JMP is an unconditional jump to a label that is usually within the same
procedure.
• Syntax: JMP target
• Logic: EIP target
• Example:
top:
.
.
jmp top
A jump outside the current procedure must be to a special type of label called
a global label (see Section 5.5.2.3 for details).
50
LOOP Instruction
• The LOOP instruction creates a counting loop
• Syntax: LOOP target
• Logic:
• ECX ECX – 1
• if ECX != 0, jump to target
• Implementation:
• The assembler calculates the distance, in bytes, between the offset of the
following instruction and the offset of the target label. It is called the
relative offset.
• The relative offset is added to EIP.
51
LOOP Example
The following loop calculates the sum of the integers 5 + 4 + 3 +2 +
1:
When LOOP is assembled, the current location = 0000000E (offset of the next
instruction). –5 (FBh) is added to the the current location, causing a jump to
location 00000009:
00000009 0000000E + FB
52
Nested Loop
If you need to code a loop within a loop, you must save the outer loop counter's
ECX value. In the following example, the outer loop executes 100 times, and the
inner loop 20 times.
.data
count DWORD ?
.code
mov ecx,100 ; set outer loop count
L1:
mov count,ecx ; save outer loop count
mov ecx,20 ; set inner loop count
L2: .
.
loop L2 ; repeat the inner loop
mov ecx,count ; restore outer loop count
loop L1 ; repeat the outer loop
53
Summing an Integer Array
The following code calculates the sum of an array of 16-bit integers.
.data
intarray WORD 100h,200h,300h,400h
.code
mov edi,OFFSET intarray ; address of intarray
mov ecx,LENGTHOF intarray ; loop counter
mov ax,0 ; zero the accumulator
L1:
add ax,[edi] ; add an integer
add edi,TYPE intarray ; point to next integer
loop L1 ; repeat until ECX = 0
54
Copying a String
The following code copies a string from source to target:
.data
source BYTE "This is the source string",0
good use of
target BYTE SIZEOF source DUP(0) SIZEOF
.code
mov esi,0 ; index register
mov ecx,SIZEOF source ; loop counter
L1:
mov al,source[esi] ; get char from source
mov target[esi],al ; store it in the target
inc esi ; move to next character
loop L1 ; repeat for entire string
55
Shift and Rotate Instructions
Logical vs Arithmetic Shifts
SHL Instruction
SHR Instruction
SAL and SAR Instructions
ROL Instruction
ROR Instruction
RCL and RCR Instructions
SHLD/SHRD Instructions
56
Logical Shift
A logical shift fills the newly created bit position with
zero:
0
CF
57
Arithmetic Shift
• An arithmetic shift fills the newly created bit position with
a copy of the number’s sign bit:
CF
58
SHL Instruction
The SHL (shift left) instruction performs a logical left
shift on the destination operand, filling the lowest bit
with 0.
59
Fast Multiplication
Shifting left 1 bit multiplies a number by 2
60
SHR Instruction
The SHR (shift right) instruction performs a logical
right shift on the destination operand. The highest bit
position is filled with a zero.
0
CF
61
ROL Instruction
ROL (rotate) shifts each bit to the left
The highest bit is copied into both the Carry flag
and into the lowest bit
No bits are lost
CF
mov al,11110000b
rol al,1 ; AL = 11100001b
mov dl,3Fh
rol dl,4 ; DL = F3h
62
SAL and SAR Instructions
SAL (shift arithmetic left) is identical to SHL.
SAR (shift arithmetic right) performs a right arithmetic
shift on the destination operand.
CF
63
ROR Instruction
ROR (rotate right) shifts each bit to the right
The lowest bit is copied into both the Carry flag and
into the highest bit
No bits are lost
CF
mov al,11110000b
ror al,1 ; AL = 01111000b
mov dl,3Fh
ror dl,4 ; DL = F3h
64
RCL Instruction
RCL (rotate carry left) shifts each bit to the left
Copies the Carry flag to the least significant bit
Copies the most significant bit to the Carry flag
CF
clc ; CF = 0
mov bl,88h ; CF,BL = 0 10001000b
rcl bl,1 ; CF,BL = 1 00010000b
rcl bl,1 ; CF,BL = 0 00100001b
65
RCR Instruction
RCR (rotate carry right) shifts each bit to the right
Copies the Carry flag to the most significant bit
Copies the least significant bit to the Carry flag
CF
stc ; CF = 1
mov ah,10h ; CF,AH = 1 00010000b
rcr ah,1 ; CF,AH = 0 10001000b
66
SHLD Instruction
Shifts a destination operand a given number of bits to
the left
The bit positions opened up by the shift are filled by
the most significant bits of the source operand
The source operand is not affected
Syntax:
SHLD destination, source, count
Operand types:
SHLD reg16/32, reg16/32, imm8/CL
SHLD mem16/32, reg16/32, imm8/CL
67
SHLD Example
Shift count of 1:
mov al,11100000b
mov bl,10011101b
shld al,bl,1
68
Another SHLD Example
Shift wval 4 bits to the left and replace its lowest 4 bits with the high 4 bits of
AX:
.data
wval AX
wval WORD 9BA6h
Before: 9BA6 AC36
.code
mov ax,0AC36h After: BA6A AC36
shld wval,ax,4
69
SHRD Instruction
Shifts a destination operand a given number of bits to
the right
The bit positions opened up by the shift are filled by
the least significant bits of the source operand
The source operand is not affected
Syntax:
SHRD destination, source, count
Operand types:
SHRD reg16/32, reg16/32, imm8/CL
SHRD mem16/32, reg16/32, imm8/CL
70
SHRD Example
Shift count of 1:
mov al,11000001b
mov bl,00011101b
shrd al,bl,1
71
Another SHRD Example
Shift AX 4 bits to the right and replace its highest 4 bits with the low 4 bits of
DX:
mov ax,234Bh DX AX
mov dx,7654h Before: 7654 234B
shrd ax,dx,4
After: 7654 4234
72
Shift and Rotate Applications
Shifting Multiple Doublewords
Binary Multiplication
Displaying Binary Bits
Isolating a Bit String
73
Shifting Multiple Doublewords
Programs sometimes need to shift all bits within an
array, as one might when moving a bitmapped graphic
image from one screen location to another.
.data
ArraySize = 3
array DWORD ArraySize DUP(99999999h) ; 1001 1001...
.code
mov esi,0
shr array[esi + 8],1 ; high dword
rcr array[esi + 4],1 ; middle dword, include Carry
rcr array[esi],1 ; low dword, include Carry
74
Binary Multiplication
mutiply 123 36
75
Binary Multiplication
We already know that SHL performs unsigned
multiplication efficiently when the multiplier is a
power of 2.
You can factor any binary number into powers of 2.
For example, to multiply EAX 36, factor 36 into 32 +
4 and use the distributive property of multiplication to
carry out the operation:
EAX 36 mov eax,123
= EAX (32 + 4) mov ebx,eax
= (EAX 32)+(EAX 4) shl eax,5 ; mult by 25
shl ebx,2 ; mult by 22
add eax,ebx
76
Displaying Binary Bits
Algorithm: Shift MSB into the Carry flag; If CF = 1, append a "1"
character to a string; otherwise, append a "0" character. Repeat in a
loop, 32 times.
.data
buffer BYTE 32 DUP(0),0
.code
mov ecx,32
mov esi,OFFSET buffer
L1: shl eax,1
mov BYTE PTR [esi],'0'
jnc L2
mov BYTE PTR [esi],'1'
L2: inc esi
loop L1
77
Isolating a Bit String
The MS-DOS file date field packs the year, month, and
day into 16 bits:
DH DL
0 0 1 0 0 1 1 0 0 1 1 0 1 0 1 0
78
Multiplication and Division Instructions
MUL Instruction
IMUL Instruction
DIV Instruction
Signed Integer Division
CBW, CWD, CDQ Instructions
IDIV Instruction
Implementing Arithmetic Expressions
79
MUL Instruction
The MUL (unsigned multiply) instruction multiplies an 8-, 16-, or 32-
bit operand by either AL, AX, or EAX.
80
MUL Examples
100h 2000h, using 16-bit operands:
.data
val1 WORD 2000h The Carry flag indicates
val2 WORD 100h whether or not the upper
.code half of the product
mov ax,val1 contains significant
mul val2 ; DX:AX = 00200000h, CF=1 digits.
mov eax,12345h
mov ebx,1000h
mul ebx ; EDX:EAX = 0000000012345000h, CF=0
81
IMUL Instruction
IMUL (signed integer multiply ) multiplies an 8-, 16-,
or 32-bit signed operand by either AL, AX, or EAX
Preserves the sign of the product by sign-extending it
into the upper half of the destination register
Example: multiply 48 4, using 8-bit operands:
mov al,48
mov bl,4
imul bl ; AX = 00C0h, OF=1
82
IMUL Examples
Multiply 4,823,424 -423:
mov eax,4823424
mov ebx,-423
imul ebx ; EDX:EAX = FFFFFFFF86635D80h, OF=0
83
DIV Instruction
The DIV (unsigned divide) instruction performs 8-bit,
16-bit, and 32-bit division on unsigned integers
A single operand is supplied (register or memory
operand), which is assumed to be the divisor
Instruction formats:
DIV reg/mem8 Default Operands:
DIV reg/mem16
DIV reg/mem32
84
DIV Examples
Divide 8003h by 100h, using 16-bit operands:
mov dx,0 ; clear dividend, high
mov ax,8003h ; dividend, low
mov cx,100h ; divisor
div cx ; AX = 0080h, DX = 3
85
Signed Integer Division (IDIV)
Signed integers must be sign-extended before division
takes place
fill high byte/word/doubleword with a copy of the low
byte/word/doubleword's sign bit
For example, the high byte contains a copy of the sign
bit from the low byte:
10001111
11111111 10001111
86
CBW, CWD, CDQ Instructions
The CBW, CWD, and CDQ instructions provide
important sign-extension operations:
CBW (convert byte to word) extends AL into AH
CWD (convert word to doubleword) extends AX into DX
CDQ (convert doubleword to quadword) extends EAX into EDX
Example:
.data
dwordVal SDWORD -101 ; FFFFFF9Bh
.code
mov eax,dwordVal
cdq ; EDX:EAX = FFFFFFFFFFFFFF9Bh
87
IDIV Instruction
IDIV (signed divide) performs signed integer division
Same syntax and operands as DIV instruction
88
IDIV Examples
Example: 16-bit division of –48 by 5
mov ax,-48
cwd ; extend AX into DX
mov bx,5
idiv bx ; AX = -9, DX = -3
89
Unsigned Arithmetic Expressions
Some good reasons to learn how to implement integer
expressions:
Learn how do compilers do it
Test your understanding of MUL, IMUL, DIV, IDIV
Check for overflow (Carry and Overflow flags)
90
Signed Arithmetic Expressions (1 of 2)
91
Signed Arithmetic Expressions (2 of 2)
92