Sunteți pe pagina 1din 29

Arithmetic Operations

Chapter 7, Section 7.11

Binary Addition

C examples
int i, j, k; char m,n; float w,y,z; i = j + k; m = n + 5; w = y + z; // // // // // // 16-bit integers 8-bit integers 32-bit floating-point * add 16-bit integers add 8-bit integers add 32-bit numbers *

* floating-point not directly supported by 68HC12

Simple addition in 68HC12

Sum = Addend + Augend


Addend (left-hand source operand) must be in a register Augend (right-hand operand may be immediate or memory Sum replaces the addend in its register ADDA op ; A + op => A ADDB op ; B + op => B ABA ; A + B => A ADDD op ; D + op => D (op = immediate or memory) (op = immediate or memory) (only register+register form) (D = A:B)

8-bit add instructions:


16-bit add instruction:

Addition examples
adda #50 addb #$50 addd #$2fa3 adda data adda data+1 addd data ldx #data adda 0,x

A = A + 5010 A = A + 5016 = A + 8010 D = D + 2fa316 (D = A:B) A = A + M[data] A = A + M[data+1] D = D + M[data,data+1] A = A + M[data]

Condition codes

Five ALU outputs signal different conditions related to the result of an operation

Z => the result was Zero (all 0s) C => there was a Carry out of the most significant bit of the result N => the result was Negative V => there was a 2s complement oVerflow H => 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

Condition code examples


Unsigned 00100110 interpretation: 38 + 10111101 + 189 0 11100011 227 Sign (N = 1) Carry (C = 0) Result is non-zero (Z = 0) No 2s complement overflow (V = 0) Signed interpretation: +38 +(-67) -29

Testing an arithmetic result


Conditional branch jumps to a designated location if some condition is true Examples:


adda beq bne bmi bpl bcc bcs bvs bvc data Zero Nonzero Minus Plus NoCY CY Overflow NoOver ;add instruction ;branch if result=0 ;branch if result <> 0 ;branch if result minus ;branch if result positive ;branch if carry flag clear (0) ;branch if carry flag set (1) ;branch if overflow flag set (1) ;branch if overflow flag clear (0)

Testing signed results

Signed examples:
suba bge bgt ble blt #5 GE GT LE LT ;A = A 5 ;branch if result ;branch if result ;branch if result ;branch if result > < 0 0 0 0

Flags tested:

GE : LT : LE : GT :

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

Testing unsigned results

Unsigned examples: (higher/lower)


cmpa #5 bhs GE bhi GT bls LE blo LT ;A 5 ;branch ;branch ;branch ;branch if if if if result result result result > < 0 0 0 0

Flags tested:

HS : LO : LS : HI :

C = 0 (no borrow/carry) C = 1 (borrow/carry) (C or Z) = 1 (C=1 or Z=1) (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

Multi-precision arithmetic
Carry links bytes of multi-precision #s 45 5+7 = 12 = 10 plus 2 + 27 write 2 in 1s column, carry 10 to 10s column 72 4+2 plus carry = 7 Add with carry instructions (8-bit only)

ADCA m ;A = A + m + C (add prev. carry) SBCA m ;A = A - m C (borrow from this byte) C = current value of carry flag

Subtract with borrow instructions (8-bit)

Multi-precision addition example

Example: 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 N3 ldaa N1+1 ;mid byte of N1 adca N2+1 ;add mid byte of N2 staa N3+1 ;store mid byte of N3 ldaa N1 ;high byte of N1 adca N2 ;add high byte of N2 staa N3 ;store high byte of N3
* big-endian format

Setting of condition codes


Arithmetic ops: all flags reflect result Load & store instructions:

INC,INCA,INCB,DEC,DECA,DECB:

N & Z flags set according to data moved V cleared to 0 C is not altered N, Z, and V flags reflect result C does not change set Z only no other flags change no flags changed

INX,INY,DEX,DEY:

DES,INS:

Multi-precision addition example


General add routine ldab #8 tfr b,x clc Loop dex ldaa N1,x adca N2,x staa N3,x dbne b,Loop bra *

for K-byte numbers ;assume 8-byte #s ;x has index ;clear carry flag ;first offset is 7 ;byte of N1 ;add byte of N2 ;store byte of N3 ;repeat

Note: only clc & adca affect the carry flag

More on flags
adda #1 produces same result as inca BUT, inca does not change carry flag

loop: ldaa N1,x adca N2,x staa N3,x inx subb #1 bne loop
subb overwrites C flag needed for next iteration

loop: ldaa N1,x adca N2,x staa N3,x inx decb bne loop
decb leaves C flag intact for next iteration

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

Multi-precision subtraction
Similar to ldab tfr clc Loop dex ldaa sbca staa dbne bra

multi-precision addition #8 ;assume 8-byte #s b,x ;x has index ;clear carry flag ;first offset is 7 N1,x ;byte of N1 N2,x ;subtract byte of N2 N3,x ;store byte of N3 b,Loop ;repeat *

Note: only clc & sbca affect the carry flag

Addition/Subtraction Summary

Increment & Decrement Summary

Compare & Test Summary

Binary-coded decimal arithmetic


$38 BCD values + $25 $5D Binary result + 6 Decimal adjust $63 BCD result $38 $29 $61 + 6 $67

Half carry H = carry from bit 3 to bit 4 * From low to high digit of packed BCD byte BCD : value of H is 10 Adjustment is 6 HEX : value of H is 16

BCD number support

Packed BCD addition supported by DAA (decimal adjust addition)


Uses half carry (H flag) Only works on accumulator A following an addition (ADDA/ADCA/ABA)

ldaa BCD1 adda BCD2 daa

;1st BCD number ;add 2nd BCD number ;adjust result to BCD

No support for BCD subtraction!

Multiplication and Division

Multiplication
Multiplying K-bit #s produces 2K-bit product (no overflow possible) MUL : 8 x 8 bit multiply unsigned

(A) x (B) -> A:B (16-bit result) Carry flag = bit 7 of B (no other flags change) (D) x (Y) -> Y:D N and Z reflect result, C = bit 15 of result

EMUL : 16 x 16 multiply unsigned


EMULS : same as EMUL, but for signed #s


For 8 x 8 signed multiply, convert operands to 16 bits and use EMULS.

Example 7-28
Write a small program to multiply the contents of DATA1 and DATA2. Store the result in DATA3:DATA3+1.
0000 0003 0006 0007 000A 000B 000C B6000A F6000B 12 7C000C 1 2 3 4 5 6 7 8 ldaa ldab mul std - - DS DS DS DATA1 DATA2 DATA3 1 1 2 ; 8-bit multiplier ; 8-bit multiplicand ; 16-bit product ; Get the multiplier ; Get the multiplicand ; The product is in D

; DATA1: DATA2: DATA3:

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

Example 7-30 8-bit Signed Multiply


0000 0003 0005 0008 000A B6000F B706 B60010 B704 1813 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ; - - ; 8-bit * 8-bit signed multiply ldaa DATA1 ; Get the multiplicand SEX a,y ; Sign extend into Y ldaa DATA2 ; Get the multiplier SEX a,d ; Same as SEX a,d emuls ; Extended multiply Y*D ; 32-bit product is in Y:D ; The 16 bits we need are in D std DATA3 ; - - DATA1: DS 1 ; 8-bit multiplicand DATA2: DS 1 ; 8-bit multiplier DATA3: DS 2 ; 16-bit product

000C 7C0011 000F 0010 0011

Divide instructions

IDIV = integer divide


Computes D/X (both 16 bits) Quotient in X, Remainder in D Identical to IDIV, except for signed numbers Identical to IDIV, except result is a 16-bit fraction Divisor must be greater than the dividend
32-bit dividend 16-bit divisor Computes (Y:D)/X Quotient -> Y, Remainder -> D

IDIVS

FDIV fractional divide


EDIV/EDIVS (extended divide signed/unsigned)


Overflow detection

If divisor is 0 (undefined result)


C flag set Quotient = $FFFF Remainder is indeterminate V 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)

IDIVS overflow

Example 7-34
Assume D contains 17610 and X = 1010. What is in A, B and X before and after an IDIV instruction? Solution: Before the IDIV instruction: D = 17610 = $00B0, therefore A = $00, B = $B0 X = 1010 = $000A After the IDIV instruction: 17610/1010 = 1710 with a remainder of 610, therefore X = 1710 = $0011, D = 610 = $0006, A = $00, B = $06.

Example 7-35
Assume D contains 10010 and X = 40010. What is in A, B and X before and after an FDIV instruction? Solution: Before the FDIV instruction: D = 10010 = $0064, therefore A = $00, B = $64 X = 40010 = $0190 After the FDIV instruction: 10010/40010 = 0.2510 with a remainder of 0, therefore X = 0.2510 = $4000, D = 0 = $0000, A = $00, B = $00.

Example 7-36 Extended, Unsigned Multiply and Divide Instructions


Write a small program to produce DATA1*DATA2/100 where DATA1 and DATA2 are both 16-bit unsigned numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 ; - - ; DATA1*DATA2/100 ldd ldy emul ldx ediv sty std ; - - DATA1: DS DATA2: DS DATA3: DS DATA4: DS

0000 0003 0006 0007 000A 000B 000E 0011 0013 0015 0017

FC0011 FD0013 13 CE0064 11 7D0015 7C0017

DATA1 DATA2 ; 32-bit prod in Y:D #!100 DATA3 DATA4 2 2 2 2 ; Do the division ; Save quotient ; Save remainder ; ; ; ; Unsigned Unsigned Unsigned Unsigned multiplier multiplicand quotient remainder

S-ar putea să vă placă și