Você está na página 1de 35

CSE-221 Microprocessor and Interfacing

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Converting Assembly Language
Instructions to Machine Code
• An instruction can be coded with 1 to 6 bytes
• Byte 1 contains three kinds of information
– Opcode field (6 bits) specifies the operation (add, subtract, move)
– Register Direction Bit (D bit) Tells the register operand in REG field in
byte 2 is source or destination operand
1: destination 0: source
- Data Size Bit (W bit) Specifies whether the operation will be performed
on 8-bit or 16-bit data
0: 8 bits 1: 16 bits

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Converting Assembly Language Instructions to
Machine Code(REG MODE)
• Byte 2 has three fields
– Mode field (MOD)
– Register field (REG) used to identify the register for the first
operand
– Register/memory field (R/M field)

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Converting Assembly Language Instructions to
Machine Code(MEM MODE)
R/M \ MOD 00 01 10
000 BX+SI BX+SI+D8 BX+SI+D16
001 BX+DI BX+DI+D8 BX+SI+D16
010 BP+SI BP+SI+D8 BP+SI+D16
011 BP+DI BP+DI+D8 BP+DI+D16
100 SI SI+D8 SI+D16
101 DI DI+D8 DI+D16
110 D16(DIR BP+D8 BP+D16
ECT
ADD)
111 BX BX+D8 BX+D16

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Problem

• MOV CL,[BX]
– Copy a byte to CL from memory location whose EA is
contained in BX.
– M.A  BX + DS
– 6-bit code 100010
– D1 ; data being moved to reg CL
– W0; Byte
– 3 bit code  Reg CL  001
– MOD 00; R/M  111
– Assembling all of these bits together 1000101000001111

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Problems
• MOV 43H[SI], DH
– 1000100001110100

• Suppose that DS=1000H,


SS=2000H,BP=1000H,DI=0100H.Determine the memory address
accessed by each of the following instruction
– MOV AL, [BP +DI]
– MOV CX,[DI]
– MOV DX,[BP]

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Instruction Set of 8086

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Instruction Set of 8086
• 8086 Instructions can be classified into
following six groups.
– Data transfer instructions
– Arithmetic instructions
– Logical instructions
– Flag manipulation
– Control transfer instructions
– String manipulating instructions
– Processor control instructions
SCSE VIT UNIVERSITY A place to learn; A chance to grow
Instruction Set of 8086
• Data transfer group
– Instructions for moving data b/w reg, reg & mem, reg & stack
mem, acc & I/O device
• Arithmetic group
– Inst for add and sub of binary, BCD and ASCII data.
– Inst for mul and div of signed and unsigned binary data.
• Logical group
– Ins for performing logical operations like AND, OR, X-OR,
Complement, Shift, Rotate, etc.,

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Instruction Set of 8086
• String manipulation group
– Ins for moving string data b/w two memory locations,
comparing string data w by w or B by B
• Control transfer group
– Ins to call a procedure/Subroutine in the main program
– Ins to jump from one part to another part-Program either
conditionally(aft checking flag) or un-c(without checking flag)
• Processor control group
– Ins to set/clear the flags, to delay and halt the processor
execution.

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Instruction format
• 1 byte ins- Implied operand or reg mode
• 2 byte ins- Reg to/from mem/reg with no dis
• 3 byte ins-Reg to/from memory with 8-bit dis/d
• 4 byte ins-Reg to/from memory with 16-bit dis or 16-bit
immediate data to reg/memory
• 5 byte ins-Immediate 8-bit data to memory with 16-bit
displacement
• 6 byte ins-Immediate 16-bit data to memory with 16-bit
displacement

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Data Transfer Instructions

• MOV, PUSH, POP, XCHG, XLAT, IN, OUT, LEA,


