Beruflich Dokumente
Kultur Dokumente
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
Assembly Language
3
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
Pascal Program
Compiler
Assembler language Program
Assembler
Machine Code Program
This version is
actually loaded and
executed
AH
AL
AX
BH
BL
BX
CH
CL
CX
DH
DL
DX
MOV AX,3
ADD AX,2
MOV BX,AX
INC CX
DEC DX
SUB AX,4
MUL BX
DIV BX
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
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
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
MOV CX, AX
SUB AX,BX
JZ MAKE1
MAKE1:
RESET:
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
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
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
(3)
= 00000101B
= 00110101B
= 00110101B
value for 5)
Masking
16
Location ADDRESS
[0000]
[0001]
[0002]
[0003]
[0004]
[0005]
[0006]
[0007]
[0008]
[0009]
[000A]
[000B]
[000C]
Location CONTENTS
Changing addresses
18
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
START
.
.
.
MOV AX,BX
MOV CX,05H
MOV DX,AX
.
.
.
This is the
line which is
executing
IP
The Stack
21
[0000]
[0002]
[0004]
[0006]
[0008]
[000A]
[000C]
[000E]
[0010]
[0012]
[0014]
[0016]
[0018]
PUSH AX
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
START
.
.
.
CALL SUB1
.
.
.
SUB1 PROC
.
.
.
RET
START
.
.
.
SUB1 PROC
.
.
.
RET
CALL SUB1
.
.
.
CALL SUB1
.
.
.
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
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
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
Prompt
Song1
Song2
DB
DB
DB
ASK:
MOV
MOV
INT
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
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 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
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
(DS:210H)
75H
General Purpose
AX
AH
AL
BX
BH
BL
CX
CH
CL
DX
DH
DL
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
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