Assembly Language Lecture2
Assembly Language Lecture2
segment
Real mode
Processor 8086 works only in real mode.
20-bit address bus =>
access up to 1,048,576 (or 220) memory locations (1 MB)
Base address ... 16-bit value
Offset ... 16-bit value =>
segments are limited to 216 bytes = 64 kB
Linear address = base address * 16 + offset
15
0
base address
15
0000
0
offset
19
0
linear address
Assembly Language 2/ 2
Protected mode
introduced in the 80386 processor.
32-bit address bus =>
access up to 232 bytes = 22 . 230 B = 4 GB
Base address ... 32-bit value
Offset ... 16-bit or 32-bit value
Linear address = base address + offset
Linear address physical address.
paging
Paging memory management scheme
transforms a huge logical address space to a smaller
physical address space;
permits multitasking (execution multiple tasks in parallel)
by providing tools for protection of the programs address
space (a program cannot overwrite other programs data)
Modes according to offset size:
16-bit mode (real or protected)
32-bit mode (only protected)
Virtual mode
Processor runs in protected mode, but simulates real mode: a
20-bit linear address is translated by paging to a 32-bit
physical address.
A processor is switched to virtual mode when running a DOS
application under Windows operating system.
Assembly Language 2/ 3
Registers
memory locations inside the processor.
user registers used by user applications
system registers used by the operating system
User registers
general-purpose registers
segment registers
instruction pointer
flags register
Assembly Language 2/ 4
General-purpose registers
They may
hold instruction operands
contain the effective address (offset) of an operand or
participate in its calculation.
31
16 15
EAX
8 7
AH AX
AL
EBX
BH
BX
BL
ECX
CH
CX
CL
EDX
DH
DX
DL
ESI
SI
EDI
DI
EBP
BP
ESP
SP
Assembly Language 2/ 5
Segment registers
16-bit registers
In real mode they contain the base address of a segment:
CS base address of the code segment
SS base address of the stack segment
DS base address of the data segment
ES, FS, GS base address of other data segments
CS a SS are initialised automatically by the operating
system at the moment of the program start.
The other segment registers are set by program
instructions.
In protected mode they contain indexes (selectors) to a
table where segment descriptors are stored; they are
initialised automatically by the operating system.
Assembly Language 2/ 6
Assembly Language 2/ 7
Flags register
32-bit register
It contains information about
the result of the recent arithmetic or logical operation
the state of the processor
the state of the current task
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
0 0 0 0 0 0 0 0 0 0
RF
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
OF DF IF TF SF ZF 0 AF 0 PF 1 CF
0
used by the operating system
Assembly Language 2/ 8
Carry Flag CF
CF is set to 1 if the result of an arithmetic operation on
unsigned numbers is too big to be held in the specified
register or memory location, i.e. the operation has produced a
carry from the most significant bit.
Otherwise it is set to 0.
mov al,255; store 255 = 0FFh to register al
add al,4; add 4 to al
mov al,127
add al,4
Overflow Flag OF
OF is set to 1 if an arithmetic operation on signed numbers
gives a result which is out of range, i.e.
the operation has produced a carry to the most significant
(sign) bit but not from the most significant bit,
the operation has produced a carry only from the most
significant bit.
mov al,-128; 80h
add al,-1; 0FFh
Assembly Language 2/ 9
Sign Flag SF
is only useful when operating with signed numbers.
SF is set to 1 if the result of an operation is negative,
otherwise it is set to 0.
SF takes the same value as the most significant bit of the
result.
Zero Flag ZF
ZF is set to 1 if the result of an operation is zero, otherwise it
is set to 0.
Assembly Language 2/ 10
Parity Flag PF
PF is set to 1 if the lowest byte of the result of an operation
contains an even number of 1s, otherwise it is set to 0. It is
mainly useful when transmitting data between devices.
Direction Flag DF
DF is useful when manipulating a data array (string, vector
etc.). It is set by instructions in a program.
If DF is set to 0, the string instruction increments the index by
the size of the array entry and so progress is forward through
memory (the string is manipulated from the left to the right).
If DF is set to 1, the string instruction decrements the index
and so progress is backward through memory (the string is
manipulated from the end to the beginning).
Assembly Language 2/ 11
Trap Flag TF
Resume Flag RF
These flags are useful when debugging programs.
They are set by instructions in a program (debugger).
By setting TF to 1 the processor is forced to operate in single
step mode in which an internal interrupt is generated after
every instruction.
By setting RF to 1, a potential breakpoint on the next
instruction will be ignored.
Debugger sets RF to 1 after the breakpoint service routine
has finished so the program can continue from the instruction
labelled by breakpoint. After the instruction has been
executed, RF is set to 0.
Assembly Language 2/ 12