Microprocessor System Design - PowerPoint PPT Presentation

1 / 68
About This Presentation
Title:

Microprocessor System Design

Description:

aam ;ASCII Adjust after Multiplication. div cl ;al ax/cl Quot. AND ah ... aad ;ASCII Adjust after Division. University of Tehran 12. Addition Instruction Types ... – PowerPoint PPT presentation

Number of Views:158
Avg rating:3.0/5.0
Slides: 69
Provided by: OmidF2
Category:

less

Transcript and Presenter's Notes

Title: Microprocessor System Design


1
Microprocessor System Design
  • Omid Fatemi
  • Instructions (1)
  • (omid_at_fatemi.net)

2
Review
  • Flag instruction
  • ADD and ADC
  • A loop program
  • Data entering
  • MASM
  • Directives

3
Outline
  • Data transfer operations
  • Arithmetic operations
  • Logic operation
  • Control operations
  • String operations

4
MASM Program Example(another way to define
segments)


This
is an example program. It prints the
character string "Hello World" to the DOS
standard output using the DOS service
interrupt, function 9.


hellostk
SEGMENT BYTE STACK 'STACK' Define the stack
segment DB 100h DUP(?) Set maximum
stack size to 256 bytes (100h) hellostk ENDS
hellodat SEGMENT BYTE 'DATA' Define the data
segment dos_print EQU 9
define a constant via EQU strng DB 'Hello
World',13,10,'' Define the character
string hellodat ENDS hellocod SEGMENT BYTE
'CODE' Define the Code segment START mov ax,
SEG hellodat ax lt-- data segment start
address mov ds, ax ds
lt-- initialize data segment register
mov ah, dos_print ah lt-- 9 DOS 21h
string function mov dx,OFFSET strng
dx lt-- beginning of string int 21h
DOS service interrupt
mov ax, 4c00h ax lt-- 4c DOS 21h
program halt function int 21h
DOS service interrupt hellocod ENDS
END START END label
defines program entry
5
Yet another way to define Segs

Use .stack,.data,.code
directives to define segment types


.stack 100h reserve 256
bytes of stack space .data
dos_print EQU 9
define a constant strng DB 'Hello
World',13,10,'' Define the character string
.code START mov ax, SEG strng
ax lt-- data segment start address
mov ds, ax ds lt--
initialize data segment register mov
ah, dos_print ah lt-- 9 DOS 21h string
function mov dx,OFFSET strng
dx lt-- beginning of string int 21h
DOS service interrupt
mov ax, 4c00h ax lt-- 4c DOS 21h
program halt function int 21h
DOS service interrupt END START
6
Masm Assembler Directives
end label end of program, label is entry
point proc farnear begin a
procedure far, near keywords specify if
procedure in different code segment (far), or
same code segment (near) endp end of
procedure page set a page format for the
listing file title title of the listing
file .code mark start of code
segment .data mark start of data
segment .stack set size of stack segment
7
Data Allocation Directives
db define byte dw define word (2
bytes) dd define double word (4
bytes) dq define quadword (8 bytes) dt define
tenbytes equ equate, assign numeric expression
to a name Examples db 100 dup (?) define
100 bytes, with no initial values for bytes db
Hello define 5 bytes, ASCII equivalent of
Hello. maxint equ 32767 count equ 10 20
calculate a value (200)
8
Data Transfer Instructions
  • Very Common Instruction mov desti, source
  • Allowed Operands

Destination Source Memory
Accumulator Accumulator Memory Register
Register Register Memory Memory
Register Register Immediate Memory
Immediate Seg. Reg. Register Seg. Reg.
Memory Register Seg. Reg. Memory Seg. Reg.
9
Arithmetic
10
Arithmetic/Logic Instructions
  • Basic Mathematical Operations
  • Signed/Unsigned Integer Only
  • Default is 2s Complement
  • Computes Result AND Modifies Status Flags
  • Logic Instructions
  • Bit Level
  • Word Level
  • Computes Results AND Modifies Status Flags

