Professional Documents
Culture Documents
PROGRAMMING
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
In situations where a high-level
0010010101010100
language is inappropriate we
1010101010100101
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
This version is
actually loaded and
executed
General Purpose Registers
5
AH AL
There are 4 general
purpose registers in AX
the 8086.
BH BL
They are all 16-bit
registers
Each byte can be BX
addressed individually CH CL
by specifying the High
order or the Low order
byte of the register. CX
DH DL
DX
Some Simple Commands
6
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
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
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 ; Compares the contents of BX with that of AX
JE SAME ; 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
SAME: MOV CX,AX ; 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
OR TEST
Contents of AX = 0000101011100011 Contents of AX = 0000101011100011
Contents of BX = 1001100000100001 Contents of BX = 1001100000100001
Contents of AX = 1001101011100011 Contents of AX = 0000101011100011
after OR AX,BX is executed after TEST AX,BX is executed
Symbol ASCII (Dec) ASCII (Hex) By Making an AND between an ASCII value and 0FH
0 48 30 we can obtain the required number.
1 49 31 Say we AND 33H = 00110011B
2 50 32 with 0FH = 00001111B
3 51 33
We obtain = 00000011B (3)
4 52 34
5 53 35 By Making an OR between a number value and 30H we
6 54 36 can obtain its ASCII code.
7 55 37 Say we OR 05H = 00000101B
8 56 38 with 30H = 00110101B
We obtain = 00110101B
9 57 39
(ASCII value for 5)
Masking
16
0
SHL/SAL CF
MSB
MSB
0
CF CF
MSB
SHR SAR
Shift Examples
18
The rotate is similar to the shift with the exception that the outgoing bit is not
lost but rotated back into the shifted register.
An Alternative is to rotate through carry, which includes the carry in the rotation
process.
ROR ROL
MSB LSB
CF CF
RCR RCL
MSB LSB
CF CF
Rotates Examples
20
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
23
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.
MOV BX,200H
MOV AX,1010101001010101B
NEXT: MOV [BX],AX
INC BX
CMP BX,300H
JLE NEXT
The DS Register
24
The 8086 can address a total of 1 Megabyte. Rather than representing each address as
a 20-bit unsigned number, memory is thought of as being divided uo into segments
each of which contains 216 locations.
In this way an address can be thought of as consisting of two parts:
a 16-bit segment address and
Thus , 020A:1BCD denotes offset 1BCDH from the start of segment 020AH.
Effective Address
25
[0000]
[0002]
The Stack pointer keeps track of the [0004]
position of the last item placed on the [0006]
[0008]
stack (i.e. the Top Of Stack) [000A]
[000C]
SP [000E]
[0010]
[0012]
[0014]
The Stack is organized in words, (i.e. two [0016]
bytes at a time). Thus the stack pointer is [0018]
incremented or decremented by 2. Note that on placing items on the
The Stack Pointer points to the last stack the address decreases
occupied locations on the stack
PUSH & POP
29
PUSH AX AX
The two set of instructions which [0000]
[0002]
[0004]
explicitly modify the stack are the [0006]
[0008] NEW SP
PUSH (which places items on the OLD SP [000A]
[000C]
stack) and the POP (which [000E]
[0010]
retrieves items from the stack). In [0012]
[0014]
both cases, the stack pointer is [0016]
[0018]
adjusted accordingly to point
POP AX
always to the top of stack. [0000]
AX
[0002]
Thus PUSH AX means SP=SP-2 [0004]
[0006]
and AX -> [SP] [0008]
[000A]
OLD SP NEW SP
[000C]
POP AX means [SP] -> AX and [000E]
[0010]
SP=SP+2. [0012]
[0014]
[0016]
[0018]
Subroutines
30
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)
NEAR and FAR
33
The easiest way to pass a parameter to and fro a subprogram is by the use of the
general purpose registers.
MOV BX,4 ;Assign first parameter
MOV CX,7 ;Assign second parameter
CALL ADDITION ;Call their addition
MOV [RES],AX ;Store the result returned
Parameters on STACK
35
[BP+6]
Items on the
stack prior
to CALL
Discarding Parameters after CALL
38
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
41
8 Keyboard Waits until a character is typed at the keyboard and then puts the ASCII
Input code for that character in register AL and NOT echoed to screen
(No echo)
9 Display Prints a series of characters stored in memory starting with the one in the
String address given in DX (relative to DS).Stop when the ASCII code for $ is
encountered
INT 21H Example
42
3 7 KEYS
One of the disadvantage of reading numbers as a
series of ASCII codes for digits is that, before any
arithmetic can be performed on such numbers, 33H 37H ASCII
their binary equivalents have to be calculated.
Thus, if the decimal number 37 was typed at the
keyboard, the ASCII codes for 3 and 7 (33H and 3 7 BINARY
37H) would have to be converted to their binary x10
equivalents, and then the binary equivalents, and
then the binary equivalent of the first digit 30
multiplied by ten and added to the second. +
BCD is a way of representing numbers which
avoids the need for conversions of this sort.
37
Binary Coded Decimal (BCD)
44
BCD Binary
BCD is a way of representing numbers which avoids the
need of a lot of conversions. 0 0000
The principle used is to encode each decimal digit separately 1 0001
in their unsigned 4-bit equivalents. 2 0010
Since the computer memory is organized in bytes of 8 bits, 3 0011
we can represent BCD digits as: 4 0100
Packed BCD : where two digits are packed in a byte Ex. 5 0101
37 = 0011 0111 6 0110
Unpacked BCD: where each digit is expanded on 8 bits. 7 0111
Ex 37 = 00000011 000001111 8 1000
9 1001
Arithmetic Operations Binary BCD
45
0000 0
0001 1
0010 2
At first sight we can see that there are 6 unused 0011 3
binary patterns in BCD corresponding to the hex 0100 4
6 letter digits A, B, C, D, E and F. 0101 5
Thus this system is less compact. 0110 6
0111 7
In the arithmetic operations it also involves some
1000 8
complications if the answer contains any of the 1001 9
bit patterns not represented in BCD 1010 Unused
Not all is lost however, additional instructions 1011 Unused
exist to overcome this problem 1100 Unused
1101 Unused
1110 Unused
1111 Unused
BCD Addition
46
24 0010 0100 -
If the subtraction doesnt produce a result which passes 13 0001 0011
through the forbidden range or no borrow is required no 11 0001 0001 OK
problem arises.
If however the result produced contains a forbidden digit, it 35 0011 0101 -
has to be adjusted. 16 0001 0110
This is done by subtracting another 6 to the result to 1? 0001 1111 - NOT OK
overcome the forbidden range. 06 0000 0110 ADJUST
This adjustment is done automatically using the DAS 19 0001 1001
instruction for packed and the AAS instruction for
unpacked BCD 1 AUXILIARY
21 0010 0001 -
The Auxiliary carry here become a borrow and the result
19 0001 1001
must be adjusted whenever this carry is set since the borrow
08 0000 1000 - BORROW
is worth 16 not 10.
06 0000 0110 ADJUST
02 0000 0010
Example 1 : Addition
48
MOV AL,00H
MOV [DI],AL ;Set previous carry to zero
NEXT: MOV AL,[SI] ;Get digit of Multiplicand
MUL DL ;Multiply by Multiplier
AAM ;ASCII adjust digit
ADD AL,[DI] ;Add previous carry
AAA ;ASCII adjust
MOV [DI],AL ;Write down Digit
INC DI ;point to next digits
INC SI
MOV [DI],AH ;Store previous carry
DEC CX ;Check if ready
JNZ NEXT
The Compare Instruction
54
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
55
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
56
AX BX
Ex. MOV AX,BX
Immediate and Direct Addressing
57
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
63
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