You are on page 1of 6

EE3803 -- Lecture 7

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!).

TITLE Test1 Program illustrating Assembly Operations


;-----------------------------------------------------------
STACKSG SEGMENT PARA STACK STACK
DB 12 DUP(STACKSEG)
STACKSG ENDS
;-----------------------------------------------------------
DATASG SEGMENT PARA DATA DATA
VARA DW 250
VARB DB 75
DATASG ENDS
;-----------------------------------------------------------
CODESG SEGMENT PARA CODE
BEGIN PROC FAR
ASSUME SS:STACKSG,CS:CODESG,DS:DATASG
PUSH DS ;Save DS on stack.
SUB AX, AX ;Clear the AX register.
PUSH AX ;Push a zero on the stack.
MOV AX,DATASG ;Load the segment address into AX.
MOV DS,AX ;Inititalize the data segment.
MOV AX,VARA ;Load VARA into AX.
MOV BL,VARB ;Load VARB into BL.
MOV BH,BL ;Put VARB into BH as well.
ADD AX,BX ;Compute 250 + 30069.
MOV VARA,AX ;Save in VARA
RET
BEGIN ENDP
CODESG ENDS
END BEGIN

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).

The title directive provides a way

to label your printouts of the

assembled code.

TITLE Test1 Program illustrating Assembly Operations


;-----------------------------------------------------------
STACKSG SEGMENT PARA STACK STACK
DB 12 DUP(STACKSEG)
STACKSG ENDS
;-----------------------------------------------------------
DB and DW define space for

DATASG SEGMENT PARA DATA DATA bytes and words in memory.

VARA DW 250
VARB DB 75
DATASG ENDS
;-----------------------------------------------------------

The segment directive tells the

assembler how to align the

segment and its combine and

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

Inside CPU Segment 0


ES Space

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

XXXX0 offset into the segment.

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?

CODESG SEGMENT PARA CODE


BEGIN PROC FAR
ASSUME SS:STACKSG,CS:CODESG,DS:DATASG
PUSH DS ;Save DS on stack.
SUB AX, AX ;Clear the AX register.
PUSH AX ;Push a zero on the stack.
MOV AX,DATASG ;Load the segment address into AX.
MOV DS,AX ;Inititalize the data segment.

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.

MOV AX,VARA ;Load VARA into AX.


MOV BL,VARB ;Load VARB into BL.
MOV BH,BL ;Put VARB into BH as well.
ADD AX,BX ;Compute 250 + 30069.
MOV VARA,AX ;Save in VARA

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

You might also like