11
Arithmetic Instruction Summary
add ax, bx ax?axbx and set flags adc ax,
bx ax?axbxCF(lsb) and set flags inc ax ax?ax
1 and set flags aaa ASCII Adjust after
Addition daa Decimal (BCD) Adjust after
Addition sub ax, bx ax?ax-bx and set
flags sbb ax, bx ax?(ax-CF)-bx and set
flags dec ax ax?ax-1 neg ax ax?(-1)(ax) --
2s Complement cmp ax, bx Flags are set
according to ax-bx das Decimal (BCD) Adjust
after Subtraction aas ASCII Adjust after
Subtraction mul cx dxax? ax cx
(unsigned) imul cx dxax? ax cx (2s
complement) aam ASCII Adjust after
Multiplication div cl al?ax/cl Quot. AND
ah?ax/cl Rem. idiv cx ax?(dxax)/cx Quot. AND
dx ? Rem. aad ASCII Adjust after Division
12
Addition Instruction Types
add ax, bx ax?axbx and set flags adc ax,
bx ax?axbxCF(lsb) and set flags inc ax ax?a
x1 and set flags aaa ASCII Adjust after
Addition daa Decimal (BCD) Adjust after
Addition add al, bl al?albl and set
flags add bx, 35afh bx?bx35afh add bx,
al ds(bx)?ds(bx)al add cl,
bp cl?clss(bp) add al, ebx al?alds(e
bx) add bx, TEMPdi bx?bxds(TEMPdi) add bx,
eax2ecx bx?bxds(eax(2ecx)) Scaled
Index Addressing 386 ecx may contain 1, 2 , 4
only
13
Increment Examples
inc bl bl?bl1 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
14
Add with Carry
BX
AX
1
1
DX
CX
0
1
CF1
BX
CF
AX
add ax, cx ax?axcx and flags set adc bx,
dx bx?bxdxCF(lsb) and flags set 33-bit
Sum Present in CFbxax
15
Decimal Adjust after Addition
  • For BCD Arithmetic
  • Corrects Result
  • 0110 6
  • 0111 7
  • 1101 13?should be 0001 0011
  • (1101 is illegal BCD)
  • 2 Digits/Word Intel Refers to as Packed Decimal
  • daa Uses Implicit Operand, al Register
  • Follows add, adc to Adjust

16
Decimal Adjust after Addition Example
mov dx, 1234h dx?1234 BCD mov bx, 3099h bx?3099
BCD mov al, bl al?99 BCD add al, dl al?cdh
illegal BCD, need 3499133 daa al?33h (33
BCD) and CF1 mov cl, al cl?33
BCD mov al, bh al?30 BCD adc al, dh al?30h12h1
43h daa al?43h (43 BCD) not illegal BCD this
time mov ch, al cx4333h BCD for 12343099
17
ASCII Adjust after Addition
  • For Addition Using ASCII Encoded Numbers
  • 30h through 39h Represent 0 through 9
  • ax is Default Source and Destination for aaa
  • 31 1
  • 39 9
  • 6a 10?should be 3130h
  • (6ah is incorrect ASCII result j)
  • mov ax, 31h ax?0031h1
  • add al, 39h ax?31h39h006ahltnulgtj
  • aaa ax?0100h (this is BCD of result)
  • add ax, 3030h Convert from BCD to ASCII
  • ax?0100h3030h3130h10

18
Subtraction Instruction Types
sub ax, bx ax?ax-bx and set flags sbb ax,
bx ax?(ax-CF)-bx and set flags dec ax ax?ax-1 n
eg ax ax?(-1)(ax) - 2s Complement cmp ax,
bx Flag is set according to ax-bx das Decimal
(BCD) Adjust after Subtraction aas ASCII
Adjust after Subtraction
19
Allowable Operands for add, sub
Gen Reg
-
Gen Reg
Mem Loc
Immediate
Destination
Source
Gen Reg
-
Mem Loc
Immediate
20
Subtract with Borrow, sbb
CF
BX
AX
SI
DI
BX
CF
AX
sub ax, di ax?ax-di and CF gets borrow
bit sbb bx, si bx?(bx-CF(lsb))-si and flags
set 32-bit Difference Present in bxax CF
Indicates If Difference is Negative
21
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, eax
  • mul - Unsigned Mnemonic
  • imul - Signed Mnemonic

22
Multiply Instructions
  • Product can be Twice the Size
  • 2 ? 3 6 (same size)
  • 2 ? 8 16 (double size, EXT)
  • OFCF0 means product is same size as result
    (faster)
  • OFCF1 means EXT product size (slower)
  • AF, PF, ZF, SF Contents Unpredictable