LDS, LES, LSS, LAHF and SAHF
• MOV- Copies a word or byte of data from a specified
source to as specified destination

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Data Transfer Instructions

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Data Transfer Instructions
• PUSH
– Used to store the word in a register or a memory location into
the stack
– SP is decremented by two after the execution of PUSH
• Ex: PUSH CX ; PUSH the content of CX into the stack
PUSH [BX] ; PUSH the word in the memory at [BX]
into the stack
• POP
– It copies the top word from the stack to a dest specified in the
instruction
– Dest can be a GPR, SR or a memory location
– After word is copied to the specified dest; SP Incremented by 2
• Ex: POP BX ; POP the content of BX from the stack
SCSE VIT UNIVERSITY A place to learn; A chance to grow
Data Transfer Instructions

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Data Transfer Instructions
• XLAT: Translate a byte in AL from one code
to another code.

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Data Transfer Instructions
• IN: Copies data from a port to the AL or AX
reg.
– Fixed port type, Variable port type
– Fixed : 8-bit address of a port is specified directly
• Ex: IN AL, 80H ;Input a byte from port with addr 80HAL
– Variable: Port addr is loaded into the DX reg
• DX  16 bit reg  0000H to FFFFH.
• Ex: IN AX, [DX]; I/P a word from the 16-bit port with port
address in DX.

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Data Transfer Instructions
• OUT:
– AL  Specified Port
– Fixed port Eg: OUT 48H, AL ;Content of AL add
– Variable port Eg: OUT [DX], AX;
• LEA(Load effective address)
– General Format LEA reg, source
– Ex: LEA CX, [BX] [SI]; Load CX with the value equal to (BX)
+ (SI) represent the content of BX & SI

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Data Transfer Instructions
• LDS: It loads the register and DS with words from the memory.
– Format  LDS reg, memory addr of first word
– It is useful in initializing the SI and DS reg at the start of a
string
– Eg: LDS SI, [2000H]
• LES and LSS: LES and LSS inst are similar to the LDS inst,
except that instead of DS reg, the ES and SS reg are loaded along
with the reg specified in the instruction.
• LAHF : Copies the lower-order byte of the flag reg into AH
• SAHF: Stores the content of AH in the lower-order byte of flag reg
– Except the SAHF & POPF instructions, no other data transfer instructions
affects the flag register.

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Arithmetic Instructions

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Arithmetic Instructions
Ex.1 ADD AX,2
ADC AX,2
STC ; set CF =1;
MOV Al,5 ; Al=5
ADC Al,1 ; Al=7
RET
Ex.2 INC BX; Increment the content of BX by 1

Note: AAA inst must always follow the addition of two unpacked
BCD operands in AL.

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Arithmetic Instructions
Ex.3 ASCII Adjust after Addition. (AL=05(decimal);BH=06(decimal))
Corrects result in AH and AL after addition when working with BCD
values.
It works according to the following Algorithm:
if low nibble of AL > 9 or AF = 1 then: When this result is to be sent to the
terminal(printer), the ASCII Code
of each decimal digit is easily
found by adding 30H to each byte.
AL = AL + 6
AH = AH + 1
AF = 1
CF = 1
else AF = 0; CF = 0 in both cases:
clear the high nibble of AL. (Addition of 5 and 6 gives a decimal result of 11,
which is equal to 0101H in unpacked BCD form, stored in AX)

Example:MOV AX, 15 ; AH = 00, AL = 0Fh


