University of Tehran 1 Microprocessor System Design Instructions (1)
-
Upload
damian-atkinson -
Category
Documents
-
view
221 -
download
2
Transcript of University of Tehran 1 Microprocessor System Design Instructions (1)
University of Tehran 2
Outline
• Data transfer operations
• Arithmetic operations
• Logic operation
• Control operations
• String operations
University of Tehran 3
Data Transfer Instructions• Very Common Instruction: mov desti, source
• Allowed Operands
Destination SourceMemory AccumulatorAccumulator MemoryRegister RegisterRegister MemoryMemory RegisterRegister ImmediateMemory ImmediateSeg. Reg. RegisterSeg. Reg. MemoryRegister Seg. Reg.Memory Seg. Reg.
University of Tehran 4
Arithmetic Instruction Summary
add ax, bx ;axax+bx and set flagsadc ax, bx ;axax+bx+CF(lsb) and set flagsinc ax ;axax+1 and set flagsdaa ;Decimal (BCD) Adjust after Additionsub ax, bx ;axax-bx and set flagssbb ax, bx ;ax(ax-CF)-bx and set flagsdec ax ;axax-1neg ax ;ax(-1)*(ax) -- 2’s Complementcmp ax, bx ;Flags are set according to ax-bxdas ;Decimal (BCD) Adjust after Sub.mul cx ;dx:ax ax * cx (unsigned)imul cx ;dx:ax ax * cx (2’s complement)div cl ;alax/cl Quot. AND ahax/cl Rem.idiv cx ;ax(dx:ax)/cx Quot. AND dx Rem.
University of Tehran 5
Increment Examples
inc bl ;blbl+1 and set flags
inc BYTE PTR [bx] ;Byte at ds:(bx)ds:(bx)+1
New MASM Directive: BYTE POINTER
00ffh 0000h
inc [bx] ;Word at ds:(bx)ds:(bx)+1
00ffh 0100h
inc [DATA1] ;ds:(DATA1)ds:(DATA1)+1
University of Tehran 6
Add with Carry
add ax, cx ;axax+cx and flags setadc bx, dx ;bxbx+dx+CF(lsb) and flags set
33-bit Sum Present in CF:bx:ax
BX
DX
AX
CX
1 1
0 1
CF=1
BX AXCF
University of Tehran 7
Decimal Adjust after Addition
• For BCD Arithmetic
• “Corrects” Result
0110 6 +0111 7 1101 13should be 0001 0011 (1101 is illegal BCD)
•daa Uses Implicit Operand, al Register
•Follows add, adc to “Adjust”
University of Tehran 8
Decimal Adjust after Addition Example
mov dx, 1234h ;dx1234 BCDmov bx, 3099h ;bx3099 BCDmov al, bl ;al99 BCDadd al, dl ;alcdh illegal BCD, need 34+99=133daa ;al33h (33 BCD) and CF=1mov cl, al ;cl33 BCDmov al, bh ;al30 BCDadc al, dh ;al30h+12h+1=43hdaa ;al43h (43 BCD) not illegal BCD this timemov ch, al ;cx=4333h BCD for 1234+3099
University of Tehran 9
Subtraction Instruction Types
sub ax, bx ;axax-bx and set flags
sbb ax, bx ;ax(ax-CF)-bx and set flags
dec ax ;axax-1
neg ax ;ax(-1)*(ax) - 2’s Complement
cmp ax, bx ;Flag is set according to ax-bx
das ;Decimal (BCD) Adjust after Subtraction
University of Tehran 10
Allowable Operands for add, sub
Gen Reg
Gen Reg
Mem Loc
Immediate
+-
Gen Reg
Mem Loc
Immediate
+-
Destination Source
University of Tehran 11
Subtract with Borrow, sbb
sub ax, di ;axax-di and CF gets borrow bitsbb bx, si ;bx(bx-CF(lsb))-si and flags set
32-bit Difference Present in bx:axCF Indicates If Difference is Negative
BX
SI
AX
DI
CF
BX AXCF
University of Tehran 12
Multiplication
• 8086/8088 One of First to Include mul/div Instruction
• Allowable Operands: Bytes, Words, DoubleWords
•Allowable Results: Words, DoubleWords, QuadWords
•OF, CF Give Useful Information
•AF, PF, ZF, SF Change but Contents Unpredictable
•Multiplicand Always in al, ax
•mul - Unsigned Mnemonic
•imul - Signed Mnemonic
University of Tehran 13
Multiply Instructions
• Product can be Twice the Size2 3 = 6 (same size)
2 8 = 16 (double size, EXT)
•OF=CF=0 means product is same size as result
•OF=CF=1 means EXT product size
•AF, PF, ZF, SF Contents Unpredictable
mul bl ;axal*bl, Unsignedmul bx ;dx:axbx*ax, Unsignedimul bl ;axal*bl, Signedimul bx ;dx:axbx*ax, Signed
University of Tehran 14
Division
• 8, 16 bit Operands
• No Immediate Addressing Mode
• No Flag Bits Change Predictably
• Operands: Divisor is Programmer Specified
• Dividend is Implied
• Quotient, Remainder Implied
Size Dividend Quotient Remainder 8 bits ax al ah
16 bits dx:ax ax dx
University of Tehran 15
Division Instruction Examples
• idiv Signed and div Unsigned
dividend / divisor = quotient, rmdr
div cx ;dx:ax is divided by value in cx;unsigned quotient is placed in ax;positive remainder is placed in dx
University of Tehran 17
Logic Instruction TypesBITWISE LOGICAL
not ax ;1’s Complement-Logical Invertand ax, bx ;Bitwise logical and operationor ax, bx ;Bitwise logical inclusive-or operationxor ax, bx ;Bitwise logical exclusive-or operationtest ax, fffh ;Bitwise and but result discarded
SHIFTshl ax, 4 ;Logical shift leftsal ax, 3 ;Arithmetic shift leftshr ax, 4 ;Logical shift rightsar ax, 3 ;Arithmetic shift right
ROTATErol bx, 3 ;Rotate leftror cx, 4 ;Rotate rightrcl ax, 1 ;Rotate left through carryRcr dx, 6 ;Rotate right through carry
University of Tehran 18
Bit Level Logic
and, or, xor, not, test,
• Affect Status Flags as Follows:1) Always Clears CF and OF2) SF, ZF, AF, PF Change to Reflect Result
• Common Usage:
and ax, ax ;clear CF and OF
xor ax, ax ;clear ax=CF=OF=PF=AF=SF=0 and ZF=1
University of Tehran 19
Masking OperationsXXXX XXXX (unknown word)
(AND) 0000 1111 (mask word)0000 XXXX (result)
What if we wanted 1111 XXXX instead?
EXAMPLE: Convert ASCII to BCD
;First convert to BCD - change 3235h into 0025hmov bx, 3235h ;bx ‘25’and bx, 0f0fh ;bx0205hmov dx, bx ;dx0205hshl bh, 4 ;bh20hor bl, bh ; bl = bh or bl = 20 or 05 = 25hxor bh, bh ;zero out bh, so bx = 0025 (BCD value)
University of Tehran 20
Bit Test Instruction, test
• Same as and But Result is Discarded
• Only Affects Flags (like cmp)
• Use test for Single Bit and cmp for Byte, Word
• ZF=1 if Tested Bit=0 and ZF=0 if Tested Bit=1
test al, 1 ;XXXX XXXX (AND) 0000 0001test al, 128 ;XXXX XXXX (AND) 1000 0000
University of Tehran 21
Shiftsshl - Logical Shift Left
REGCF 0
REG CF0
REGCF 0
REG CF
shr - Logical Shift Right
sal - Arithmetic Shift Left (same as logical)
sar - Arithmetic Shift Right (sign bit is preserved)
MSB
University of Tehran 22
Simple Arithmetic Using Shifts
;Compute (-3)*VALUE Using Only Shifts and Adds
mov ax, VALUE ;ax Word from memory with label VALUEmov bx, ax ;bx Word from memory with label VALUEshl ax, 2 ;ax 4*VALUEadd ax, bx ;ax 5*VALUEshl bx, 3 ;bx 8*VALUEsub ax, bx ;ax (-3)*VALUE
University of Tehran 23
Rotatesrol - Rotate Left
REGCF
rcl - Rotate Through Carry Left
ror - Rotate Right
rcr - Rotate Through Carry Right
REGCF
REGCF
REGCF
University of Tehran 24
Example Using Rotates
;Multiply a 48-bit value in dx:bx:ax by 2
shl ax, 1 ;ax 2*axrcl bx, 1 ;bx 2*bx + CF(lsb)rcl dx, 1 ;dx 2*dx + CF(lsb)
;End result is dx:bx:ax 2*(dx:bx:ax)
• Operand for rotates and shifts can be either:
1) Immediate value
2) Quantity in cl
University of Tehran 26
Program Control Instructions
• Generally modify CS:IP • Causes modification in execution sequence (of instructions)• When such a program flow change occurs:
a) Instructions in the BIU inst. queue become invalid
b) BIU directly fetches CS:IP instruction from memory
c) While EU executes new instruction, BIU refills inst. Queue
• Classificationa) Jumps - Unconditional control transfers
b) Branches - Conditional control transfer
c) Interrupts - Unconditional control transfers (asynchronous)
d) Iteration – More complex type of branch
University of Tehran 27
Control Instruction Summary
UNCONDITIONALjmp LABEL ;next instruction executed has LABELcall LABEL ;next instruction executed has LABELret ;next instruction executed is after the callhlt ;nothing executed until RESET signal
ITERATIONloop LABEL ;cx cx - 1, jump to LABEL if cx > 0loope/loopz LABEL ;same as loop but ZF=1 also requiredloopne/loopnz LABEL ;same as loop but ZF=0 also required
INTERRUPTSint <immed8> ;Invoke the int. handler specified by immed8 iret ;Return from interrupt handler
University of Tehran 28
Simplest Control Instruction, jmp
jmp LABEL ;LABEL is offset address of instruction ;in the code segment
3 Forms of jmp
SHORT - 2 bytes, allows jump to ±127 locations from current address
NEAR - 3 bytes, allows jump to ±32K locations from current address
FAR - 5 bytes anywhere in memory
EB disp
E9 disphi displo
EA IP lo IP hi CS lo CS hi
University of Tehran 29
Example with Short Jump
;Causes bx to count by 1 from 0 to 65535 to 0 to 65535 to …
xor bx, bx ;Clear bx and initialize status flagsstart: mov ax, 1 ;ax 1
add ax, bx ;ax ax+bxjmp next ;add a displacement to IP
;(+2 from xor to mov)xor bx, bx ;Clear bx and initialize flagsxor ax, ax ;Clear ax and initialize flags
next: mov bx, ax ;bx axjmp start ;add a displacement to IP
;(a negative value - 2’s comp.)
University of Tehran 30
Indirect Jump
jmp ax ;ip ax
• Address of target is in register
• Does NOT add disp to IP - Transfer REG contents to IP
University of Tehran 31
Conditional Control Instruction SummarySimple Flag Branches
CONDITIONAL
jc LABEL ;jump on carry (CF=1)jnc LABEL ;jump on no carry (CF=0)je/jz LABEL ;jump if ZF=1 - jump if equal/zerojne/jnz LABEL ;jump if ZF=0 - jump not equal/jump if zerojo LABEL ;jump if OF=1 - jump on overflowjno LABEL ;jump if OF=0 - jump if no overflowjs LABEL ;jump on sign flag set (SF=1)jns LABEL ;jump if no sign flag (SF=0)jp/jpe LABEL ;jump if PF=1 - jump on parity/parity evenjnp/jpo LABEL ;jump if PF=0 - jump on no parity/parity odd
Jump based on single flag
University of Tehran 32
Conditional Control Instruction SummaryBranches for unsigned comparisons
Jump is based on flags used for unsigned number comparison (based on C, Z flag)
CONDITIONALja/jnbe LABEL ;jump if CF=ZF=0 - jump above-jump not below/equaljae/jnb LABEL ;jump if CF=0 - jump above/equal-jump not belowjb/jnae LABEL ;jump if CF=1 - jump below-jump not above/equaljbe/jna LABEL ;jump if CF=1 or ZF=1 - jump equal - jump zero
Typical use: cmp al,bljb there ; jump if al is ‘below’ bl
; unsigned comparison
University of Tehran 33
Conditional Control Instruction SummaryBranches for signed comparisons
Typical use: cmp al,bljl there ; jump if al is less than bl
; signed comparison
Jump is based on flags used for signed number comparison (based on Z, S, V flags)
CONDITIONAL
jg/jnle LABEL ;jump if ZF=0 and (SF=OF) - jump greater/not less ; nor equal
jge/jnl LABEL ;jump if SF=OF - jump greater-equal/not less thanjl/jnge LABEL ;jump if SF OF - jump less than/not greater nor
; equal jle/jng LABEL ;jump if ZF=1 or SF OF - jump less or equal/not
; greater than
University of Tehran 34
Iteration Instruction, loop
• Combination of decrement cx and conditional Jump
• Decrements cx and if cx0 jumps to LABEL
Example:
ADDS PROC NEARmov cx, 100 ;cx 64h - number of words to addmov si, OFFSET BLOCK1 ;si offset of BLOCK1 (in ds)mov di, OFFSET BLOCK2 ;di offset of BLOCK2 (in es)cld ;Auto-increment si and di, DF=0
AGAIN: mov bx, di ;bx di, save offset of BLOCK2lodsw ;ax ds:[si], sisi+2, didi+2add ax, [bx] ;ax ax + ds:[bx]mov di, bx ;di bx, restore di with
; offset in BLOCK2stosw ;es:[di] ax, sisi+2, didi+2loop AGAIN ;cx cx - 1, if cx0 jump to AGAINret ;ip ss:[sp]
ADDS ENDP
University of Tehran 35
Procedures
• Group of instructions that perform single task– (can be used as) a SUBROUTINE
call - invokes subroutine - pushes ipret - returns from subroutine - pops ip
• Uses MASM directives: PROC and ENDP
• Must specify
NEAR - intrasegmentFAR - intersegment
• Difference is op-code of ret
NEAR - c3h - pops IPFAR - cbh - pops CS, pops IP
University of Tehran 36
call Instruction
• Differs from jmp since return address on stack
NEAR call: 3 bytes - 1 opcode and 2 for IP FAR call: 5 bytes - 1 opcode, 2 for IP and 2 for CS
• call with operand - can use 16-bit offset in any register except segment registers
call bx ;pushes ip then jumps to cs:[bx]
University of Tehran 37
String Transfer Instructions
• String Forms:
movsb ;move string byte by bytemovsw ;move string word by word
EXAMPLE:movsb ;Copies 8 bits at DS:SI to ES:DI
University of Tehran 38
Other String Instructions
lodsb ;loads al with contents of ds:si;Inc/Dec si by 1 depending on DF
lodsw ;loads ax with ds:si ;Inc/Dec si by 2 depending on DF
stosb ;loads es:di with contents of al;Inc/Dec di by 1 depending on DF
stosw ;loads es:di with contents of ax;Inc/Dec di by 2 depending on DF
University of Tehran 39
String Scan Instruction, scas•scasb, scasw,
• Compares al, ax with memory data
• Does an integer subtraction - result not saved
• Generally used with a REPEAT prefix
•DF controls auto-increment/decrement
•Example:
mov di, OFFSET BLOCK ;di address of memory location BLOCKcld ;DF 0, auto-increment modemov cx, 100 ;cx 64h, initialize counter to 100xor al, al ;clear alrepne scasb ;test for 00h in location es:di
;if es:di not equal to 00h then; cx cx - 1, di di + 1, repeat;else if cx = 00h; do not repeat test;else if es:di equals 00h; ZF = 1, do not repeat test
University of Tehran 40
Compare String Instruction, cmps
•cmpsb, cmpsw,
• Compares 2 sections of memory
• Does an integer subtraction - result not saved
• Generally used with a REPEAT prefix
•si, di auto-increment/decrement depending on DF
•Example: Test two strings for equivalence
;Assume that ds and es are already set-up (NOTE:ds can equal es)lea si, LINE ;si gets offset of location labeled LINElea di, TABLE ;di gets offset of location labeled TABLEcld ;DF=0, auto-increment modemoc cx, 10 ;initialize counter register to 10repe cmpsb ;while ds:si=es:di decrement cx and incr. si, di
;if cx=0 stop testing;after complete, if cx not equal 0, then;strings do not match