0% found this document useful (0 votes)
81 views38 pages

Computer Organization and Assembly Language

This document discusses data types and arithmetic instructions in assembly language. It introduces different operand types like registers, memory locations, and immediates that can be used in assembly instructions. It describes instructions to move data between registers and memory like MOV, MOVZX, MOVSX. It also covers arithmetic instructions like INC, DEC, ADD, SUB and how they affect the status flags. Examples are provided to demonstrate direct addressing modes and how to implement arithmetic expressions. The document provides a good overview of fundamental data transfer and arithmetic concepts in assembly language.
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)
81 views38 pages

Computer Organization and Assembly Language

This document discusses data types and arithmetic instructions in assembly language. It introduces different operand types like registers, memory locations, and immediates that can be used in assembly instructions. It describes instructions to move data between registers and memory like MOV, MOVZX, MOVSX. It also covers arithmetic instructions like INC, DEC, ADD, SUB and how they affect the status flags. Examples are provided to demonstrate direct addressing modes and how to implement arithmetic expressions. The document provides a good overview of fundamental data transfer and arithmetic concepts in assembly language.
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/ 38

Computer Organization and

Assembly Language
Lecture 4 Data Transfers,
Addressing and Arithmetic

Introducing Data Types in


Assembler
In higher-level languages (like Java and
C++), the compiler is very strict in
enforcing the rules regarding how data of
different types are used.
Assembly language does not enforce any of
these rules; this requires that the
programmer be more careful in declaring,
moving and using data of different types.

Operand Types
Operand Description
r8

8-bit general purpose register: AH, AL, BH, BL, etc.

r16

16-bit general purpose register: AX, BX, CX, DX

r32

16-bit general purpose register: EAX, EBX, etc.

reg

any general-purpose register

sreg

16-bit segment register CS, DS, SS, ES, FS, GS

imm

8-, 16- or 32-bit immediate value

imm8

8-bit immediate value

imm16

16-bit immediate value

imm32

32-bit immediate value

r/m8

8-bit operand which can be in a register or in memory

r/m16

16-bit operand which can be in register or memory

r/m32

32-bit operand which can be in register or memory

mem

an 8-, 16-, or 32-bit memory operand

Direct Memory Operands


Imagine that your program contains the following in the
data segment:
.data
var1

BYTE

10h

and that var1 is located at offset 10400h.You


could write:
mov al, [00010400]

or
mov al, var1

This is considered a direct memory operand because the


value in the instruction is the actual offset.

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

The following formats are legal for immediate operands


mov mem, immed
mov reg, immed

The following format are legal for segment registers:


mov segreg, r/m16
mov r/m16, segreg

; not CS

Moving Data From Memory to Memory


Memory to memory moves cannot be done in a
single instruction; it requires two instructions:
.data
var1 WORD ?
var2 WORD ?

.code

mov ax, var1


mov var1, ax

mov Instruction Examples


Examples of mov instructions
.data
Count
Total
Bigval

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

;
;
;
;
;
;
;
;

8-bit register to register


8-bit memory to register
8-bit immediate to memory
8-bit immediate to register
16-bit register to register
16-bit immediate to register
32-bit register to register
32-bit memory to register

Zero/Sign Extension of Integers


Extending a 8- or 16-bit value into a 16- or
32-bit value is different for signed and
unsigned values:
1

if unsigned:
0

0 ( 214)

0 (-42)

if signed:
1

Copying Smaller Values into Larger Ones


What happens if we write:
.data
count
.code

WORD 1
mov
mov

ecx, 0
cx, count; ECX is 0

Copying Smaller Values into Larger Ones


What happens if we write:
.data
signedVal
.code

SWORD
mov
mov

-1
ecx, 0
cx, SignedVal; ECX is
; 0000FFF0h
; (+65520)

We could solve the problem by writing:


mov
mov

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

What movzx Does


0
10001111

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

What movsx Does

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

LAHF and SAHF Instructions


LAHF (Load into AH Flags) copies the status flags in the low
byte of EFLAGS into the AH register.
The flags copies are Sign, Zero, Auxiliary Carry, Parity, and
Carry.
.data
saveflags
.code
lahf
mov

BYTE

saveflags, ah

; load flags into AH


; save flags in memory

SAHF (Save AH into Flags)


copies the AH register
values in the low byte of EFLAGS
mov ah, saveflags
sahf

The xchg Instruction


The xchg (exchange) instruction exhanges the
contents of two memory locations.
The syntax is:
xchg
xchg

reg, reg
reg, mem

xchg

mem, reg

This does not require the use of a third location to


swap values, making it very useful.

The xchg Instruction - Examples


Register-register or Register-Memory exchanges
xchg
xchg
xchg

ax, bx
ah, al
var1, bx

xchg

eax, ebx

; exchange 16-bit regs


; exchange 8-bit regs
; exchange 16-bit
; memory operand with BX
;exchange 32-bit regs

Memory-Memory exchange
mov
xchg
mov

ax, value1
value2, ax
value1, ax

; load the AX register


; exchange AX and value2
; return AX to value

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

BYTE 10h, 20h