mul bl ax?albl, Unsigned mul bx dxax?bxax,
Unsigned mul ebx edxeax?ebxeax,
Unsigned imul bl ax?albl, Signed imul bx dxax?
bxax, Signed imul ebx edxeax?ebxeax, Signed
23
Special Immediate Multiply Instruction
  • 286
  • Uses imul Mnemonic but with 3 Operands
  • first 16-bit dest. register
  • second reg/mem location
  • third 8/16-bit immediate value
  • Always Performs Signed Multiplication
  • Product is Limited to 16-bits

imul cx, dx, 12h cx?dx12h imul bx, NUMBER,
12h bx?ds(NUMBER)12h
24
Division
  • 8, 16, 32 bit Operands (32 bit is 386)
  • No Immediate Addressing Mode
  • No Flag Bits Change Predictably
  • Can Cause Two Types of Error
  • 1) Divide by 0 (Mathematically Undefined)
  • 2) Divide Overflow (Wordlength Problem)
  • Operands Divisor is Programmer Specified
  • Dividend is Implied
  • Quotient, Remainder Implied

25
Division Instruction Examples
  • idiv Signed and div Unsigned
  • dividend / divisor quotient, rmdr

div cx dxax is divided by value in
cx unsigned quotient is placed in
ax positive remainder is placed in
dx idiv ebx edxeax is divided by value in
ebx signed quotient is placed in
eax remainder (ALWAYS same sign as
dividend) is placed in edx
26
Logical Instructions
27
Logic Instruction Types
BITWISE LOGICAL not ax 1s Complement-Logical
Invert and ax, bx Bitwise logical and
operation or ax, bx Bitwise logical
inclusive-or operation xor ax, bx Bitwise
logical exclusive-or operation test ax, fffh
Bitwise and but result discarded SHIFT shl ax,
4 Logical shift left sal ax, 3 Arithmetic
shift left shr ax, 4 Logical shift
right sar ax, 3 Arithmetic shift
right ROTATE rol bx, 3 Rotate left ror cx, 4
Rotate right rcl ax, 1 Rotate left through
carry rcr dx, 6 Rotate right through carry
28
Bit Level Logic
  • and, or, xor, not, test, bt, btc, btc, btr, bts
  • Affect Status Flags as Follows
  • 1) Always Clears CF and OF
  • 2) SF, ZF, AF, PF Change to Reflect Result
  • Common Usage
  • and ax, ax clear CF and OF
  • xor ax, ax clear axCFOFPFAFSF0 and ZF1
  • does more than mov ax, 0h
  • faster than push 00h then popf

29
Masking Operations
XXXX XXXX (unknown word) (AND) 0000 1111 (mask
word) 0000 XXXX (result) What if we wanted
1111 XXXX instead? EXAMPLE Convert ASCII to
BCD to Binary First convert to BCD - change
3235h into 0025h mov bx, 3235h bx?
25 and bx, 0f0fh bx?0205h mov
dx, bx dx?0205h shl bh, 4 bh?20hor bl, bh
bl bh or bl 20 or 05 25h xor bh, bh zer
o out bh, so bx 0025 (BCD value) Now convert
to binary - change 3235h into 0019h mov al, dh al
?02h mov cl, 10 cl?0ah mul cl ax 2 0Ah
14h (decimal value is 20) add al, dl al?14h05h1
9h (decimal value is 25)
30
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
  • ZF1 if Tested Bit0 and ZF0 if Tested Bit1
  • test al, 1 XXXX XXXX (AND) 0000 0001
  • test al, 128 XXXX XXXX (AND) 1000 0000

31
Shifts
shl - Logical Shift Left
shr - Logical Shift Right
sal - Arithmetic Shift Left (same as logical)
sar - Arithmetic Shift Right (sign bit is
preserved)
MSB
32
Simple Arithmetic Using Shifts
Compute (-3)VALUE Using Only Shifts and
Adds mov ax, VALUE ax ? Word from memory with
label VALUE mov bx, ax bx ? Word from memory
with label VALUE shl ax, 2 ax ?
4VALUE add ax, bx ax ? 5VALUE shl bx, 3 bx ?
8VALUE sub ax, bx ax ? (-3)VALUE
33
Rotates
rol - Rotate Left
REG
CF
rcl - Rotate Through Carry Left
REG
CF
ror - Rotate Right
REG
CF
rcr - Rotate Through Carry Right
REG
CF
34
Example Using Rotates
Multiply a 48-bit value in dxbxax by
2 shl ax, 1 ax ? 2ax rcl bx, 1 bx ? 2bx
CF(lsb) rcl dx, 1 dx ? 2dx CF(lsb) End
result is dxbxax ? 2(dxbxax)
  • Operand for rotates and shifts can be either
  • 1) Immediate value
  • 2) Quantity in cl

