msi lab lecture 1-2

32
Introduction to 8086 Assembly Language Assembly Language Programming COMSATS Institute of Information Technology Ahmed Daud 1

Upload: ahmed-daud

Post on 15-Jul-2016

220 views

Category:

Documents


2 download

DESCRIPTION

8086 Assembly Lectures 1-2

TRANSCRIPT

Page 1: MSI Lab Lecture 1-2

1

Introduction to 8086 Assembly Language

Assembly Language ProgrammingCOMSATS Institute of Information Technology

Ahmed Daud

Page 2: MSI Lab Lecture 1-2

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

Page 3: MSI Lab Lecture 1-2

3

Overview of Assembly Language

Advantages:

Disadvantages:

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

´ 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

Page 4: MSI Lab Lecture 1-2

4

Example of Assembly Language Program

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

.MODEL SMALL

.STACK

.CODE

.STARTUP

MOV AX,40H ;set AX to 0040H

D1: MOV DS,AX ;load data segment with 0040H

MOV SI,17H ;load SI with 0017H

AND BYTE PTR [SI],0DFH ;clear NUM-LOCK bit

.EXIT

END

Comments

Assembly directive

Instructions

Assembly directive

Label

Page 5: MSI Lab Lecture 1-2

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

5

Page 6: MSI Lab Lecture 1-2

6

Basic Microcomputer Design• clock synchronizes CPU operations• control unit (CU) coordinates sequence of execution steps• ALU performs arithmetic and bitwise processing

Central Processor Unit(CPU)

Memory StorageUnit

registers

ALU clock

I/ODevice

#1

I/ODevice

#2

data bus

control bus

address bus

CU

Page 7: MSI Lab Lecture 1-2

7

Instruction Execution Cycle

• Fetch• Decode• Fetch operands• Execute • Store output

I-1 I-2 I-3 I-4

PC program

I-1instructionregister

op1op2

memory fetch

ALU

registers

writ

e

decode

execute

read

writ

e

(output)

registers

flags

Page 8: MSI Lab Lecture 1-2

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

Page 9: MSI Lab Lecture 1-2

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

9

Page 10: MSI Lab Lecture 1-2

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

Page 11: MSI Lab Lecture 1-2

Example

• In c++, you can do A = (B+C)*100• In assembly language, only one instruction per

statementA = B ; only one instruction - MOVEA = A+C ; only one instruction - ADDA = A*100 ; only one instruction - Multiply

11

Page 12: MSI Lab Lecture 1-2

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

Page 13: MSI Lab Lecture 1-2

8086 Software Model

13

Page 14: MSI Lab Lecture 1-2

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

Page 15: MSI Lab Lecture 1-2

15

Segmented Memory

00000

10000

20000

30000

40000

50000

60000

70000

80000

90000

A0000

B0000

C0000

D0000

E0000

F0000

8000:0000

8000:FFFF

seg ofs

8000:0250

0250

linea

r ad d

ress

e s

one segment

Segmented memory addressing: absolute (linear) address is a combination of a 16-bit segment value added to a 16-bit offset

Page 16: MSI Lab Lecture 1-2

16

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

Page 17: MSI Lab Lecture 1-2

17

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 0Add the offset: 0 1 0 0Linear address: 0 9 0 1 0

Page 18: MSI Lab Lecture 1-2

18

Execution Unit - Flags

Page 19: MSI Lab Lecture 1-2

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

Page 20: MSI Lab Lecture 1-2

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

Page 21: MSI Lab Lecture 1-2

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

Page 22: MSI Lab Lecture 1-2

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

Page 23: MSI Lab Lecture 1-2

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

Page 24: MSI Lab Lecture 1-2

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

Page 25: MSI Lab Lecture 1-2

25

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

Page 26: MSI Lab Lecture 1-2

26

Defining Data

• Numeric data values– 100 - decimal– 100B - binary– 100H - hexadecimal– '100' - ASCII– "100" - ASCII

• Use the appropriate DEFINE directive (byte, word, etc.)

• A list of values may be used - the following creates 4 consecutive wordsDW 40CH,10B,-13,0

• A ? represents an uninitialized storage locationDB 255,?,-128,'X'

Page 27: MSI Lab Lecture 1-2

27

Naming Storage Locations

• Names can be associated with storage locationsANum DB -4 DW 17ONEUNO DW 1X 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

• X is an unitialized doubleword

Page 28: MSI Lab Lecture 1-2

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 byte-wide or word-

wide• For signed integer (2’s 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!!!

28

Page 29: MSI Lab Lecture 1-2

A sample program

.code ; indicate start of code segment

.startup ; indicate start of programmov AX, 0 mov BX, 0000Hmov CX, 0mov SI, AXmov DI, AXmov BP, AX

END ; end of fileThe flow of the program is usually top-down and instructions are executed one by one!!!

29

Page 30: MSI Lab Lecture 1-2

Assembly programming

In general, an assembly program must include the code segment!!Other segments, such as stack segment, data segment are notcompulsory

There are key words to indicate the beginning of a segment aswell as the end of a segment. Just like using main(){} in C++ Programming

ExampleDSEG segment ‘data’ ; define the start of a data segment

DSEG ENDS ; defines the end of a data segmentSegment is the keyword DSEG is the name of the segmentSimilarly key words are used to define the beginning of a program,as well as the end.

30

Page 31: MSI Lab Lecture 1-2

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 procedureCSEG ends

End start ; end of everything

Different assembler may have different syntax for the definitionof the key words !!!!!Start is just a name it could be my_prog, ABC etc

31

Page 32: MSI Lab Lecture 1-2

32

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