Data Transfers, Addressing, and Arithmetic
Data Transfers, Addressing, and Arithmetic
Engineering Faculty
Department of Computer Engineering
ECOM 2025: Assembly Language Discussion
Chapter 4
Data Transfers, Addressing, and
Arithmetic
MOV Instruction
The MOV instruction copies data from a source operand to a destination operand.
The destination operand’s contents change, but the source operand is unchanged.
MOV rules:
Both operands must be the same size.
Both operands cannot be memory operands.
CS, EIP, and IP cannot be destination operands.
An immediate value cannot be moved to a segment register.
Example:
.data
oneByte BYTE 78h
oneWord WORD 1234h
oneDword DWORD 12345678h
.code
mov eax,0 ; EAX = 00000000h
mov al,oneByte ; EAX = 00000078h
mov ax,oneWord ; EAX = 00001234h
mov eax,oneDword ; EAX = 12345678h
mov ax,0 ; EAX = 12340000h
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. (Fills the upper part of the
destination with zeros)
Examples:
.data
byteVal BYTE 10001111b
.code
movzx ax,byteVal ; AX = 0000000010001111b
mov bx,0A69Bh
movzx eax,bx ; EAX = 0000A69Bh
movzx edx,bl ; EDX = 0000009Bh
movzx cx,bl ; CX = 009Bh
4 Assembly Language Discussion
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. (Fills the upper part of the
destination with a copy of the source operand's highest bit)
Examples:
.data
byteVal BYTE 10001111b
.code
movsx ax,byteVal ; AX = 1111111110001111b
mov bx,0A69Bh
movsx eax,bx ; EAX = FFFFA69Bh
movsx edx,bl ; EDX = FFFFFF9Bh
movsx cx,bl ; CX = FF9Bh
XCHG Instruction
The XCHG (exchange data) instruction exchanges the contents of two operands.
Examples:
xchg ax,bx ; exchange 16-bit regs
xchg ah,al ; exchange 8-bit regs
xchg var1,bx ; exchange 16-bit mem op with BX
xchg eax,ebx ; exchange 32-bit regs
5 Assembly Language Discussion
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.
Direct-Offset Operands
You can add a displacement to the name of a variable, creating a direct-offset operand. This lets
you access memory locations that may not have explicit labels.
If we use MOV with arrayB as the source operand, we automatically move the first byte in the
array:
mov al,arrayB ; AL = 10h
We can access the second byte in the array by adding 1 to the offset of arrayB:
mov al,[arrayB+1] ; AL = 20h
The brackets are not required by MASM, so the following statements are equivalent:
mov al,[arrayB+1]
mov al,arrayB+1
Similarly, the second element in a doubleword array is 4 bytes beyond the first one:
.data
arrayD DWORD 10000h,20000h
.code
mov eax,arrayD ; EAX = 10000h
mov eax,[arrayD+4] ; EAX = 20000h
Note:
The MOV instruction never affects the flags.
6 Assembly Language Discussion
4. (True/False): The EIP register cannot be the destination operand of a MOV instruction.
Use the following variable definitions for the remaining questions in this section:
.data
var1 SBYTE -4,-2,3,1
var2 WORD 1000h,2000h,3000h,4000h
var3 SWORD -16,-42
var4 DWORD 1,2,3,4,5
7. For each of the following statements, state whether or not the instruction is valid:
a. mov ax,var1 not valid, both operands must be the same size
b. mov ax,var2 valid
c. mov eax,var3 not valid, both operands must be the same size
d. mov var2,var3 not valid, both operands cannot be memory operands.
e. movzx ax,var2 not valid, destination must be larger than source
f. movzx var2,al not valid, destination must be register
g. mov ds,ax valid
h. mov ds,1000h not valid, an immediate value cannot be moved to a segment register.
8. What will be the hexadecimal value of the destination operand after each of the
following instructions execute in sequence?
9. What will be the value of the destination operand after each of the following
instructions execute in sequence?
10. What will be the value of the destination operand after each of the following
instructions execute in sequence?
Examples:
.data
myWord WORD 1000h
.code
inc myWord ; myWord = 1001h
mov bx,myWord
dec bx ; BX = 1000h
The INC and DEC instructions affect Overflow, Sign, Zero, Auxiliary Carry, and Parity flags. Do
not affect the Carry flag.
ADD Instruction
The ADD instruction adds a source operand to a destination operand of the same size.
Source is unchanged by the operation, and the sum is stored in the destination operand. The
set of possible operands is the same as for the MOV instruction.
8 Assembly Language Discussion
Examples:
.data
var1 DWORD 10000h
var2 DWORD 20000h
.code
mov eax,var1 ; EAX = 10000h
add eax,var2 ; EAX = 30000h
The Carry, Zero, Sign, Overflow, Auxiliary Carry, and Parity flags are changed according to the
value that is placed in the destination operand.
SUB Instruction
The SUB instruction subtracts a source operand from a destination operand.
Internally, the CPU can implement subtraction as a combination of negation and addition.
Two’s-complement notation is used for negative numbers.
The Carry, Zero, Sign, Overflow, Auxiliary Carry, and Parity flags are changed according to the
value that is placed in the destination operand.
9 Assembly Language Discussion
NEG Instruction
The NEG (negate) instruction reverses the sign of a number by converting the number to its
two’s complement.
NEG reg
NEG mem
The Carry, Zero, Sign, Overflow, Auxiliary Carry, and Parity flags are changed according to the
value that is placed in the destination operand.
Notes:
- The Carry and Auxiliary Carry flags always set after NEG instruction, except NEG 0 the
Carry and Auxiliary Carry flags are zeros, and NEG any number its least 4 bits are zeros
the Auxiliary Carry flags is zero.
- The Overflow flag always zero after NEG instruction, except NEG the smallest signed
number the Overflow flag is set.
4. If val2 is incremented by 1 using the ADD instruction, what will be the values of the
Carry and Sign flags?
CF = 0, SF = 1
5. If val4 is incremented by 1 using the ADD instruction, what will be the values of the
Overflow and Sign flags?
OF = 1, SF = 1
6. Where indicated, write down the values of the Carry, Sign, Zero, and Overflow flags
after each instruction has executed:
mov ax,7FF0h
add al,10h ; a. CF = 1 SF = 0 ZF = 1 OF = 0
add ah,1 ; b. CF = 0 SF = 1 ZF = 0 OF = 1
add ax,2 ; c. CF = 0 SF = 0 ZF = 0 OF = 0
8. (Yes/No): Is it possible to set the Overflow flag if you add a positive integer to a negative
integer?
9. (Yes/No): Will the Overflow flag be set if you add a negative integer to a negative
integer and produce a positive result?
11 Assembly Language Discussion
10. (Yes/No): Is it possible for the NEG instruction to set the Overflow flag?
11. (Yes/No): Is it possible for both the Sign and Zero flags to be set at the same time?
12. Write a sequence of two instructions that set both the Carry and Overflow flags at the
same time.
mov al,80h
add al,80h
• The OFFSET operator returns the distance of a variable from the beginning of its enclosing
segment.
• The PTR operator lets you override an operand’s default size.
• The TYPE operator returns the size (in bytes) of an operand or of each element in an array.
• The LENGTHOF operator returns the number of elements in an array.
• The SIZEOF operator returns the number of bytes used by an array initializer.
Use the following data definitions for the next seven exercises:
.data
myBytes BYTE 10h,20h,30h,40h
myWords WORD 3 DUP(?),2000h
myString BYTE "ABCDE"
7. What will be the value of EAX after each of the following instructions execute?
mov eax,TYPE myBytes ; a. 1
mov eax,LENGTHOF myBytes ; b. 4
mov eax,SIZEOF myBytes ; c. 4
mov eax,TYPE myWords ; d. 2
mov eax,LENGTHOF myWords ; e. 4
mov eax,SIZEOF myWords ; f. 8
mov eax,SIZEOF myString ; g. 5
12 Assembly Language Discussion
8. Write a single instruction that moves the first two bytes in myBytes to the DX register. The
resulting value will be 2010h.
mov dx, WORD PTR myBytes
9. Write an instruction that moves the second byte in myWords to the AL register.
mov al, BYTE PTR myWords+1
10. Write an instruction that moves all four bytes in myBytes to the EAX register.
mov eax,DWORD PTR myBytes
Immediate addressing is when an immediate value (a constant) is used for a source operand.
It cannot be used to specify a destination operand. The immediate constant is part of the
instruction itself.
Memory addressing is used to specify the address of the source and destination operands
located in memory. It can be divided into direct and indirect memory addressing.
Direct memory addressing is when the address of a memory operand is specified directly by
name.
13 Assembly Language Discussion
For example:
mov sum, eax ; sum is a variable in memory
Direct memory addressing is useful for accessing simple variables in memory, but it is useless
for addressing arrays or data structures. To address the elements of an array, we need to use a
register as a pointer to the array elements. This is called indirect memory addressing.
Register Indirect
Register Indirect can be any 32-bit general-purpose register (EAX, EBX, ECX, EDX, ESI, EDI, EBP,
and ESP) surrounded by brackets. In real-address mode, a 16-bit register holds the offset of a
variable. If the register is used as an indirect operand, it may only be SI, DI, BX, or BP. Avoid BP
unless you are using it to index into the stack. The register is assumed to contain the address of
some data.
Example:
.data
byteVal BYTE 10h
.code
mov esi,OFFSET byteVal
mov al,[esi] ; AL = 10h
The size of an operand may not be evident from the context of an instruction. The following
instruction causes the assembler to generate an “operand must have size” error message:
inc [esi] ; error: operand must have size
The assembler does not know whether ESI points to a byte, word, doubleword, or some other
size. The PTR operator confirms the operand size:
inc BYTE PTR [esi]
Arrays:
.data
arrayB BYTE 10h,20h,30h
.code
mov esi,OFFSET arrayB
mov al,[esi] ; AL = 10h
inc esi
mov al,[esi] ; AL = 20h
inc esi
mov al,[esi] ; AL = 30h
If we use an array of 16-bit integers, we add 2 to ESI to address each subsequent array element:
.data
arrayW WORD 1000h,2000h,3000h
14 Assembly Language Discussion
.code
mov esi,OFFSET arrayW
mov ax,[esi] ; AX = 1000h
add esi,2
mov ax,[esi] ; AX = 2000h
add esi,2
mov ax,[esi] ; AX = 3000h
.data
arrayD DWORD 10000h,20000h,30000h
.code
mov esi,OFFSET arrayD
mov eax,[esi] ; first number
add esi,4
add eax,[esi] ; second number
add esi,4
add eax,[esi] ; third number
Indexed Addressing
Indexed Addressing adds a constant to a register to generate an effective address.
constant[indexReg]
[constant + indexReg]
Example:
.data
arrayB BYTE 10h,20h,30h
.code
mov esi,0
mov al,[arrayB + esi] ; AL = 10h
inc esi
mov al,[arrayB + esi] ; AL = 20h
inc esi
mov al, arrayB[esi] ; AL = 30h
Index Scaling
The scale factor is the size of the array component (word = 2, doubleword = 4, quadword = 8).
constant[indexReg * scale]
[constant + indexReg * scale]
15 Assembly Language Discussion
Example:
.data
arrayD DWORD 1,2,3,4
.code
mov esi,3
mov eax,arrayD[esi*4] ; EAX = 4
mov esi,3
mov eax,arrayD[esi*TYPE arrayD] ; EAX = 4
Based Addressing
The based addressing combines a register with a constant offset. The base register holds the
base address of an array or structure, and the constant identifies offsets of various array
elements.
[BaseReg + Offset]
Example:
.data
arrayW WORD 1000h,2000h,3000h
.code
mov esi,OFFSET arrayW
mov ax,[esi] ; AX = 1000h
mov ax,[esi+2] ; AX = 2000h
mov ax,[esi+4] ; AX = 3000h
Based-Indexed Addressing
Example:
.data
matrix DWORD 0, 1, 2, 3, 4 ; 4 rows, 5 cols
DWORD 10,11,12,13,14
DWORD 20,21,22,23,24
DWORD 30,31,32,33,34
ROWSIZE EQU SIZEOF matrix ; 20 bytes per row
16 Assembly Language Discussion
.code
mov ebx, 2*ROWSIZE ; row index = 2
mov esi, 3 ; col index = 3
mov eax, matrix[ebx+esi*4] ; EAX = matrix[2][3]
LEA Instruction
LEA = Load Effective Address
Use the following data definitions for the remaining questions in this section:
7. Fill in the requested register values on the right side of the following instruction sequence:
mov esi,OFFSET myBytes
mov al,[esi] ; a. AL = 10h
17 Assembly Language Discussion
8. Fill in the requested register values on the right side of the following instruction sequence:
mov esi,OFFSET myBytes
mov ax,[esi] ; a. AX = 2010h
mov eax,DWORD PTR myWords ; b. EAX = 003B008Ah
mov esi,myPointer
mov ax,[esi+2] ; c. AX = 0000
mov ax,[esi+6] ; d. AX = 0000
mov ax,[esi-4] ; e. AX = 0044h
JMP Instruction
The JMP instruction causes an unconditional transfer to a destination, identified by a code
label.
JMP destination
The JMP instruction provides an easy way to create a loop by jumping to a label at the top of
the loop:
top:
.
.
jmp top ; repeat the endless loop
JMP is unconditional, so a loop like this will continue endlessly unless another way is found to
exit the loop.
18 Assembly Language Discussion
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.
LOOP destination
The loop destination must be within -128 to +127 bytes of the current location counter.
The execution of the LOOP instruction involves two steps:
ECX ECX – 1
If ECX != 0, jump to destination label
In the following example, we add 1 to AX each time the loop repeats. When the loop ends,
AX = 5 and ECX = 0:
mov ax,0
mov ecx,5
L1:
inc ax
loop L1
When initialize ECX to zero before beginning a loop, the LOOP instruction decrements ECX to
FFFFFFFFh, and the loop repeats 4,294,967,296 times!
Nested Loops
If you need to code a loop within a loop, you must save the outer loop counter's ECX value.
.DATA
count DWORD ?
.CODE
mov ecx, 100 ; set outer loop count to 100
L1:
mov count, ecx ; save outer loop count
mov ecx, 20 ; set inner loop count to 20
L2: .
.
loop L2 ; repeat the inner loop
mov ecx, count ; restore outer loop count
loop L1 ; repeat the outer loop
19 Assembly Language Discussion
Copying a String
3. If ECX is initialized to zero before beginning a loop, how many times will the LOOP
instruction repeat? (Assume ECX is not modified by any other instructions inside the
loop.)
4,294,967,296 times
4. (True/False): The LOOP instruction first checks to see whether ECX is not equal to zero;
then LOOP decrements ECX and jumps to the destination label.
5. (True/False): The LOOP instruction does the following: It decrements ECX; then, if ECX is
not equal to zero, LOOP jumps to the destination label.
6. In real-address mode, which register is used as the counter by the LOOP instruction?
CX
8. (True/False): The target of a LOOP instruction must be within 256 bytes of the current
location.
mov eax,0
mov ecx,10 ; outer loop counter
L1:
mov eax,3
mov ecx,5 ; inner loop counter
L2:
add eax,5
loop L2 ; repeat inner loop
loop L1 ; repeat outer loop
The program does not stop, because the first LOOP instruction decrements ECX to zero. The
second LOOP instruction decrements ECX to FFFFFFFFh, causing the outer loop to repeat.
10. Revise the code from the preceding question so the outer loop counter is not erased
when the inner loop starts.
.DATA
count DWORD ?
.CODE
mov eax,0
mov ecx,10 ; outer loop counter
L1:
mov count, ecx
mov eax,3
mov ecx,5 ; inner loop counter
21 Assembly Language Discussion
L2:
add eax,5
loop L2 ; repeat inner loop
mov ecx, count
loop L1 ; repeat outer loop
Best Wishes