35
Program Control Instructions
36
Program Control Instructions
  • Generally modify CSIP
  • 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 CSIP instruction from
    memory
  • c) While EU executes new instruction, BIU
    flushes/refills inst. queue
  • Classification
  • a) Jumps - Unconditional control transfers
    (synchronous)
  • b) Branches - Conditional control transfer
  • c) Interrupts - Unconditional control transfers
    (asynchronous)
  • d) Iteration - More complex type of branch

37
Control Instruction Summary
UNCONDITIONAL jmp LABEL next instruction
executed has LABEL call LABEL next instruction
executed has LABEL ret next instruction
executed is after the call hlt nothing
executed until RESET signal ITERATION loop LABEL
cx ? cx - 1, jump to LABEL if cx gt
0 loope/loopz LABEL same as loop but ZF1 also
required loopne/loopnz same as loop but ZF0
also required INTERRUPTS int ltimmed8gt Invoke
the int. handler specified by immed8
into ltimmed8gt same as int but OF1
also iret Return from interrupt
handler CONDITIONAL to follow
38
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
39
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 flags start mov ax, 1 ax ?
1 add ax, bx ax ? axbx jmp next add a
displacement to IP (2 from xor to
mov) xor bx, bx Clear bx and initialize
flags xor ax, ax Clear ax and initialize
flags next mov bx, ax bx ? ax jmp start add
a displacement to IP (a negative value -
2s comp.)
40
Indirect Jump
  • Address of target is in register
  • Does NOT add disp to IP - Transfer REG
    contents to IP

assume that si contains either 0, 1 or
2 add si, si si ? 2si add si, OFFSET
TABLE si ? si ltaddress of TABLEgt mov ax,
cssi ax gets an address from the jump
table jmp ax ip ? ax the following jump TABLE
is defined in the code segment!!!! TABLE DW ZERO
DW ONE DW TWO ZERO code for ZERO
option . . ONE code for ONE
option . . TWO code for TWO option . .
41
Indirect Addressed Jump
  • Address of target is in register
  • Does NOT add disp to IP - Transfer MEM
    contents to IP

assume that si contains either 0, 1 or
2 add si, si si ? 2si add si, OFFSET
TABLE si ? si ltaddress of TABLEgt jmp cssi
ip gets an address from the jump table the
following jump TABLE is defined in the code
segment!!!! TABLE DW ZERO DW ONE DW TWO ZERO
code for ZERO option . . ONE code for ONE
option . . TWO code for TWO option . .
42
Conditional Control Instruction SummarySimple
Flag Branches
Jump based on single flag
CONDITIONAL jc LABEL jump on carry
(CF1)jnc LABEL jump on no carry
(CF0) je/jz LABEL jump if ZF1 - jump if
equal/zero jne/jnz LABEL jump if ZF0 - jump not
equal/jump if zerojo LABEL jump if OF1 - jump
on overflow jno LABEL jump if OF0 - jump if no
overflowjs LABEL jump on sign flag set
(SF1) jns LABEL jump if no sign flag
(SF0) jp/jpe LABEL jump if PF1 - jump on
parity/parity even jnp/jpo LABEL jump if PF0 -
jump on no parity/parity odd
43
Conditional Control Instruction SummaryBranches
for unsigned comparisons
Jump is based on flags used for unsigned number
comparison (based on C, Z flag)
CONDITIONAL ja/jnbe LABEL jump if CFZF0 - jump
above-jump not below/equal jae/jnb LABEL jump if
CF0 - jump above/equal-jump not
below jb/jnae LABEL jump if CF1 - jump
below-jump not above/equal jbe/jna LABEL jump if
CF1 or ZF1 - jump equal - jump zero
Typical use cmp al,bl jb there
jump if al is below bl unsigned
comparison
44
Conditional Control Instruction SummaryBranches
for signed comparisons
Jump is based on flags used for signed number
comparison (based on Z, S, V flags)
CONDITIONAL jg/jnle LABEL jump if ZF0 and
(SFOF) - jump greater/not less nor
equal jge/jnl LABEL jump if SFOF - jump
greater-equal/not less than jl/jnge LABEL jump
if SF ? OF - jump less than/not greater nor
equal jle/jng LABEL jump if ZF1 or SF
? OF - jump less or equal/not greater
than
Typical use cmp al,bl jl there
jump if al is less than bl signed
comparison
45
SET condition Instruction
  • Sets a byte operand to 1 if a given condition is
    true, or it set the byte to 0 if the condition
    is false
  • Useful for saving flag contents
  • Syntax is SETcondition reg8 or mem8
  • condition includes the suffixes of all
    conditional jump instructions
  • EXAMPLE
  • setb T1 T1 ? 1 if CF1 else T1 ? 0
  • seto T1 T1 ? 1 if OF1 else T1 ? 0
  • setz al AL ? 1 if ZF1 else AL ? 0
  • setnc myFlag myFlag ? 1 if CF0 else myFlag ?
    0
  • setge byte ptr si set si to 1 if SF OF

