Chapter 4
Chapter 4
• This chapter introduces some essential instructions for transferring data , performing
arithmetic, basic addressing modes, such as direct, immediate, and indirect, which
make it possible to process arrays.
• Along with that, we show how to create loops, and use some of the basic operators,
such as OFFSET, PTR, and LENGTHOF.
2
Operand Types
3
Operand Types (Cont.)
The destination operand’s contents change, but the source operand is unchanged.
• Both operands must be the same size.
• Both operands cannot be memory operands.
• The instruction pointer register (IP, EIP, or RIP) cannot be a destination operand.
Here is a list of the standard MOV instruction formats:
MOV reg,reg
MOV mem,reg
MOV reg,mem
MOV mem,imm
MOV reg,imm
MOV Instruction
• Memory to Memory
A single MOV instruction cannot be used to move data directly from one memory
location to another. Instead, you must move the source operand’s value to a
register before assigning its value to a memory operand:
.data
var1 WORD ?
var2 WORD ?
.code
mov ax,var1
mov var2,ax
Zero/Sign Extension of Integers
• Although MOV cannot directly copy data from a smaller operand to a larger
one
Zero/Sign Extension of Integers
Zero/Sign Extension of Integers
The effective result of this example was to use the highest bit of the source operand (1) to fill
the upper 16 bits of the destination operand, ECX. This technique is called sign extension.
• MOVZX Instruction
The MOVZX instruction (move with zero-extend) copies the contents of a source operand into
a destination operand and zero-extends the value to 16 or 32 bits.
This instruction is only used with unsigned integers.
There are three variants:
MOVZX reg32,reg/mem8
MOVZX reg32,reg/mem16
MOVZX reg16,reg/mem8
Zero/Sign Extension of Integers
• MOVZX Instruction
Zero/Sign Extension of Integers
• MOVZX Instruction
.data
Byte1 BYTE 9Bh
Word1 WORD 0A69Bh
.code
Movzx eax,word1 ;EAX=0000A69Bh
Movzx edx,byte1 ;EDX=0000009Bh
Movzx cx,byte1 ;CX=009Bh
Zero/Sign Extension of Integers
• MOVSX Instruction
The MOVSX instruction (move with sign-extend) copies the contents of a source operand into
a destination operand and sign-extends the value to 16 or 32 bits.
This instruction is only used with signed integers.
There are three variants:
MOVSX reg32,reg/mem8
MOVSX reg32,reg/mem16
MOVSX reg16,reg/mem8
Zero/Sign Extension of Integers
• MOVSX Instruction
Example:
XCHG Instruction
The XCHG (exchange data) instruction exchanges the contents of two operands. There are
three variants:
XCHG reg,reg
XCHG reg,mem
XCHG mem,reg
The rules for operands in the XCHG instruction are the same as those for the MOV
instruction except that XCHG does not accept immediate operands.
XCHG Instruction (Cont.)
Direct-Offset Operands
Lets you access memory locations that may not have explicit labels.
A constant is added to a data label to produce an effective address (EA). The address is
dereferenced to get the value inside its memory location.
• SUB Instruction
The SUB instruction subtracts a source operand from a destination operand.
The syntax is
SUB dest,source
• The set of possible operands is the same as for the ADD and MOV instructions.
Addition & Subtraction
Addition & Subtraction
• NEG Instruction
The NEG (negate) instruction reverses the sign of a number by converting the
number to its two’s complement.
The following operands are permitted:
NEG reg
NEG mem
Addition & Subtraction
• The ALU has a number of status flags that reflect the out come of arithmetic
(and bitwise) operations.
•based on the contents of the destination operand
• We use the values of CPU status flags to check the outcome of arithmetic
operations and to activate conditional branching instructions.
Addition & Subtraction
When adding two unsigned integers, the Carry flag is a copy of the carry out of the
most significant bit of the destination operand.
we can say CF=1, when the sum exceeds the storage size of its destination operand.
Addition & Subtraction
• Auxiliary Carry
The Auxiliary Carry (AC) flag indicates a carry or borrow out of bit 3 in the destination
operand.
• It is primarily used in binary coded decimal (BCD) arithmetic.
Addition & Subtraction
• Parity Flag
The Parity flag (PF) is set when the least significant byte of the destination has an
even number of 1 bits.
Addition & Subtraction
• Sign Flag
The Sign flag is set when the result of a signed arithmetic operation is negative.
• Overflow Flag
The Overflow flag is set when the result of a signed arithmetic operation overflows or
underflows the destination operand
Addition & Subtraction
NEG Instruction
The NEG instruction produces an invalid result if the destination operand cannot
be stored correctly. For example, if we move 128 to AL and try to negate it, the
correct value (128) will not fit into AL. The Overflow flag is set, indicating that AL
contains an invalid value:
• But the WORD PTR operator makes it possible to move the low-order word (5678h)
to AX:
Data-Related Operators and Directives
• PTR OPERATOR [Cont.]
.code
mov ax, type var1 ; AX = 0001
Data-Related Operators and Directives
• LENGTHOF OPERATOR
The LENGTHOF operator counts the number of elements in an array, defined by the values
appearing on the same line as its label.
For example:
.code
mov ax, LENGTHOF byte1 ; AX = 3
Data-Related Operators and Directives
• SIZEOF OPERATOR
The SIZEOF operator returns a value that is equivalent to multiplying LENGTHOF by TYPE.
For example:
Data-Related Operators and Directives
• LABEL Directive
• The LABEL directive assigns an alternate label name and type to an existing storage
location. LABEL does not allocate any storage of its own.
• A common use of LABEL is to provide an alternative name and size attribute for the variable
declared next in the data segment.
For example:
Indirect Addressing
• Indirect operand
• Direct addressing is rarely used for array processing because it is impractical to use constant
off-sets to address more than a few array elements. Instead, we use a register as a pointer
(called indirect addressing) and manipulate the register’s value.
• An indirect operand holds the address of a variable, usually an array or string. It can be
dereferenced (just like a pointer).
For example
Indirect Addressing
• Indirect operand [Cont.]
The size of an operand may not be evident from the context of an instruction.
The assembler does not know whether ESI points to a byte, word, doubleword, or some other
size. The PTR operator confirms the operand size
Indirect Addressing
• Arrays
Indirect operands are ideal tools for stepping through arrays.
• By default, the CPU loads and executes programs sequentially. But the current instruction
might be conditional, meaning that it transfers control to a new location in the program
based on the values of CPU status flags (Zero, Sign, Carry, etc.)
• A transfer of control, or branch, is a way of altering the order in which statements are
executed. There are two basic types of transfers:
• Conditional Transfer: The program branches if a certain condition is true. A wide variety
of conditional transfer instructions can be combined to create conditional logic structures.
The CPU interprets true/false conditions based on the contents of the ECX and Flags
registers.
Jump and Loop Instructions
JMP instruction
• The JMP instruction causes an unconditional transfer to a destination, identified by a code label that is
translated by the assembler into an offset.
• The syntax is
JMP destination
• When the CPU executes an unconditional transfer, the offset of destination is moved into the instruction
pointer, causing execution to continue at the new location.
SAMPLE:
inc eax
mov bx,ax
jmp SAMPLE
Jump and Loop Instructions
Loop instruction
• The LOOP instruction, formally known as Loop According to ECX Counter, repeats a block
of statements a specific number of times.
• ECX is automatically used as a counter and is decremented each time the loop
repeats. Its syntax is
LOOP destination
• The loop destination must be within -128 to +127 bytes of the current location
counter.
• -128 bytes is the largest backward jump from current instruction +127 bytes is the
largest forward jump.
Jump and Loop Instructions
Loop instruction
• The execution of the LOOP instruction involves two steps:
1. First, it subtracts 1 from ECX.
2. it compares ECX to zero.
• If ECX is not equal to zero, a jump is taken to the label identified by destination.
Otherwise, if ECX equals zero, no jump takes place, and control passes to the instruction
following the loop.
.code
main PROC
mov eax,0
mov ecx,5
L1:
inc ax
call DumpRegs
loop L1
Jump and Loop Instructions
Loop instruction
INCLUDE Irvine32.inc
.data
intArray WORD 100h, 200h, 300h, 400h, 500h
.code
main PROC
mov esi,0
mov eax,0
mov ecx,LENGTHOF intArray
L1:
mov ax, intArray[esi]
add esi, TYPE intArray
call DumpRegs
loop L1
TASK
• Calculates the sum of the integers 5 + 4 + 3 +2 + 1 using Loop
TASK
• Calculates the sum of the integers 5 + 4 + 3 +2 + 1 using Loop
Jump and Loop Instructions
Loop instruction
If you need to modify ECX inside a loop, you can save it in a variable at the beginning of the loop and
restore it just before the LOOP instruction
Jump and Loop Instructions
Nested Loop instruction
When creating a loop inside another loop, special consideration must be given to the outer loop counter in
ECX. You can save it in a variable.
Jump and Loop Instructions
Nested Loop instruction
INCLUDE Irvine32.inc
.code
main PROC
mov eax, 0
mov ebx, 0
mov ecx, 5
L1:
inc eax
mov edx, ecx
call dumpregs
mov ecx, 10
L2:
inc ebx
call dumpregs
loop L2
mov ecx, edx
loop L1
Jump and Loop Instructions