YOU ARE DOWNLOADING DOCUMENT

Please tick the box to continue:

Transcript
Page 1: Assembly Branches Loops and Arithmatics II

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

Page 2: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 2/22

Arithmetic Instructions (cont’d)

• Unsigned multiplication mul source

» Depending on the source operand size, the location of theother source operand and destination are selected

Arithmetic Instructions (cont’d)∗ Example

mov AL,10 mov DL,25 mul DL

produces 250D in AX register (result fits in AL)

• The imul instruction can use the same syntax» Also supports other formats

∗ Example mov DL,0FFH ; DL = -1 mov AL,0BEH ; AL = -66

imul DL

produces 66D in AX register (again, result fits in AL)

Page 3: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 3/22

MUL examples

100h * 2000h, using 16-bit operands:

.dataval1 WORD 2000hval2 WORD 100h.code

mov ax,val1 mul val2 ; DX:AX=00200000h, CF=1

The Carry flag indicateswhether or not the upperhalf of the productcontains significant digits.

mov eax,12345h mov ebx,1000h mul ebx ; EDX:EAX=0000000012345000h, CF=0

12345h * 1000h, using 32-bit operands:

Arithmetic Instructions (cont’d)

• Division instruction∗ Even more complicated than multiplication

» Produces two results– Quotient– Remainder

» In multiplication, using a double-length register, there will not

be any overflow– In division, divide overflow is possible

Pentium provides a special software interrupt when adivide overflow occurs

∗ Two instructions as in multiplicationdiv source for unsigned numbers

idiv source for signed numbers

Page 4: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 4/22

Arithmetic Instructions (cont’d)

• Dividend is twice the size of the divisor• Dividend is assumed to be in

∗ AX (8-bit divisor)∗ DX:AX (16-bit divisor)∗ EDX:EAX (32-bit divisor)

Arithmetic Instructions (cont’d)

Page 5: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 5/22

Divide overflow

• Divide overflow happens when the quotient is toolarge to fit into the destination. mov ax, 1000h mov bl, 10h

div bl

It causes a CPU interrupt and halts the program.(divided by zero cause similar results)

Arithmetic Instructions (cont’d)

• Example mov AX,251 mov CL,12

div CL

produces 20D in AL and 11D as remainder in AH

• Examplesub DX,DX ; clear DX

mov AX,141BH ; AX = 5147D mov CX,012CH ; CX = 300D

div CX

produces 17D in AX and 47D as remainder in DX

Page 6: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 6/22

Arithmetic Instructions (cont’d)

• Signed division requires some help» We extended an unsigned 16 bit number to 32 bits by placing

zeros in the upper 16 bits» This will not work for signed numbers

– To extend signed numbers, you have to copy the sign bitinto those upper bit positions

∗ 80x86 provides three instructions in aiding signextension

» All three take no operandscbw converts byte to word (extends AL into AH)

cwd converts word to doubleword (extends AX into DX)cdq converts doubleword to quadword

(extends EAX into EDX)

1 0 0 0 1 1 1 1

1 0 0 0 1 1 1 11 1 1 1 1 1 1 1

Arithmetic Instructions (cont’d)

∗ Some additional related instructions» Sign extension

cwde converts word to doubleword(extends AX into EAX)

» Two move instructions movsx dest,src (move sign-extended src to dest )

movzx dest,src (move zero-extended src to dest )

» For both move instructions, dest has to be a register» The src operand can be in a register or memory

– If src is 8-bits, dest must be either a 16- or 32-bitregister

– If src is 16-bits, dest must be a 32-bit register

Page 7: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 7/22

Arithmetic Instructions (cont’d)

• Example mov AL,-95

cbw ; AH = FFH mov CL,12

idiv CLproduces − 7D in AL and − 11D as remainder in AH

• Example mov AX,-5147

cwd ; DX := FFFFH mov CX,300

idiv CXproduces − 17D in AX and − 47D as remainder in DX

Arithmetic Instructions (cont’d)

• Use of Shifts for Multiplication and Division∗ Shifts are more efficient∗ Example: Multiply AX by 32

mov CX,32imul CX

takes 12 clock cycles

∗ Usingsal AX,5

takes just one clock cycle

Page 8: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 8/22

Arithmetic expressions

Implementing arithmetic expressions

• 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

Page 9: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 9/22

Implementing arithmetic expressions

Example: eax = (-var1 * var2) + var3 mov eax,var1

neg eax mul var2

jo TooBig ; check for overflowadd eax,var3

Example: var4 = (var1 * 5) / (var2 – 3) mov eax,var1 ; left side mov ebx,5 mul ebx ; EDX:EAX = product mov ebx,var2 ; right side

sub ebx,3div ebx ; final division

mov var4,eax

Implementing arithmetic expressions

Example: var4 = (var1 * -5) / (-var2 % var3); mov eax,var2 ; begin right side

neg eaxcdq ; sign-extend dividend idiv var3 ; EDX = remainder

mov ebx,edx ; EBX = right side mov eax,-5 ; begin left side

imul var1 ; EDX:EAX = left sideidiv ebx ; final division

mov var4,eax ; quotient

Sometimes it's easiest to calculate the right-hand term of anexpression first.

Page 10: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 10/22

Your turn . . .

mov eax,20 mul ebx

div ecx

Implement the following expression usingsigned 32-bit integers:

eax = (ebx * 20) / ecx

Your turn . . .

push ecx push edx

push eax ; EAX needed later mov eax,ecx mul edx ; left side: EDX:EAX pop ecx ; saved value of EAX

