Вы находитесь на странице: 1из 8

EE2801 -- Lecture 7

Assembly Language
and
Computer Organization

EE2801-L07P01

Assembly Programming Is A 3 Step Process


Everything in a computer system is located at some well-defined location in the memory or
the IO space of the computer.

Doing something useful with a computer involves reading data from one location, operating
on it, and storing it in another location. It really doesnt matter what the information is, or
what the desired action is, the process remains the same.

For assembly language programs, creating a program is a 3 step process:

Text

Assembler

Editor
EDIT

ASCII

TASM

Source

Linker

(.asm)
Object

TLINK

Files

Executable

(.obj)

(.exe)

Debugger
TD

EE2801-L07P02

Addresses - A Place For Everything


Both memory and IO are accessed using addresses. Data is sent to a memory or IO
location by

writing to an address.

Data is received from memory or IO locations by

reading

from an address.

Portability is achieved (in part) by having rules, or standards, for how addresses are
assigned and for the behavior that is supposed to occur when addresses are accessed.

For example, the PC has a

memory map that looks something like:


FFFFFh
64Kb Boot
ROM
64Kb Option
ROM
128Kb Reserved
for device ROMs

F0000h
EFFFFh
E0000h
DFFFFh
C0000h
BFFFFh

128Kb Video
Memory

A0000h
9FFFFh

640Kb
Conventional
Memory

00000h

EE2801-L07P03

Program Loading And The Operating System


In order to execute, somehow the executable program image must be mapped to the
memory space of the computer. In general, parts of the executable will be stored in ROM
and some in RAM. Under an operating system like Linux or Windows, the program will
load into RAM, but theres no way to tell exactly where!

It is the job of a loader to assign the image to the proper memory space(s).

9FFFFh

Data
Loader

Stack

Executable

640Kb
Conventional
Memory

(.exe)

Note: On a PC, when a program

Code

starts, its stack, data, and extra


segments are

not initialized!

00000h

EE2801-L07P04

Segment-Based Addressing In The 80x86


In order to calculate a real, physical memory address the 80x86 uses a 16 bit segment
register and combines it with a 16 bit offset.
For example, consider the instruction pointer (IP). To determine the actual physical
memory location of the code, we shift the CS register left 4 bits, and add it to the IP.
Thus, if CS = A000h and IP = 27A5H then CS:IP = A27A5h as follows:

CS = A000h

Shift CS Left

IP = 27A5h

CS:IP = A27A5h

All of the segment registers in the 80x86 work this way, however the offset does not usually
come from the IP register when the DS, SS, and ES registers are used.

EE2801-L07P05

What Segmenting Is Really Doing


Any segment register can hold any 16 bit value. Likewise, any 16 bit offset can be added
to any segment register. Thus, there are 65,536 possible segment locations, and each can
be thought of as a pointer to an array of 65536 memory locations

FFFFFh

64K Code
Segment
64K Extra

CS

Segment
ES

64K Stack
Segment
SS

64K Data
Segment

DS
00000h

EE2801-L07P06

Working With Segment Registers

Although segments and offset might sound a little confusing, there some typical ways that
segments and offsets are associated with each other, and some typical applications of the
various segments.
CS - Code Segment Register
This register is used to find addresses for program instructions. The physical address of an
instruction is normally calculated using the IP register (CS:IP).
DS - Data Segment Register
This register is used to find addresses for data items in memory. There are a number of
ways that an offset may be obtained such as direct input of a number or using the BX, DI,
or SI registers (or a combination).
ES - Extra Segment Register
The extra segment is not used very often in simple programs. When it is, it is usually
associated with string operations and the offset usually comes from the DI register.
SS - Stack Segment Register
The SP register is the most common source of an offset into the stack segment. BP is also
commonly used.

EE2801-L07P07

Keeping Track Of Segment Usage


Lets review the assembly language program we saw in the last lecture and think about
what the segments are doing:

A
B
C

.Data
;Create a symbolic name
db 7d ;Create a symbolic name
db 12d ;Create a symbolic name
db ?
;Create a symbolic name

start:

.Code
nop
mov ax,@data
mov ds,ax

stop:

for
for
for
for

the beginning of the data segment.


DS:0000 and put a value there.
DS:0001 and put a value there.
DS:0002

;Create a symbolic name for the beginning of the code segment.


;Instruction at CS:0000
;Instruction at CS:0001
;Instruction at CS:0004

mov
mov
add
mov

al,byte ptr[A]
bl,byte ptr[B]
al, bl
byte ptr[C],al

;Instruction
;Instruction
;Instruction
;Instruction

at
at
at
at

CS:0006 gets data from location DS:0000


CS:0009 gets data from location DS:0001
CS:000D
CS:000F stores data in location DS:0002

jmp

stop

;Instruction at CS:0012

Notice:

not all instructions access data from memory.


some data values are compiled in

EE2801-L07P08

Вам также может понравиться