68
University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) ([email protected])

University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) ([email protected])

Embed Size (px)

Citation preview

Page 1: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 1

Microprocessor System Design

Omid Fatemi

Instructions (1)

([email protected])

Page 2: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 2

Review

• Flag instruction

• ADD and ADC

• A loop program

• Data entering

• MASM

• Directives

Page 3: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 3

Outline

• Data transfer operations

• Arithmetic operations

• Logic operation

• Control operations

• String operations

Page 4: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 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 segmentdos_print EQU 9 ;define a constant via EQUstrng DB 'Hello World',13,10,'$' ;Define the character stringhellodat ENDS

hellocod SEGMENT BYTE 'CODE' ;Define the Code segmentSTART: mov ax, SEG hellodat ;ax <-- data segment start address mov ds, ax ;ds <-- initialize data segment register mov ah, dos_print ;ah <-- 9 DOS 21h string function mov dx,OFFSET strng ;dx <-- beginning of string int 21h ;DOS service interrupt mov ax, 4c00h ;ax <-- 4c DOS 21h program halt function int 21h ;DOS service interrupthellocod ENDS END START ; ‘END label’ defines program entry

Page 5: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 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 constantstrng DB 'Hello World',13,10,'$' ;Define the character string

.code START: mov ax, SEG strng ;ax <-- data segment start address mov ds, ax ;ds <-- initialize data segment register mov ah, dos_print ;ah <-- 9 DOS 21h string function mov dx,OFFSET strng ;dx <-- beginning of string int 21h ;DOS service interrupt mov ax, 4c00h ;ax <-- 4c DOS 21h program halt function int 21h ;DOS service interrupt

END START

Page 6: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 6

Masm Assembler Directives

end label end of program, label is entry point

proc far|near 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

Page 7: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 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)

Page 8: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 8

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.

Page 9: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 9

Arithmetic

Page 10: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 10

Arithmetic/Logic Instructions

• Basic Mathematical Operations

– Signed/Unsigned Integer Only– Default is 2’s Complement– Computes Result AND Modifies Status Flags

• Logic Instructions

– Bit Level– Word Level– Computes Results AND Modifies Status Flags

Page 11: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 11

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 flagsaaa ;ASCII Adjust after Additiondaa ;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 Subtractionaas ;ASCII Adjust after Subtractionmul cx ;dx:ax ax * cx (unsigned)imul cx ;dx:ax ax * cx (2’s complement)aam ;ASCII Adjust after Multiplicationdiv cl ;alax/cl Quot. AND ahax/cl Rem.idiv cx ;ax(dx:ax)/cx Quot. AND dx Rem.aad ;ASCII Adjust after Division

Page 12: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 12

Addition Instruction Types

add ax, bx ;axax+bx and set flagsadc ax, bx ;axax+bx+CF(lsb) and set flagsinc ax ;axax+1 and set flagsaaa ;ASCII Adjust after Additiondaa ;Decimal (BCD) Adjust after

Addition add al, bl ;alal+bl and set flagsadd bx, 35afh ;bxbx+35afhadd [bx], al ;ds:(bx)ds:(bx)+al add cl, [bp] ;clcl+ss:(bp)add al, [ebx] ;alal+ds:(ebx)add bx, TEMP[di] ;bxbx+ds:(TEMP+di)add bx, [eax+2*ecx] ;bxbx+ds:(eax+(2*ecx))

Scaled Index Addressing: 386+ecx may contain 1, 2 , 4 only

Page 13: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 13

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

Page 14: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 14

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

Page 15: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 15

Decimal Adjust after Addition

• For BCD Arithmetic

• “Corrects” Result

0110 6 +0111 7 1101 13should 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”

Page 16: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 16

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

Page 17: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 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 ;ax0031h=‘1’add al, 39h ;ax31h+39h=006ah=‘<nul>j’aaa ;ax0100h (this is BCD of result)add ax, 3030h ;Convert from BCD to ASCII

;ax0100h+3030h=3130h=‘10’

Page 18: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 18

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

aas ;ASCII Adjust after Subtraction

Page 19: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 19

Allowable Operands for add, sub

Gen Reg

Gen Reg

Mem Loc

Immediate

+-

Gen Reg

Mem Loc

Immediate

+-

Destination Source

Page 20: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 20

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

Page 21: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 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

Page 22: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 22

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 (faster)

•OF=CF=1 means EXT product size (slower)

•AF, PF, ZF, SF Contents Unpredictable

mul bl ;axal*bl, Unsignedmul bx ;dx:axbx*ax, Unsignedmul ebx ;edx:eaxebx*eax, Unsignedimul bl ;axal*bl, Signedimul bx ;dx:axbx*ax, Signedimul ebx ;edx:eaxebx*eax, Signed

Page 23: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 23

Special Immediate Multiply Instruction

• 286+

• Uses imul Mnemonic but with 3 Operandsfirst: 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 ;cxdx*12himul bx, [NUMBER], 12h ;bxds:(NUMBER)*12h

Page 24: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 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

Size Dividend Quotient Remainder8 bits ax al ah16 bits dx:ax ax dx32 bits edx:eax eax edx

