Professional Documents
Culture Documents
Aaa
Aaa
Programming I
Week 4 Overview of the x86
assembly language
prgoramming
Assembly language
programming
Learning assembly language programming
will help understanding the operations of the
microprocessor
To learn:
Need to know the functions of various registers
Need to know how external memory is organized
and how it is addressed to obtain instructions
and data (different addressing modes)
Need to know what operations (or the
instruction set) are supported by the CPU. For
example, powerful CPUs support floating-point
operations but simple CPUs only support integer
operations
C Concept
L Logic thinking
P Practice
Concept we must learn the basic syntax,
such as how a program statement is written
Logic thinking programming is problem
solving so we must think logically in order
to derive a solution
Practice write programs
Assembly Program
The native language is machine language
(using 0,1 to represent the operation)
A single machine instruction can take up
one or more bytes of code
Assembly language is used to write the
program using alphanumeric symbols (or
mnemonic), eg ADD, MOV, PUSH etc.
The program will then be assembled
(similar to compiled) and linked into an
executable program.
The executable program could be .com,
.exe, or .bin files
Flow of program
development
Program
.asm
Assembl
e
Object file
.obj
link
Executable file
.exe
Example
Machine code for mov AL, 00H
B4 00 (2 bytes)
After assembled, the value B400 will
be stored in the memory
When the program is executed, then
the value B400 is read from
memory, decoded and carry out the
task
Assembly Program
Each instruction is represented by one
assembly language statement
The statement must specify which
operation (opcode) is to be performed and
the operands
Eg ADD AX, BX
ADD is the operation
AX is called the destination operand
BX is called the source operand
The result is AX = AX + BX
When writing assembly language program,
you need to think in the instruction level
Example
In c++, you can do A = (B+C)*100
In assembly language, only one
instruction per statement
A = B ; only one instruction - MOVE
A = A+C
; only one instruction - ADD
A = A*100 ; only one instruction - Multiply
Format of Assembly
language
General format for an assembly
language statement
Label Instruction Comment
Start: Mov AX, BX ; copy BX into AX
Start is a user defined name and you only put in
a
label in your statement when necessary!!!!
The symbol
10
Software model
In 8086, memory is divided into segments
Only 4 64K-byte segments are active and these are:
code, stack, data, and extra
When you write your assembly language program
for an 8086, theoretically you should define the
different segments!!!
To access the active segments, it is via the
segment register: CS (code), SS (stack), DS (data),
ES (extra)
So when writing assembly language program, you
must make use of the proper segment register or
index register when you want to access the
memory
11
Registers
In assembly programming, you cannot
operate on two memory locations in the same
instruction
So you usually need to store (move) value of
one location into a register and then perform
your operation
After the operation, you then put the result
back to the memory location
Therefore, one form of operation that you will
use very frequent is the store (move)
operation!!!
And using registers!!!!!
12
Example
In C++ A = B+C ; A, B, C are variables
In assembly language A,B, C
representing memory locations so you
cannot do A = B+C
MOV AL, B ; move value of B into AL
register
ADD, AL, C ; do the add AL = AL +C
MOV A, AL ; put the result to A
13
Data registers
AX, BX, CX,and DX these are the general purpose
registers but each of the registers also has special
function
Example
AX is called the accumulator to store result in arithmetic
operations
14
Data register
In based addressing mode, base register BX is
used as a pointer to an operand in the current
data segment.
CX is used as a counter in some instructions,
eg. CL contains the count of the number of bits
by which the contents of the operand must be
rotated or shifted by multiple-bit rotate
DX, data register, is used in all multiplication
and division, it also contains an input/output
port address for some types of input/output
operations
15
16
Data types
Data can be in three forms: 8-bit, 16-bit, or 32-bit
(double word)
Integer could be signed or unsigned and in bytewide or word-wide
For signed integer (2s complement format), the MSB
is used as the sign-bit (0 for positive, 1 for negative)
Signed 8-bit integer 127 to 128,
For signed word 32767 to 32768
Latest microprocessors can also support 64-bit or
even 128-bit data
In 8086, only integer operations are supported!!!
18
A sample program
.code
; indicate start of code segment
.startup ; indicate start of program
mov
AX, 0
mov
BX, 0000H
mov
CX, 0
mov
SI, AX
mov
DI, AX
mov
BP, AX
END
; end of file
The flow of the program is usually top-down and
instructions are executed one by one!!!
19
Assembly programming
In general, an assembly program must include the code segment!!
Other segments, such as stack segment, data segment are not
compulsory
There are key words to indicate the beginning of a segment as
well as the end of a segment. Just like using main(){} in C++
Programming
Example
DSEG segment data
DSEG ENDS
; defines the end of a data segment
Segment is the keyword DSEG is the name of the segment
Similarly key words are used to define the beginning of a program,
as well as the end.
20
Assembly language
programming
Example
CSEG segment code
START PROC FAR
; define the start of a program (procedure)
RET
START ENDP
CSEG ends
End start
; return
; define the end of a procedure
; end of everything
21
More
sample
Stacksg segment para stack
.
; define the stack segment
Stacksg ends
Datasg segment para
End of everything
22
Definitions
To declare a segment, the syntax is:
segment_name SEGMENT alignment class
Example Stacksg segment PARA (this statement is used in previous
slide)
PARA define the alignment of the segment base address, the segment
with a starting addressing that is evenly Divisible by 16. But the default
value is also base address divisible by 16 so the key word PARA can be
ignored!
23
Definition
data, code class entry. Is used to
group related segments when linking.
The linker automatically groups
segments of the same class in memory
PROC define procedures (similar to a
function) inside the code segment. Each
procedure must be identified by an
unique name. At the end of the
procedure, you must include the ENDP
24
Definitions
FAR is related to program execution. When you request execution
of a program, the program loader uses this procedure as the entry
point for the first instruction to execute.
Assume to associate, or to assign, the name of a segment with a
segment register
In some assembler, you need to move the base address of a segment
directly into the segment register!!!
END ends the entire program and appears as the last statement.
Usually the name of the first or only PROC designated as FAR is put after
END
25
26
start:
mov DL, 0H ; move 0H to DL
mov CL, op1 ; move op1 to CL
mov AL, data; move data to AL
step:
cmp AL, op1 ; compare AL and op1
jc label1
; if carry =1 jump to label1
sub AL, op1 ; AL = AL op1
inc DL
; DL = DL+1
jmp step
; jump to step
label1:
mov AH, DL ; move DL to AH
HLT
; Halt end of program
data db 45
; define a variable called data
op1 db 6
; define a variable called op1
27
28
; Dn is either DB or DW
29
DB
FLDB
DB
25 ; initialize a data to 25
DB
30
31
Assembly Program
32
Assembly language
programming
The syntax for different
microprocessor may be different but
the concept is the same so once you
learn the assembly programming for
one microprocessor, you can easily
program other kinds of system
For example, programming the 8051
series is very similar to the 8086
33
Addressing Modes
Function of the addressing modes is to
access the operands
Available modes (9 modes): register
addressing, immediate addressing, direct
addressing, register indirect addressing,
based addressing, indexed addressing,
based indexed addressing, string
addressing, and port addressing
Addressing modes provide different ways
of computing the address of an operand
34
35
36
37
38
40
41
Direct addressing
Data is assumed to be stored in the
data segment so DS is used in
calculating the physical address!!!
External memory bus cycle is needed to
do the read
Example of direct addressing: mov AL,
var1
Where Var1 can be regarded as a
variable
43
44
Register indirect
addressing
Eg MOV AX, [SI]
Value stored in the SI register is used as the offset
address
The segment register is DS in this example
Meaning of the above is to move the data stored in
the memory location : DS + SI to the AX register
In register indirect addressing mode, the EA
(effective address) is a variable and depends on the
index, or base register value
Eg mov [BX], CL
Which segment register will be used for the above
operation
45
Address(in
HEX)
Content
01236
19
01235
18
01234
20
01233
46