Professional Documents
Culture Documents
Assemply Anatomy
Assemply Anatomy
The Anatomy Of An
Assembly Language Program
EE3803-L07P01
The Anatomy Of An Assembly Language Program
Consider the following assembly language program (which isnt very interesting except
maybe in the debugger!).
EE3803-L07P02
A Closer Look - Segment Initialization
The first part of this program initializes the various segments that are used by the program.
Its generally a good idea to initialize at least 32 bytes of stack. Data need not be initialized
unless it is used in the program (most programs do use data though).
assembled code.
VARA DW 250
VARB DB 75
DATASG ENDS
;-----------------------------------------------------------
class type.
EE3803-L07P03
What Are These Segments?
There are four segment registers in the 8086. Each points to a memory area that is
reserved for a particular type of data. Real, physical, memory addresses are determined
based on these segments.
Main Memory
CS Register
ZZZZ0 + Offset
DS Register
Mem Address
SS Register
CS Space
ES Register
WWWW0
SS Space
Each segment register YYYY0 Memory addresses are all 20 bits. They are
points to the bottom of calculated by shifting the segment address left
a 64K space! DS Space by 4 bits and then adding it to the desired
EE3803-L07P04
More Anatomy - Program Initialization
The code segment is where the actual program instructions all reside. The main
procedure is declared as being far. This is because we have no idea where in the
memory space the program will be located initially.
Note that we start by saving the original data segment on the stack followed by pushing a
zero in the stack. This is done to allow the operating system to recover its data segment
after youre done. Likewise, we need to initialize our data segment. Why? Why dont
we initialize our code and stack segments as well?
EE3803-L07P05
More Anatomy - Real Program Code!
Although this program isnt very interesting, single stepping it through the debugger and
watching the registers and memory locations that are being manipulated will show clearly
whats going on.
This is the kind of code that might result when the following C program is compiled:
void main()
{
int VARA = 250;
char VARB = 75;
int TEMP;
TEMP = VARB << 8;
VARA = VARA + TEMP + VARB;
}
Thinking about analogies to C programs might actually help figure out how to do something
in assembly language!
EE3803-L07P06