div ecx ; EAX = quotient pop edx ; restore EDX, ECX pop ecx

Implement the following expression using signed32-bit int egers. Save and restore ECX and EDX:

eax = (ecx * edx) / eax

Page 11: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 11/22

Your turn . . .

mov eax,var1 mov edx,var2

neg edx mul edx ; left side: edx:eax mov ecx,var3

sub ecx,ebx

div ecx ; eax = quotient mov var3,eax

Implement the following expression using signed 32-bit

integers. Do not modify any variables other than var3:var3 = (var1 * -var2) / (var3 – ebx)

Extended addition and subtraction

Page 12: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 12/22

ADC instruction

• ADC (add with carry) instruction adds both asource operand and the contents of the Carryflag to a destination operand.

• Example: Add two 32-bit integers (FFFFFFFFh+ FFFFFFFFh), producing a 64-bit sum:

mov edx,0 mov eax,0FFFFFFFFh

add eax,0FFFFFFFFhadc edx,0 ;EDX:EAX =

00000001FFFFFFFEh

Extended addition example• Add two integers of any size• Pass pointers to the addends (ESI, EDI) and sum (EBX), ECX

indicates the number of doublewords

L1: mov eax,[esi] ; get the first integer

adc eax,[edi] ; add the second integer

pushfd ; save the Carry flag mov [ebx],eax ; store partial sum add esi,4 ; advance all 3 pointersadd edi,4add ebx,4

popfd ; restore the Carry flagloop L1 ; repeat the loopadc word ptr [ebx],0 ; add leftover carry

Page 13: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 13/22

Extended addition example

.dataop1 QWORD 0A2B2A40674981234hop2 QWORD 08010870000234502hsum DWORD 3 dup(?)

; = 0000000122C32B0674BB5736.code...

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

Page 14: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 14/22

Binary-Coded Decimal• Binary-coded decimal (BCD) integers use 4 binary

bits to represent each decimal digit• A number using unpacked BCD representation

stores a decimal digit in the lower four bits of eachbyte∗ For example, 5,678 is stored as the following sequence

of hexadecimal bytes:

05 06 07 08

ASCII Decimal• A number using ASCII Decimal representation

stores a single ASCII digit in each byte∗ For example, 5,678 is stored as the following sequence

of hexadecimal bytes:

35 36 37 38

Page 15: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 15/22

Binary Coded Decimal (BCD)

0111 0011

0000 0111 0000 0011

7 3

7 3

Packed (2 digits per byte):

Unpacked (1 digit per byte):

AAA Instruction

• 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

• Example: Add '8' and '2'

mov ah,0 mov al,'8' ; AX = 0038h

add al,'2' ; AX = 006Ahaaa ; AX = 0100h (adjust result)or ax,3030h ; AX = 3130h = '10'

Page 16: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 16/22

AAS Instruction

• 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

Page 17: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 17/22

AAD Instruction

• The AAD (ASCII adjust before division)instruction adjusts the unpacked BCD dividend inAX before a division operation

.dataquotient BYTE ?remainder BYTE ?.code

mov ax,0307h ; dividend aad ; AX = 0025h

mov bl,5 ; divisor

div bl ; AX = 0207h mov quotient,al mov remainder,ah

Packed Decimal Arithmetic• Packed decimal integers store two decimal digits

per byte∗ For example, 12,345,678 can be stored as the following

sequence of hexadecimal bytes:12 34 56 78

Packed decimal is also known as packed BCD.

Good for financial values – extended precision possible,without rounding errors.

Page 18: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 18/22

DAA Instruction

• 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).

Page 19: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 19/22

DAA Examples• Example: calculate BCD 35 + 48

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.

Page 20: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 20/22

DAS Instruction

• The DAS (decimal adjust after subtraction)instruction converts the binary result of a SUB orSBB operation to packed decimal format.

• The value must be in AL• Example: subtract BCD 48 from 85

mov al,48h

sub al,35h ; AL = 13hdas ; AL = 13h CF = 0

DAS LogicIf (AL(lo) > 9) OR (AuxCarry = 1)

AL = AL − 6; AuxCarry = 1;

Else AuxCarry = 0;

Endif

If (AL > 9FH) or (Carry = 1)

AL = AL − 60h;Carry = 1;Else

Carry = 0;Endif

If AL = AL − 6 sets theCarry flag, its value isused when evaluatingAL in the second IF

statement.

Page 21: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 21/22

DAS Examples (1 of 2)• Example: subtract BCD 48 – 35

mov al,48hsub al,35h ; AL = 13hdas ; AL = 13h CF = 0

• Example: subtract BCD 62 – 35

mov al,62hsub al,35h ; AL = 2Dh, CF = 0das ; AL = 27h, CF = 0

• Example: subtract BCD 32 – 29

mov al,32hadd al,29h ; AL = 09h, CF = 0daa ; AL = 03h, CF = 0

DAS Examples (2 of 2)• Example: subtract BCD 32 – 39

mov al,32hsub al,39h ; AL = F9h, CF = 1das ; AL = 93h, CF = 1

Steps:

AL = F9hCF = 1, so subtract 6 from F9h

AL = F3hF3h > 9Fh, so subtract 60h from F3h

AL = 93h, CF = 1

Page 22: Assembly Branches Loops and Arithmatics II

7/31/2019 Assembly Branches Loops and Arithmatics II

http://slidepdf.com/reader/full/assembly-branches-loops-and-arithmatics-ii 22/22

Your turn . . .

• 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.


Related Documents