Page 25: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 25

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

idiv ebx ;edx:eax is divided by value in ebx;signed quotient is placed in eax;remainder (ALWAYS same sign as ;dividend) is placed in edx

Page 26: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 26

Logical Instructions

Page 27: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 27

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 carry

rcr dx, 6 ;Rotate right through carry

Page 28: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 28

Bit Level Logic

and, or, xor, not, test, bt, btc, btc, btr, bts

• 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 ;does more than mov ax, 0h ;faster than push 00h then popf

Page 29: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 29

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 to Binary

;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);Now convert to binary - change 3235h into 0019hmov al, dh ;al02hmov cl, 10 ;cl0ahmul cl ;ax = 2 * 0Ah = 14h (decimal value is 20)add al, dl ;al14h+05h=19h (decimal value is 25)

Page 30: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 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

• ZF=1 if Tested Bit=0 and ZF=0 if Tested Bit=1

test al, 1 ;XXXX XXXX (AND) 0000 0001

test al, 128 ;XXXX XXXX (AND) 1000 0000

Page 31: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 31

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

Page 32: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 32

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

Page 33: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 33

Rotatesrol - Rotate Left

REGCF

rcl - Rotate Through Carry Left

ror - Rotate Right

rcr - Rotate Through Carry Right

REGCF

REGCF

REGCF

Page 34: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 34

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

Page 35: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 35

Program Control Instructions

Page 36: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 36

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 flushes/refills inst. queue

• Classificationa) Jumps - Unconditional control transfers (synchronous)

b) Branches - Conditional control transfer

c) Interrupts - Unconditional control transfers (asynchronous)

d) Iteration - More complex type of branch

Page 37: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 37

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 ;same as loop but ZF=0 also required

INTERRUPTSint <immed8> ;Invoke the int. handler specified by immed8 into <immed8> ;same as int but OF=1 alsoiret ;Return from interrupt handler

CONDITIONAL to follow

Page 38: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 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

EB disp

E9 disphi displo

EA IP lo IP hi CS lo CS hi

Page 39: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 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 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.)

Page 40: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 40

Indirect Jump

;assume that si contains either 0, 1 or 2add si, si ;si 2*siadd si, OFFSET TABLE ;si si + <address of TABLE>mov ax, cs:[si] ;ax gets an address from the jump tablejmp ax ;ip ax

;the following jump TABLE is defined in the code segment!!!!TABLE: DW ZERO

DW ONEDW TWO

ZERO: ;code for ZERO option..

ONE: ;code for ONE option..

TWO: ;code for TWO option..

• Address of target is in register

• Does NOT add disp to IP - Transfer REG contents to IP

Page 41: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 41

Indirect Addressed Jump

;assume that si contains either 0, 1 or 2add si, si ;si 2*siadd si, OFFSET TABLE ;si si + <address of TABLE>jmp cs:[si] ;ip gets an address from the jump table

;the following jump TABLE is defined in the code segment!!!!TABLE: DW ZERO

DW ONEDW TWO

ZERO: ;code for ZERO option..

ONE: ;code for ONE option..

TWO: ;code for TWO option..

• Address of target is in register

• Does NOT add disp to IP - Transfer MEM contents to IP

Page 42: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 42

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

Page 43: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 43

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

Page 44: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 44

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

Page 45: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 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

EXAMPLEsetb T1 ;T1 1 if CF=1 else T1 0

seto T1 ;T1 1 if OF=1 else T1 0

setz al ;AL 1 if ZF=1 else AL 0

setnc myFlag ;myFlag 1 if CF=0 else myFlag 0

setge byte ptr [si] ;set [si] to 1 if SF = OF

Page 46: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 46

Iteration Instruction, loop

• Combination of decrement cx and conditional Jump

• Decrements cx and if cx0 jumps to LABEL

• 386+ loopw (cx operation) and loopd (ecx operation)

Example:ADDS PROC NEAR

mov 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

Page 47: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 47

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

Page 48: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 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 cs:[bx]

Page 49: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 49

call Instruction - Example

mov si, OFFSET COMPcall si

.

.

.COMP PROC NEAR

push dxmov dx, 03f8hin al, dxinc dxout dx, alpop dxret

COMP ENDP

Page 50: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 50

call Instruction - Example Explained

mov si, OFFSET COMP ;get offset of COMP subroutinecall si ;push ip, ipsi

.

.

.COMP PROC NEAR

push dx ;Save current contents of dxmov 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 03f8hinc dx ;dx03f9hout dx, al ;send 1 byte of data to I/O device

; input port with address 03f9hpop dx ;restore dx to value at call timeret ;ipss:[sp], spsp+2

COMP ENDP

Page 51: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 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 desiredTABLE DW ONE

DW TWODW THREEdec bxadd bx, bxmov di, OFFSET TABLEcall cs:[bx+di]jmp CONT

ONE PROC NEAR…

ONE ENDPTWO PROC NEAR

…TWO ENDPTHREE PROC NEAR

…THREE ENDPCONT: nop

Page 52: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 52

call Instruction with Indirect Address

;Table of addresses of subroutinesTABLE DW ONE

