微机原理ch04ARTHIMETICANDLOGICALINSTRUCTIONSANDPROGRAMS.doc_第1页
微机原理ch04ARTHIMETICANDLOGICALINSTRUCTIONSANDPROGRAMS.doc_第2页
微机原理ch04ARTHIMETICANDLOGICALINSTRUCTIONSANDPROGRAMS.doc_第3页
微机原理ch04ARTHIMETICANDLOGICALINSTRUCTIONSANDPROGRAMS.doc_第4页
微机原理ch04ARTHIMETICANDLOGICALINSTRUCTIONSANDPROGRAMS.doc_第5页
已阅读5页,还剩43页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

4 ARTHIMETIC AND LOGICAL INSTRUCTIONS AND PROGRAMS4.1 USING ADDITION AND SUBTRATION4.1.1 Addition of unsigned numbersThe form of the ADD/ADC instruction is:add dest, source;dest = dest + source adc dest, source;dest = dest + source + CFThe instructions ADD and ADC are used to add two operands. The destination operand can be:l a registerl in memoryThe source operand can be:l a registerl in memory, orl immediateRemember that memory-to-memory operation are NEVER allowed in 80x86 assembly language.The instruction could change any of the ZF, SF, AF, CF, or PF bit of the flag register, depending on the operands involved.Example:Show how the flag register is affected by:moval, 0f5haddal, 0bhSolution: F51111 0101+ 0B0000 1011-1000000 0000After the addition, the AL register (destination) contains 00H and the flags are as follows:CF = 1, since there is a carry out from d7SF = 0, the status of d7 of the resultPF = 1, the number of 1s is zero (zero is an even number)AF = 1, there is a carry out from d3 to d4ZF = 1, the result of the addition is zero (for 8 bits)4.1.1.1 Case 1: Addition of individual byte and word dataExample 1:Write a program to calculate the total of 5 bytes of data. The decimal data is as follow: 125, 235, 197, 91, and 48.title prog3-1A adding 5 bytes.model small.stack 64;-.datacountequ05data1db125, 235, 197, 91, 48org0008hsumdw?;-.codemainprocfarmovax, datamovds, axmovcx, count;CX is the loop countermov si, offset data1;SI is the data pointermov ax, 00;AX will be hold the sumback:addal, si;add the next byte to ALjncover;if no carry, continueincah;else accumulate the carry in AHover:incsi;increment the data pointerdeccx;decrement the loop counterjnzback;if not finished, go add next bytemovsum, ax;store summovah, 4chint21h;go back to DOSmainendpend mainCode segmentData segment and stack segmentExample 2:Write a program to calculate the total sum of 5 words of data. The decimal data is as follow: 27345, 28521, 28533, 30105, and 32375title prog3-1B adding 5 words.model small.stack 64;-.datacountequ05data2dw27345, 28521, 28533, 30105, 32375org0010hsumdw2 dup(?);-.codemainprocfarmovax, datamovds, axclc;clear carry before first additionmovcx, count;CX is the loop countermov si, offset data2;SI is the data pointermov ax, 00;AX will hold the summovbx, ax;BX will hold the carriesback:addax, si;add the next byte to AXadcbx, 0;add carry to BXincsi;increment the data pointerincsi;twice to point to next worddeccx;decrement the loop counterjnzback;if not finished, continue addingmovsum, ax;store summovsum+2, bx;store the carriesmovah, 4chint21h;go back to DOSmainendpend main4.1.1.2 Case 2: Addition of multiword numbersExample:Write a program that adds the following two multiword numbers and saves the result:data1 = 548FB9963CE7H and data2 = 3FCD4FA23B8DHtitle prog3-2 multiword addition .model small.stack 64;-.datadata1dq548FB9963CE7Horg0010hdata2dq3FCD4FA23B8DHorg0020hdata3dq?;-.codemainprocfarmovax, datamovds, axclc;clear carry before fist additionmov si, offset data1;SI is pointer for operand1mov di, offset data2;DI is pointer for operand2mov bx, offset data3;BX is pointer for the summov cx, 04;CX is the loop counterback:movax, si;move the first operand to AXadcax, di;add the secondoperand to AXmovbx, ax;store the sumincsi;point to next word of operand1incsiincdi;point to next word of operand2incdiincbx;point to next word of sumincbxloopback;if not finished, continue addingmovah, 4chint21h;go back to DOSmainendpend mainThere is a new instruction in the program, “LOOP XXXX”, which replaces the “DEC CX” and “JNZ XXXX”. The format of LOOP instruction is:loop target;loop until CX = 0Decrements CX by 1, then jumps to the offset indicated by the operand if CX is not zero, otherwise continues with the next instruction below the LOOP.4.1.2 Subtraction of unsigned numbers4.1.2.1 Subtractsub dest, source;dest = dest - source In subtraction, the 80x86 microprocessors use the 2s complement method. Although every CPU contains adder circuitry, it would be too cumbersome (and take too many transistors) to design separate subtraction circuitry. For this reason, the 80x86 uses internal adder circuitry to perform the subtraction command.Assuming that the 80x86 is executing simple subtract instruction, one can summarized the steps of the hardware of the CPU in executing the SUB instruction for unsigned numbers, as follows:1) Take the 2s complement of the subtrahend (source operand)2) Add it to the minuend (destination operand)3) Invert the carryThese three steps are performed for every SUB instruction by the internal hardware of the 80x86 CPU regardless of the source and destination of the operands as long as the addressing mode is supported. It is after these three steps that the result is obtained and the flag are set.Example 1:Show the steps involved in the following:mov al, 3fh;load AL = 3FHmov bh, 23h;load BH = 23Hsbb al, bh;subtract BH from AL, place result in ALSolution:AL3F 0011 1111 0011 1111-BH-23-0010 0011+ 1101 1101 (2s complement)- 1C1 0001 1100 CF=0 (step 3)The flags would be set as follows: CF = 0, ZF = 0, AF = 1, PF = 0, and SF = 0.After the execution of SUB, if CF = 0, the result is positive; if CF = 1, the result is negative and the destination has the 2s complement of the result.Example 2:Analyze the following program;from the data segmentdata1db4chdata2db6ehdata3db?;from the code segmentmov dh, data1;load DH with data1 value(4C)subdh, data2;subtract data2(6E) from DH(4C)jncnext;if CF=0 jump to next targetnotdh;if CF=1 then take 1s complementincdh;and increment to 2s complementnextmovdata3, dh;save the DH in data3Solution:Following the three steps for “SUB DH, DATA2” 4C0100 11000100 1100- 6E0110 1110 2s complement+1001 0010 - 2201101 1110 CF=1(step 3)not dh0010 0001inc dh0010 0010The format of the NOT instruction:not dest;dest = dest 1s complement of dest(逐位求反)Each bit is inverted.求反加1就是求补码。4.1.2.2 Subtract with borrowsbb dest, source;dest = dest CF - sourceThis instruction is used for multibyte (multiword) numbers and will take care of the borrow of the lower operand.If the carry flag is 0, SBB works like SUB. If the carry is 1, SBB subtract 1 from the result.Example:Analyze the following program:data_add62562fahdata_bdd412963bhresultdd?.movax, word ptr data_a;AX = 62FAsubax, word ptr data_b;sub 963B from AXmovword ptr result, ax;save the resultmovax, word ptr data_a+2;AX = 0625sbbax, word ptr data_b+2;sub 0412 with borrowmovword ptr result+2, ax;save the resultSolution:After the SUB, AX = 62FA 963B = CCBF and the carry flag is set. Since CF = 1, when SBB is executed, AX = 0625 0412 1 = 212. Therefore, the value stored in RESULT is 0212CCBF.The PTR (pointer) data specifier directive is widely used to specify the size of the operand when it differs from the defined size.Format:byte ptrword ptrdword ptrqword ptr4.2 UNSIGNED MULTIPLICATION AND DIVISIONIn multiplying or dividing two numbers in 80x86 microprocessor, the use of registers AX, AL, AH, and DX is necessary.4.2.1 Multiplication of unsigned numbersFormat:mul source;AX = source AL, or;DX:AX = source AXAffected flags: OF, CF. Unpredictable: SF, ZF, AF, PF图41 Illustration of the multiplicationIn discussing multiplication, the following cases will be examined:l byte times bytel word times wordl word times byte4.2.1.1 Case 1: byte byteIn byte by byte multiplication, one of the operand must be in the AL register and the second operand can be either in a register or in memory. After the multiplication, the result is in AX.;from the data segment:data1db25hdata2db65hresult1dw?;from the code segment:;- register addressing mode -moval, data1;a byte is moved to ALmovbl, data2;a byte is moved to BLmulbl;AX = 25H x 65Hmovresult1, ax;the result is saved;- direct addressing mode -moval, data1muldata2movresult1, ax;- register indirect addressing mode -moval, data1movsi, offset data2mulbyte ptr simovresult1, ax4.2.1.2 Case 2: word wordIn the word by word multiplication, one operand must be in AX and the second operand can be in a register or memory. After the multiplication, registers AX and DX will contain the result.;from the data segment:data3dw2378hdata4dw2f79hresult2dw2 dup(?);from the code segment:mov ax, data1;load first operand to AXmuldata4;multiply it bye the second operandmovresult2, ax;store the lower word resultmovresult2+2, dx;store the higher word result4.2.1.3 Case 3: word byteThis is similar to word by word multiplication except that AL contains the byte operand and AH must be set to zero.;from the data segment:data5db6bhdata6dw12C3hresult3dw2 dup(?);from the code segment:moval, data5;AL hold byte operandsubah, ah;AH must be clearedmul data6;byte in AL multiplied by ;word operandmovbx, offset result3;bx points to the storages;for productmovbx, ax;AX holds lower wordmovbx+2, dx;DX holds higher word表 41 Unsigned Multiplication SummaryMultiplicationOperand 1Operand 2Resultbyte byteALregister or memoryAXword wordAXregister or memoryDX AXword byteAL = byte, AH = 0register or memoryDX AX4.2.2 Division of unsigned numbersFormat:div source;divide AX by source, or;divide DX:AX by sourceUnpredictable flags: OF, SF, ZF, AF, PF, CF.图42 Illustration of the division4.2.2.1 Case 1: byte / byteIn dividing a byte by a byte, the numerator must be in the AL register, and AH must be set it zero. The denominator cannot be immediate but can be in a register or in memory.After the DIV instruction is performed, the quotient is in AL, and the remainder is in AH.;from the data segment:data7db95data8db10quot1db?remain1db?;from the code segment:;-;using immediate addressing mode will given an errormoval, data7;move data into ALsubah, ah;clear AHdiv10;NOT ALLOWED!;-;allowable modes includes:;-;using direct modemoval, data7;AL holds numeratorsubah, ah;AH must be cleareddivdata8;divide AX by data8movquot1, al;quotient = AL = 09movremain1, ah;remainder = AH = 05;-;using register modemoval, data7;AL holds numeratorsubah, ah;AH must be clearedmovbh, data8;move denominator to registerdivbh;divide AX by bhmovquot1, al;quotient = AL = 09movremain1, ah;remainder = AH = 05;-;using register indirect modemoval, data7;AL holds numeratorsubah, ah;AH must be clearedmovbx, offset data8;BX holds the offset of data8divbyte ptr bx;divide AX by data8movquot1, al;quotient = AL = 09movremain1, ah;remainder = AH = 054.2.2.2 Case 2: word / wordIn this case numerator is in AX and DX must be cleared. The denominator can be in a register or memory. After the DIV, AX will have the quotient and remainder will be in DX.movax, 10050;AX holds numeratorsubdx, dx;DX must be clearedmovbx, 100;BX holds denominatordivbxmovquot2, ax;quotient = AX = 64H = 100movrenaim2, dx;remainder = DX = 32H = 504.2.2.3 Case 3: word / byteAgain, numerator is in AX and the denominator can be in a register or memory. After the DIV instruction, AL will contains the quotient and AH will contains the remainder. The maximum quotient is FFH.movax, 2055;AX holds numeratormovcl, 100;BX holds denominatordivclmovquot3, al;AL holds the quotientmovrenaim3, ah;AH holds the remainder4.2.2.4 Case 4: double word / wordThe numerator is in AX and DX, with the most significant word in DX and the least significant word in AX. the denominator can be in a register or in memory.After the DIV instruction, the quotient will in AX, the remainder in DX. The maximum quotient is FFFFH.;from the data segmentdata9dd105432data10dw10000quto4dw?remain4dw?;from the code segmentmov ax, word ptr data9;AX holds lower wordmov dx, word ptr data9+2;DX holds higher worddivdata2mov quot4, ax;AX holds quotientmovremain4, dx;DX holds remainderHow does the CPU know that is must be the double word in DX:AX for the numerator?The 8086/88 automatically uses DX:AX as the numerator anytime the denominator is a word in size.表 42 Unsigned Division SummaryDivisionNumerator(分子)Denominator(分母)QuotientRemainderbyte byteAL = byte, AH = 0register or memoryALAHword wordAX = word, DX = 0register or memoryAXDXword byteAX = wordregister or memoryALAHdouble word wordDX AX = double wordregister or memoryAXDX4.3 LOGICAL INSTRCUTION AND SAMPLE PRGROM4.3.1 ANDLogical AND.Format: and dest, source;dest = dest AND sourceFunction: Perform a logical AND on the operand, bit by bit, storing the result in the destination. The destination operand can be a register or in memory. The source operand can be a register, in memory, or immediate.Flags: Affected: CF = 0, OF = 0, SF, ZF, PF. Unpredictable: AF.4.3.2 ORLogical OR.Format: or dest, sourceFunction: Perform a logical OR on the bits of two operand, bit by bit, replacing the destination with the result. Often used to turn a bit on. The destination operand can be a register or in memory. The source operand can be a register, in memory, or immediate.Flags: Affected: CF = 0, OF = 0, SF, ZF, PF. Unpredictable: AF.4.3.3 XORExclusive XOR.Format: xor dest, sourceFunction: Perform a logical exclusive OR on the bits of two operand, bit by bit and puts the result in the destination.Flags: Affected: CF = 0, OF = 0, SF, ZF, PF. Unpredictable: AF.4.3.4 Compare of unsigned numbers Format: cmp dest, source;set flag as if ”sub dest, source”Function: Compares two operands of the same size. The source and the destination operands are not altered. Performs comparison by subtracting the source operand from the destination and sets flags as if SUB were performed. Flags: Affected: OF, SF, ZF, AF, PF, CF. The relevant flags as follows:表 43 Relevant flags of CMPCFZFSFOFdest source000SFdest = source010SFdest 0MOVAH,4CHINT21H;go back to dosMAINENDPENDMAIN4.3.5 IBM BIOS method of converting from lowercase to uppercase2357;- CONVERT ANY LOWERCASE TO UPPERCASE2358EBFB2359K60:;LOWER TO UPPEREBFB3C612360CMPAL, a;FIND OUT IF ALPHABETICEBFD72062361JBK61;NOT_CAPS_STATEEBFF3C7A2362CMPAL, zEC0177022363JAK61;NOT_CAPS_STATEEC032C202364SUBAL, a-A;CONVERT TO UPPERCASEEC052365K61:EC058B1E1C002366MOVBX, BUFFER_TAIL;GET THE END POINTER;TO THE BUFFER4.3.6 Test bitsFormat: test dest, source;perform dest AND sourceFunction: Performs a logical AND on two operands, setting flags but leaving the contents of both destination and source operand unchanged. While the AND instruction changes the contents of both destination and source and the flag bits, the TEST instruction changes only the flag bits.Flags: Affected: OF, SF, ZF, PF, CF. Unpredictable: AF

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论