200 likes | 348 Views
PART D-2. Processing Binary Data. 二进制数运算. 一、 Arithmetic Carry and Overflow ( 算术运算的进位和溢出 ) 1 . The Express Limit of Binary Data Unsigned : 8 位 —— 0 ~ 255 ; 16 位 —— 0 ~ 65535 Signed : 8 位 —— -128 ~ +127 ; 16 位 —— -32768 ~ +32767
E N D
PART D-2 Processing Binary Data 二进制数运算
一、Arithmetic Carry and Overflow (算术运算的进位和溢出) 1.The Express Limit of Binary Data Unsigned: 8位—— 0~255; 16位—— 0~65535 Signed: 8位—— -128~+127; 16位—— -32768~+32767 2. 运算结果将影响OF(溢出)、CF(进位)等标志位。 For Example 1:
二、Addition and Subtraction of Binary Data (二进制数的加、减运算) ADD Object,Source ——字节或字相加 ADC Object,Source ——带进位加 INC Object ——字节或字加1 SUB Object,Source ——字节或字相减 SBB Object,Source ——带借位减法(有借位CF=1,否则CF=0) DEC Object ——字节或字减1 NEG Object ——求补(0-目的→目的)
For Example: 从键盘上输入两个一位十进制数字,并将其转换成二进制数,再将它们相加后以两位十进制数字的形式输出结果。 (如,2+3=05 or 9+7=16)
⑴ Flow Chart: 〖19-8〗
⑵ Program: CODE SEGMENT ; 数字字符l → BL ASSUME CS:CODE ; 数字字符2 → DH START:MOV DL, ’?’ MOV AH, 02 INT 21H ;显示提示符“?” MOV AH, 01 INT 21H ;键入第一个0~9数字→AL MOV BL, AL SUB BL, 30h ;将第一个数字字符转换成数字 MOV DL,’+’ MOV AH, 02 INT 21H ;显示一个“+”号 MOV AH, 01 INT 21H ;键入第M个0~9数字→AL 〖19-9〗
SUB AL, 30H ;转换成数字 MOV DH, AL MOV DL,’=’ MOV AH, 02 INT 21H ;显示“=” MOV AL, DH ADD AL,BL ;(AL)十(BL)→AL MOV AH, 0 MOV BL, 10 DIV BL ;(AX)÷10的商→AL,余数→AH MOV BL,AH ; 保存余数(个位)→BL MOV DL,AL ;商(十位数)→DL ADD DL,30H ;将其转换成ASCll码 MOV AH, 02 INT 21H ;显示十位数字 MOV DL,BL ;个位数字→DL
ADD DL,30H ;转换成ASCll码 MOV AH,02 INT 21H ;显示个位数 MOV DL,ODH INT 21H ;回车 MOV DL,OAH INT 21H ;换行 MOV AH,4CH INI 21H CODE ENDS END START 〖19-11〗
EXAMPLE: 一个双字相加的实例P.226 WORD1A:WORD1B+WORD2A:WORD2BWRD3A:WORD3B 〖19-12〗
MULTIPLICATION MUL: handles unsigned data IMUL: handles signed data 1. Format: [label:] MUL/IMUL register/memory 2. Logic: AX ← source ×AL ; if source is a byte or DX:AX ← source ×AX ; if source is a word 3. Both instructions affect the carry and overflow flags. 〖19-13〗
The Carry and Overflow flags are set if the upper half of the result (AH for a byte source, DX for a word source) contains any significant digits of the product, otherwise they are cleared. • The example ( P.230) : • Example of MUL instruction • Example of IMUL instruction 〖19-14〗
DIVISION DIV: handles unsigned data IDIV: handles signed data 1. Format: [label:] DIV/IDIV register/memory 2. Logic: AL ← AX÷source ; if source is a byte AH ← remainder ; 余数交AH or AX ← DX:AX÷source ; if source is a word DX ← remainder 〖19-15〗
3. If the result is too large to fit in the destination (AL or AX), an INT 0 (Divide by Zero) is generated, and the quotient and remainder are undefined (i.e. the result is unpredictable). • The example ( P.237) : • Example of DIV instruction • Example of IDIV instruction 〖19-16〗
2 INSTRUCTIONS RELATED TO MULTIPLICATION AND DIVISION 1. CBW: Convert Byte to Word Logic: if (AL < 80h) then AH ← 0 else AH ← FFh • CBW extends the sign bit of the AL register into the AH register. 〖19-17〗
2. CWD: Convert Word to Double word Logic: if (AX < 8000h) then DX ← 0 else DX ← FFFFh • CWD extends the sign bit of the AX register into the DX register. 〖19-18〗
Questions: Page 239 12 - 1 12 - 3 12 - 6 Note: BIN_AMT1 DW 0147H DW 139AH BIN_AMT2 DW 02B3H DW 2D41H
上机Exercise: 从键盘上输入两个一位十进制数字,并将其 转换成二进制数,再将它们相减后以两位十进制 数字的形式输出结果。 For Example, 5 – 3 = 02 or 3 – 8 = – 5 (注意差为负数的情况)