Sie sind auf Seite 1von 37

INTERMEDIATE 8086 ASSEMBLY

LANGUAGE PROGRAMMING
Cutajar & Cutajar
2012

Machine Code
2

There are occasions when the programmer must program at the machines own
level.
Machine Code programs are tedious to write and highly error prone.
0000111100001111
0010010101010100
1010101010100101

In situations where a high-level


language is inappropriate we
avoid working in machine code
most of the time by making the computer do more of the work. Thus we write
in assembly language and then the computer converts this assembly language
program into machine code.

Assembly Language
3

In assembly language, a mneumonic (i.e. memory aid) is used as a short notation


for the instruction to be used.
Assembly
Language
SUB AX,BX
MOV CX,AX
MOV DX,0

Machine Code
001010111000011
100010111001000
10111010000000000000000

Assembly language is an intermediate step between high level languages and machine
code. Most features present in HLL are not present in Assembly Language as type
checking etc.

Compilers / Assemblers
4

High-level Languages such as


Pascal programs are sometimes
converted firstly to assembly
language by a computer program
called compiler and then into
machine code by another
program called assembler

Pascal Program
Compiler
Assembler language Program
Assembler
Machine Code Program
This version is
actually loaded and
executed

General Purpose Registers


5

There are 4 general


purpose registers in
the 8086.
They are all 16-bit
registers
Each byte can be
addressed individually
by specifying the High
order or the Low order
byte of the register.

AH

AL
AX

BH

BL
BX

CH

CL
CX

DH

DL
DX

Some Simple Commands


6

MOV AX,3
ADD AX,2
MOV BX,AX
INC CX
DEC DX
SUB AX,4
MUL BX
DIV BX

; Put 3 into register AX


; Add 2 to the contents of AX
; Copy the contents of AX in BX
; Add 1 to the contents of CX
; Subtract 1 from the contents of DX
; Subtract 4 from the contents of AX
; Multiply the contents of AX with BX leaving
; the answer in DX-AX
; Divide the contents of DX-AX by BX leaving
; the quotient in AX and remainder in DX.

Number Formats
7

MOV AH,01010101B
MOV AL,00100111B
MOV AX,3
MOV AH,AL
MOV AL,10D

MOV AL,10H

0 1 0
0 1 0
0 0 0
0 0 0
0 0 0

0 0 0

AH
1 0
1 0
0 0
0 0
0 0
0 0

AX

AL

1 0 1
1 0 1 0 0 1 0 0 1 1 1
0 0 0 0 0 0 0 0 0 1 1
0 1 1 0 0 0 0 0 0 1 1
0 1 1 0 0 0 0 1 0 1 0

0 1 1 0 0 0 1 0 0 0 0

In case a number is moved (copied) into the register the base of a is specified by a
letter B for Binary, D for Decimal and H for Hex.

AMBIGUITY
8

Consider the instruction MOV DL, AH


Does it mean copy the contents of register AH to DL or
Does it mean copy A in hexadecimal into register DL
To avoid this ambiguity all hexadecimal numbers must start with a number. This can
always be done by preceding a number starting with A,B,C,D,E and F with a
preceding zero to remove ambiguity.
Thus MOV DL, AH means copy AH to DL whilst
MOV DL, 0AH means sore hexadecimal A to DL

The Flags Register


9

Some of the instructions (but not all) affect the flag register.
The flag register signals the status of the CPU after the last operation performed.
For example if SUB AX,2 results in zero the ZF get 1 (lights on) indicating that
the result of the last operation was zero.

JUMPS
10

