Presentation is loading. Please wait.

Presentation is loading. Please wait.

Decimal Arithmetic (alternative to binary arithmetic)

Similar presentations


Presentation on theme: "Decimal Arithmetic (alternative to binary arithmetic)"— Presentation transcript:

1 Decimal Arithmetic (alternative to binary arithmetic)
numbers are stored as a series of base 10 digits  binary coded decimal (BCD) or  decimal arithmetic two formats: unpacked BCD → 8 bits = 1 decimal digit in low nibble e.g. 1,98510 = % = $ packed BCD → 8 bits = 2 decimal digits e.g. 1,98510 = % = $1985

2 Why Binary Coded Decimal (BCD)?
input/output from/to systems is typically ASCII keypad systems (numbers only) are typically BCD key press → ASCII or BCD digit BCD arithmetic operations: work on one byte at a time assume packed BCD → 8 bits = 2 decimal digits unpacked BCD only used in transition from ASCII to BCD → 8 bits = 1 decimal digit in low nibble

3 Why Binary Coded Decimal (BCD)?
if binary arithmetic: input → ASCII ASCII to BCD BCD to binary calc in binary binary to BCD BCD to ASCII ASCII → output if decimal arithmetic: input → ASCII ASCII to BCD BCD to ASCII ASCII → output

4 Decimal Arithmetic operations: ABCD, SBCD, NBCD → byte only (2 BCD digits at a time) start at the least significant byte and work to most significant byte carry handled by using the X flag ABCD (source)10 + (dest)10 + X → dest10 e.g. 1985 3459

5 Decimal Arithmetic … what really happens
Example 2 Example 1 Decimal Adjust Rules: If sum > 9, then add 6 If carry from bit 3 to bit 4, then add 6 to the source of the carry. Processors either: provide BCD instructions (like 68000) provide a decimal adjust instruction and a decimal carry in CCR (like Intel x86) step 1 = binary add step 2 = decimal adjust

6 Flags for BCD clear X (extended carry) before first BCD operation so that carry is handled correctly N and V are undefined for decimal arithmetic sign is explicitly handled during the operation C and X is set if decimal carry/borrow occurs can be used to construct result if overflow Z is cleared if the result is nonzero; otherwise unchanged for addition, set Z to 1 before first BCD operation to obtain correct Z no requirement to initialize Z if it is not important to the problem

7 Decimal Arithmetic e.g. Add two numbers (bcdn1, bcdn2) and store result (bcdn2). org $1000 start clr.w D2 move.b length,D2 subq.w #1,D2 lea bcdn1+4,A1 lea bcdn2+4,A0 move.w #$04,CCR loop abcd.b -(A1),-(A0) dbra D2,loop stop #$2700 bcdn1 DC.L $ bcdn2 DC.L $ length DC.B 4 end start

8 BCD complements … signed numbers
1’s complement flip all of the bits e.g –> 1100 or, alternatively, ≡ 1111 – 0011 2’s complement 1’s complement +1 e.g –> 1101 or, alternatively, ≡ ???? – 0011 9’s complement 10’s complement

9

10 Decimal Arithmetic … subtraction
e.g. 9999 - 4930 e.g. 1817 e.g - ____ ____

11 you understand 9s and 10s complement you understand how NBCD works
Reading: Mind-boggling math: BCD (binary coded decimal) [pdf] [C. Maxfield -- EDN, 9/7/2005] More mind-boggling math: Adding and subtracting unsigned BCD [pdf] [C. Maxfield -- EDN, 9/21/2005] Even more mind-boggling math: Working with signed BCD [pdf] [C. Maxfield -- EDN, 10/5/2005] NOTE: for the above references, the local PDF version is complete while the online EDN version is missing the diagrams. M68000 Assembly Language [pdf, 92p; N. Znotinas] review the decimal arithmetic (ABCD, SBCD, NBCD) instructions Expectations: you can write programs to do decimal arithmetic for numbers of unlimited width you understand 9s and 10s complement you understand how NBCD works you know how to output negative BCD numbers as a negative decimal number (minus sign followed by the magnitude of the negative number)


Download ppt "Decimal Arithmetic (alternative to binary arithmetic)"

Similar presentations


Ads by Google