Four-byte by three-byte unsigned number division program

Publisher:mancozcLatest update time:2018-05-08 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

;I saw someone online asking for a "four-byte by three-byte" subroutine, so I modified the program I wrote before and posted it below.
;I feel good about it, and think it is the _simplest_ and shortest_ program that can complete this function.
;The program is as follows, haha, sorry for the ugliness.
;===================================================================
    ORG 0000H
    LJMP START

    ORG 0030H ; Main program starts
START:
    MOV R7, #05H ; Set a 32-bit dividend
    MOV R6, #0F5H ; 05 F5 E0 FFH
    MOV R5, #0E0H ; = 99 999 999
    MOV R4, #0FFH
; MOV R3, #69H
    MOV R2, #98H ; Set a 24-bit divisor
    MOV R1, #96H ; 98 96 80H
    MOV R0, #80H ; = 10 000 000


    CALL DIV_32_24 ;Call the division subroutine

    SJMP $ ; Quotient, can be verified by a calculator: 00 00 00 09H
                     ; Remainder: 98 96 7FH = 9 999 999
;********************************************************
;* Four-byte/three-byte unsigned number division program *
;* Already tested *
;****************************************************
; R7R6R5R4 / R2R1R0 --> R7R6R5R4...32H31H30H
;
; Dividend R7R6R5R4, divisor R2R1R0
; Quotient R7R6R5R4, remainder 32H31H30H
; Because the number is large, this program uses: A, B, 33H and 32H 31H 30H
; The protection of these units should be performed in the main program
;-----------------------------------------------------------
DIV_32_24:
    CLR A
    MOV 30H, A
    MOV 31H, A
    MOV 32H, A
    MOV R3, #32 ;Number of loops
;---------------------------------------------------
    CLR C
DIV_LOOP:
    CALL SL_R7_R4
    CALL SL_32_30
        ;(32H31H30H <-- R7R6R5R4 <-- C) Shift left one bit
    MOV F0, C
;---------------------
    CLR C ;IF (32H31H30H >= R2R1R0) 32H31H30H -= R2R1R0;
    MOV A, 30H
    SUBB A, R0
    MOV 33H, A
    MOV A, 31H
    SUBB A, R1
    MOV B, A
    MOV A, 32H
    SUBB A, R2
    JNC DIV_2
    JNB F0, CPL_C ;If not enough, don’t save the difference
    CPL C
DIV_2:
    MOV 30H, 33H
    MOV 31H, B
    MOV 32H, A
CPL_C:
    CPL C
    DJNZ R3, DIV_LOOP
;--------------------------------------------------
SL_R7_R4:
    MOV A, R4
    RLC A
    MOV R4, A
    MOV A, R5
    RLC A
    MOV R5, A
    MOV A, R6
    RLC A
    MOV R6, A
    MOV A , R7
    RLC A
    MOV R7, A
    RET
;--------------------------
SL_32_30:
    MOV A, 30H
    RLC A
    MOV 30H, A
    MOV A, 31H
    RLC A
    MOV 31H, A
    MOV A, 32H
    RLC A
    MOV 32H, A
    RET
;-------------------------------------------------------------
    END
;================================================================


Reference address:Four-byte by three-byte unsigned number division program

Previous article:A simple method to calculate the offset in table lookup instructions
Next article:How to determine the number of bytes in MCS-51 series microcontroller instructions

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号