Documente Academic
Documente Profesional
Documente Cultură
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 *
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)
Addition examples
adda #50 addb #$50 addd #$2fa3 adda data adda data+1 addd data ldx #data adda 0,x
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)
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)
Flags tested:
HS : LO : LS : HI :
*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
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
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:
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
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
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 *
Addition/Subtraction Summary
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
Uses half carry (H flag) Only works on accumulator A following an addition (ADDA/ADCA/ABA)
;1st BCD number ;add 2nd BCD number ;adjust result to BCD
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
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
Divide instructions
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
Overflow detection
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.
0000 0003 0006 0007 000A 000B 000E 0011 0013 0015 0017
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