0% found this document useful (0 votes)
137 views32 pages

MSI Lab Lecture 1-2

Assembly language programming involves understanding the microprocessor's operations and architecture at a low level. It is important to understand the microprocessor's registers, memory organization, addressing modes, and instruction set. An assembly language program translates machine instructions into a readable mnemonic form. Key aspects of an assembly language program include segments like code, stack, data, directives to define variables, and instructions to manipulate data and registers.

Uploaded by

Ahmed Daud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views32 pages

MSI Lab Lecture 1-2

Assembly language programming involves understanding the microprocessor's operations and architecture at a low level. It is important to understand the microprocessor's registers, memory organization, addressing modes, and instruction set. An assembly language program translates machine instructions into a readable mnemonic form. Key aspects of an assembly language program include segments like code, stack, data, directives to define variables, and instructions to manipulate data and registers.

Uploaded by

Ahmed Daud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Introduction to 8086

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:

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
2

Overview of Assembly Language


Advantages:
Faster as compared to programs written using high-level languages
Efficient memory usage
Control down to bit level

Disadvantages:
Need to know detail hardware implementation
Not portable
Slow to development and difficult to debug

Basic components in assembly Language:


Instruction, Directive, Label, and Comment
3

Example of Assembly Language


Program
;NUMOFF.ASM: Turn NUM-LOCK indicator off.

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

;load data segment with

SI,17H

;load SI with 0017H

Instructions

BYTE PTR [SI],0DFH ;clear NUMAssembly directive

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

Basic Microcomputer Design

clock synchronizes CPU operations


control unit (CU) coordinates sequence of execution
steps
ALU performs arithmetic and bitwise processing
data bus

registers
Central Processor Unit
(CPU)
ALU

CU

Memory Storage
Unit

I/O
Device
#1

I/O
Device
#2

clock
control bus

address bus

Instruction Execution Cycle


PC
I-1
memory
op1
op2

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

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

is used to indicate that it is a label

12

8086 Software Model

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

Segmented memory addressing: absolute (linear)


address is a combination of a 16-bit segment value
added to a 16-bit offset
F0000
E0000

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

Calculating Linear Addresses


Given a segment address, multiply
it by 16 (add a hexadecimal zero),
and add it to the offset
Example: convert 08F1:0100 to a
linear address
Adjusted Segment value: 0 8 F 1 0
Add the offset:

0 1 0 0

Linear address:

0 9 0 1 0
17

Execution Unit - Flags

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

Registers are 16-bit but can be used as 2 8-bit storage


Each of the 4 data registers can be used as the source or destination of
an operand during an arithmetic, logic, shift, or rotate operation.
In some operations, the use of the accumulator is assumed, eg in I/O
mapped input and output 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

Pointer and index


registers
Stack is used as a temporary storage
Data can be stored by the PUSH instruction and
extracted by the POP instruction
Stack is accessed via the SP (Stack Pointer) and BP
(Base Pointer)
The BP contains an offset address in the current
stack segment. This offset address is employed
when using the based addressing mode and is
commonly used by instructions in a subroutine that
reference parameters that were passed by using the
stack
23

Pointer and Index


Register
Source index register (SI) and Destination index
register (DI) are used to hold offset addresses for use
in indexed addressing of operands in memory
When indexed type of addressing is used, then SI
refers to the current data segment and DI refers to
the current extra segment
The index registers can also be used as source or
destination registers in arithmetic and logical
operations. But must be used in 16-bit mode
24

Program Data and


Storage
Pseudo-ops to
define data or
reserve storage
DB - byte(s)
DW - word(s)
DD doubleword(s)
DQ - quadword(s)
DT - tenbyte(s)

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

; define the start of a data segment

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

You might also like