Experiment No: 02
Title:
Write X86/64 ALP to perform non-overlapped and overlapped
block transfer (with and without string specific instructions). Block
containing data can be defined in the data segment.
Objective:-
1) To study the basic concepts of assembly language.
2) To practically implement instruction set of 80386 microprocessor.
3) To study How to accept and display the numbers by using assembly programming
theory.
Environment:
1.
OS:
ubuntu 12.10
2.
Assembler:
NASM
3.
Linker:
ld
Theory:
Program for non-overlapped and overlapped block
transfer of array elements. Takes the array elements from the user and also the
number of elements to be overlapped in overlapped transfer. Block transfer
here, refers to moving of block of data within the memory to a different
location. In non-overlapped transfer we move the data to a completely new
location. It is easily accomplished by copying the data using two pointers, one
data byte at a time. In overlapped transfer the data block is shifted slightly
from the present position, thus some of the starting elements may overlap with
the old position of the last elements in the array. They are therefore copied
in reverse order.
Registers:
Registers are places in the CPU
where a number can be stored and manipulated. There are three sizes of
registers: 8-bit, 16-bit and on 386 and above 32-bit. There are four different
Types of registers:
1.
General Purpose Registers,
2.
Segment Registers,
3.
Index Registers,
4.
Stack Registers.
General Registers – Following are the registers that are used for general purposes in
80386
EAX
accumulator (32 bit)
AH
accumulator high-order byte (8 bit)
AL
accumulator low-order byte (8 bit)
EBX (32 bit)
BX (16 bit)
BH (8 bit)
BL (8
bit)
ECX
count and accumulator (32 bit)
CX
count and accumulator (16 bit)
CH
count high order byte (8 bit)
CL
count low order byte (8 bit)
EDX
data and I/O address (32 bit)
DX data
and I/O address (16 bit)
DH data
high order byte (8 bit)
DL data
low order byte (8 bit)
Segment Registers - These registers are used to calculate 20 bit address from 16 bit registers.
CS code
segment (16 bit)
DS data
segment (16 bit)
SS
stack segment (16 bit)
ES
extra segment (16 bit)
Index Registers - These registers are used with the string instructions.
EDI
destination index (32 bit)
ESI
source index (32 bit)
Pointers - These registers are used with the segment register to obtain 20 bit
addresses
ESP
stack pointer (32 bit)
EBP
base pointer (32 bit)
CS, Code Segment (16 bit)
Used to
“point” to Instructions, Determines a Memory Address (along with IP)
Segmented
Address written as CS:IP
DS, Data Segment (16 bit)
Used to
“point” to Data. Determines Memory Address (along with other registers)
ES, Extra Segment (16 bit) allows 2 Data Address Registers
SS, Stack Segment (16 bit) Used to “point” to Data in Stack Structure (LIFO)
Used
with SP or BP
SS:SP
or SS:BP are valid Segmented Addresses
EIP instruction pointer (32 bit)
IP, Instruction Pointer
Used to
“point” to Instructions Determines a Memory Address (along with CS)
Segmented
Address written as CS:IP
SI, Source Index;
DI, Destination Index
Used to
“point” to Data Determines Memory Address (along with other registers)
DS, ES commonly used
ESP, Stack Pointer;
EBP, Base Pointer
Used to
“point” to Data in Stack Structure (LIFO) Used with SS SS:SP or SS:BP are valid
Segmented Addresses
Memory address
calculations
The
80386 uses a 32 bit address bus but the registers are sixteen bit as well as 32
bit. To derive 32 bit addresses from the register s two registers are combined.
Every memory reference uses one of the four segment registers plus an offset
and/or a base pointer and/or a index register. The segment register is
multiplied by sixteen (shifted to the left four bits) and added to the sixteen
bit result of the offset calculation.
The 8086 provides four segment registers for address calculations. Each
segment register is assigned a different task. The code segment register is
always used with the instruction pointer (also called the program counter) to
point to the instruction that is to be executed next. The stack segment
register is always used with the stack pointer to point to the last value
pushed onto the stack. The extra segment is general purpose segment register.
The data segment register is the default register to calculate data operations,
this can be overridden by specifying the segment register. For example mov
ax,var1 would use the offset var1 and the data segment to calculate the memory
reference but mov ax,ss:var1 would use the offset var1 and the stack segment
register to calculate the memory reference. The offset can be calculated in a
number of ways. Their are three elements that can make up an offset. The first
element is a base register, this can be one of the BX of BP registers (the BP
register defaults to the stack segment). The second element is one of the index
register, SI or DI. The third element is a displacement. A displacement can be
a numerical value or an offset to a label. An offset can contain one to three
of these elements, making a total of sixteen possibilities.
BX SI
or + or
+ Displacement
BP DI
(base)
(index)
The
offset to a label in calculated using the assembler directive OFFSET. This directive
makes the assembler calculate the distant from the start of the segment that
the label resides in to the label.
Memory Segmentation
x86
Memory Partitioned into Segments
– 8086:
maximum size is 64K (16-bit index reg.)
– 8086:
can have 4 active segments (CS, SS, DS, ES)
– 8086:
2-data; 1-code; 1-stack
– x386:
maximum size is 4GB (32-bit index reg.)
– x386:
can have 6 active segments (4-data; FS, GS)
Why have segmented
memory?
Other
microprocessors could only address 64K since they only had a single 16-bit MAR
(or smaller). Segments allowed computers to be built that could use more than
64K memory (but not all at the same time).
Note that segments can overlap. This means that two different logical
addresses can refer
to the
same physical address (aliasing).In non-overlapping method, address of source
is totally different from address of destination. Therefore, we can directly
transfer the data using MOVSB/MOVSW instruction
for transferring the data. The blocks are said to be overlapped if some of the
memory locations are common for both the blocks.
Case I:
If address in SI is greater than address in DI then start the data transfer
from last
memory
location keeping DF=1.
Case
II: If address in SI is less than address in DI, then start the data transfer
from first
memory
location by keeping DF=0.
Algorithm:-
1. Start
2. Initialize data section
3. Load counter with
number element for Addition
4. Locate first location
of array of Number in Pointer Register
5. Initialize result-low
and result high to 00
6. Scan the number from
user and convert it into Hex
7. Increment Location
Pointer Register.
8. Decrement counter
9. Jump tostep 6 if
counteris not zero
10. Locate first location
of arrayofnumbers inapointer register
11. Initialize counterwith numberof elements
for addition
12. Add the element ofarraypointed
bypointerregister
13. Check the carryofaddition.If
carryisgenerated then go to step 14elsegoto step15
14. Increment result-high register
15. Increment pointer registerto array
16. Decrement counter
17. Jump tostep 12 if counteris not equal to
zero
18. Displaythecontent ofResult-High ascarryof
addition byconvertinghextoASCII
19. Displaythecontent ofResult-Lowas result of
addition byconvertinghextoASCII
20. Stop
No comments:
Post a Comment