(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
Previous article:RS232 and RS422 interface circuits and their differences
Next article:1-wire bus assembler
- Popular Resources
- Popular amplifiers
- STC MCU Principles and Applications: Analysis and Design from Devices, Assembly, C to Operating Systems: A Three-Dimensional Tutorial (He Bin)
- RP2040 Assembly Language Programming ARM Cortex-M0+ on the Raspberry Pi Pico
- Patterson-Computer Organization and Design_The Hardware_Software Interface
- TMS320C28x_Assembly_Language_Tools_User’s_Guide
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- 51 STM32 reference study manuals, one-click download without points!
- Has anyone seen this chip?
- How to insert assembly code in Keil 5 version?
- Gaoyun FPGA logic analyzer function usage
- Does anyone have a good thermostat switch?
- Ultra-low power MCU-How to reduce the power consumption of MCU
- [MM32 eMiniBoard Review] Part 4: Questions about multi-channel ADC
- Modbus_RTU (RS485) Fieldbus Remote Inputs/Outputs Modules System Design Based on GD32E231
- Particle Xenon nRF52840 BLE Development Board
- COCOFLY Tutorial - Crazy Shell Drone Series · Quick Start · [6] Use and Introduction of Ground Station Host Computer