Assembly Language Programming II
Instruction set
In order to write an assembly language program for a microprocessor, you must understand the instructions supported by the device. Different microprocessors may support different operations, however, the basic concept of assembly language programming is very similar. Moreover, the syntax for the instruction is more or less the same. Therefore, once you learn assembly language programming for one microprocessor, then you can easily write programs for other microprocessors.
For example, the move operation (syntax MOV) can be found in the Intel microprocessors, and the ADuC832 microcontroller. In PowerPC, there is the lwz, which stands for the load operation. Other processors may use the load instruction.
The 8086 instruction set is relatively simple and it has a total of 117 basic instructions. If you understand all basic instructions then you can fully control the 8086. Moreover, since Intel processors are upward compatible so your assembly language program can run on all Intel processor including the P5 or Core2 Duo!!!!
The instructions are divided into 5 groups:
Data transfer – move
Arithmetic – addition, subtraction, multiply, divide
Logic – AND, OR, XOR
Shift
Rotate
We will go through the different groups one by one, however, for details descriptions, you should refer to the document provided by the emu8086 software or a proper reference.
Data transfer instructions
The main purpose of the data transfer instructions is to move data either between its internal registers or between an internal register and a storage location in memory. The most frequently used data transfer instruction is the move operation, mnemonic MOV.
Syntax: MOV destination, source ; move data from source to destination
The data being moved could be 16bit or 8bit
The system does not support moving data from one memory location to another memory location. When the segment register is used in a data transfer operation then the move instruction must be applied to a 16bit data.
For example, Mov SS, data
where SS represents a segment register, such as DS, ES, then data must be a 16bit value.
Mnemonic

Meaning

Format

Operation

Flags affected

Mov

Move

mov D,S

(S) to (D)

none

Destination (D)

Source (S)


Memory

AX


AX

Memory


Register

Register


Register

Memory


Memory

Register


Register

Immediate


Memory

Immediate


Segreg

Reg16


Segreg

Mem16


Reg16

Segreg


Mem16

Segreg


The above table shows the syntax for the MOV instruction and the possible combinations for the source and destination.
Example:
MOV AL, BL ; register to register
MOV AL, #16 ; immediate (a value 16) to register
Special data transfer instructions
Exchange data between the source and destination – XCHG
Syntax : XCHG AX, DX ; value in DX is stored in AX and value in AX stored in DX
Load effective address (LEA) is a frequently used instruction for saving the offset address of a variable. This is similar to x = &y in C++ programming!
Syntax: LEA SI, INPUT ; effective address of INPUT will be stored in SI register
INPUT in the above example represents a variable
Since an offset address is stored so the destination must be a 16bit register! An offset address is a 16bit value!!!
LEA usually is used to access data defined in the program, as shown below.
Example
LEA BX, dat
MOV AL, [BX+2]
What is inside AL if dat is defined as the following?
dat DB 11h, 22h, 33h, 44h, 55h
Load register and DS (LDS)
As its name implies, LDS will load a specified register and the DS register in a single operation!
Syntax: LDS SI, [200] ; 4 bytes of data will be fetched, the first 2 bytes (location 200 and 201) stored in SI and the following 2 bytes (locations 202 and 203) stored in DS.
There is also the load register and ES (LES). Similar to LDS, LES will load a specific register and the ES register.
Arithmetic instructions
Table 1 depicts arithmetic instructions available in the 8086.
Some examples of available arithmetic operations: add, subtract, multiply, etc.
The operations can apply to unsigned value, signed value, binary bytes, words, unpacked, packed decimal bytes, or even ASCII numbers.
Packed decimal – two BCD digits are packed into a byte register or memory location. Each BCD is 4 bits representing digit 0 to 9.
Unpacked decimal numbers are stored one BCD digit per byte
After an arithmetic operation, the flags (such as Carry, Sign, Overflow, Zero) are updated accordingly. Based on the flag status, different operations can be performed, similar to an IF ELSE syntax.
Table 1 Arithmetic operation of 8086
ADD

