[英]Unsigned Integer Division ARM Cortex-M0+ Assembly
大家好,我是新来的,但无论如何,这是我的问题:我正在尝试为Assembly中的无符号整数除法编写一个子例程,但我真的无法弄清楚。 如果有人可以教我如何做,那就太好了。
我将调用子例程DIVU。 R1将是股息。 除数将在R0中。 商将在RO中,而余数在R1中。
基本上,我试图做这样的事情:R1÷R0 = R0remainderR1
如果R0 = 0,我想保持输入参数不变,并在返回时设置C标志。 否则,我只想清除C标志。 我不想在返回后更改任何其他寄存器的值。
先谢谢您的帮助!
到目前为止,这是我想出的。 你们怎么想 看起来会起作用吗? 对于新手的建设性批评会很好,以及将其更改为新内容的一些示例。 谢谢 :)
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
对于无法正确显示代码,我深表歉意,因为这是我的新手,所以我仍在尝试找出将其复制到浏览器并在此处正确显示的最佳方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.