How to do an assemble-time modulus on constants in ARM assembly language

后端 未结 3 1136
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-18 16:16

I want to know how to do modulus in arm assembly language

I\'ve tried the code in this page MOD operator in arm website:

MOV     R1,#12 MOD 7   ; R1         


        
3条回答
  •  长情又很酷
    2020-12-18 16:46

    If you're looking for runtime modulo instead of assemble-time, you can do div and mod using two instructions:

    ;Precondition: R0 % R1 is the required computation
    ;Postcondition: R0 has the result of R0 % R1
                  : R2 has R0 / R1
    
    ; Example comments for 10 % 7
    UDIV R2, R0, R1      ; 1 <- 10 / 7       ; R2 <- R0 / R1
    MLS  R0, R1, R2, R0  ; 3 <- 10 - (7 * 1) ; R0 <- R0 - (R1 * R2 )
    

    Documentation for MLS, which was designed exactly for this use-case.

提交回复
热议问题