chapter7 arithmetic

29
 Arithmetic Operations Chapter 7, Section 7.11

Upload: raghu-kanchiraju

Post on 06-Apr-2018

237 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 1/29

Arithmetic OperationsChapter 7, Section 7.11

Page 2: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 2/29

Binary AdditionC examples

int i, j, k; // 16-bit integerschar m,n; // 8-bit integersfloat w,y,z; // 32-bit floating-point *

i = j + k; // add 16-bit integersm = n + 5; // add 8-bit integersw = y + z; // add 32-bit numbers *

* floating-point not directly supported by 68HC12

Page 3: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 3/29

Simple addition in 68HC12Sum = Addend + Augend

Addend (left-hand source operand) must be in a registerAugend (right-hand operand may be immediate ormemory

Sum replaces the addend in its register8-bit add instructions:

ADDA op ; A + op => A (op = immediate or memory)ADDB op ; B + op => B (op = immediate or memory)

ABA ; A + B => A (only register+register form)16-bit add instruction:

ADDD op ; D + op => D (D = A:B)

Page 4: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 4/29

Addition examplesadda #50 A = A + 50 10

addb #$50 A = A + 50 16 = A + 80 10

addd #$2fa3 D = D + 2fa3 16 (D = A:B)adda data A = A + M[data]adda data+1 A = A + M[data+1]addd data D = D + M[data,data+1]ldx #dataadda 0,x A = A + M[data]

Page 5: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 5/29

Condition codesFive ALU outputs signal different “conditions” related to the result of an operation

Z => the result was Zero (all 0’s)C => there was a Carry out of the most significant bit of the resultN => the result was NegativeV => there was a 2’s complement oVerflowH => “Half” carry – there was a carry out of bit 3 of the

result (used in BCD arithmetic)The 5 “condition codes” are saved in the

“Condition Code Register”

Page 6: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 6/29

Condition code examples

00100110+ 101111010 11100011

Carry (C = 0)

Sign (N = 1)

Result is non-zero (Z = 0)No 2’s complement overflow (V = 0)

Unsigned Signedinterpretation: interpretation:

38 +38

+ 189 +(-67)227 -29

Page 7: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 7/29

Testing an arithmetic resultConditional branch jumps to a designated locationif some condition is trueExamples:

adda data ;add instructionbeq Zero ;branch if result=0bne Nonzero ;branch if result <> 0bmi Minus ;branch if result minusbpl Plus ;branch if result positivebcc NoCY ;branch if carry flag clear (0)bcs CY ;branch if carry flag set (1)

bvs Overflow ;branch if overflow flag set (1)bvc NoOver ;branch if overflow flag clear (0)

Page 8: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 8/29

Testing signed resultsSigned examples:

suba #5 ;A = A – 5bge GE ;branch if result ≥ 0

bgt GT ;branch if result > 0ble LE ;branch if result ≤ 0

blt LT ;branch if result < 0Flags tested:

GE : N xor V = 0 (N=0 and V=0, or N=1 and V=1)LT : N xor V = 1 (complement of GE)LE : (N xor V) or Z = 1GT : (N xor V) or Z = 0 (complement of LE)

Page 9: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 9/29

Testing unsigned resultsUnsigned examples: (higher/lower)

cmpa #5 ;A – 5bhs GE ;branch if result ≥ 0

bhi GT ;branch if result > 0bls LE ;branch if result ≤ 0

blo LT ;branch if result < 0Flags tested:

HS : C = 0 (no borrow/carry)LO : C = 1 (borrow/carry)LS : (C or Z) = 1 (C=1 or Z=1)HI : (C or Z) = 0 (C=0 and Z=0)

*cmpa identical to suba (subtraction performed) except

flags are set without saving the result in A

Page 10: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 10/29

Multi-precision arithmeticCarry links bytes of multi-precision #s

45 5+7 = 12 = 10 plus 2

+ 27 write 2 in 1’s column, “carry” 10 to 10’s column

72 4+2 plus “carry” = 7

“Add with carry” instructions (8-bit only)ADCA m ;A = A + m + C (add prev. carry)

“Subtract with borrow” instructions (8-bit)SBCA m ;A = A - m – C (borrow from this byte)

C = current value of carry flag

Page 11: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 11/29

Multi-precision addition exampleExample: add 24-bit #s N3 = N1 + N2

ldaa N1+2 ;low byte of N1 *adda N2+2 ;add low byte of N2

staa N3+2 ;store low byte of N3ldaa N1+1 ;mid byte of N1adca N2+1 ;add mid byte of N2staa N3+1 ;store mid byte of N3ldaa N1 ;high byte of N1adca N2 ;add high byte of N2staa N3 ;store high byte of N3

* big-endian format

Page 12: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 12/29

Setting of condition codesArithmetic op’s: all flags reflect resultLoad & store instructions:

N & Z flags set according to data movedV cleared to 0C is not altered

INC,INCA,INCB,DEC,DECA,DECB:N, Z, and V flags reflect resultC does not change

INX,INY,DEX,DEY:set Z onlyno other flags change

DES,INS:no flags changed

Page 13: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 13/29

Multi-precision addition exampleGeneral add routine for K-byte numbers

ldab #8 ;assume 8-byte #stfr b,x ;x has index

clc ;clear carry flagLoop dex ;first offset is 7

ldaa N1,x ;byte of N1adca N2,x ;add byte of N2

staa N3,x ;store byte of N3dbne b,Loop ;repeatbra *

Note: only clc & adca affect the carry flag

Page 14: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 14/29

More on flagsadda #1 produces same result as incaBUT, inca does not change carry flag

loop: ldaa N1,x loop: ldaa N1,x

adca N2,x adca N2,xstaa N3,x staa N3,xinx inxsubb #1 decbbne loop bne loop

subb overwrites C flagneeded for next iteration

decb leaves C flag intactfor next iteration

ldaa , staa , inx, bne do not alter the C flag

Page 15: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 15/29

Multi-precision subtractionSimilar to multi-precision addition

ldab #8 ;assume 8-byte #stfr b,x ;x has index

clc ;clear carry flagLoop dex ;first offset is 7

ldaa N1,x ;byte of N1sbca N2,x ;subtract byte of N2

staa N3,x ;store byte of N3dbne b,Loop ;repeatbra *

Note: only clc & sbca affect the carry flag

Page 16: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 16/29

Addition/Subtraction Summary

Page 17: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 17/29

Increment & Decrement Summary

Page 18: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 18/29

Compare & Test Summary

Page 19: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 19/29

Binary-coded decimal arithmetic

$38 $38+ $25 $29

$5D $61+ 6 + 6$63 $67

Binary resultDecimal adjust

BCD values

BCD result

“Half carry” H = carry from bit 3 to bit 4* From low to high digit of packed BCD byte

BCD : value of H is 10HEX : value of H is 16 “Adjustment” is 6

Page 20: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 20/29

BCD number supportPacked BCD addition supported by DAA(decimal adjust addition)

Uses “half carry” (H flag)

Only works on accumulator A following anaddition (ADDA/ADCA/ABA)

ldaa BCD1 ;1 st BCD numberadda BCD2 ;add 2 nd BCD numberdaa ;adjust result to BCD

No support for BCD subtraction!

Page 21: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 21/29

Multiplication and Division

Page 22: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 22/29

MultiplicationMultiplying K-bit #’s produces 2K-bitproduct (no “overflow” possible)M UL : 8 x 8 bit multiply – unsigned

(A) x (B) -> A:B (16-bit result)Carry flag = bit 7 of B (no other flags change)

EMUL : 16 x 16 multiply – unsigned(D) x (Y) -> Y:DN and Z reflect result, C = bit 15 of result

EMULS : same as EMUL, but for signed #sFor 8 x 8 signed multiply, convert operands to 16 bits

and use EMULS.

Page 23: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 23/29

Example 7-28Write a small program to multiply the contents of DATA1 and DATA2.Store the result in DATA3:DATA3+1.

0000 B6000A 1 ldaa DATA1 ; Get the multiplier0003 F6000B 2 ldab DATA2 ; Get the multiplicand0006 12 3 mul ; The product is in D

0007 7C000C 4 std DATA35 ; - - -

000A 6 DATA1: DS 1 ; 8-bit multiplier000B 7 DATA2: DS 1 ; 8-bit multiplicand000C 8 DATA3: DS 2 ; 16-bit product

Question: What if result must be stored in a byte?

Page 24: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 24/29

Example 7-30 8-bit Signed Multiply 1 ; - - -2 ; 8-bit * 8-bit signed multiply

0000 B6000F 3 ldaa DATA1 ; Get the multiplicand0003 B706 4 SEX a,y ; Sign extend into Y0005 B60010 5 ldaa DATA2 ; Get the multiplier0008 B704 6 SEX a,d ; Same as SEX a,d000A 1813 7 emuls ; Extended multiply Y*D

8 ; 32-bit product is in Y:D9 ; The 16 bits we need are in D

000C 7C0011 10 std DATA311 ; - - -

000F 12 DATA1: DS 1 ; 8-bit multiplicand0010 13 DATA2: DS 1 ; 8-bit multiplier0011 14 DATA3: DS 2 ; 16-bit product

Page 25: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 25/29

Divide instructionsIDIV = “integer divide”

Computes D/X (both 16 bits)Quotient in X, Remainder in D

IDIVSIdentical to IDIV, except for signed numbers

FDIV “fractional divide” Identical to IDIV, except result is a 16-bit fractionDivisor must be greater than the dividend

EDIV/EDIVS (“extended” divide signed/unsigned)32-bit dividend 16-bit divisorComputes (Y:D)/XQuotient -> Y, Remainder -> D

Page 26: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 26/29

Overflow detectionIf divisor is 0 (undefined result)

C flag setQuotient = $FFFFRemainder is indeterminate

IDIVS overflowV is set if quotient requires more than 16 bits

Quotient outside of range [$8000 … $7FFF]

Only happens for $8000/$FFFF-32768/-1 = +32768 ($7FFF = +32767)V always 0 for IDIV (unsigned)

Page 27: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 27/29

Example 7-34Assume D contains 176 10 and X = 10 10 .

What is in A, B and X before and after an IDIV instruction?

Solution:

Before the IDIV instruction:D = 176 10 = $00B0, therefore A = $00, B = $B0X = 10 10 = $000A

After the IDIV instruction:176 10 /10 10 = 17 10 with a remainder of 6 10 , therefore

X = 17 10 = $0011, D = 6 10 = $0006, A = $00, B = $06.

Page 28: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 28/29

Example 7-35Assume D contains 100 10 and X = 400 10 . What is in A, B and X beforeand after an FDIV instruction?

Solution:

Before the FDIV instruction:D = 100 10 = $0064, therefore A = $00, B = $64X = 400 10 = $0190

After the FDIV instruction:

100 10 /400 10 = 0.25 10 with a remainder of 0, thereforeX = 0.25 10 = $4000, D = 0 = $0000, A = $00, B = $00.

Page 29: Chapter7 Arithmetic

8/3/2019 Chapter7 Arithmetic

http://slidepdf.com/reader/full/chapter7-arithmetic 29/29

Example 7-36 Extended, UnsignedMultiply and Divide Instructions

Write a small program to produce DATA1*DATA2/100 where DATA1 and DATA2are both 16-bit unsigned numbers.

1 ; - - -2 ; DATA1*DATA2/100

0000 FC0011 3 ldd DATA10003 FD0013 4 ldy DATA20006 13 5 emul ; 32-bit prod in Y:D0007 CE0064 6 ldx #!100000A 11 7 ediv ; Do the division000B 7D0015 8 sty DATA3 ; Save quotient000E 7C0017 9 std DATA4 ; Save remainder

10 ; - - -

0011 11 DATA1: DS 2 ; Unsigned multiplier0013 12 DATA2: DS 2 ; Unsigned multiplicand0015 13 DATA3: DS 2 ; Unsigned quotient0017 14 DATA4: DS 2 ; Unsigned remainder