3-Assembly Program Structure
3-Assembly Program Structure
Microprocessor
Next
Chapter 3 : The Intel 8086 Microprocessor Dr. Tarek ALthami CCSE YANBU 2
Why use the assembler ?
To execute a sequence of instructions by a processor, we must provide
It codes
machine is difficult
(binary data)
assembly language is much
to read by
But the instructions codes in binary forms are difficult to remember, that is
why the assembly language more understandable
has been developed. to a
Assembly humans! human, because
language gives instructions the namesnames such as
codes a meaningful
are generally easier to
MOV, ADD, etc.. (provided by the designers of processors : Intel, etc.)
An example of a machine language program that adds the values of two
remember than numbers
memory cells and store the result in a third cell:
A1 01 10 03 06 01 12 A3 01 14
Chapter 3 : The Intel 8086 Microprocessor Dr. Tarek ALthami CCSE YANBU 3
Assembly Program Structure
A program written in assembly includes data and instructions
Data are declared by directives which are special keywords used to
direct the Assembler to do certain operations such as creating
memory space for variables used in the program
The instructions are executed by the microprocessor
A directive: Provides information to the assembler while translating
a program
Not transformed into a machine language instruction
Directives are statements that will guide the assembler
Label: Mnemonic {Operands} ;Comments
Syntax of an instruction
Lab:: MOV AX, 500H ;put 500H in AX
Used as the target of a
jump or
Mnemonic: identifies thebranching
operation (e.g. MOV, ADD, SUB,JMP,…)
Operand: specify instructions
the data required by the operation (sometimes not required)
Comment: explain the program’s purpose, begin with a semicolon ;
Label: marks the address of an instruction
Chapter 3 : The Intel 8086 Microprocessor Dr. Tarek ALthami CCSE YANBU 4
Assembly Program Structure
data SEGMENT ;Data is the name of Data Segment
;data declarations
ENDS
;list of instructions
ENDS
END begin ;end of the program with the label of first instruction
Data are declared as data segment, which is delimited by SEGMENT & ENDS directives
The instructions are placed in the code segment
The first program instruction should always be marked with a label
The file must end with the END directive followed by the label of the first instruction
The semicolons indicate comments
Chapter 3 : The Intel 8086 Microprocessor Dr. Tarek ALthami CCSE YANBU 5
Assembly Program Structure
STACK Segment
Defines a runtime stack for the program
The size of the stack can be optionally specified by this directive
The runtime stack is required for procedure calls
DATA Segment
Defines an area in memory for the program data
The program's variables should be defined within these area
Assembler will allocate and initialize the storage of variables
CODE Segment
Defines the code section of a program containing instructions
Assembler will place the instructions in the code area in
memory
Chapter 3 : The Intel 8086 Microprocessor Dr. Tarek ALthami CCSE YANBU 6
Data directives (1) constant
Data Directive: EQU
EQU defines a constant
Examples:
VAL EQU 50 ; assigns 50 to the VAL name
PressKey EQU <"Press any key..",0> ;text appear within <>
Integer Constants
Examples: –10, 42d, 10001101b, 0FF3Ah, 777o
Radix: b = binary, d = decimal, h = hexadecimal, and o =
octal
If no radix is given, the integer constant is decimal
A hexadecimal beginning with a letter must have a leading “0”
Character and String Constants
Enclose character or string in single or double quotes
Examples: 'A', "d", 'ABC', "ABC", '4096'
Each ASCII character occupies a single byte
Chapter 3 : The Intel 8086 Microprocessor Dr. Tarek ALthami CCSE YANBU 7
Data directives (2) Variables
Data Directives: DB / DW / DD
DB / DW / DD / DQ : used to declare variables (1/2/4/8 bytes)
The assembler assigns an address to each variable (we can assign
an initial value) offset
VIL 18
34 1000
20
33 1001
Example TAB 6
24 1002
21
55 1003
19
31 1004
VIL DB 12h,14h ; define a variable (a byte) Initial value of 12 Mess 0
‘Y’ 1005
‘a’
17 1006
TAB DB 18h, 15h, 13h ; 3 cells array (3 bytes) 77
‘n’ 1007
‘b’
91 1008
Mess DB 'Yanbu' ; defines a table with the ASCII code
‘u’
87 1009
name DB ? ; defines a variable 8-bit initial random value Name 60 100A
33 100B
43 100C
52 100D
240 100E
255 100F
Chapter 3 : The Intel 8086 Microprocessor Dr. Tarek ALthami CCSE YANBU 8
Data directives (2) Variables
Examples
The initial values can be given in hexadecimal or binary
data SEGMENT
object DW 0F0Ah ; in hex
mask DB 01110000b ; binary
ENDS
The variables are used in the program by its designating name.
After previous declaration, we can write (in the code segment):
MOV AX, object
AND AL, mask
MOV object, AX
A variable is a name given to a memory address.
The variables are used in assembly language in order to facilitate writing
programs.
It is easier to remember a name than address of memory!
The assembler takes care of replacing variable names with their
addresses
Chapter 3 : The Intel 8086 Microprocessor Dr. Tarek ALthami CCSE YANBU 9
Data directives (Variables)
DW (define word): reserves a memory space of 2 bytes (a word) offset
TT1 00H
34 1000
Example
05H
33 1001
TT1 DW 500h ; reserves two bytes from TT1 TAB1 6
10H 1002
00H
55 1003
TAB1 DW 10h, 11h, 14h ; reserve a table of six cells 11H
31 1004
YY DW ? ; Reserve a word in the memory 0
00H 1005
14H
17 1006
77
00H 1007
Example
YY 91 1008
87 1009
data SEGMENT object 0AH
60 100A
object db 10, 0FH, -2 0FH
33 100B
43
FEH 100C
thing db, 'YANBU' thing 59H
52 100D
ENDS 41H
240 100E
4EH
255 100F
DD: (Define Double): reserves a memory space of 32 bits 255
42H 1010
255
55H 1011
Example
Var1 DD 15500000h
Chapter 3 : The Intel 8086 Microprocessor Dr. Tarek ALthami CCSE YANBU 10
Data directives (Variables)
If we want to replace the character B of YANBU by X, we can write: offset
TT1 00
34 1000
MOV AL, 'X' 05
33 1001
TAB1 6
10 1002
MOV thing +3, AL 00
55 1003
11
31 1004
Note that 'thing' is a constant 0
00 1005
14
17 1006
Its value is known at the assembly time 77
00 1007
Instruction generated by the assembler is YY 91 1008
87 1009
MOV [adr] , AL object 0A
60 100A
0F
33 100B
Or
43
FE 100C
MOV [1010H] , AL thing 59
52 100D
thing +1 41
240 100E
thing +2 4E
255 100F
thing +3 42
58 1010
255
55 1011
Chapter 3 : The Intel 8086 Microprocessor Dr. Tarek ALthami CCSE YANBU 11
Data directives (Variables)
offset
Dup Directive tab 0FH
34 1000
0FH
33 1001
When we want to declare an array of “n” cells, all 6
0FH 1002
initialized to the same value, we use the dup 0FH
55 1003
Directive (duplicate): 0FH
31 1004
ahmed 0 1005
17 1006
Data Segment
77 1007
tab DB 5 dup(15) ;100 bytes valued at 15 91 1008
ahmed DW 5 dup(?) ;not initialized 10 words 87 1009
60 100A
thing DW 1020H 33 100B
ENDS 43 100C
52 100D
240 100E
thing 20H
255 100F
231
10H 1010
255 1011
Chapter 3 : The Intel 8086 Microprocessor Dr. Tarek ALthami CCSE YANBU 12
Segment Registers Initialization
CS Register
When the processor fetch an instruction, the 20-bit address is formed
using CS segment register and the instruction pointer register IP.
The pair of these two registers is denoted CS: IP
The CS register is automatically initialized to the segment containing
the first instruction (by the operating system loader)
DS Register
The DS register is used to access the data used by the program.
Thus, the instruction MOV AX,[0145] will result in reading the
word at memory address DS:0145H
The value of DS register will not be automatically initialized:
The user must initialize DS in the assembly code
MOV AX, name_of_data_segment
MOV DS, AX
It would be easier to MOV DS, name_of_data_segment, but this instruction does not exist
Chapter 3 : The Intel 8086 Microprocessor Dr. Tarek ALthami CCSE YANBU 13
OFFSET Operator
OFFSET is a directive used to get the Offset address of a variable
Example offset
BVal 34H 400:0000
WVal 33H 400:0001
data SEGMENT
06H 400:0002
BVal DB ? ;Assume bVal is at 400:0000h
DVal 55H 400:0003
WVal DW ? 31H 400:0004
DVal DD ? 00H 400:0005
DVal2 DD ? 17H 400:0006
ENDS DVal2 77H 400:0007
91H 400:0008
Code SEGMENT 87H 400:0009
data SEGMENT
dval DD 12345678h
array DB 00h,10h,20h,30h
ENDS
Code SEGMENT
MOV AX,data
MOV DS,AX
MOV AL, dval ; error – why?
MOV AL, BYTE PTR dval ; AL= 78h
MOV AX, dval ; error – why?
MOV AX, WORD PTR dval ; AX = 5678h
Chapter 3 : The Intel 8086 Microprocessor Dr. Tarek ALthami CCSE YANBU 15
Your first Assembly Program
Example: Assembler program for computing the sum of two 16-bits integers
data SEGMENT
A DW 10 ; A = 10
B DW 20 ; B = 20
result DW ? ; result
ENDS
stack segment
DW 128 dup(0)
ends
Code SEGMENT
start: ; label for the first instruction
MOV AX, data
MOV DS, AX ; initializes DS
; The program:
MOV AX, A
ADD AX, B
MOV result, AX ; ranges result
Chapter 3 : The Intel 8086 Microprocessor Dr. Tarek ALthami CCSE YANBU 17