MSI Lab Lecture 1-2
MSI Lab Lecture 1-2
Assembly Language
Assembly Language Programming
COMSATS Institute of Information Technology
Ahmed Daud
1
Assembly language
programming
Learning assembly language programming will help
understanding the operations of the microprocessor
To learn:
Disadvantages:
Need to know detail hardware implementation
Not portable
Slow to development and difficult to debug
Comments
.MODEL SMALL
.STACK
Assembly directive
.CODE
.STARTUP
MOV
D1: MOV
0040H
MOV
AND
LOCK bit
.EXIT
AX,40H
;set AX to 0040H
DS,AX
SI,17H
Instructions
END
Label
4
How to learn
programming
L Logic thinking
C Concept
P Practice
Logic thinking programming is problem solving so
we must think logically in order to derive a solution
Concept we must learn the basic syntax, such as
how a program statement is written
Practice write programs
registers
Central Processor Unit
(CPU)
ALU
CU
Memory Storage
Unit
I/O
Device
#1
I/O
Device
#2
clock
control bus
address bus
fetch
read
registers
registers
instruction
I-1 register
decode
write
Fetch
Decode
Fetch operands
Execute
Store output
write
program
I-2 I-3 I-4
flags
ALU
execute
(output)
7
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
8
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
10
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
11
12
13
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
14
Segmented Memory
8000:FFFF
linear addresses
D0000
C0000
B0000
one segment
A0000
90000
80000
70000
60000
8000:0250
50000
0250
40000
30000
8000:0000
20000
10000
00000
seg
ofs
15
Segment
Segment : Offset
Segment: one of CS, DS, SS, ES
Real address = Segment * 16 + Offset
Overlapping segments. For example:
0000:01F0 = 0001:01E0 = 0010:00F0
16
0 1 0 0
Linear address:
0 9 0 1 0
17
18
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!!!!!
19
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
20
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
21
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
22
These directives
require one or
more operands
define memory
contents
specify amount of
storage to reserve
for run-time data
25
Defining Data
Numeric data
values
100 - decimal
100B - binary
100H hexadecimal
'100' - ASCII
"100" - ASCII
Use the
appropriate
DEFINE directive
A list of values
may be used - the
following creates
4 consecutive
words
DW 40CH,10B,-13,0
A ? represents an
uninitialized
storage location
DB 255,?,-128,'X'
26
Naming Storage
Locations
Names can be
associated with
storage locations
ANum DB -4
DW 17
ONE
UNO DW 1
X DD ?
These names are
called variables
ANum refers to a
byte storage
location,
initialized to FCh
The next word
has no associated
name
ONE and UNO
refer to the same
word
27
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
28
In 8086, only integer operations are supported!!!
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!!!
29
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.
30
Assembly language
programming
Example
CSEG segment code
START PROC FAR
; define the start of a program (procedure)
RET
; return
START ENDP
; define the end of a procedure
CSEG ends
End start ; end of everything
Different assembler may have different syntax for the definition
of the key words !!!!!
Start is just a name it could be my_prog, ABC etc
31
LAB 3
Understand the working and use of following arithmetic instructions
ADD instruction
ADC instruction
SUB instruction
SBB instruction
INC instruction
DEC instruction
NEG instruction
MUL instruction
DIV instruction
32