Upload
arvind-shrivastava
View
175
Download
8
Tags:
Embed Size (px)
DESCRIPTION
Unit 2 of CS601 RGPV
Citation preview
CS601: Microprocessor & Interfacing : UNIT 2
X86 Programming model• Instruction Set Architecture (ISA) : the definition of the
registers and instructions that define the programmer’s view of a processor
• Can have different implementations of the same ISA– Intel and AMD processors both execute the x86 ISA, but internally are
very different!!– Intel has several different implementations of the x86 ISA!
• New versions of an ISA extend the previous version by adding instructions, registers – but never invalidate the old ISA
2/27/2010 Arvind Shrivastava 2
Intel x86 Microprocessors8086 - 20 bit Addr. Bus - 1MB of Memory
80286 - 24 Addr. Bus - Added Protection Mode
80386 - 32 bit regs/busses - Virtual 86 Mode
80486 - RISC Core - L1 Cache - FPU
Pentium - Superscalar - Dual Pipeline - Split L1 Cache
Pentium Pro - L2 Cache - Branch Pred. - Speculative Exec.
Pentium MMX - 57 Instructions - Integrated DSP (MMX)
Pentium II - 100 MHz Bus - L2 Cache - MMX
Celeron - 66 MHz Bus - True L2 Cache Integration
Pentium III - 100 MHz Bus - 70 Internet Streaming SIMD
Ext.2/27/2010 Arvind Shrivastava 3
8086 Register File
2/27/2010 Arvind Shrivastava 4
CSDSSSES
AHBHCHDH
ALBLCLDL
IPSPBPSIDI
07
015
07
015
Accumulator
Base
Counter
Data
Code Segment
Data Segment
Stack Segment
Extra Segment
Instruction Pointer
Stack Pointer
Base Pointer
Source Index
Destination Index
}
}}
AXBXCXDX
8086 Register File Contd..
• IP Contains Address of NEXT Instruction to be Fetched– Automatically Incremented– Programmer can control with jump and branch
2/27/2010 Arvind Shrivastava 5
015
Instruction Pointer Register
AX, BX, CX, DX
2/27/2010 Arvind Shrivastava 6
• General Purpose Registers
AHBHCHDH
ALBLCLDL
07 07
Accumulator
Base
Counter
Data
AXBXCXDX
• Can Be Used Separately as 1-byte Registers
AX = AH:AL• Temporary Storage to Avoid Memory Access
– Faster Execution – Avoids Memory Access
• Some Special uses for Certain Instructions
AX, BX, CX, DX - Some Specialized Uses
2/27/2010 Arvind Shrivastava 7
• AX, Accumulator
– Main Register for Performing Arithmetic
– mult/div must use AH, AL
– “accumulator” Means Register with Simple ALU
• BX, Base
– Point to Translation Table in Memory
– Holds Memory Offsets; Function Calls
• CX, Counter
– Index Counter for Loop Control
• DX, Data
– After Integer Division Execution - Holds Remainder
CS, DS, ES, SS - Segment Registers
2/27/2010 Arvind Shrivastava 8
Contains “Base Value” for Memory Address
CS, Code SegmentUsed to “point” to InstructionsDetermines a Memory Address (along with IP)Segmented Address written as CS:IP
DS, Data SegmentUsed to “point” to DataDetermines Memory Address (along with other registers)ES, Extra Segment allows 2 Data Address Registers
SS, Stack SegmentUsed to “point” to Data in Stack Structure (LIFO)Used with SP or BPSS:SP or SS:BP are valid Segmented Addresses
IP, SP, BP, SI, DI - Offset Registers
2/27/2010 Arvind Shrivastava 9
IP, Instruction PointerUsed to “point” to InstructionsDetermines a Memory Address (along with CS)Segmented Address written as CS:IP
SI, Source Index; DI, Destination IndexUsed to “point” to DataDetermines Memory Address (along with other registers)DS, ES commonly used
SP, Stack Pointer; BP, Base PointerUsed to “point” to Data in Stack Structure (LIFO)Used with SSSS:SP or SS:BP are valid Segmented Addresses
Contains “Index Value” for Memory Address
INSTRUCTION SET OF 8086 Classified into 7 categories1] Data Transfer 2] Arithmetic 3] Logical 4] Control 5]Processor Control Instructions6] String Manipulation7] Interrupt Control
2/27/2010 Arvind Shrivastava 10
Data Transfer Instructions Note: Data Transfer Instructions do not affect any flags1] MOV dest, srcNote that source and destination can not be memory
location. Also source and destination must be same type.
2] PUSH Src: Copies word on stack.3] POP dest: Copies word from stack into dest. Reg.4] INacc, port : Copies 8 or 16 bit data from port to
accumulator. a) Fixed Port b) Variable Port 5] OUTport,acc
2/27/2010 Arvind Shrivastava 11
Data Transfer Instructions Contd… 6] LESReg, Mem: Load register and extra segment
register with words from memory.7] LDSReg,Mem: Load register and data segment
register with words from memory.8] LEAReg,Src: load Effective address. (Offset is loaded
in specified register)9] LAHF: Copy lower byte of flag register into AH
register.10] SAHF: Copy AH register to lower byte of flag
register
2/27/2010 Arvind Shrivastava 12
Data Transfer Instructions Contd…
11] XCHG dest, src: Exchange contains of source and destination.
12] XLAT: Translate a byte in AL. This instruction replaces the byte in AL with byte pointed by BX.To point desired byte in look up table instruction adds contains of BX with AL ( BX+ AL). Goes to this location and loads into AL.
2/27/2010 Arvind Shrivastava 13
Arithmetic Instructions 1]ADD dest,src 2] ADC dest,src: Add with carry3] AAA : ASCII adjust after addition. We can add
two ASCII numbers directly and use AAA after addition so as to get result directly in BCD. (Works with AL only)
4] DAA : Decimal adjust accumulator. ( Works with AL only)
5] SUB dest, src6] SBB dest, src: Subtract with borrow.2/27/2010 Arvind Shrivastava 14
Arithmetic Instructions Contd … 7] AAS: ASCII adjust for subtraction ( Same as
AAA and works with AL only)8] DAS : Decimal adjust after Subtraction.
(Works with AL only)9] MUL src10 ] IMULsrc: Multiplication of signed byte.11] AAM: BCD adjust after multiply. ( Works
with AL only)12]DIV src If any one attempts to divide by 0 , then ?2/27/2010 Arvind Shrivastava 15
Arithmetic Instructions Contd … 13] IDIV: Division of signed numbers14]AAD: BCD to Binary convert before Division.15] DEC dest16] INCdest17] CWD: Convert signed word to signed double
word.18] CBW : Convert signed byte to signed word.
( CBW and CWD works only with AL,AX and DX registers.)
• 19] NEG dest: Forms Twos complement.2/27/2010 Arvind Shrivastava 16
Logical Instructions1] AND dest,src2] NOT dest: Invert each bit in destination3] OR dest, src4] XOR dest, src5] RCL dest, count : Rotate left through Carry If rotate once
count is directly specified in the instruction. For more no. of rotations count is specified in CL register.
6] RCR dest, count : Rotate right through carry7] ROL dest, count : Rotate left (into carry as well as into LSB)8] ROR dest, count : Rotate left (into carry as wellas into
MSB)
2/27/2010 Arvind Shrivastava 17
Logical Instructions Contd…9] SAL/ SHL dest, count : Shift left and put 0in LSB.10] SAR dest,count : Shift right New MSB = Old MSB11]SHR dest,count : Shift right .MSB is filled with 0’s.12] TEST dest,src: AND logically,updates flags but
source and destare unchanged.13] CMP dest,srcCF, ZF and SF are used Ex .CMP CX,BX
2/27/2010 Arvind Shrivastava 18
CF ZF SF
CX = BX 0 1 0
CX > BX 0 0 0
CX < BX 1 0 1
Control transfer Instructions1]CALL : Call a procedure - Two types of calls
i) Near Call ( Intrasegment)ii) Far Call ( Intersegment)
2] RET : Return execution from procedure3] JMP : Unconditional Jump to specified
destination. Two types near and Far4] JA / JNBE: Jump if above / Jump if not below• The terms above and below are used when we
refer to the magnitude of Unsigned number . Used normally after CMP.
5]JAE / JNB / JNC2/27/2010 Arvind Shrivastava 19
Control Transfer Inst. Contd…6] JB / JC / JNAE7] JBE / JNA8] JE/ JZ9]JCXZ: Jump if CX is Zero.10] JG / JNLE: Jump if Greater /Jump if not less than or
equal. The term greater than or less than is used in connection with two signed numbers.
• 11] JGE / JNL:• 12] JL / JNGE :• 13] JLE / JNG :• 14]JNE / JNZ :
2/27/2010 Arvind Shrivastava 20
Control Transfer Inst. Contd…15] JNO : Jump if no overflow16] JNS :Jump if no sign17] JS18] JO19] JNP / JPO 20] JP / JPE
In all above conditional instructions the destination of jump is in the range of -128 to + 127 bytes from the address after jump.
2/27/2010 Arvind Shrivastava 21
Control Transfer Inst. Contd…21] LOOP: Loop to the specified label if CX not equal
to Zero. The count is loaded in CX reg. Every time LOOP is executed then CX is automatically decremented . ( Used in delay programs.)
22] LOOPE/ LOOPZ: Loop while CX not equal to zero and ZF = 1.
23] LOOPNE / LOOPNZ: Loop while CX not equal to zero and ZF = 0.
In all above LOOP instructions the destination of jump is in the range of -128 to + 127 bytes from the address after LOOP.
2/27/2010 Arvind Shrivastava 22
Processor Control1] CLC: Clear Carry flag.2] STC : Set carry Flag3] CMC : Complement Carry Flag4] CLD: Clear Direction Flag.5] STD: Set Direction Flag6] CLI : Clear Interrupt Flag.7] STI : Set Interrupt Flag.8] HLT: Halt Processing.
2/27/2010 Arvind Shrivastava 23
Processor Control Contd…
9] NOP : No Operation10] ESC: Escape, Executed by Co-processors and
actions are performed according to 6 bit coding in the instruction.
11] LOCK : Assert bus lock Signal, This is prefix instruction.
12] WAIT :Wait for test or Interrupt Signal. Assert wait states.
2/27/2010 Arvind Shrivastava 24
String Control1] MOVS/ MOVSB/ MOVSW : Dest string name, src
string name. This inst moves data byte or word from location in DS to location in ES.
2] REP / REPE / REPZ / REPNE / REPNZ: Repeat string instructions until specified conditions exsist. This is prefix instruction.
3] CMPS / CMPSB / CMPSW: Compare string bytes or string words.
4] SCAS / SCASB / SCASW: Scan a string byte or string word. Compares byte in AL or word in AX. String address is to be loaded in DI.
2/27/2010 Arvind Shrivastava 25
String Control Contd…
5] STOS / STOSB / STOSW: Store byte or word in a string. Copies a byte or word in AL or AX to memory location pointed by DI.
6] LODS / LODSB /LODSW : Load a byte or word in AL or AX Copies byte or word from memory location pointed by SI into AL or AX register.
2/27/2010 Arvind Shrivastava 26
Interrupt Control
• 1]INT type• 2] INTO Interrupt on overflow• 3] IRET Interrupt return
2/27/2010 Arvind Shrivastava 27
Assembler Directives
1] ASSUME : Used to tell assembler the name of logical segment. Ex. ASSUME CS: Code here
2] END 3] DB4] DW5] DD Define Double Word6] DQ Define Quad Word7] DT Define Ten Bytes2/27/2010 Arvind Shrivastava 28
Assembler Directive Contd..8] PROC Procedure : PROC DELAY NEAR9] ENDP10] ENDS11] EQU12] EVEN: Align on even memory address.13] ORG 14] OFFSET: Ex MOV BX,Offset of Data Here15] PTR Pointer16] LABEL : Ex AGAIN LABEL FAR17] EXTRN: Tells the assembler that the names or labels
following this directive is in some other assembly module.
2/27/2010 Arvind Shrivastava 29
Assembler Directive Contd..18] PUBLIC : Links modules together19] INCLUDE : Include source code from file.20] NAME : To give specific name to module.21] GROUP : Grouping of logical segments.22] SEGMENT23] SHORT : Operator that tells assembler about
short displacement.24] TYPE : Type of variable whether byte or
word.
2/27/2010 Arvind Shrivastava 30
Data Transfer• IN AL,0C8H ;Input a byte from port 0C8H to
;AL• IN AX, 34H ;Input a word from port 34H to• AXA_TO_D EQU 4AH• IN AX, A_TO_D ;Input a word from port
;4AH to AX• MOV DX, 0FF78H ;Initialize DX point to port• IN AL, DX ;Input a byte from a 8 bit port
;0FF78H to AL• IN AX, DX ;Input a word from 16 bit port to
;0FF78H to AX.
2/27/2010 Arvind Shrivastava 31
Arithmetic Instruction• EXAMPLE: • ADD AL,74H ;Add immediate number 74H
;to content of AL• ADC CL,BL ;Add contents of BL plus
;carry status to contents of CL. ;Results in CL
• ADD DX, BX ;Add contents of BX to ;contents of DX
• ADD DX, [SI] ;Add word from memory at ;offset [SI] in DS to contents of DX
2/27/2010 Arvind Shrivastava 32
Logical instruction• OR op1, op2• OR AH, CL ;CL ORed with AH, result in AH.
;CX = 00111110 10100101• OR CX,FF00h ;OR CX with immediate FF00h
;result in CX = 1111111110100101;Upper byte are all 1’s lower bytes;are unchanged
• ;DX =F038h• NOT DX ;after the instruction DX = 0FC7h
2/27/2010 Arvind Shrivastava 33
Control transfer Instruction• ;Direct within-segment - near• CALL MULTO ;MULTO is the name ofthe
;procedure.
• ;Indirect within-segment -near• CALL BX ; BX contains the offset of the first
instruction of the procedure .Replaces contents of word of IP with contents o register BX.
2/27/2010 Arvind Shrivastava 34
Control transfer contd..
• ;Direct to another segment- far• CALL SMART ;SMART is the name of the
;Procedure• SMART PROC FAR ; Procedure must be
;declared as an far• RET Instruction – Return execution from
procedure to calling program
2/27/2010 Arvind Shrivastava 35
Program to multiply two 16bit binary no. & give 32 bit result
;ABSTRACT This program multiplies two 16bit words in the memory locations called MULTIPLICAND and MULTIPLIER. Result is stored in memory location , PRODUCT.
;REGISTERS : Uses CS, DS, AX, DXDATA_HERE SEGMENT
MULTIPLICAND DW 204AH ; first word hereMULTIPLIER DW 3B2AH ; second word herePRDUCT DW 2 DUP(0) ; Result of
multiplication hereDATA_HERE ENDS
2/27/2010 Arvind Shrivastava 36
Program Contd..CODE_HERE SEGMENT
ASSUME CS:CODE_HERE, DS:DATA_HERESTART:MOV AX, DATA_HERE ;Initialize DS
MOV DS, AX ; registerMOV AX, MULTIPLICAND ; Get one wordMUL MULTIPLIER ; Multiply by 2nd wordMOV PRODUCT, AX ;Store low word of resultMOV PRODUCT+2, DX ; Store high word of resultINT 3 ; wait for command from user
CODE_HERE ENDSEND START
2/27/2010 Arvind Shrivastava 37
JUMP, FLAGS and conditional Jumps• 8086 fetches next instruction from some new
location• JMP always causes a jump to occur• Loads a new no. into IP register• Sometimes CS is also loaded with new no.• Direct or indirect jump, destination address• Program should be relocatable
2/27/2010 Arvind Shrivastava 38
Backward Jump;ABSTRACT : This program illustrates a backward
jump;REGISTERS : Uses CS, AL; PORTS : None CODE SEGMENT
ASSUME CS:CODEBACK: ADD AL, 03H
NOPNOPJMP BACK
CODE ENDSEND2/27/2010 Arvind Shrivastava 39
Forward Jump;ABSTRACT : Program illustrates forward jump;REGISTERS : Uses CS, AX;PORS : None
CODE SEGMENTASSUME CS: CODEJMP THERENOPNOP
THERE: MOV AX, 0000HNOPENDSEND2/27/2010 Arvind Shrivastava 40
8086 conditional flags- Six conditional Flags: Carry Flag (CF), parity flag (PF),
auxiliary carry flag (AF), zero flag(ZF), sign flag(SF), overflow flag(OF).
- Carry Flag is affected by ADD, SUB, AND , CMP instructions
- CMP BX, CXCondition CF ZFCX > BX 1 0CX<BX 0 0CX=BX 0 1
2/27/2010 Arvind Shrivastava 41
Conditional flags contd..• AF: DAA, DAS instructions• ZF: INC, DEC, CMP inst.• OF:ADD
2/27/2010 Arvind Shrivastava 42
Conditional Jump• All conditional jump instructions are short-
type jumps. • Destination address must be in the range -128
to +127• Destination label must be in the same code
segment
2/27/2010 Arvind Shrivastava 43
Conditional jump contd..JE /JZ ZF = 1 Jump if Equal or Jump if Zero (=)JNE/JNZ ZF = 0 Jump if Not Equal or Jump if Not
ZeroSigned ComparisonsJG ZF = 0 and SF = 0 Jump if Greater than ( > )JGE SF = 0 Jump if Greater than or Equal
( >= )JL SF = 1 Jump if Less than ( < )JLE ZF = 1 or SF = 1 Jump if Less than or Equal ( <=)
2/27/2010 Arvind Shrivastava 44
Conditional jump contd..Unsigned ComparisonsJA ZF = 0 and CF = 0 Jump if Above ( > )JAE CF = 0 Jump if Above or Equal ( >= )JB CF = 1 Jump if Below ( < )JBE ZF = 1 or CF = 1 Jump if Below or Equal ( <= )MiscellaneousJO OF = 1 Jump if Overflow, ditto for CF, SF &
PFJNO OF = 0 Jump if No Overflow, ditto for
CF, SF & PFJCXZ CX = 0 Jump if CX = 0JECXZ ECX = 0 Jump if ECX = 02/27/2010 Arvind Shrivastava 45
IF-THENIf <condition> THEN CMP AX,BX
action JE THERE…… ADD AX, 0020H
THERE: MOV CL, 20H
CMP AX, BXJNE FIXJMP THEREFIX: ADD AX, 0020HTHERE: MOV CL, 07H2/27/2010 Arvind Shrivastava 46
IF-THEN-ELSEREAD TEMPERATUREIF TEMPERATURE < 30 THEN
LIGHT YELLOW LAMPELSE
LIGHT GREEN LAMPREAD pH SENSOR
IF TEMPERATURE ≥ 30 THENLIGHT GREENLAMP
ELSELIGHT YELLOWLAMP
READ SENSOR
2/27/2010 Arvind Shrivastava 47
IF-THEN-ELSE contd…;ABSTRACT : this program reads the temp, if temp < 30, a
yellow lamp is lit, if temp is ≥ 30, a green lamp is lit;REGISTERS : CS, AL, DX;PORTS : FFF8H – temp I/P, FFFAH – Control O/P
CODE SEGMENTASSUME CS: CORE
; intialize port FFFAH as O/P and FFF8H as I/PMOV DX, 0FFFEH ; point DX to port control regsMOV AL, 99H ; Load control word to initialize portsOUT DX, AL ; send control word to port control regs
2/27/2010 Arvind Shrivastava 48
IF-THEN-ELSE contd…MOV DX, 0FFF8H ; point DX at I/P portIN AL, DX ; read temp from sensor on I/P portCMP AL, 30JB YELLOWJMP GREEN
YELLOW : MOV AL, 01H ; load code to light yellow
MOV DX, 0FFFAH ; point DX at O/P portOUT DX, AL ; send code to light yellow lampJMP EXIT ; go to exit mainline instructions
2/27/2010 Arvind Shrivastava 49
IF-THEN-ELSE contd…GREEN: MOV AL, 02H ; load code to light green
MOV DX, 0FFFAH ; point DX at O/P portOUT DX, AL ; send code to light green lamp
EXIT : MOV DX, 0FFFCH ; next mainline instIN AL, DX ; read pH sensor
CODE ENDSEND
2/27/2010 Arvind Shrivastava 50
IF-THEN-ELSE contd…ALGO (B)…..
CMP AL, 30 ; compare temp with 30JAE GREEN ; if temp ≥ 30 then light greenJMP YELLOW ; ELSE light YELLOW
GREEN : MOV AL, 02H ; load code to light green lampMOV DX, 0FFFAH ; point DX at O/P portOUT DX, AL ; send code to light greenJMP EXIT ; go to next mainline instruction
2/27/2010 Arvind Shrivastava 51
IF-THEN-ELSE contd…YELLOW: MOV AL, 01H ; Load code to light
yellowMOV DX, 0FFFAH ; point DX at O/P portOUT DX, AL ; send code to light yellow
EXIT : MOV DX, 0FFFCH ; next mainline instruction
IN AL, DX ; Read pH sensorCODE ENDS
END
2/27/2010 Arvind Shrivastava 52
Think about
1. How high level language statements are translated to 8086 instructions
2. How logical operations can be used to set and clear bits
3. Execution/Implementation of IF-THEN, IF-THEN-ELSE,REPEAT-UNTIL,LOOP,WHILE in assembly language.
2/27/2010 Arvind Shrivastava 53