DW TWODW THREE

;bx contains 1, 2 or 3 - desired subroutinedec bx ;bx 0, 1 or 2add bx, bx ;bx 0, 2 or 4mov di, OFFSET TABLE ;di TABLE offsetcall cs:[bx+di] ;push ip, ipoffset of subroutinejmp CONT ;ip offset of nop instruction

ONE PROC NEAR…

ONE ENDPTWO PROC NEAR

…TWO ENDPTHREE PROC NEAR

…THREE ENDPCONT: nop

Page 53: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 53

ret Instruction

NEAR - pops 16-bit value places in IPFAR - pops 32-bit value places in CS:IP

• 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

Page 54: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 54

Page 55: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 55

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

• New String Form (386+):

movsd ;move string in double words

Page 56: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 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 al=0 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

Page 57: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 57

Repeated String Move Example;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; ; This is an example program which shows how ; ; the string move instruction works. ; ; ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; strngstk SEGMENT BYTE STACK 'STACK' ;Define the stack segment DB 100h DUP(?) ;Set stack size to 100 bytes strngstk ENDS strngdat SEGMENT BYTE 'DATA' ;Define the data segment strng1 DB 'This is string 1',13,10,'$' ;Define the first string strng2 DB 'THIS IS STRING 2',13,10,'$' ;Define the second string crlf DB 13,10,'$' ;Space to new line string strngdat ENDS strngcod SEGMENT BYTE 'CODE' ;Define the Code segment START: mov ax, SEG strngdat ;ax <-- data segment start address mov ds, ax ;ds <-- initialize data segment register mov es, ax ;es <-- initialize extra segment register ; ; Print the strings to the display before moving them ; mov ah, 9 ;ah <-- 9 DOS 21h string function lea dx, strng1 ;dx <-- offset of first string int 21h ;DOS service interrupt lea dx, strng2 ;dx <-- offset of second string int 21h ;DOS service interrupt lea dx, crlf ;dx <-- offset of crlf string int 21h ;DOS service interrupt

Page 58: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 58

Repeated String Move Example (Cont.) ; ; Now do a repeated string move byte by byte ; cld ;Autoincrement set DF=0 lea si, strng2 ;Source is second string lea di, strng1 ;Destination is first string mov cx, 19 ;Strings have 19 (decimal) chars. rep movsb ;Repeated stirng move from 2 to 1 ; ; Print the strings to the display after moving them ; mov ah, 9 ;ah <-- 9 DOS 21h string function lea dx, strng1 ;dx <-- offset of first string int 21h ;DOS service interrupt lea dx, strng2 ;dx <-- offset of second string int 21h ;DOS service interrupt lea dx, crlf ;dx <-- offset of crlf string int 21h ;DOS service interrupt ; ; Invoke DOS interrupt that returns processor to OS ; mov ax, 4c00h ;ax <-- 4c DOS 21h program halt function int 21h ;DOS service interrupt strngcod ENDS END START

Page 59: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 59

Assembling/Linking

Page 60: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 60

Running the String Move Program

Page 61: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 61

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

lodsd ;loads eax with ds:si;Inc/Dec si by 4 depending on DF;386+

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

stosd ;loads es:di with contents of eax;Inc/Dec di by 4 depending on DF;386+

Page 62: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 62

Logic Instruction Types (386+)

SHIFTshld ax, 12 ;Double precision logical shift leftshrd ax, 14 ;Double precision logical shift right

BIT TESTbt ax, 12 ;CF12th bit from right in axbts bx, 8 ;CF8th bit of bx and bx[8]1btr cx, 1 ;CF1st bit in cx and cx[1]0btc dx, 2 ;CF2nd bit of dx and dx[2]dx[2]’

BIT SCANbsf ax, bx ;ZF=1 if all bits in bx=0

;else ZF=0 and ax gets index of first;set bit (1) starting from right (LSB) of bx

bsr ax, bx ;ZF=1 if all bits in bx=0;else ZF=0 and ax gets index of first;set bit (1) starting from left (MSB) of bx

Page 63: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 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 !!!!!!!

Page 64: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 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 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

Page 65: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 65

Skip ASCII Space Character

lea di, STRING ;di offset of memory location labeled STRINGcld ;DF=0 auto-increment modemov cx, 256 ;cx ffh, initialize counter to 256mov al, 20h ;al ‘ ’, an ASCII <space> Characterrepe scasb ;while es:di=20h, continue scanning

;when cx=0 or es:di not equal 20h stop;after stopping cx contains offset from ;STRING where first non-20h resides (if not 0)

Page 66: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 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 (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

Page 67: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 67

Skip ASCII Space Character

lea di, STRING ;di offset of memory location labeled STRINGcld ;DF=0 auto-increment modemov cx, 256 ;cx ffh, initialize counter to 256mov al, 20h ;al ‘ ’, an ASCII <space> Characterrepe scasb ;while es:di=20h, continue scanning

;when cx=0 or es:di not equal 20h stop;after stopping cx contains offset from ;STRING where first non-20h resides (if not 0)

Page 68: University of Tehran 1 Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)

University of Tehran 68

Summary