Jump instructions allow the 8086 to take decisions according to information provided by
the flag register.
For example, if AX and BX contain the ASCII code for the same letter then do one
thing, if not then do another.
`

CMP AX,BX
JE SAME

SAME:

MOV CX,AX

; Compares the contents of BX with that of AX


; Jump if they are equal to the point
; in the code labeled SAME
; Obey these instructions if the contents of AX
; is not equal to that of BX
; Program continues from here if AX = BX.

Labels
11

We saw that the jump instruction has a general format JE <label> where <label> is a
facility offered by the assembler.
These labels are converted by the assembler to exact address where the program is to
continue.
Labels must start with a letter and can contain thereafter letters, numbers and
underscores (_).
Spaces and punctuation marks are not permitted
Avoid using keywords in labels
Once_again, Next, Name34, this_37 are permitted as labels
3rdday, tues+wed and semi;colons are not permitted as labels.

JUMP Conditions
12

JA/JNBE
JAE/JNB
JB/JNAE/JC
JBE/JNA
JE/JZ
JMP
JNC
JNE/JNZ
JNO
JNP/JPO
JNS
JO
JP/JPE
JS
JG/JNLE
JGE/JNL
JL / JNGE
JLE/JNG
JCXZ

(CF and ZF) = 0


CF = 0
CF = 1
(CF or ZF) = 1
ZF = 1
none
CF = 0
ZF = 0
OF = 0
PF = 0
SF = 0
OF = 1
PF = 1
SF = 1
ZF = 0 and SF = OF
SF = OF
SF <> OF
(ZF = 1) or (SF <> OF)
Register CX = 0

Above / Not Below or Equal


Above or Equal / Not Below
Below / Not Above or Equal / Carry
Below or Equal / Not Above
Equal / Zero
Unconditionally
No Carry
Not Equal / Not Zero
No Overflow
No Parity / Parity Odd
No Sign / Positive
Overflow
Parity / Parity Even
Sign
Greater / Not Less nor Equal
Grater or Equal / Not Less
Less / Not Greater nor Equal
Less or equal / not greater
CX is equal to zero

Example using Jumps


13

MOV CX, AX
SUB AX,BX
JZ MAKE1

MAKE1:
RESET:

; Keep a copy of AX before modification


; AX := AX BX
; This is instruction will cause execution
; to continue from MAKE1 if AX was
; equal to BX (subtraction resulted in Zero)
MOV DX, 0
; Otherwise store 0 in DX
JMP RESET ; Jump to RESTORE where AX is restored
; thus avoiding the next instruction
MOV DX, 1
; If AX = BX then we set DX to 1
MOV AX, CX
; Restore the old value of AX

Note that in the Code a colon ends a


label position

The Logical Family


14

AND
Contents of AX = 0000101011100011
Contents of BX = 1001100000100001
Contents of AX = 0000100000100001
after AND AX,BX is executed
OR
Contents
Contents
Contents
after OR

NOT (Invert: Ones Complement)


Contents of AX = 0000101011100011
Contents of AX = 1111010100011100
after NOT AX is executed

TEST
of AX = 0000101011100011 Contents of AX =
of BX = 1001100000100001 Contents of BX =
of AX = 1001101011100011 Contents of AX =
AX,BX is executed
after TEST AX,BX

XOR
Contents of AX = 0000101011100011
Contents of BX = 1001100000100001
Contents of AX = 1001001011000010
after XOR AX,BX is executed

0000101011100011
1001100000100001
0000101011100011
is executed

Similar to AND but the result is not stored in AX


but only the Z-flag is changed
NEG (Twos Complement)
Contents of AX = 0000101011100011
Contents of AX = 1111010100011101
after NEG AX is executed

Use of Logical Family


15

Symbol

ASCII (Dec)

ASCII (Hex)

48

30

49

31

50

32

51

33

52

34

53

35

54

36

55

37

56

38

57

39

By Making an AND between an ASCII value and 0FH


we can obtain the required number.
Say we AND 33H = 00110011B
with 0FH = 00001111B
We obtain = 00000011B

(3)

By Making an OR between a number value and 30H we


can obtain its ASCII code.
Say we OR 05H
with 30H
We obtain
(ASCII

= 00000101B
= 00110101B
= 00110101B
value for 5)

Masking
16

By the use of masking we can set or test individual bits of a register


Suppose we want to set the 3rd.
bit of AX to 1 leaving the
others unchanged.
AX = 0101010100011001
04H = 0000000000000100
OR AX,04H = 0101010100011101
Suppose we want to set the 5th.
bit of AX to 0 leaving the
others unchanged.
AX = 0101010100011001
0FFEFH = 1111111111101111
AND AX,0FFEFH = 0101010100001101

Suppose we want to test the if the


6th. bit of AX is 1 or 0:
AX = 0101010100011001
20H = 0000000000100000
AND AX,20H = 0000000000000000
So if the result is 0 then that
particular bit was 0, 1 otherwise

Instructions which affect Memory


17

Computer memory is best thought of numbered pigeon holes (called locations),


each capable of storing 8 binary digits (a byte)

Data can be retrieved from memory, one or


two bytes at a time:
MOV AL, [20H] will transfer the
Contents of location 20H to AL.
MOV BX, [20H] will transfer the contents of
locations 20H and 21H to BX.
MOV [20H], AL will transfer the contents of
AL to memory location 20H

Location ADDRESS

[0000]
[0001]
[0002]
[0003]
[0004]
[0005]
[0006]
[0007]
[0008]
[0009]
[000A]
[000B]
[000C]

Location CONTENTS

Changing addresses
18

Varying an address whilst a program is running involves specifying the locations


concerned in a register.
From all the general purpose registers BX is the only capable of storing such
addresses.
Thus MOV AX, [CX] is illegal
Whilst MOV CL, [BX] copies the contents of memory location whose address is
specified by BX into the register CL.
And MOV [BX], AL copies the contents of AL in the memory location whose
address is specified in BX

Examples Affecting Memory


19

Consider the checkerboard memory test where a section of memory is filled with
alternate 01010101 and 10101010.
The following program does the checkerboard test on locations 200H-300H
inclusive.

NEXT:

MOV BX,200H
MOV AX,1010101001010101B
MOV [BX],AX
INC BX
CMP BX,300H
JLE NEXT

The Instruction Pointer (IP)


20

The computer keeps track of the next line to


be executed by keeping its address in a special
register called the Instruction Pointer (IP) or
Program Counter.
This register is relative to CS as segment
register and points to the next instruction to
be executed.
The contents of this register is updated with
every instruction executed.
Thus a program is executed sequentially line
by line

START
.
.
.
MOV AX,BX
MOV CX,05H
MOV DX,AX

.
.
.

This is the
line which is
executing

IP

The Stack
21

The Stack is a portion of memory


which, like a stack of plates in a
canteen, is organized on a LastIn-First-Out basis.
Thus the item which was put last
on the stack is the first to be
withdrawn

The Stack Pointer


22

The Stack pointer keeps track of the


position of the last item placed on the
stack (i.e. the Top Of Stack)
SP

The Stack is organized in words, (i.e. two


bytes at a time). Thus the stack pointer is
incremented or decremented by 2.
The Stack Pointer points to the last
occupied locations on the stack

[0000]
[0002]
[0004]
[0006]
[0008]
[000A]
[000C]
[000E]
[0010]
[0012]
[0014]
[0016]
[0018]

Note that on placing items on the


stack the address decreases

PUSH & POP


23

PUSH AX

The two set of instructions which


explicitly modify the stack are the
PUSH (which places items on the
stack) and the POP (which
retrieves items from the stack). In
both cases, the stack pointer is
adjusted accordingly to point
always to the top of stack.
Thus PUSH AX means SP=SP-2
and AX -> [SP]
POP AX means [SP] -> AX and
SP=SP+2.

OLD SP

[0000]
[0002]
[0004]
[0006]
[0008]
[000A]
[000C]
[000E]
[0010]
[0012]
[0014]
[0016]
[0018]

POP AX

OLD SP

[0000]
[0002]
[0004]
[0006]
[0008]
[000A]
[000C]
[000E]
[0010]
[0012]
[0014]
[0016]
[0018]

AX
NEW SP

AX

NEW SP

Subroutines
24

In high-level languages, procedures


make it possible to break a large
program down into smaller pieces so
that each piece can be shown to work
independently. In this way the final
program is built up of a number of
trusty bricks and is easier to debug
because the error is either localized to
one subprogram or its interlinking.
This has also the advantage of reusability of bricks.

START
.
.
.
CALL SUB1
.
.
.

SUB1 PROC
.
.
.
RET

The CALL Mechanism


25

Although at first sight the CALL


and RET mechanism can be
implemented by using two JMPs.
In fact this cannot be done since
the CALL mechanism remembers
the place where it was called from
and returns to the line following it.
Thus this is not a fixed address.

START
.
.
.

SUB1 PROC
.
.
.
RET

CALL SUB1
.
.
.
CALL SUB1
.
.
.

The Return Mechanism


26

When a CALL is encountered the current value of the instruction pointer is pushed
on the stack and the it is filled with the address stated by the call.
Since the fetch cycle goes to search for the instruction pointed at by the instruction
pointer, the program continues its execution from the first statement in the
subroutine.
On encountering the RET instruction the contents of the IP is popped from the stack
thus continuing the execution where it was suspended.
Thus care must be taken to leave the return address intact before leaving a
subroutine. (i.e. a symmetrical number of pushes and pops within the subroutine)

Software Interrupts
27

Software interrupts are like hardware interrupts which are generated by the program
itself. From the interrupt number, the CPU derives the address of the Interrupt service
routine which must be executed.
Software interrupts in assembly language can be treated as calls to subroutines of
other programs which are currently running on the computer.
One of the most famous software interrupt is Interrupt No. 21H, which branches in
the operating system, and permits the use of PC-DOS functions defined there.
The function required to be performed by DOS is specified in AH prior to the the
interrupt.
The functions return and accept values in various registers.
AN interrupt is called using the instruction INT followed by the interrupt number
. For example: INT 21H

Some INT 21H functions


28

Function
Number

Description

Explanation

Keyboard
Input
(echoed)

Waits until a character is typed at the keyboard and then puts the ASCII
code for that character in register AL and echoed to screen

Display
Output

Prints the character whose ASCII code is in DL

Keyboard
Input
(No echo)

Waits until a character is typed at the keyboard and then puts the ASCII
code for that character in register AL and NOT echoed to screen

Display
String

Prints a series of characters stored in memory starting with the one in the
address given in DX (relative to DS).Stop when the ASCII code for $ is
encountered

INT 21H Example


29

Prompt
Song1
Song2

DB
DB
DB

Please enter 1 or 2: ,13D,10D,$


So you think you can tell heaven from hell
Blue Sky is in pain,13D,10D,$

ASK:

MOV
MOV
INT

DX, OFFSET Prompt


AH,09H
21H

MOV
INT

AH,01H
21H

CMP
JE
MOV
MOV
INT

AL,01H
NEXT
DX, OFFSET Song1
AH,09H
21H

GET:

This is only a
program fragment to
illustrate the use of
interrupt 21H For
full details consult the
MASM notes

Addition and Subtraction with carry or


borrow
30

In assembly language there are two versions


of addition and two versions of subtraction.
ADD - Simple addition of two numbers
ADC - Adds two numbers together with
the carry flag
SUB Simple subtraction of two
numbers
SBB Subtracts the second number and
the carry flag (borrow)
This provides a means of adding numbers
greater than 32-bits.
CLC clears the carry for the first digit
addition

CF

CF

0
0

Last
addition in
case of an
outgoing
carry

00 01 98 41 +
00 02 71 64
00 04 70 05

The Compare Instruction


31

The compare instruction does not change the contents of the registers involved but only
sets the flag register accordingly.
The actual operation performed by the compare is a subtraction, leaving the source and
destination registers intact
Consider CMP AX,BX : Flags are set according to the result of subtracting BX from AX:
If AX = BX then the ZF is set to 1
If AX > BX then the ZF is set to 0 and CF is set to 0 too
If AX < BX then we need an external borrow, which is reflected in CF = 1
These flags are tested in the ABOVE or BELOW jumps which test unsigned numbers
The GREATER and LESS jumps are for signed numbers and work on the SF, OF
and the ZF instead

Addressing Modes
32

The addressing modes deal with the source and destination of the data required by
the instruction. This can be either a register or a location in memory, or even a port.
Various addressing modes exist:
Register Addressing
Immediate and Direct Addressing
Indirect Addressing
Indexed Addressing
Based Addressing
Based-Indexed Addressing

Computer Logic II

Register Addressing
33

This addressing mode


involves the contents of
the register directly as
for example:
MOV AX, BX
MOV CL, DL
Note that the IP and
Flags register cannot be
accessed directly by the
programmer

General Purpose

Segment Registers

AX

AH

AL

CS

BX

BH

BL

DS

CX

CH

CL

SS

DX

DH

DL

ES

SI

FLAGS

DI
IP

SP
BP

AX
Ex. MOV AX,BX

BX

Immediate and Direct Addressing


34

In Immediate addressing for example


MOV CL,61H the immediate operand
61H is stored as part of the instruction.
Thus the number 61H is loaded directly in
CL.
Direct addressing is similar except that in
this case the effective address of one of the
operands is taken directly from the
instruction. Thus in MOV AL, [210H] the
contents of location 210H relative to DS is
put in AL

Ex. MOV CL,61H


CL
61H

Ex. MOV AL,[210H]


AL

(DS:210H)

75H

Default Segment Register


35

General Purpose
AX

AH

AL

BX

BH

BL

CX

CH

CL

DX

DH

DL

Note that the default segment register can be changed


using the segment override, i.e. stating the whole
address in the form DS: Offset
Relative to DS by default

NORMALLY

FOR STRINGS

SI

Relative to DS by default

DS

DI

Relative to DS by default

ES

SP

Relative to SS by default

BP

Relative to SS by default

IP

Relative to CS by default

Some other useful Instructions


36

CLC: Clear Carry Flag (CF = 0)


STC: Set Carry Flag (CF = 1)
CMC : Complement Carry Flag (CF = CF)
CBW: Convert Byte to Word
CWD: Convert Word to Double-Word
NEG: Negate (2s Complement)
NOT: Compliment (1s Complement)

Reference Books
37

Programming the 8086/86 for the IBM PC and Compatibles . Michael Thorne
Microprocessors and Interfacing Programming and Hardware Douglas V.Hall
Microsoft Macro Assembler for the MS-DOS Operating Systems Reference
Manual

Das könnte Ihnen auch gefallen