简体   繁体   English

无符号整数分部ARM Cortex-M0 +组件

[英]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. 大家好,我是新来的,但无论如何,这是我的问题:我正在尝试为Assembly中的无符号整数除法编写一个子例程,但我真的无法弄清楚。 If anyone could show me how to do it, that would be great. 如果有人可以教我如何做,那就太好了。

I will call the subroutine DIVU. 我将调用子例程DIVU。 R1 will be the dividend. R1将是股息。 The divisor will be in R0. 除数将在R0中。 The quotient is going to be in RO and the remained in R1. 商将在RO中,而余数在R1中。

Basically, I am trying to make something like this: R1÷R0=R0remainderR1 基本上,我试图做这样的事情:R1÷R0 = R0remainderR1

If R0=0, I want to leave the input parameters unchanged and set the C flag when it returns. 如果R0 = 0,我想保持输入参数不变,并在返回时设置C标志。 Otherwise, I just want to clear the C flag. 否则,我只想清除C标志。 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. 对于无法正确显示代码,我深表歉意,因为这是我的新手,所以我仍在尝试找出将其复制到浏览器并在此处正确显示的最佳方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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