2

I am trying to implement a counter in assembly where aside from doing normal tasks, I want to print the counter. So if my counter is 10 seconds, I want to perform the tasks in that 10 seconds, and print 10,9,8..and so on

The only way I can think of to do this is by checking if the current - starting time in milliseconds is a perfect thousand, e.g it is 10,000 for 10, 9000 for 9, etc, and print those numbers.

So my question is how do I do the modulus function in ARM assembly? I am working in ARMSIM which has no division instruction.

It does have binary right shift and binary left shift, binary AND, XOR etc Is there a way of achieving this using those operations?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
user1411893
  • 608
  • 2
  • 8
  • 19
  • Although phrased a little differently, this is a possible duplicate of [How can I take mod of a number in assembly in Motorola M6800](http://stackoverflow.com/questions/5189631/how-can-i-take-mod-of-a-number-in-assembly-in-motorola-m6800). With the limitations you're giving, what you have is pretty similar to the 6800, except you have larger registers. – Jerry Coffin Jul 17 '12 at 05:57
  • 1
    Or for ARM division: http://stackoverflow.com/questions/8348030/integer-division-on-arm – Ray Toal Jul 17 '12 at 05:58
  • Or this one: http://stackoverflow.com/questions/938038/assembly-mod-algorithm-on-processor-with-no-division-operator – Sumo Jul 17 '12 at 06:02

3 Answers3

2

Your code obviously starts doing your task at some moment in time, T.

If you know the starting time, T, you can simply add 1000 to it to get the time U when you should print an answer.

Then simply compare the time to U. If greater, print an answer, and set U to U+1000 as the limit for the next answer to be printed.

People often build "timers" for real time operating systems using this essential idea.

No division necessary :-}

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
0

Well, first I'm not convinced this is the best way to solve the problem.

But it's not so hard to divide by 1000 if you can multiply by 1000. Do binary search. Something like this:

; Compute x / 1000 by binary search.
lo = x >> 10 ; x / 1024
hi = x >> 9  ; x / 512
while lo < hi 
  mid = (lo + hi) >> 1;
  test = mid * 1000
  if test < x then lo = mid + 1
  else if test > x then hi = mid - 1
  else return mid
end
return hi

With this quotient you can do the mod with x - q * 1000

Of course multiplying x by 1000 is just x << 10 - x << 4 - x << 3

Gene
  • 46,253
  • 4
  • 58
  • 96
-1

The last digit of an odd number in binary is always = 1, such that 3--> 11 5--> 101 7--> 111 So suppose:

Mov R0,#0x01
Mov R1,#0x23
And R3,R0,R1
CMP R3,#0X01
BEQ ODD
BNE EVEN
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • The OP isn't asking about `0b1000`, he's asking about decimal 1000. It's a bad question title. BTW, the 2nd branch should be unconditional, or not exist. – Peter Cordes May 08 '16 at 06:12