Add byte or word

ADC

Add byte or word with carry

Inc

Increment byte or word by 1

AAA

ASCII adjust for addition

DAA

Decimal adjust for addition

Sub

Subtract byte or word

Sbb

Subtract byte or word with borrow

DEC

Decrement byte or word by 1

NEG

Negate byte or word

AAS

ASCII adjust for subtraction

DAS

Decimal adjust for subtraction

MUL

Multiply byte or word unsigned

IMUL

Integer multiply byte or word

AAM

ASCII adjust for multiply

Div

Divide byte or word unsigned

Idiv

Integer divide byte or word

AAD

ASCII adjust for division

CBW

Convert byte to word

CWD

Convert word to doubleword

Addition related operations
Mnemonic

Meaning

Format

Operation

Flags affected

Add

Addition

Add D, S

D = S+D
CF = carry

OF, SF, ZF, AF, PF, CF

ADC

Add with carry

ADC D, S

D = S+D+CF
CF = carry

OF, SF, ZF, AF, PF, CF

INC

Increment by 1

INC D

D = D+1

OF, SF, ZF, AF, PF

AAA

ASCII adjust for addition

AAA


AF, CF, OF, SF, ZF, PF undefined

DAA

Decimal adjust for addition

DAA


SF, ZF, AF, PF, CF, OF undefined

Destination

Source

Register

Register

Register

Memory

Memory

Register

Register

Immediate

Memory

Immediate

AX

Immediate

Example
AL = 12H, BL = 70H
Carry = 1
ADC AL, BL
Then result is in AL = 83H
The carry is only 1bit and it is being added to the LSB (least significant bit).
Example
BX = 1234 CX= 0123
Carry is 0
SBB BX, CX ; subtract with borrow
Give 1111
Subtraction
Mnemonic

Meaning

Format

Operation

Flags affected

Sub

Subtract

Sub D,S

D = DS
CF = borrow

OF, SF, ZF, AF, PF, CF

Sbb

Subtract with borrow

SBB D,S

D = DSCF

OF, SF, ZF, AF, PF, CF

Dec

Decrement by 1

DEC D

D=D1

OF, SF, ZF, AF, PF

Neg

Negate

Neg D

D=0D
CF =1

OF, SF, ZF, AF, PF, CF

DAS

Decimal adjust for subtraction

DAS


OF, SF, ZF, AF, PF, CF undefined

AAS

ASCII adjust for subtraction

AAS


OF, SF, ZF, AF, PF, CF undefined

Destination

Source

Register

Register

Register

Memory

Memory

Register

AX

Immediate

Register

Immediate

Memory

immediate

If BX = 3A
NEG BX
Give FFC6
NEG – make the operand negative (based on 2’s complement)
Executing the NEG instruction causes the following
2’s complement subtraction
00  (BX) = 0000 + 2’s complement of 3A
= 0000 + FFC6
= FFC6
Multiplication
When multiplying 8bit data, result is 16bit and stored in AX (a 16bit register).
When multiplying two 16bit data, result is 32bit and result stored in AX and DX. The AX holds the LSW (Least significant word) and DX stores the MSW (Most significant word). Only the source operand is specified in the multiply operation and the other operand must be stored in AL, or AX. This also applies in the division operation.
In 8bit division, the result is stored in AH and AL. Where AH is the remainder and AL is the quotient.
For 16bit division, AX contains the quotient and DX contains the remainder
If AL is –1 and CL is –2, what will be the result produced in AX for
MUL CL ; this is CL * AL (unsigned)
IMUL CL ; this is CL*AL (signed)
MUL CL is unsigned multiply = FD02 (1 = FFH; 2 = FEH so FFxFE = FD02H)
IMUL CL is signed multiply = 2 (1*2 = 2)
Special functions
CBW – convert byte (8bit) to word (16bit). So what is the additional 8 bits?
CBW – fill AH with 0 if AL is positive; fill AH with 1s if AL is negative then AH becomes FF. In doing so, the sign of the original data will be retained.
CWD – convert word (16bit) to double word (32bit) and the DX register is used to store the high order word.
CWD – fill DX with 0 if AL is positive; fill DX with 1s if AX is negative then DX becomes FF
Both CBW and CWD do not require operand, the data must be stored in AL or AX.
Use CBW when doing 8bit operation, AX is used by default.
Use CWD when doing 16bit operation, AX and DX are used by default.
What is the result of executing the following sequence of instructions?
MOV AL, A1H
CBW
CWD
Ans.
AX = FFA1 after CBW ; AL 8bit converts to 16bit AX
DX = FFFF after CWD ; AX is 16bit converts to 32bit (DX + AX)
Multiplication and division functions
Mnemonic