WORD 100h, 200h
DWORD 10000h, 20000h

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 100h, 200h, 300h


DWORD 10000h, 20000h

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

; Direct-offset Addressing (byte array)


mov
mov
mov
call

al, arrayB
al, [arrayB+1]
al, [arrayB+2]
DumpRegs

; AL = 10h
; AL = 20h
; AL = 30h

; Direct-offset Addressing (word array)


mov
mov
call

ax, arrayW
ax, [arrayW+2]
DumpRegs

; AX = 100h
; AX = 200h

; Direct-offset Addressing (doubleword array)


mov
eax, arrayD
; EAX = 100h
mov
call

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

inc and dec Instructions

The inc and dec instructions have the format:


inc

reg/mem

; add 1 to destinations
; contents

dec

reg/mem

; subtract 1 to
; destinations contents

The operand can be either a register or memory


operand.
All status flags (except Carry) are affected.

inc and dec - Examples


Simple examples
inc
dec
inc
inc

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.

add Instruction - Examples


Simple examples
add
add
add
add
add

cl, al
eax, edx
bx, 1000h
var1, ax
var1, 10

; add 8 -bit register to register


; add 32-bit register-to-register
; add immediate value to 16-bit reg
; add 16-bit register to memory
; add immediate value to memory

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.

sub Instruction - Examples


Simple examples
sub
sub
sub
sub
sub

12345h, eax
cl, al
var1, ax
dx, var1
var1, 10

;
;
;
;
;

32-bit immediate from reg


8-bit reg from reg
16-bit reg from memory
16-bit memory from reg
immediate from memory

Numeric example
.data
var1 DWORD 30000h
var2 DWORD 10000h
.code
mov
eax, var1
sub
eax, var2

; 10000h

Flags Affected by add and sub


If add or sub generates a result of zero, ZF is set
If add or sub generates a negative result, SF is set.
Examples:
mov
sub
mov
sub

ax,
ax,
bx,
bx,

10
10
1
2

; AX = 0, ZF = 1
; BX = FFFF, SF = 1

inc and dec affect ZF but not CF.


mov
add
mov
inc

bl, 4Fh
bl, 0B1h
ax, 0FFFFh
ax

; BF = 00, ZF = 1, CF = 1
; ZF = 1 (CF unchanged)

Flags Affected by add and sub (continued)


The Carry flag is useful when performing
unsigned arithmetic
mov
add

ax, 0FFh
al, 1

; AL = 00, CF = 1

This should have been a 16-bit operation:


mov
add

ax, 0FFh
ax, 1

; AX = 0100, CF = 0

A similar situation happens when subtracting a


larger unsigned value from a smaller one:
mov
sub

al, 1
al, 2

; AL = FF, CF = 1

Flags Affected by add and sub (continued)


The Overflow flag is useful when performing
signed arithmetic:
mov
add
126
+2
-128

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

Implementing Arithmetic Expressions


Imagine we are implementing the statement
Rval = -Xval + (Yval Zval)
.data
Rval
SDWORD
Xval
SDWORD
Yval
SDWORD
Zval
SDWORD
.code
; first term: -Xval
mov
neg

eax. Xval
eax

?
26
30
40

; EAX = -26

Implementing Arithmetic Expressions (continued)

; second term: (Yval Zval)


mov
sub

ebx, Yval
ebx, Zval

; EBX = -10

; add the terms and store


add
eax, ebx
mov
Rval, eax
; Rval = -36

Example Program: AddSum3.asm


TITLE

Addition and Subtraction (AddSum3.as)

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

; Expression: Rval = -Xval + (Yval - Zval)


mov eax, Xval
neg eax
; EAX = -26
mov ebx, Yval
sub ebx, Zval
; EBX = -10
add eax, ebx
mov Rval, eax
; Rval = -36
call
DumpRegs
; Zero flag example
mov cx, 1
sub cx, 1
; ZF = 1
mov ax, 0FFFFh
inc ax
; ZF = 1
call
DumpRegs

; Sign flag example


mov cx,
sub cx,
mov ax,
add ax,
call

0
1
; SF = 1
7FFFh
2
; ZF = 1
DumpRegs

; Carry flag example


mov al, 0FFh
add al, 1
; CF = 1, AL = 00
call
DumpRegs

; Overflow flag example


mov
add
mov
sub
call

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

NEG affects the same flags as AND, OR and XOR.


It is important to check the Overflow flag after NEG in case
you have negated 128 to +128, producing an invalid
answer.
mov
neg

al, -128
al

; AL = 10000000b
; AL = 10000000b, OF = 1

+127 can be negated without problem


mov
neg

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.

OFFSET Example in 16 Bits


mov

bx, offset count

; 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

OFFSET Example in 32 Bits


.data
bVal
wVal
dVal
dVal2

BYTE
WORD
DWORD
DWORD

?
?
?
?

IF bVal is located at offset 00404000h, we would get:


mov
mov
mov
mov

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

Data-Related Operators and


Directives

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]

PTR is useful in overriding default sizes for an operand:


.data
val32
.code
mov
mov

DWORD

12345678h

ax, word ptr val32


dx, word ptr val32+2

; AX = 5678H
; DX = 1234H

