1Arithmetic/Logic Instructions• Basic Mathematical Operations– Signed/Unsigned Integer Only– Default is 2’s Complement– Computes Result ANDModifies Status Flags• Logic Instructions– Bit Level– Word Level– Computes Results ANDModifies FlagsArithmetic Instruction Summaryadd ax, bx ;ax<-ax+bx and set flagsadc ax, bx ;ax<-ax+bx+CF(lsb) and set flagsinc ax ;ax<-ax+1 and set flagsaaa ;ASCII Adjust after Additiondaa ;Decimal (BCD) Adjust after Additionsub ax, bx ;ax<-ax-bx and set flagssbb ax, bx ;ax<-(ax-CF)-bx and set flagsdec ax ;ax<-ax-1neg ax ;ax<-(-1)*(ax) - 2’s Complementcmp ax, bx ;ZF is 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 ;al<-ax/cl Quot. AND ah<-ax/cl Rem.idiv cx ;ax<-(dx:ax)/ax Quot. AND dx <- Rem.aad ;ASCII Adjust after Division2Addition Instruction Typesadd ax, bx ;ax<-ax+bx and set flagsadc ax, bx ;ax<-ax+bx+CF(lsb) and set flagsinc ax ;ax<-ax+1 and set flagsaaa ;ASCII Adjust after Additiondaa ;Decimal (BCD) Adjust after Additionadd al, bl ;al<-al+bl and set flagsadd bx, 35afh ;bx<-bx+35afhadd [bx], al ;ds:bx<-ds:bx+al add cl, [bp] ;cl<-cl+ss:bpadd al, [ebx] ;al<-al+ds:ebxadd bx, TEMP[di] ;bx<-bx+ds:(TEMP+di)add bx, [eax+2*ecx] ;bx<-bx+ds:(eax+(2*ecx))Scaled Index Addressing: 386+ecx may contain 1, 2 , 4 onlyIncrement Examplesinc bl ;bl<-bl+1 and set flagsinc BYTE POINTER [bx] ;Byte at ds:bx<-ds:bx+1New MASM Directive: BYTE POINTER00ffh → 0000hinc [bx] ;Word at ds:bx<-ds:bx+100ffh → 0100hinc DATA1 ;ds:DATA1<-ds:DATA1+13Add with Carryadd ax, cx ;ax<-ax+cx and flags setadc bx, dx ;bx<-bx+dx+CF(lsb) and flags set33-bit Sum Present in CF:bx:axBXDXAXCX1 10 1CF=1BXAXCFDecimal Adjust after Addition• For BCD Arithmetic• “Corrects” Result0110 6+0111 71101 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”4Decimal Adjust after Addition Examplemov dx, 1234h ;dx<--1234 BCDmov bx, 3099h ;bx<--3099 BCDmov al, bl ;al<--99 BCDadd al, dl ;al<--cdh illegal BCD, need 34+99=133daa ;al<--33h (33 BCD) and CF=1mov cl, al ;cl<--33 BCDmov al, bh ;al<--30 BCDadc al, dh ;al<--30h+12h+1=43hdaa ;al<--43h (43 BCD) not illegal BCD this timemov ch, al ;cx=4333h BCD for 1234+3099ASCII Adjust after Addition• For Addition Using ASCII Encoded Numbers30h through 39h Represent ‘0’ through ‘9’• ax is Default Source and Destination for aaa31 ‘1’+39 ‘9’6a ‘10’→should be 3130h(6ah is incorrect ASCII result ‘j’)mov ax, 31h ;ax<--0031h=‘1’add al, 39h ;ax<--31h+39h=006ah=‘<nul>j’aaa ;ax<--0100h (this is BCD of result)add ax, 3030h ;Convert from BCD to ASCII;ax<--0100h+3030h=3130h=‘10’5Subtraction Instruction Typessub ax, bx ;ax<-ax-bx and set flagssbb ax, bx ;ax<-(ax-CF)-bx and set flagsdec ax ;ax<-ax-1neg ax ;ax<-(-1)*(ax) - 2’s Complementcmp ax, bx ;ZF is set according to ax-bxdas ;Decimal (BCD) Adjust after Subtractionaas ;ASCII Adjust after SubtractionAllowable Operands for add, subGen RegGen RegMem LocImmediate+-Gen RegMem LocImmediate+-Destination Source6Subtract with Borrow, sbbsub ax, di ;ax<--ax-di and CF gets borrow bitsbb bx, si ;bx<-(bx-CF(lsb))-si and flags set32-bit Difference Present in bx:axCF Indicates If Difference is NegativeBXSIAXDICFBXAXCFMultiplication• 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 Mnemonic7Multiply 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 Unpredictablemul bl ;ax<--al*bl, Unsignedmul bx ;dx:ax<--bx*ax, Unsignedmul ebx ;edx:eax<--ebx*eax, Unsignedimul bl ;ax<--al*bl, Signedimul bx ;dx:ax<--bx*ax, Signedimul ebx ;edx:eax<--ebx*eax, SignedSpecial Immediate Multiply Instruction• 286+• Uses imul Mnemonic but with 3 Operandsfirst: 16-bit dest. registersecond: reg/mem locationthird: 8/16-bit immediate value•Always Performs Signed Multiplication•Product is Limited to 16-bitsimul cx, dx, 12h ;cx<--dx*12himul bx, NUMBER, 1000h ;bx<--ds:NUMBER*12h8Division• 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 ImpliedSizeDividendQuotientRemainder8 bitsax al ah16 bitsdx:ax ax dx32 bitsedx:eax eax edxDivision Instruction Examples• idiv Signed and div Unsigneddividend / divisor = quotient, rmdrdiv cx ;dx:ax is divided by value in cx;unsigned quotient is placed in ax;positive remainder is placed in dxidiv ebx ;edx:eax is divided by value in ebx;signed quotient is placed in eax;remainder (ALWAYS same sign as ;dividend) is placed in
View Full Document