46
Iteration Instruction, loop
  • Combination of decrement cx and conditional Jump
  • Decrements cx and if cx?0 jumps to LABEL
  • 386 loopw (cx operation) and loopd (ecx
    operation)
  • Example
  • ADDS PROC NEAR
  • mov cx, 100 cx ? 64h - number of words to add
  • mov 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, DF0
  • AGAIN mov bx, di bx ? di, save offset of BLOCK2
  • lodsw ax ? dssi, si?si2, di?di2
  • add ax, bx ax ? ax dsbx
  • mov di, bx di ? bx, restore di with
  • offset in BLOCK2
  • stosw esdi ? ax, si?si2, di?di2
  • loop AGAIN cx ? cx - 1, if cx?0 jump to AGAIN
  • ret ip ? sssp
  • ADDS ENDP

47
Procedures
  • Group of instructions that perform single task
  • (can be used as) a SUBROUTINE
  • call - invokes subroutine - pushes ip
  • ret - returns from subroutine - pops ip
  • Uses MASM directives PROC and ENDP
  • Must specify
  • NEAR - intrasegment
  • FAR - intersegment
  • Difference is op-code of ret
  • NEAR - c3h - pops IP
  • FAR - cbh - pops CS, pops IP

48
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 csbx

49
call Instruction - Example
mov si, OFFSET COMP call si . . . COMP
PROC NEAR push dx mov dx, 03f8h in al, dx
inc dx out dx, al pop dx ret COMP EN
DP
50
call Instruction - Example Explained
mov si, OFFSET COMP get offset of COMP
subroutine call si push ip,
ip?si . . . COMP PROC NEAR push dx Save
current contents of dx mov dx, 03f8h dx ? 03f8h
(an immediate data Xfer) in al, dx al receives
1 byte of data from I/O device with output
port address 03f8h inc dx dx?03f9h out dx, al
send 1 byte of data to I/O device input
port with address 03f9h pop dx restore dx to
value at call time ret ip?sssp,
sp?sp2 COMP ENDP
51
call Instruction with Indirect Address
  • Useful for choosing different subroutines at
    runtime
  • Can use a table (like the jump table example)

Assume bx contains 1, 2 or 3 for subroutine
desired TABLE DW ONE DW TWO DW THREE dec bx ad
d bx, bx mov di, OFFSET TABLE call csbxdi j
mp CONT ONE PROC NEAR ONE ENDP TWO PROC NEAR
TWO ENDP THREE PROC NEAR THREE ENDP CONT nop
52
call Instruction with Indirect Address
Table of addresses of subroutines TABLE DW ONE D
W TWO DW THREE bx contains 1, 2 or 3 - desired
subroutine dec bx bx ? 0, 1 or
2 add bx, bx bx ? 0, 2 or 4 mov di, OFFSET
TABLE di ? TABLE offset call csbxdi push
ip, ip?offset of subroutine jmp CONT ip ?
offset of nop instruction ONE PROC NEAR ONE END
P TWO PROC NEAR TWO ENDP THREE PROC NEAR THR
EE ENDP CONT nop
53
ret Instruction
  • NEAR - pops 16-bit value places in IP
  • FAR - pops 32-bit value places in CSIP
  • Type is determined by PROC directive
  • Other form of ret has immediate operand (8 bit)
  • The immediate operand is added to the SP
    after popping the return address
  • Example
  • ret 6