PTR Operators - An Example


.data
myDouble
.code
mov
mov

DWORD 12345678h
ax, myDouble
ax, word ptr MyDouble

; ERROR
; WORKS!

0000

78

myDouble

12345678

0001

56

0002

34

0003

12

word ptr MyDouble

5678

byte ptr MyDouble

78

byte ptr [myDouble+1]

56

word ptr [myDouble+2]

1234

byte ptr [myDouble+2]

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

TYPE Operator - An Example


.data
var1
var2
var3
var4
msg
.code
L1:mov
mov
mov
mov
mov
mov

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

The LENGTHOF operator counts the number of individual elements in


a variable that has been defined using DUP.
Example
.data
val1
val2
array
array2
message
.code
mov
mov
mov
mov
mov

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

WORD 32 DUP(0) ; SIZEOF = 64

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).

Indirect Operands: A Real Mode Example


We create a string in memory at offset 0200 and set the BX
to the strings offset; we can process any element in the
string by adding to the offset:
.data

aString
.code
mov
add
mov

BYTE

ABCDEFG

bx, offset aString


bx, 5
dl, [bx]

; BX = 0200
; BX = 0205
; DL = F

0205

0200

aString

Indirect Operands: A Protected Mode Example


.data
val1 BYTE
.code
mov
mov
mov

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

10h, 20h, 30h

mov
mov
inc
mov

esi, OFFSET arrayB


al, [esi]
; AL = 10h
esi
al, [esi]
; AL = 20h

inc
mov

esi
al, [esi]

; AL = 30h

Arrays of Words
Offset

If we use an array of 16-bit integers, we add 2


to ESI to address each subsequent array
element:
.data
arrayW
.code

WORD
mov
mov
add
mov
add
mov

1000h, 2000h, 3000h

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

10000h, 20000h, 30000h

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]

In both cases, we are combining the constant


offset of a variable label with the contents of a
register.

Indexed Operands An Example


.data
arrayB
arrayW
.code

BYTE
WORD

10h, 20h, 30h


1000h, 2000h, 3000h

mov
mov

esi, 0
al, [arrayB+esi]

mov
mov
mov
mov

esi, OFFSET arrayW


ax, [esi]
; AX = 1000h
ax, [esi+2]
; AX = 2000h
ax, [esi+4]
; AX = 3000h

; AL = 10h

Indexed Operands Using 16-Bit Registers


The 16-bit registers can be used as indexed
operand in real mode; however, you are limited to
the SI, DI BP and BX registers:
mov
mov
mov

al, arrayB[si]
ax, arrayW[di]
eax, ArrayD[bx]

As with indirect operand, avoid using the BP


except when addressing data on the stack.

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

10h, 20h, 30h, 40h


1000h, 2000h, 3000h
arrayB
arrayW

The OFFSET operator can also be used in


initializing pointers to make the relationship
clearer:
ptrB
ptrW

DWORD
DWORD

OFFSET arrayB
OFFSET arrayW

Using the TYPEDEF Operator


The TYPEDEF operator lets you create
user-defined types that can be used in the
same way as the built-in type:
PBYTE
.data

TYPEDEF

PTR BYTE

arrayB
ptr1
ptr2

BYTE 10h, 20h, 30h, 40


PBYTE ?
; uninitialized
PBYTE arrayB
; points to an
;
array

Pointers: An Example
TITLE Pointers
INCLUDE

(Pointers.asm)

Irvine32.inc

; Create user-defined types


PBYTE TYPEDEF
PTR BYTE
; points to bytes
PWORD TYPEDEF
PTR WORD
; points to words
PDWORD
TYPEDEF
PTR DWORD
; points to doublewords
.data
arrayB
arrayW
arrayD

BYTE 10h, 20h, 30h


WORD 1, 2, 3
DWORD 4, 5, 6

; create some pointer variables


ptr1
PBYTE arrayB
ptr2
PWORD arrayW
ptr3
PDWORD arrayD
.code
main
PROC
; Use the pointers to access data
mov esi, ptr1
mov al, [esi]
; 10h
mov esi, ptr2
mov ax, [esi]
; 1
mov esi, ptr3
mov eax, [esi]
; 4h
exit
main
endp
END main

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

where destination is the label of the statement to


which it jumps if the (E)CX register is nonzero.
Because the (E)CX register controls the loop, it is
extremely unwise to change it during the loop.

Using the ECX Register During Loops


If it is necessary to use the (ECX) register during
loops, it is important to restore its value before the
LOOP instruction:
.data
count
.code
mov

mov

mov

mov
loop

DWORD ?
ecx, 100
count, ecx

; save the count

ecx, 20

; modify ECX

ecx, count
top

; restore Loop count

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

; set outer loops count


; save outer loop count

ecx, 20

; set inner loop count

L2
ecx, count
L1

; repeat inner loop


; restore outer loop count

Summing An Integer Array


TITLE Summing An Array
INCLUDE
.data
intarray

(SumArray.asm)

Irvine32.inc
WORD

100h, 200h, 300h, 400h

.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

"This is the source string",0


SIZEOF
source DUP(0), 0

.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

You might also like