Meaning

Format

Operation

Flags affected

MUL

Multiply
(Unsigned)

MUL S

AX = AL*S8
DX,AX = AX*S16

OF, CF depends on result
S, Z, A, P undefined

DIV

Division
(unsigned)

DIV S

AL = Q (AX/S8)
AH = R(AX/S8)

OF, CF, SF, ZF, AF, PF all undefined

IMUL

Integer multiply
(signed)

IMUL S

AX = AL*S8
DX,AX=AX*S16

OF, CF depends on result
S, Z, A, P undefined

IDIV

Integer divide
(signed)

IDIV S

AX = Q(AX/S8)
AH=R(AX/S8)
AX = Q(DX,AX)/S16
DX=R(DX,AX)/S16
If Q is positive and exceeds 7FFF or if Q is negative and becomes less than 8001 then type 0 interrupt occurs

OF, CF, SF, ZF, AF, PF all undefined

Multiplication related functions
Mnemonic

Meaning

Format

Operation

Flags affected

AAM

Adjust AL for multiplication
Ascii adjust after Multiplication for BCD values

AAM

AH=Q(AL/10)
AL=R(AL/10)

SF, ZF, PF, OF, AF, CF undefined

AAD

Adjust AX for division
Prepare 2 BCD values for division

AAD

AL=(AH*10+AL)
AH =00

SF, ZF, PF, OF, AF, CF undefined

CBW

Convert byte to word

CBW

All bits of AH = (MSB of AL)

None

CWD

Convert word to double word

CWD

All bits of DX = MSB of AX

none

A simple application of assembly language programming
Try to write a simple assembly language program to convert from Centigrade (Celsius) to Fahrenheit assuming result is an integer.

The equation for the conversion is

f = c * 9 / 5 + 32

What operations are required?

Do you need to define variables?

Sample is available in the ftp site
Logic instructions
Logic operations include: AND, OR, exclusiveOR (XOR) and NOT
Eg AND AX, BX
The above is applying logical AND to value in AX and value in BX. Result is stored in AX. The bits in AX and BX are logical ANDed bitbybit.
Example:
MOV AL, 10101100B
AND AL, 11110000B ; bit by bit operation
AL

1

0

1

0

1

1

0

0


1

1

1

1

0

0

0

0

AND

1

0

1

0

0

0

0

0

Describe the result of executing the following:
MOV AL, 01010101B
AND AL, 00011111B (AL = 00010101B)
OR AL, 11000000B (AL = 11010101B)
XOR AL, 00001111B (AL = 11011010B)
NOT AL (AL = 00100101B)
Ans. AL = 00100101
Shift instructions
As its name implies, shift instructions are for shifting the bit(s) out of the operand. So what will happen to the bit shifted out? What will happen to the vacated bit position?
There are 4 shift operations
Can perform: logical shift and arithmetic shift
Can shift left as well as right
Logical shift – the vacated bit will be filled with ‘0’ and the bit shifted out will be stored in the Carry flag.
Arithmetic shift – the vacated bit will be filled with the original most significant bit (this only applies in right shift, in left shift vacated bit is filled with ‘0’ as well.)
Arithmetic shift will maintain the ‘sign’ of the original data
If shift more than 1 bit out then the number of shift operation to be performed is stored in the CL register.
Shift operations are important because for a binary pattern, shifting 1bit to the left is equivalent to multiply the value by 2. For example, if the original value is 1, shifting 1 bit to the left, the value becomes 2!!!!
Original

