简体   繁体   中英

Unsigned Integer Division ARM Cortex-M0+ Assembly

Hi all I am new here but anyway here is my problem: I am trying to write a subroutine for unsigned integer division in Assembly but I really cannot figure it out. If anyone could show me how to do it, that would be great.

I will call the subroutine DIVU. R1 will be the dividend. The divisor will be in R0. The quotient is going to be in RO and the remained in R1.

Basically, I am trying to make something like this: R1÷R0=R0remainderR1

If R0=0, I want to leave the input parameters unchanged and set the C flag when it returns. Otherwise, I just want to clear the C flag. I do not want to change any other registers' values after returning.

Thanks in advance for the help!

Here is what I have come up with so far. What do you all think? Does it seem like it will work? Constructive criticism for a newbie would be nice, as well as some examples of what to change it to. Thanks :)

DIVU
      CMP   R1,#0       ;compares R1 to 0
      BEQ   AnsZero     ;if R1=0, it branches to AnsZero (the final answer will be 0)
      CMP   R0,#0       ;compares R0 to 0
      BEQ   EndFlag     ;if R0=0, it will go to the end to set C flag
      PUSH  {R3}        ;saves R3 so it can used as a counter for quotient
      MOV   R3,#0       ;sets R3 to 0
While CMP   R0,R1       ;start of while loop 
      BLT   EndWhil     ;Branches to end of while when dividend < divisor, otherwise goes through loop
      SUB   R1,R1,R0    ;R1=R1-R0 , dividend=dividend-divisor
      ADD   R3,R3,#1    ;R3=R3+1, quotient=quotient+1 (init is zero, so 0+1=1 if one successful loop)
      B While       ;continues loop
EndWhil MOV R0,R3       ;R0=R3, the register that had the divisor gets the quotient
      PULL  {R3}        ;R3's original value is returned
      B Return      ;goes to end of subroutine labeled return
EndFlag PUSH {R0}       ;saves original R0 value
      PUSH  {R1}        ;saves original R1 value
      MRS   R0,APSR     ;this line and the next few down all set the C flag without changing other flags
      MOVS  R1,#0x20    
      LSLS  R1,R1,#24   
      ORRS  R0,R0,R1    
      MSR   APSR,R0
      PULL  {R1}        ;gets original R1 value
      PULL  {R0}        ;gets original R0 value
      B Return      ;goes to end of subroutine
AnsZero MOV R0,#0       ;sets R0=0 because R1=1, 0/X=0r0
      B Return      ;goes to end of subroutine
Return  BX  LR      ;returns from subroutine

I apologize if the code is not displayed correctly, I am new to this so I am still trying to figure out the best way to copy it to the browser and show properly here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM