1

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 = 5
MOV     R2,#99 MOD 10  ; R2 = 9

but it doesn't assemble.

I'm using the keil assembler.

Peter Cordes
  • 286,368
  • 41
  • 520
  • 731
user2954718
  • 67
  • 1
  • 8
  • 1
    Define _"it isn't working"_. You also need to specify which assembler you're using, and whether you want to do a compile-time or run-time MOD operation. – Michael Nov 14 '13 at 12:48
  • What have you tried already, and with which toolchain? The ARM documentation should match the ARM toolchain, so I'm guessing you're using something else. – ams Nov 14 '13 at 12:48
  • @michael i updated my question, i hope it's clear now. ams i didnt try anything, i'm learning arm and i'm testing simple stuff, so when i googled mod it led me to that page i posted in my original post – user2954718 Nov 14 '13 at 12:56
  • You should learn how to make divisions first. Do some research prior to asking. – Jake 'Alquimista' LEE Nov 14 '13 at 20:23

3 Answers3

8

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.

Peter Cordes
  • 286,368
  • 41
  • 520
  • 731
ns.
  • 2,600
  • 1
  • 22
  • 26
  • 1
    Should note that this is a _run-time_ modulus. – ns. Dec 31 '17 at 22:44
  • This answer really belongs in a separate Q&A. This question's title is about something different so it looks weird to use it as a duplicate target for questions that include how to do runtime modulo. – Peter Cordes Oct 22 '19 at 22:51
4

Keil/armasm spells it :MOD:. See the manual http://www.keil.com/support/man/docs/armasm/armasm_Cacechjd.htm

scott
  • 708
  • 4
  • 8
1

If you're using the GNU assembler (you don't say), then the mod (remainder) operator is %, same as C.

The fine manual is here.

ams
  • 23,632
  • 4
  • 50
  • 73
  • sry, i didnt know it depends on the assembler, well i updated my question – user2954718 Nov 14 '13 at 13:05
  • 2
    Just to be clear, this operator is evaluated at compile time and does not produce an executable modulus operation in ARM assembly code. –  May 25 '14 at 18:01