AAA ; AH = 01, AL = 05
SCSE RET VIT UNIVERSITYA place to learn; A chance to grow
Arithmetic Instructions
Ex.4
Decimal adjust After Addition.
Corrects the result of addition of two packed BCD values.
Algorithm:
if low nibble of AL > 9 or AF = 1 then:
AL = AL + 6; AF = 1
if AL > 9Fh or CF = 1 then:AL = AL + 60h CF = 1
Example:MOV AL, 0Fh ; AL = 0Fh (15)
DAA ; AL = 15h
RET
Problem: AL=10001000=88BCD
CL=01001001=49BCD
DAA ins affects AF,CF,PF,ZF only

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Arithmetic Instructions

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Arithmetic Instructions

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Arithmetic Instructions
• MUL: Multiplies an unsigned byte from some source an unsigned
byte in AL reg or AX reg
• If the MSB of a 16-bit or MSW of a 32-bit is 0, the CF and OF = 0.
• AF,PF,SF,ZF are undefined after a MUL inst.
• Multiply byte by word-Extend reg-Fill upper byte 0’s.
• Ex: MUL BH ; AL times BH, result in AX
– MUL CX; AX times CX, result high word in DX
; Low word in AX

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Arithmetic Instructions
• IMUL inst
– Multiplies a signed byte from source times a signed byte in AL
or AX
– If the upper byte of a 16-bit result or the upper word of a 32-bit
result contains only copies of the sign bit(all 0’s or all 1’s) then
CF & OF both be 0’s
– Or it contains part of the product, the CF & OF will both be 1’s.
– Multiply a signed byte by a signed word, first move the byte
into a word location and fill upper byte of the word with copies
of the sign bit. CBW, to do this.(Extend sign bit of AL into AH)

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Arithmetic Instructions
• IMUL inst
• ;AL = 01000101 ; 69 decimal
• ;BL = 00001110 ;14 decimal
• IMUL BL; AX = +966 decimal
• ; AX = 0000001111 000 110
• ;MSB = 0  Positive result
• ; Magnitude in true form
• ; SF = 0 ; CF = 1; OF = 1.

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Arithmetic Instructions
• DIV inst
– Divide an unsigned word by a byte
– The word must be in AX reg. After division, the AL will contain an 8-bit
result (quotient) and AH will contain 8-bit remainder
– Divide by 0, or quotient is too large to fit in AL(greater than FFH), the 8086
automatically do a type 0 interrupt.
– Double word divided by a word, the MSW of the double word must be in
DX and LSW must be in AX. After div, AX contain 16 bit q, DX-16 bit Rem
– Ex: DIV BL; word in AX/byte in BL; Quo in AL, Rem in AH

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Arithmetic Instructions

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Arithmetic Instructions
• IDIV inst
– Divide a signed word by a signed byte
– Sign of the remainder will be the same as the sign of the dividend
– Divide by 0, quo is greater than 127(7FH) or less than -127(81H)-Type 0
– Ex: 939/-45

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Arithmetic Instructions
• IDIV inst
• Ex 1: Signed word divided by a signed byte
– AX = 939 decimal = 0000001110101011b = 03ABH
– BL = -45 decimal = -2DH = D3H = 11010011b
– IDIV BL ; Quotient in AL = 11101100
; AL = ECH = -14 H = -20 decimal
; Remainder in AH = 00100111
; AH = 27H = +39 decimal
Note: Sign of the remainder will be the same as the sign of the dividend.

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Arithmetic Instructions
• IDIV inst
• Ex 2: Signed byte divided by a signed byte
– AL = -38decimal = -26 H = 11001010b
– CH = +3 decimal = +3 H = 00000011b
• CBW ; Extend sign of AL through AH
• ; AX = 11111111 11001010
– IDIV CH ; Divide AX by CH
– ; AL = 11110100 = -0CH = -12 decimal
– ; AH = 11111110 = -2H = -2 decimal
– Note: Sign of the remainder will be the same as the sign of the dividend.
– AAD – ASCII adjust before the division
– AAM -ASCII adjust AX after multiplication

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Logical Instructions

SCSE VIT UNIVERSITY A place to learn; A chance to grow


Logical Instructions
• TEST instruction
– Instruction ANDs the content of a source byte or word with the
content of specified dest byte or wo
– Flags are updated, but neither operand is changed
– TEST inst is often used to set flags before a conditional jump
inst
– Format TEST dest, src
– Ex: AL = 0111 1111 =7FH
– TEST AL, 80H ; AL = 7FH(unchanged); ZF=PF=1;SF = 0

SCSE VIT UNIVERSITY A place to learn; A chance to grow

Você também pode gostar