260 likes | 568 Views
8086/88 Instructions set. 8086/88 Instruction Type Classifications. DATA TRANSFER General mov ax, [3000] ;ax gets contents of mem ARITHMETIC/LOGIC Arithmetic add ax, bx ;ax gets ax+bx Logical and ax, bx ;ax gets ax AND bx
E N D
8086/88 Instruction Type Classifications • DATA TRANSFER • General mov ax, [3000] ;ax gets contents of mem • ARITHMETIC/LOGIC • Arithmetic add ax, bx ;ax gets ax+bx • Logical and ax, bx ;ax gets ax AND bx • Shifting shr ax, 2 ;ax contents shifted-2 right • CONTROL TRANSFER • Conditional Jump jnz 2000 ;if ZF=0 then IP=2000 • Unconditional Jump JMP 4500 ; IP = 4500 • Subroutine call 3500 ;IP=3500 • iteration loop 4000
Data Transfer Instructions • Very Common Instruction: movdesti, 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 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 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 mulcx ;dx:ax ax * cx (unsigned) imulcx ;dx:ax ax * cx (2’s complement) divcl ;alax/cl Quot. AND ahax/cl Rem. idivcx ;ax(dx:ax)/cx Quot. AND dx Rem.
Example for 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) • Follows add,adcto “Adjust”
Decimal Adjust after Addition Example movdx, 1234h ;dx1234 BCD movbx, 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 movcl, al ;cl33 BCD mov al, bh ;al30 BCD adc al, dh ;al30h+12h+1=43h daa ;al43h (43 BCD) not illegal BCD this time movch, al ;cx=4333h BCD for 1234+3099
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 sbbbx, si ;bx(bx-CF(lsb))-si and flags set 32-bit Difference Present inbx:ax CF Indicates If Difference is Negative
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 rolbx, 3 ;Rotate left rorcx, 4 ;Rotate right rcl ax, 1 ;Rotate left through carry rcrdx, 6 ;Rotate right through carry
Logic Instruction Application (Masking Operations) XXXX XXXX (unknown byte) (AND) 0000 1111 (mask byte) 0000 XXXX (result) What if we wanted 1111 XXXX instead? EXAMPLE: Convert ASCII to BCD ; change 3235h into 0025h movbx, 3235h ;bx ‘25’ and bx, 0f0fh ;bx0205h shlbh, 4 ;bh20hor bl, bh ; bl = bh or bl = 20 or 05 = 25h xorbh, bh ;zero out bh, so bx = 0025 (BCD value)
Bit Test Instruction, test • Same as and But Result is Discarded • Only Affects Flags • ZF=1 if Tested Bit=0 and ZF=0 if Tested Bit=1 • Example: • 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 movbx, ax ;bx Word from memory with label VALUE shl ax, 2 ;ax 4*VALUE add ax, bx ;ax 5*VALUE shlbx, 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 rclbx, 1 ;bx 2*bx + CF(lsb) rcldx, 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 ( for example: shl ax, 3) • 2) Quantity in cl (for example: movcl, 3 • Shl ax, cl )
Program Control Instructions • Generally modify CS:IP • Causes modification in execution sequence (of instructions) • Classification • a) Jumps - Unconditional control transfers • b) Conditional Jump- Conditional control transfer • c) Iteration - More complex type of branch • d) Call Subroutine and RET (Return from Subroutine)
Branches or conditional Jumps 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
Procedures • Group of instructions that perform single task • (can be used as) a SUBROUTINE • Uses MASM directives: PROC and ENDP • Must specify • FAR - intrasegment • NEAR - intersegment
call Instruction • NEAR call: 3 bytes - 1 opcode and 2 for IP • FAR call: 5 bytes - 1 opcode, 2 for IP and 2 for CS ret Instruction • NEAR - pops 16-bit value places in IP • FAR - pops 32-bit value places in CS:IP
String Transfer Instructions • movsb ;Copies 8 bits at DS:SI to ES:DI and • ; Inc/Dec SI and DI by 1 • ; depending on DF • movsw ;Copies 16 bits at DS:SI and DS :SI+1 to ES:DI • ;and ES:DI+1 and • ; Inc/Dec SI and DI by 2 depending • ; on DF
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
Home work • فصل 2 کتاب مزیدی: برنامه نویسی به زبان اسمبلی • تمرینات: 11-12-13 • فصل 3 کتاب مزیدی: دستورات محاسباتی، منطقی و برنامه ها • تمرینات: 1-2-4-6-7-9-10-20-21 • مهلت تحویل: 3 هفته