lecture18 - 0306-250 Assembly Language Programming Lecture...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 0306-250 Assembly Language Programming Lecture Eighteen: BCD Arithmetic • BCD Format • Byte BCD Operations • Multibyte BCD Operations Binary Coded Decimal (BCD) Numbers • Binary of each digit, di, of a decimal number – di Î [0, 9] – 4 bits (i.e., 1 nibble) to represent each digit – Same as hex encoding of each digit (i.e., four binary bits to one hex digit) • Example: Express 65 in BCD – %01100101 – $65 • Example: Express 90 in BCD – %10010000 – $90 2 Binary Addition of Packed BCD Bytes • What happens with binary add? – Consider 47 + 25 (stored as packed BCD bytes) ldaa ldab aba #$47 #$25 – Result: A = $6C • Carry does not match decimal carry – Binary nibble carry: result > 1510 – BCD nibble carry needed: result > 910 • Need to adjust addition result for BCD 3 DAA Decimal Adjust A S12CPUV2 Reference Manual, p. 161 4 BCD Arithmetic: CCR Bits • H: (used by DAA) – Carry from low nibble (digit) to high nibble (digit) generated: Set – Otherwise: Cleared • C: (used by DAA and ADCA) – Decimal carry generated: Set – Otherwise: Cleared 5 Addition of Packed BCD Bytes • What happens with BCD add? – Consider 47 + 25 (stored as packed BCD bytes) ldaa ldab aba daa #$47 #$25 – Result: A = $72 6 Binary Coded Decimal Addition • Add single bytes of single BCD digits – ADDA or ABA – DAA • Add single bytes of packed BCD digits – ADDA or ABA – DAA • Add multiple or single bytes of packed BCD digits – ADCA bytes from least to most significant – DAA after each add – Clear C before least significant ADCA 7 ADCA Add with Carry to A S12CPUV2 Reference Manual, p. 103 8 BCD Arithmetic: CCR Initialization Special consideration with ADCA •C – ADCA Operand A ¬ A + Source + C ÞProgrammer clears before first ADCA operation ANDCC #%11111110 9 BCD in Week 6 Lab • BCD I/O – Read BCD number from keyboard – Write BCD number to screen ÞWhat numbers will be used? Up to 8-digit decimal numbers (8 bits allows two 4-bit digits, so need 4 bytes per number ) • BCD arithmetic operations – Add 8-digit BCD numbers 10 BCD Addition • Algorithm – Initialize CCR for BCD operation – Perform BCD operation (add/subtract) – Check for invalid result • What is an invalid result? – Occurs when result must fit within a specific number of decimal digits, (i.e., limited number of packed bytes) – C after final add indicates invalid result 11 ...
View Full Document

Ask a homework question - tutors are online