Computer Organization and Assembly Language
Computer Organization and Assembly Language
Assembly Language
Lecture 4 Data Transfers,
Addressing and Arithmetic
Operand Types
Operand Description
r8
r16
r32
reg
sreg
imm
imm8
imm16
imm32
r/m8
r/m16
r/m32
mem
BYTE
10h
or
mov al, var1
mov Instruction
The mov instruction copies data from one location to
another.
The following formats are legal for moving data to or from
general purpose registers:
mov reg, reg
mov mem, reg
mov reg, mem
; not CS
BYTE
WORD
DWORD
10
4126h
12345678h
.code
mov
mov
mov
mov
mov
mov
mov
mov
al, bl
bl, count
count, 26
bl, 1
dx, cx
bx, 8FE2h
eax, ebx
edx, bigVal
;
;
;
;
;
;
;
;
if unsigned:
0
0 ( 214)
0 (-42)
if signed:
1
WORD 1
mov
mov
ecx, 0
cx, count; ECX is 0
SWORD
mov
mov
-1
ecx, 0
cx, SignedVal; ECX is
; 0000FFF0h
; (+65520)
ecx, FFFFFFFFx
cx, signedVal ; ECX=FFFFFFF0h
; (-16)
MOVZX Instruction
The movzx (move with zero-extended)
copies of the contents of source operand
into a destination operand and zero-extends
the operand to 16 or 32 bits.
There are 3 formats:
movzx
movzx
movzx
r32, r/m8
r32, r/m16
r16, r/m8
00000000
10001111
Examples Of movzx
Register to register
mov
movzx
movzx
movzx
bx, 0A69Bh
eax, bx
edx, bl
cx, bl
;
;
;
EAX = 0000A69Bh
EDX = 0000009Bh
CX = 009Bh
;
;
;
EAX = 0000A69Bh
EDX = 0000009Bh
CX = 009Bh
Memory to register
.data
byte1
word1
.code
movzx
movzx
movzz
BYTE
WORD
9Bh
0A69Bh
eax, word1
edx, byte1
cx, byte1
MOVSX Instruction
The movsx (move with sign-extended)
copies of the contents of source operand
into a destination operand and sign-extends
the operand to 16 or 32 bits.
movsx is only used with signed integers.
There are 3 formats:
movsx
movsx
movsx
r32, r/m8
r32, r/m16
r16, r/m8
10001111
11111111
10001111
Examples Of movsx
Register to register
mov
bx, 0A69Bh
movzx
movzx
movzx
eax, bx
edx, bl
cx, bl
;
;
;
EAX = FFFFA69Bh
EDX = FFFFFF9Bh
CX = FF9Bh
BYTE
saveflags, ah
reg, reg
reg, mem
xchg
mem, reg
ax, bx
ah, al
var1, bx
xchg
eax, ebx
Memory-Memory exchange
mov
xchg
mov
ax, value1
value2, ax
value1, ax
Direct-Offset Operands
A displacement can be added to the name of a memory
operand, allowing the program to access data without their
own memory labels:
Examples:
arrayB
arrayW
arrayD
mov
mov
mov
mov
mov
mov
al, arrayB
al, arrayB+1
ax, arrayW;
ax, arrayW+2
eax, arrayD
eax, arrayD+4
;
;
;
;
;
;
AL = 10h
AL = 20h
AX = 100h
AX = 200h
EAX = 10000h
EAX = 20000h
Example moves.asm
TITLE Data Transfer Examples
(Moves.asm)
INCLUDE
.data
val1
val2
arrayB
Irvine32.inc
arrayW
arrayD
WORD
WORD
BYTE
1000h
2000h
10h, 20h, 30h, 40h, 50h
.code
main PROC
; MOVZX
mov
movzx
movzx
movzx
call
bx, 0A69Bh
eax, bx
edx, bl
cx, bl
DumpRegs
;
;
;
;
Initialize BX reg
EAX = 0000A69Bh
EDX = 0000009Bh
CX = 009Bh
; MOVSX
mov
movsx
movsx
movsx
call
bx, 0A69Bh
eax, bx
edx, bl
cx, bl
DumpRegs
;
;
;
;
Initialize BX reg
EAX = FFFFA69Bh
EDX = FFFFFF9Bh
CX = FF9Bh
; Memory-to-memory exchange
mov
xchg
mov
call
ax, val1
ax, val2
val1, ax
DumpRegs
; AX = 1000h
; AX = 2000h,val2 = 1000h
; val1 = 2000h
al, arrayB
al, [arrayB+1]
al, [arrayB+2]
DumpRegs
; AL = 10h
; AL = 20h
; AL = 30h
ax, arrayW
ax, [arrayW+2]
DumpRegs
; AX = 100h
; AX = 200h
main
eax, [arrayD+4]
DumpRegs
exit
ENDP
END
main
; EAX = 200h
Arithmetic Instructions
Assembly language include many instructions
to perform basic arithmetic. They include:
inc
dec
add
sub
reg/mem
; add 1 to destinations
; contents
dec
reg/mem
; subtract 1 to
; destinations contents
al
bx
eax
val1
;
;
;
;
increment
decrement
increment
increment
8-bit register
16-bit register
32-bit register
memory operand
Another example
.data
myWord
.code
WORD
inc
mov
dec
1000h
myWord
bx, myWord
bx
; 1001h
; 1000h
add Instruction
add adds a source operand to the destination
operand of the same size.
Format:
add
destination, source
Source is unchanged; destination stores the sum.
All the status flags are affected.
The sizes must match and only one can be a
memory location.
cl, al
eax, edx
bx, 1000h
var1, ax
var1, 10
Numeric example
.data
var1 DWORD 10000h
var2 DWORD 20000h
.code
mov
eax, var1
add
eax, var2
; 30000h
sub Instruction
sub subtracts a source operand from the
destination operand of the same size.
Format:
sub
destination, source
Source is unchanged; destination stores the
difference. All the status flags are affected.
The sizes must match and only one can be a
memory location.
12345h, eax
cl, al
var1, ax
dx, var1
var1, 10
;
;
;
;
;
Numeric example
.data
var1 DWORD 30000h
var2 DWORD 10000h
.code
mov
eax, var1
sub
eax, var2
; 10000h
ax,
ax,
bx,
bx,
10
10
1
2
; AX = 0, ZF = 1
; BX = FFFF, SF = 1
bl, 4Fh
bl, 0B1h
ax, 0FFFFh
ax
; BF = 00, ZF = 1, CF = 1
; ZF = 1 (CF unchanged)
ax, 0FFh
al, 1
; AL = 00, CF = 1
ax, 0FFh
ax, 1
; AX = 0100, CF = 0
al, 1
al, 2
; AL = FF, CF = 1
al, +126
al, 2
01111110
+ 00000010
1 00000000
mov
sub
-128
- -2
-130
al, -128
al, 2
10000000
-00000010
0 11111110
; AL = 80h, OF = 1
; AL = 7Eh, OF = 1
eax. Xval
eax
?
26
30
40
; EAX = -26
ebx, Yval
ebx, Zval
; EBX = -10
INCLUDE
.data
Rval
SDWORD
Xval
SDWORD
Yval
SDWORD
Zval
SDWORD
Irvine32.inc
?
26
30
40
.code
main
PROC
; INC and DEC
mov ax, 1000h
inc ax
; 1001h
dec ax
; 1000h
call DumpRegs
0
1
; SF = 1
7FFFh
2
; ZF = 1
DumpRegs
main
al, +127
al, 1
al, -128
al, 1
DumpRegs
exit
ENDP
END main
; OF = 1
; OF = 1
NEG Instruction
The NEG instruction reverses the sign of an operand (in
twos complement form):
NEG
NEG
reg
mem
al, -128
al
; AL = 10000000b
; AL = 10000000b, OF = 1
al, + 127
al
; AL = 01111111b
; AL = 10000001b
OFFSET Operator
The OFFSET operator returns the number of bytes
between the label and the beginning of its
segment.
In Real mode it produces a 16-bit immediate
value; therefore, the destination must be a 16-bit
operand.
In Protected mode it produces a 32-bit immediate
value; therefore, the destination must be a 32-bit
operand.
; BX points to count
or
.data
bList db
10h, 20h, 30h, 40h
wList dw
1000h, 2000h, 3000h
.code
mov
di, offset bList ; DI = 0000
mov bx, offset bList+1 ; BX = 0001
mov si, offset wList+2 ; SI = 0006
BYTE
WORD
DWORD
DWORD
?
?
?
?
esi,
esi,
esi,
esi,
OFFSET
OFFSET
OFFSET
OFFSET
bval
wVal
dVal
dVal2
;
;
;
;
ESI
ESI
ESI
ESI
=
=
=
=
00404000
00404001
00404003
00404007
ALIGN Directive
ALIGN aligns the next variable on a byte, word,
doubleword, or paragraph boundary.
The syntax is
ALIGN bound
where bound is 1, 2 or 4.
Example
bVal
wVal
bVal
BYTE
ALIGN
WORD
BYTE
?
2
?
?
ALIGN 4
dVal DWORD ?
DVal2 DWORD ?
; 00404000
; 00404002
; 00404004
; 00404008
; 0040400C
PTR Operator
TYPE Operator
LENGTHOF Operator
SIZEOF Operator
LABEL Directive
PTR Operator
PTR operator overrides the default size for a operands
address.
It is useful when operands size is not clear from the
context:
inc [bx]
would produce an operand must have size error message.We can
fix it by writing
inc
byte ptr [bx]
DWORD
12345678h
; AX = 5678H
; DX = 1234H
DWORD 12345678h
ax, myDouble
ax, word ptr MyDouble
; ERROR
; WORKS!
0000
78
myDouble
12345678
0001
56
0002
34
0003
12
5678
78
56
1234
34
TYPE Operator
The TYPE operator returns the size (in bytes) of a single
element of a variable.
For variables, it is 1, 2 or 4 for bytes, words and
doublewords respectively.
For near labels, it is FFFFh; for far labels FFFEh.
Example
.data
var1
var2
BYTE
WORD
? ; TYPE var1 = 1
? ; TYPE var2 = 2
var3
var4
DWORD
QWORD
? ; TYPE var3 = 4
? ; TYPE var4 = 8
BYTE
WORD
DWORD
BYTE
BYTE
ax,
ax,
ax,
ax,
ax,
ax,
type
type
type
type
type
type
20h
1000h
?
10, 20, 30, 40, 50
File not found, 0
var1
var2
var3
var4
msg
L1
;
;
;
;
;
;
AX
AX
AX
AX
AX
AX
=
=
=
=
=
=
0001
0002
0004
0001
0001
FFFF
LENGTHOF Operator
WORD
SWORD
WORD
WORD
BYTE
ax,
ax,
ax,
ax,
ax,
1000h
10, 20, 30
32 dup(0)
5 dup(3dup(0))
File not found, 0
LENGTHOF
LENGTHOF
LENGTHOF
LENGTHOF
LENGTHOF
val1
val2
array
array2
message
;
;
;
;
;
AX
AX
AX
AX
AX
=
=
=
=
=
1
1
32
5
1
SIZEOF Operator
The SIZEOF operator returns the number of
bytes an array takes up.
It is equivalent to multiplying LENGTHOF by
TYPE.
Example
intArray
LABEL Directive
operator inserts a label without allocating storage.
It points to the same address as the variable declared below
it.
Example
LABEL
.data
val16
val32
LABEL word
DWORD 12345678h
.code
mov
mov
ax,
dx,
val16
val32+2
; AX = 5678h
; DX = 1234h
Indirect Operands
An indirect operand is a register containing the
offset for data in a memory location.
The register points to a label by placing its offset in that
register
This is very convenient when working with arrays; it is
just a matter of incrementing the address so that it
points to the next array element.
The ESI, EDI, EBX, EBP, SI, DI, BX and BP registers
can be used for indirect operands as well as the 32-bit
general purpose registers (with a restriction on the
ESP).
aString
.code
mov
add
mov
BYTE
ABCDEFG
; BX = 0200
; BX = 0205
; DL = F
0205
0200
aString
mov
mov
inc
inc
10h
esi
OFFSET val1
al, [esi]
; AL = 10h
[esi], bl
; The variable to
; which ESI points is
; changed
esi, 0
ax, [esi]
; General Protection
; Error
[esi] ; Error - needs size
byte ptr [esi]
; Works!
Arrays
Indirect arrays are useful when manipulating
arrays:
.data
arrayB
.code
BYTE
mov
mov
inc
mov
inc
mov
esi
al, [esi]
; AL = 30h
Arrays of Words
Offset
WORD
mov
mov
add
mov
add
mov
esi, OFFSET
ax, [esi]
esi, 2
ax, [esi]
esi, 2
ax, [esi]
arrayW
; AX = 1000h
; AX = 2000h
; AX = 3000h
Value
10200 1000h
10202 2000h
10204 3000h
[esi]
Arrays of Doublewords
If we use an array of 32-bit integers, we add 4
to ESI to address each subsequent array
element:
.data
arrayD
.code
DWORD
mov
mov
add
mov
add
mov
esi,
eax,
esi,
eax,
esi,
eax,
OFFSET
[esi]
4
[esi]
4
[esi]
Offset
Value
10200
10000h
10204
20000h
10208
30000h
arrayD
; first #
[esi]
; second #
; third #
Indexed Operands
An indexed operand adds a constant to a register
to generate an effective address.
Any of the 32-bit general purpose register may be
used as an index registers.
There are two forms that are legal:
constant[reg]
[constant+reg]
BYTE
WORD
mov
mov
esi, 0
al, [arrayB+esi]
mov
mov
mov
mov
; AL = 10h
al, arrayB[si]
ax, arrayW[di]
eax, ArrayD[bx]
Pointers
A variable that contains the address of another
variable is called a pointer (because it points to
the variable).
In Intel assembler, there are two basic types of
pointers:
pointers, an offset from the beginning of the data
segment (in real mode, a 16-bit offset; in protected
mode, a 32-bit offset).
FAR pointers, a segment-offset address (in real mode, a
32-bit address; in protected mode, a 48-bit address).
NEAR
Initializing Pointers
Near pointers in protected are stored in
doubleword variables:
arrayB
arrayW
ptrB
ptrW
BYTE
WORD
DWORD
DWORD
DWORD
DWORD
OFFSET arrayB
OFFSET arrayW
TYPEDEF
PTR BYTE
arrayB
ptr1
ptr2
Pointers: An Example
TITLE Pointers
INCLUDE
(Pointers.asm)
Irvine32.inc
Transfer of Control
A transfer of control is way of altering the
order in which instructions are executed.
The two basic ways are:
Unconditional transfer the program branches
to a statement elsewhere in the program
Conditional transfer the program branches to
a statement elsewhere in the program IF some
condition is true.
JMP Instruction
The JMP statement causes an unconditional
transfer to the target address within the same code
segment.
The syntax is:
JMP
targetLabel
where the targetLabel is the offset of an
instruction elsewhere in the program.
Example:
top:
jmp top; infinite loop
LOOP Instruction
The LOOP instruction is used to end a block of
statements that will be performed a predetermined
number of times, with the number of times stored
in the ECX (or CX) register.
The syntax is:
LOOP
destination
DWORD ?
ecx, 100
count, ecx
ecx, 20
; modify ECX
ecx, count
top
Nested Loops
In writing nested loops, it is important to
save the outer loops counter:
.data
count DWORD ?
.code
mov
ecx, 100
L1:
mov
count, ecx
L2:
mov
loop
mov
loop
ecx, 20
L2
ecx, count
L1
(SumArray.asm)
Irvine32.inc
WORD
.code
main PROC
mov edi, OFFSET intarray
; address of
intarray
mov ecx, LENGTHOF intarray; ; loop counter
mov ax, 0
L1:
add ax, [edi]
; add an integer
add edi, TYPE intarray; point to next integer
loop
L1
; repeat ECX = 0
call DumpRegs
exit
main endp
end main
Copying A String
TITLE Copying A String
INCLUDE
.data
source
target
(CopyStr.asm)
Irvine32.inc
BYTE
BYTE
.code
main PROC
mov esi, 0
mov ecx, SIZEOF source
; index register
; loop counter
L1:
mov al, source[esi]
; get a char. from source
mov target[esi], al
; store it in the target
inc esi
; move it to next character
loop
L1
; repeat for whole string
exit
main endp
end main