Assembly language multi-byte BCD code addition and subtraction

Publisher:翅膀小鹰Latest update time:2013-01-07 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

(1) Label: BCDA Function: Multi-byte BCD code addition

Here is a multi-byte BCD code addition and subtraction subroutine written in MCU assembly language.
Entry conditions: byte number in R7, addend in [R0], addend in [R1].
Exit information: sum in [R0], highest carry in CY.
Affected resources: PSW, A, R2 Stack requirements: 2 bytes
BCDA: MOV A, R7; get byte number to R2
MOV R2, A
ADD A, R0; initialize data pointer
MOV R0, A
MOV A, R2
ADD A, R1
MOV R1, A
CLR C
BCD1: DEC R0; adjust data pointer
DEC R1
MOV A, @R0
ADDC A, @R1; add byte by byte
DA A; decimal adjustment
MOV @R0, A; store sum back to [R0]
DJNZ R2, BCD1; process all bytes
RET

Assembly language multi-byte BCD code addition and subtraction

(2) Label: BCDB Function: Multi-byte BCD code subtraction
Entry conditions: Byte number in R7, minuend in [R0], subtrahend in [R1].
Exit information: Difference in [R0], highest borrow in CY.
Affected resources: PSW, A, R2, R3 Stack requirement: 6 bytes
BCDB: LCALL NEG1; Complement the decimal value of the subtrahend [R1] LCALL
BCDA; Process as multi-byte BCD code addition
CPL C; Convert the carry flag of the complement addition to the borrow flag
MOV F0,C; Protect the borrow flag
LCALL NEG1; Restore the original value of the subtrahend [R1]
MOV C,F0; Restore the borrow flag
RET
NEG1: MOV A,R0; Complement the decimal value of [R1] Subroutine entry
XCH A,R1; Exchange pointer
XCH A,R0
LCALL NEG; Complement [R1] through [R0]
MOV A,R0
XCH A,R1; Exchange pointer
XCH A,R0
RET

(3) Label: NEG Function: Complement multi-byte BCD code

Assembly language multi-byte BCD code addition and subtraction

Entry condition: byte number in R7, operand in [R0].
Exit information: result is still in [R0].
Affected resources: PSW, A, R2, R3 Stack requirements: 2 bytes
NEG: MOV A,R7; load (byte number minus one) into R2
DEC A
MOV R2,A
MOV A,R0; protect pointer
MOV R3,A
NEG0: CLR C
MOV A,#99H
SUBB A,@R0; complement decimal byte
MOV @R0,A; store back to [R0]
INC R0; adjust data pointer
DJNZ R2,NEG0; process (R2) bytes
MOV A,#9AH; complement the lowest byte separately
SUBB A,@R0
MOV @R0,A
MOV A,R3; restore pointer
MOV R0,A
RET

Reference address:Assembly language multi-byte BCD code addition and subtraction

Previous article:RS232 and RS422 interface circuits and their differences
Next article:1-wire bus assembler

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号