7/31/2019 Assembly Branches Loops and Arithmatics II http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 1/22 Advanced Arithmetic II Arithmetic Instructions (cont’d) • Multiplication ∗ More complicated than add / sub » Produces double-length results – E.g. Multiplying two 8 bit numbers produces a 16-bit result » Cannot use a single multiply instruction for signed and unsigned numbers – add and sub instructions work both on signed and unsigned numbers – For multiplication, we need separate instructions mul for unsigned numbers imul for signed numbers
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
7/31/2019 Assembly Branches Loops and Arithmatics II
• Some good reasons to learn how to implement expressions:∗ Learn how compilers do it∗ Test your understanding of MUL, IMUL, DIV, and IDIV∗ Check for 32-bit overflow
Example: var4 = (var1 + var2) * var3 mov eax,var1
add eax,var2 mul var3
jo TooBig ; check for overflow mov var4,eax ; save product
7/31/2019 Assembly Branches Loops and Arithmatics II
mov esi,OFFSET op1 ; first operand mov edi,OFFSET op2 ; second operand mov ebx,OFFSET sum ; sum operand mov ecx,2 ; number of doublewords
call Extended_Add ...
SBB instruction
• The SBB (subtract with borrow) instruction subtracts botha source operand and the value of the Carry flag from adestination operand.
• The following example code performs 64-bit subtraction. Itsets EDX:EAX to 0000000100000000h and subtracts 1from this value. The lower 32 bits are subtracted first,
setting the Carry flag. Then the upper 32 bits aresubtracted, including the Carry flag: mov edx,1 ; upper half mov eax,0 ; lower half
sub eax,1 ; subtract 1sbb edx,0 ; subtract upper
half
7/31/2019 Assembly Branches Loops and Arithmatics II
• The AAA (ASCII adjust after addition) instructionadjusts the binary result of an ADD or ADCinstruction. It makes the result in AL consistentwith ASCII decimal representation.∗ The Carry value, if any ends up in AH
• The AAS (ASCII adjust after subtraction)instruction adjusts the binary result of an SUB orSBB instruction. It makes the result in AL consistentwith ASCII decimal representation.∗ It places the Carry value, if any, in AH
• Example: Subtract '9' from '8'
mov ah,0 mov al,'8' ; AX = 0038h
sub al,'9' ; AX = 00FFhaas ; AX = FF09h, CF=1or al,30h ; AL = '9'
AAM Instruction
• The AAM (ASCII adjust after multiplication)instruction adjusts the binary result of a MULinstruction. The multiplication must have beenperformed on unpacked BCD numbers.
mov bl,05h ; first operand mov al,06h ; second operand mul bl ; AX = 001Eh
aam ; AX = 0300h
7/31/2019 Assembly Branches Loops and Arithmatics II
• The DAA (decimal adjust after addition)instruction converts the binary result of an ADDor ADC operation to packed decimal format.∗ The value to be adjusted must be in AL∗ If the lower digit is adjusted, the Auxiliary Carry flag is
set.∗ If the upper digit is adjusted, the Carry flag is set.
DAA Logic
If (AL(lo) > 9) or (AuxCarry = 1) AL = AL + 6 AuxCarry = 1
Else AuxCarry = 0
Endif
If (AL(hi) > 9) or Carry = 1 AL = AL + 60h
Carry = 1Else
Carry = 0Endif
If AL = AL + 6 sets theCarry flag, its value isused when evaluatingAL(hi).
7/31/2019 Assembly Branches Loops and Arithmatics II
mov al,35hadd al,48h ; AL = 7Dhdaa ; AL = 83h, CF = 0
• Example: calculate BCD 35 + 65
mov al,35hadd al,65h ; AL = 9Ahdaa ; AL = 00h, CF = 1
• Example: calculate BCD 69 + 29
mov al,69hadd al,29h ; AL = 92hdaa ; AL = 98h, CF = 0
Your turn . . .
• A temporary malfunction in your computer's processorhas disabled the DAA instruction. Write a procedure inassembly language that performs the same actions asDAA.
• Test your procedure using the values from the previous
slide.
7/31/2019 Assembly Branches Loops and Arithmatics II
• A temporary malfunction in your computer's processorhas disabled the DAS instruction. Write a procedure inassembly language that performs the same actions asDAS.
• Test your procedure using the values from the previoustwo slides.