54
(No Transcript)
55
String Transfer Instructions
  • String Forms
  • movsb move string byte by byte
  • movsw move string word by word
  • EXAMPLE
  • movsb Copies 8 bits at DSSI to ESDI
  • New String Form (386)
  • movsd move string in double words

56
String Transfer Instructions
  • New mov forms (386)
  • movsx move string with sign extended
  • - Reads source as byte or word and sign extends
  • to word or double word before storing in
    destination
  • EXAMPLE
  • movsx cx, al cl get al
  • if MSB of al0 then
  • ch gets 00h
  • else ch gets ffh
  • movzx move string with zero extended
  • - Reads source as byte or word and zero extends
  • to word or doub. word before storing in
    destination
  • EXAMPLE
  • movzx cx, al ch gets 00h and cl gets al

57
Repeated String Move Example
58
Repeated String Move Example (Cont.)
59
Assembling/Linking
60
Running the String Move Program
61
Other String Instructions
lodsb loads al with contents of
dssi Inc/Dec si by 1 depending on
DF lodsw loads ax with dssi Inc/Dec si by
2 depending on DF lodsd loads eax with
dssi Inc/Dec si by 4 depending on
DF 386 stosb loads esdi with contents of
al Inc/Dec di by 1 depending on
DF stosw loads esdi with contents of
ax Inc/Dec di by 2 depending on
DF stosd loads esdi with contents of
eax Inc/Dec di by 4 depending on DF 386
62
Logic Instruction Types (386)
SHIFT shld ax, 12 Double precision logical shift
left shrd ax, 14 Double precision logical shift
right BIT TEST bt ax, 12 CF?12th bit from right
in ax bts bx, 8 CF?8th bit of bx and
bx8?1 btr cx, 1 CF?1st bit in cx and
cx1?0 btc dx, 2 CF?2nd bit of dx and
dx2?dx2 BIT SCAN bsf ax, bx ZF1 if all
bits in bx0 else ZF0 and ax gets index of
first set bit (1) starting from right (LSB) of
bx bsr ax, bx ZF1 if all bits in bx0 else
ZF0 and ax gets index of first set bit (1)
starting from left (MSB) of bx
63
Double Precision Shifts
  • 386
  • shld - Logical Shift Left
  • shrd - Logical Shift Right
  • Uses 3 Operands Instead of 2
  • Example
  • shrd ax, bx, 12 logical right shift of ax by 12
  • rightmost 12 bits of bx into
  • leftmost 12 bits of ax
  • Contents of bx remain unchanged !!!!!!!

64
String Scan Instruction, scas
  • scasb, scasw, scasd (386)
  • Compares al, ax, eax 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 BLOCK
  • cld DF ? 0, auto-increment mode
  • mov cx, 100 cx ? 64h, initialize counter to 100
  • xor al, al clear al
  • repne scasb test for 00h in location esdi
  • if esdi not equal to 00h then
  • cx ? cx - 1, di? di 1, repeat

65
Skip ASCII Space Character
lea di, STRING di ? offset of memory location
labeled STRING cld DF0 auto-increment
mode mov cx, 256 cx ? ffh, initialize counter to
256 mov al, 20h al ? , an ASCII ltspacegt
Character repe scasb while esdi20h, continue
scanning when cx0 or esdi not equal 20h
stop after stopping cx contains offset from
STRING where first non-20h resides (if not 0)
66
Compare String Instruction, cmps
  • cmpsb, cmpsw, cmpsd (386)
  • 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
    (NOTEds can equal es)
  • lea si, LINE si gets offset of location labeled
    LINE
  • lea di, TABLE di gets offset of location labeled
    TABLE
  • cld DF0, auto-increment mode
  • moc cx, 10 initialize counter register to 10
  • repe cmpsb while dssiesdi decrement cx and
    incr. si, di
  • if cx0 stop testing

67
Skip ASCII Space Character
lea di, STRING di ? offset of memory location
labeled STRING cld DF0 auto-increment
mode mov cx, 256 cx ? ffh, initialize counter to
256 mov al, 20h al ? , an ASCII ltspacegt
Character repe scasb while esdi20h, continue
scanning when cx0 or esdi not equal 20h
stop after stopping cx contains offset from
STRING where first non-20h resides (if not 0)
68
Summary
Write a Comment
User Comments (0)
About PowerShow.com