Chapter2 - Assembly Language Programming
Chapter2 - Assembly Language Programming
Chapter2 - Assembly Language Programming
Programming
n Dr. Ali Mohamed
n Faculty of Engineering
n Aswan University
The 80x86 Microprocessors 1.1 Assembly Language
Objectives
1
Program template
Program Statements
2
Fields of Assembly language instructions
[label:] mnemonic [operands] [;comment]
Brackets indicate that the field is optional and should not be typed
The label field allows the program to refer to a line of code by name.
The label field cannot exceed 31 characters.
Labels for directives do not need to end with a colon.
A label must end with a colon when it refers to an opcode generating
instruction
The colon indicates to the assembler that this refers to code within this
code segment.
The Assembly language mnemonic and operand (s) fields together perform
the real work of the program and accomplish the tasks for which the program
was written.
ADD AL,BL ; ADD is the mnemonic and "AL,BL" are operands
MOV AX,6764 ; MOV is the mnemonic and "AX,6764" are operands
The comment field begins with a “;” they be used to describe the program, to
make it easier for someone to read and understand.
Memory model
SMALL MODEL: This is one of the most widely used memory models for
Assembly language programs and is sufficient for the small programs.
The SMALL model uses a maximum of 64K bytes of memory for code and
another 64K bytes for data.
MEDIUM MODEL: In this model, the data must also fit into 64K bytes, but
the code can exceed 64K bytes of memory.
COMPACT MODEL: This is the opposite of the MEDIUM model. While
the data can exceed 64K bytes of memory, the code cannot.
LARGE MODEL: Combining the two preceding models gives the LARGE
model. Although this model allows both data and code to exceed 64K
bytes of memory, no single set of data (such as an array) should exceed
64K bytes.
HUGE MODEL: Both code and data can exceed 64K bytes of memory,
and a single set of data, such as an array, can exceed 64K bytes as well.
There is another memory model called TINY. This model is used with
COM files in which the total memory for both code and data must fit into
64K bytes. This model cannot be used with the simplified segment
definition.
3
Simple SEGMENT Definition
DATA Segment
.DATA
DATA1 DB 52H
DATA2 DB 29H
SUM DB ?
When the program starts only the CS and SS registers have the
proper values (by the OS). The DS register must be initialized by
the program.
MOV AX,@DATA
MOV DS,AX
4
CODE Segment
Program Structure
5
Program Creation Cycle
6
Program Files
Sample Program
7
Control Transfer Instructions
Conditional Jump
8
The 80x86 Microprocessors 1.17 Assembly Language
Backward Jump
9
Forward Jump
Unconditional Jump
10
Unconditional Jump
FAR Jump. The jump is out of the current code segment. So, the value of
CS and IP should be changed.
JMP FAR PTR Label
CALL statements
11
CALL statements
12
Rules for names in Assembly language
The assembler supports all the various data types of the 80x86
microprocessor by providing data directives that define the data types
and set aside memory for them.
The 8088/86 microprocessor supports many data types, but none are
longer than 16 bits wide since the size of the registers is 16 bits.
It is the job of the programmer to break down data larger than 16 bits to be
processed by the CPU.
The data types used by the 8088/86 can be 8-bit or 16-bit, positive or
negative.
If a number is less than 8 bits wide, it still must be coded as an 8-bit
register with the higher digits as zero.
Similarly, if the number is less than 16 bits wide it must use all 16 bits,
with the rest being 0s.
13
ORG (origin) Directive
The number that comes after ORG can be either in hex or in decimal.
The ORG directive is used extensively in the data segment to separate fields of
data to make it more readable for the student, it can also be used for the offset
of the code segment (IP).
14
The 80x86 Microprocessors 1.29 Assembly Language
15
The 80x86 Microprocessors 1.31 Assembly Language
16
The 80x86 Microprocessors 1.33 Assembly Language
17
DD (define double word) Directive
ORG 00A0H
DATA16 DD 1023 ;DECIMAL
DATA17 DD 10001001011001011100B ;BINARY
DATA18 DD 5C2A57F2H ;HEX
DATA19 DD 23H,34789H ,65533
ORG 00C0H
DATA20 DQ 4523C2H ;HEX
DATA21 DQ „HI‟ ;ASCI I CHARACTERS
DATA22 DQ ? ;NOTHING
18
DT (define ten bytes) Directive
ORG OOEOH
DATA23 DT 867943569829 ;BCD
DATA24 DT ? ;NOTHING
DT can also be used to allocate 10-byte integers by using the "D" option:
DEC DT 65535d ;the assembler will convert the decimal number to
hex and store it
19
Stack Segment definition
STSEG SEGMENT
DB 64 DUP (?)
STSEG ENDS
DTSEG SEGMENT
DATA1 DB 52
DATA1 DB 29
SUM DB ?
DTSEG ENDS
The data segment defines three data items
DATA1, DATA2, and SUM
The DB directive is used by the assembler to allocate memory in byte-
sized chunks.
The data items defined in the data segment will be accessed in the code
segment by their labels.
DATA1 and DATA2 are given initial values in the data section.
SUM is not given an initial value, but storage is set aside for later use by
the program.
20
Code Segment Definition
CDSEG SEGMENT
MAIN PROC FAR
ASSUME CS:CDSEG, DS:DTSEG, SS:STSEG
MOV AX,DTSEG
MOV DS,AX
MOV AL,DATA1
………
The first line of the segment after the SEGMENT directive is the PROC
directive.
A procedure is a group of instructions designed to accomplish a specific
function.
Every procedure must have a name defined by the PROC directive,
followed by the assembly language instructions and closed by the ENDP
directive.
The PROC and ENDP statements must have the same label.
The PROC directive may have the option FAR or NEAR.
The operating system that controls the computer must be directed to the
beginning of the program in order to execute it.
21
ASSUME Directive
ASSUME CS:CDSEG, DS:DTSEG, SS:STSEG
The ASSUME directive associates segment registers with specific
segments by assuming that the segment register is equal to the segment
labels used in the program.
If an extra segment had been used, ES would also be included in the
ASSUME statement.
The ASSUME statement is needed because a given Assembly language
program can have several code segments, one or two or three or more
data segments and more than one stack segment, but only one of each
can be addressed by the CPU at a given time since there is only one of
each of the segment registers available inside the CPU.
ASSUME directive tells the assembler which of the segments defined by
the SEGMENT directives should be used.
ASSUME directive also helps the assembler to calculate the offset
addresses from the beginning of that segment.
For example, in “MOV AL,[BX]” the BX register is the offset of the data
segment.
ASSUME
What value is actually assigned to the CS, DS, and SS registers for
execution of the program?
The operating system must pass control to the program so that it may
execute, but before it does that it assigns values for the segment
registers.
The operating system must do this because it knows how much memory
is installed in the computer, how much of it is used by the system, and
how much is available.
One cannot tell DOS to give the program a specific area of memory, say
from 25FFF to 289E2. Therefore, it is the job of DOS to assign exact
values for the segment registers.
Upon taking control from DOS, of the three segment registers, only CS
and SS have the proper values.
The DS value (and ES, if used) must be initialized by the program.
This is done as follows:
MOVAX,DTSEG
MOV DS,AX
22
The Form of an Assembly Language Program
STSEG SEGMENT
DB 64DUP(?)
STSEG ENDS
DTSEG SEGMENT
;place data here
DTSEG ENDS
CDSEG SEGMENT
MAIN PROC FAR ;this is the program entry point
ASSUME CS:CDSEG, DS:DTSEG, SS:STSEG
MOV AX,DTSEG ;bring in the segment for data
MOV DS,AX ;assign the DS value
MOV AH,4CH
INT 21H
MAIN ENDP
CDSEG ENDS
END MAIN
MOV AH,4CH
INT 21H
23
INT 21H Option 01H
Inputting a single character with echo
MOV AH, 01
INT 21H
This function waits until a character is input for the keyboard, then echoes
it to the monitor.
The input character (ASCII) will be in AL
Without echo
MOV AH, 07
INT 21H
MOV AH, 02
MOV DL, ‘M’
INT 21H
DL is loaded wit the character to be displayed
24
INT 21H Option 0AH
Inputting a string of data from keyboard, with echo
25
HW
26