680 likes | 824 Views
Microprocessor System Design. Omid Fatemi Instructions (1) (omid@fatemi.net). Review. Flag instruction ADD and ADC A loop program Data entering MASM Directives. Outline. Data transfer operations Arithmetic operations Logic operation Control operations String operations.
E N D
Microprocessor System Design Omid Fatemi Instructions (1) (omid@fatemi.net)
Review • Flag instruction • ADD and ADC • A loop program • Data entering • MASM • Directives
Outline • Data transfer operations • Arithmetic operations • Logic operation • Control operations • String operations
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 <-- 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 hellocod ENDS END START ; ‘END label’ defines program entry
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 <-- 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
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
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)
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.
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
Arithmetic Instruction Summary add ax, bx ;axax+bx and set flags adc ax, bx ;axax+bx+CF(lsb) and set flags inc ax ;axax+1 and set flags aaa ;ASCII Adjust after Addition daa ;Decimal (BCD) Adjust after Addition 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 ;Flags are set according to ax-bx das ;Decimal (BCD) Adjust after Subtraction aas ;ASCII Adjust after Subtraction mul cx ;dx:ax ax * cx (unsigned) imul cx ;dx:ax ax * cx (2’s complement) aam ;ASCII Adjust after Multiplication div cl ;alax/cl Quot. AND ahax/cl Rem. idiv cx ;ax(dx:ax)/cx Quot. AND dx Rem. aad ;ASCII Adjust after Division
Addition Instruction Types add ax, bx ;axax+bx and set flags adc ax, bx ;axax+bx+CF(lsb) and set flags inc ax ;axax+1 and set flags aaa ;ASCII Adjust after Addition daa ;Decimal (BCD) Adjust after Addition add al, bl ;alal+bl and set flags add bx, 35afh ;bxbx+35afh add [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
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
Add with Carry BX AX 1 1 DX CX 0 1 CF=1 CF BX AX add ax, cx ;axax+cx and flags set adc bx, dx ;bxbx+dx+CF(lsb) and flags set 33-bit Sum Present in CF:bx:ax
Decimal Adjust after Addition • For BCD Arithmetic • “Corrects” Result • 0110 6 • +01117 • 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”
Decimal Adjust after Addition Example mov dx, 1234h ;dx1234 BCD mov bx, 3099h ;bx3099 BCD mov al, bl ;al99 BCD add al, dl ;alcdh illegal BCD, need 34+99=133 daa ;al33h (33 BCD) and CF=1 mov cl, al ;cl33 BCD mov al, bh ;al30 BCD adc al, dh ;al30h+12h+1=43h daa ;al43h (43 BCD) not illegal BCD this time mov ch, al ;cx=4333h BCD for 1234+3099
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’
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
Allowable Operands for add, sub Gen Reg + - Gen Reg Mem Loc Immediate Destination Source Gen Reg + - Mem Loc Immediate
Subtract with Borrow, sbb CF BX AX SI DI CF BX AX sub ax, di ;axax-di and CF gets borrow bit sbb bx, si ;bx(bx-CF(lsb))-si and flags set 32-bit Difference Present in bx:ax CF Indicates If Difference is Negative
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
Multiply Instructions • Product can be Twice the Size • 2 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, Unsigned mul bx ;dx:axbx*ax, Unsigned mul ebx ;edx:eaxebx*eax, Unsigned imul bl ;axal*bl, Signed imul bx ;dx:axbx*ax, Signed imul ebx ;edx:eaxebx*eax, Signed
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 ;cxdx*12h imul bx, [NUMBER], 12h ;bxds:(NUMBER)*12h
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
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
Logic Instruction Types BITWISE LOGICAL not ax ;1’s 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
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 ax=CF=OF=PF=AF=SF=0 and ZF=1 • ;does more than mov ax, 0h • ;faster than push 00h then popf
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 ;bx0205h mov dx, bx ;dx0205h shl bh, 4 ;bh20hor bl, bh ; bl = bh or bl = 20 or 05 = 25h xor bh, bh ;zero out bh, so bx = 0025 (BCD value) ;Now convert to binary - change 3235h into 0019h mov al, dh ;al02h mov cl, 10 ;cl0ah mul cl ;ax = 2 * 0Ah = 14h (decimal value is 20) add al, dl ;al14h+05h=19h (decimal value is 25)
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
CF REG 0 0 REG CF CF REG 0 REG CF 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
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 4*VALUE add ax, bx ;ax 5*VALUE shl bx, 3 ;bx 8*VALUE sub ax, bx ;ax (-3)*VALUE
Rotates rol - Rotate Left CF REG rcl - Rotate Through Carry Left CF REG ror - Rotate Right CF REG rcr - Rotate Through Carry Right CF REG
Example Using Rotates ;Multiply a 48-bit value in dx:bx:ax by 2 shl ax, 1 ;ax 2*ax rcl 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
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 • 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
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 > 0 loope/loopz LABEL ;same as loop but ZF=1 also required loopne/loopnz ;same as loop but ZF=0 also required INTERRUPTS int <immed8> ;Invoke the int. handler specified by immed8 into <immed8> ;same as int but OF=1 also iret ;Return from interrupt handler CONDITIONAL to follow
EB disp E9 disphi displo EA IP lo IP hi CS lo CS hi 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
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 ax+bx 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 - 2’s comp.)
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 2*si add si, OFFSET TABLE ;si si + <address of TABLE> mov ax, cs:[si] ;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 . .
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 2*si add 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 ONE DW TWO ZERO: ;code for ZERO option . . ONE: ;code for ONE option . . TWO: ;code for TWO option . .
Conditional Control Instruction SummarySimple Flag Branches Jump based on single flag 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/zero jne/jnz LABEL ;jump if ZF=0 - jump not equal/jump if zerojo LABEL ;jump if OF=1 - jump on overflow jno 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 even jnp/jpo LABEL ;jump if PF=0 - jump on no parity/parity odd
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 CF=ZF=0 - jump above-jump not below/equal jae/jnb LABEL ;jump if CF=0 - jump above/equal-jump not below jb/jnae LABEL ;jump if CF=1 - jump below-jump not above/equal jbe/jna LABEL ;jump if CF=1 or ZF=1 - jump equal - jump zero Typical use: cmp al,bl jb there ; jump if al is ‘below’ bl ; unsigned comparison
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 ZF=0 and (SF=OF) - jump greater/not less ; nor equal jge/jnl LABEL ;jump if SF=OF - 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 ZF=1 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
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 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
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 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, DF=0 • AGAIN: mov bx, di ;bx di, save offset of BLOCK2 • lodsw ;ax ds:[si], sisi+2, didi+2 • add ax, [bx] ;ax ax + ds:[bx] • mov di, bx ;di bx, restore di with • ; offset in BLOCK2 • stosw ;es:[di] ax, sisi+2, didi+2 • loop AGAIN ;cx cx - 1, if cx0 jump to AGAIN • ret ;ip ss:[sp] • ADDS ENDP
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
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]
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 ENDP
call Instruction - Example Explained mov si, OFFSET COMP ;get offset of COMP subroutine call si ;push ip, ipsi . . . 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 ;dx03f9h 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 ;ipss:[sp], spsp+2 COMP ENDP