0% found this document useful (0 votes)
7 views

MP_Lect_5

Lecture 5 covers microprocessor systems design focusing on data transfers, addressing, and arithmetic operations. It discusses various operators such as OFFSET, PTR, TYPE, LENGTHOF, and SIZEOF, along with their applications in assembly language. Additionally, it introduces indirect and indexed operands, unconditional JMP, and LOOP instructions for efficient programming and array manipulation.

Uploaded by

amohamed5373
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

MP_Lect_5

Lecture 5 covers microprocessor systems design focusing on data transfers, addressing, and arithmetic operations. It discusses various operators such as OFFSET, PTR, TYPE, LENGTHOF, and SIZEOF, along with their applications in assembly language. Additionally, it introduces indirect and indexed operands, unconditional JMP, and LOOP instructions for efficient programming and array manipulation.

Uploaded by

amohamed5373
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Microprocessor Systems Design

Lecture 5
Contents (Chapter 4: part 2)
 Data Transfers, Addressing and Arithmetic
 Data-Related Operators
 Indirect Operands
 Indexed Operands
 Unconditional JMP and LOOP Instructions
Data-Related Operators and Directives
 OFFSET Operator
 PTR Operator
 TYPE Operator
 LENGTHOF Operator
 SIZEOF Operator
(1) OFFSET Operator
 OFFSET returns the distance in bytes, of a
label from the beginning of its enclosing
segment. (see examples in next slide).
 To illustrate, the following figure shows a
variable named myByte inside the data
segment: offset

data segment:

myByte

 In Protected mode , an offset is always 32 bits long.


 In Real- address mode, offsets are only 16 bits.
OFFSET Examples
Let's assume that the data segment (block of
data) 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

esi: extended source index register (32-bit)


used by high-speed memory transfer instructions.
Relating to C/C++
The value returned by OFFSET is a pointer.
Compare the following code written for both C++ and assembly
language:

; C++ version:
char array[1000];
char *p = array;

.data
array BYTE 1000 DUP(?)
.code
mov esi,OFFSET array ; ESI is p
(2) PTR Operator
Overrides the default type of a label (variable).

This is only necessary when you 're trying to access the


variable using a size attribute that’s different from the
one used to declare the variable .
Provides the flexibility to access part of a
variable.
.data
myDouble DWORD 12345678h
.code
mov ax,myDouble ; error – why?
mov ax,WORD PTR myDouble ; loads 5678h
mov WORD PTR myDouble,4321h ; saves 4321h
Little Endian Order
 Little Endian order refers to the way Intel stores
integers in memory.
 Multi-byte integers are stored in reverse order,
with the least significant byte stored at the
lowest address
 For example, the double word 12345678h would
be stored as:
doubleword word byte offset

12345678 5678 78 0000 myDouble


56 0001 myDouble + 1 When integers are loaded from
1234 34 0002 myDouble + 2 memory into registers, the bytes
are automatically re-reversed into
12 0003 myDouble + 3
their correct positions.
PTR Operator Examples
.data
myDouble DWORD 12345678h

doubleword word byte offset

12345678 5678 78 0000 myDouble

56 0001 myDouble + 1

1234 34 0002 myDouble + 2

12 0003 myDouble + 3

mov al,BYTE PTR myDouble ; AL = 78h


mov al,BYTE PTR [myDouble+1] ; AL = 56h
mov al,BYTE PTR [myDouble+2] ; AL = 34h
mov ax,WORD PTR myDouble ; AX = 5678h
mov ax,WORD PTR [myDouble+2] ; AX = 1234h
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
Assessment (1)
Write down the value of each destination operand:

.data
varB BYTE 65h,31h,02h,05h
varW WORD 6543h,1202h
varD DWORD 12345678h
.code
mov ax,WORD PTR [varB+2] ; a.
mov bl,BYTE PTR varD ; b. 0502h
mov bl,BYTE PTR [varW+2] ; c. 78h
mov ax,WORD PTR [varD+2] ; d. 02h
mov eax,DWORD PTR varW ; e. 1234h
12026543h
(3) 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
(4) LENGTHOF Operator
The LENGTHOF operator counts the
number of elements in a single data
declaration (array).
.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
(5) 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
Spanning Multiple Lines (1 of 2)
A data declaration covers 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
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 ;no error
.code
mov eax,LENGTHOF array ; 2
mov ebx,SIZEOF array ; 4
Section Review
4.3.8
[3] Indirect Operands (1 of 2)

You've probably noticed already that direct addressing is


completely impractical for array processing.

We would never provide a different label name for every


element of an array, nor would we use constant offsets to
address more than a few array elements.

The only practical way to handle an array is to use a register


as a pointer (called indirect addressing) and manipulate the
register’s value.

When an operand uses indirect addressing, it is


called an indirect operand.
[3] Indirect Operands (1 of 2)

.data
val1 BYTE 10h,20h,30h
.code
mov esi,OFFSET val1
mov al,[esi] ; dereference ESI (AL = 10h)

inc esi ;(esi: direct operand)


mov al,[esi] ;([esi]:indirect operand); AL = 20h

inc esi
mov al,[esi] ; AL = 30h
[3] Indirect Operands (2 of 2)

Use PTR operator 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

Should PTR be used here? yes, because [esi] could


add [esi],20 point to a byte, word, or
doubleword
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
.data
array
arrayW type.
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

ToDo: Modify this example for an array of doublewords.


[4] 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.

ToDo: Modify this example for an array of doublewords.


[4] Indexed Operands
You can declare a pointer variable that
contains the offset of another variable.
.data
arrayW WORD 1000h,2000h,3000h
ptrVar DWORD arrayW
.code
mov esi,ptrVar
mov ax,[esi] ; AX = 1000h

Alternate format:
ptrVar DWORD OFFSET arrayW
Section Review
4.4.5
Unconditional JMP and LOOP
Instructions
 JMP Instruction
 LOOP Instruction
 LOOP Example
 Summing an Integer Array
 Copying a String
JMP Instruction
• JMP is an unconditional jump to a label that is
usually within the same procedure.
• Syntax: JMP target (EIP: Exteneded Instruction Pointer Reg 3

• Logic: EIP  target


• Example:
top:
.
.
jmp top

Equivalent to (goto statement in C/C++)


LOOP Instruction
• The LOOP instruction creates a counting loop
• Syntax: LOOP target
• Logic: ECX: used by the CPU automatica
• ECX  ECX – 1 as a loop counter

• 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.
LOOP Example
The following loop calculates the sum of
the integers 5 + 4 + 3 +2 + 1:
mov ax,0 ; sum = 0
mov ecx,5 ; i = 5
L1:
add ax,cx ; sum = sum + i
loop L1 ; i = i - 1

ecx: used by the CPU automatically as a loop


counter
Assessment (2)
mov ax,6
What will be the final value of AX? mov ecx,4
L1:
inc ax
10 loop L1

mov ecx,0
How many times will the loop X2:
execute? inc ax
4,294,967,296 loop X2
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
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 ; or(2),point to next
integer
loop L1 ; repeat until ECX = 0
Copying a String
The following code copies a string from source
to target: (strcpy function in C++)
.data
source BYTE "This is the source string",0 good use
of SIZEOF
target BYTE SIZEOF source DUP(0)

.code
mov esi,0 ; index register
mov ecx,SIZEOF source ; loop counter (26)
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
Section Review
4.5.5

Programming Exercises

You might also like