0

0

0

0

0

0

0

1

Shifted 1 bit left

0

0

0

0

0

0

1

0

On the other hand, shift to the right is the same as divide the value by 2. For example, if the original value is 2 then shift to the right by 1 bit, the value becomes 1.
Shift operations can be used in multiply and divide operations.
Mnemonic

Meaning

Format

Operation

Flags affected

SAL/SHL

Shift arithmetic left/shift logical left

SAL/SHL D, Count

Shift the (D) left by the number of bit positions equal to count and fill the vacated bits positions on the right with zeros

C, O depends on result
OF = 0 if first operand keeps original sign

SHR

Shift logical right

SHR D, Count

Shift the (D) right by the number of bit positions equal to count and fill the vacated bits positions on the left with zeros

C, O depends on result
OF = 0 if first operand keeps original sign

SAR

Shift arithmetic right

SAR D, Count

Shift the (D) right by the number of bit positions equal to count and fill the vacated bits positions on the left with the original most significant bit

C, O depends on result
OF = 0 if first operand keeps original sign

Shift left
Example
Mov AL, 10001100b
MOV CL, #1
SHR AL, CL ; shift right 1 time
Result = 0100 0110 Carry = 0
Mov AL, 10001100b
Mov CL, #1
SAL AL, CL ; arithmetic shift right 1 time
Result 1100 0110b Carry = 0
The CL register holds the number of shift to be performed.
Rotate instructions
Rotate is similar to shift but the vacate bit is filled with the bit rotated out from the other side.
Rotate left through carry (RCL) – bit rotated out is stored in the carry flag, the vacated bit is filled by the carry flag
Eg RCR BX, CL ; rotate right thro’ carry 4 times
If CL = 04H and BX = 1234H what is the result of the above operation?
The value of CL in the above example represents the number of the rotate operations.
So the rotate 4 times
1000 0001 0010 0011 if C is 0 at the beginning
Rotate left thro’ carry
OF=0 if first operand keeps original sign.
Rotate operations
Mnemonic

Meaning

Format

Operation

Flags affected

ROL

Rotate left

ROL D, Count

Rotate the D left by the number of bit positions equal to Count. Each bit shifted out from the leftmost bit goes back into the rightmost bit position

C, O depends on result
OF = 0 if first operand keeps original sign

ROR

Rotate
Right

ROR D, Count

Rotate the D right by the number of bit positions equal to Count. Each bit shifted out from the rightmost bit goes back into the leftmost bit position

C, O depends on result
OF = 0 if first operand keeps original sign

RCL

Rotate left thro’ carry

RCL D, Count

Same as ROL except carry is attached to D for rotation

C, O depends on result
OF = 0 if first operand keeps original sign

RCR

Rotate right thro’ carry

RCR D, Count

Same as ROL except carry is attached to D for rotation

C, O depends on result
OF = 0 if first operand keeps original sign

Exercise

How would the decimal number 1234 be coded in ASCII and stored in memory starting at address 0C000H? Assume that least significant digit is stored at the lower addressed memory location
Ans. 1 = (31H in 0C003H) 2 = (32H in 0C002H) 3 = (33H in 0C001H) 4 = (34H in 0C000H)
2. If register BX contains the value 0100_{H}, register DI contains 0010 and register DS contains 1075, what physical memory location is swapped when the following instruction is executed
XCHG [BX+DI], AX
PA = 10750+0100+0010 = 10860H
3. Compute the physical address for the specified operand in each of the following instructions:
MOV [DI], AX (destination operand) (0B000+0200 = 0B200)
MOV DI, [SI] (source operand) (0B000+0100 = 0B100)
MOV DI+XYZ, AH (destination operand) (0B000+0200+0400 = 0B600)
Given CS=0A00, DS=0B00, SI=0100, DI=0200, BX=0300, XYZ=0400
Share with your friends: 