PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Yes, it's another arm asm threas :)

Ok, I was trying to do a few things with the number 10000 in asm, but I keep getting errors. Now, I noticed that when the compiler does something, let's say set's a regiser to 10,000 it does this
Code:
mov r3, #0x27, 24 ; 0x2700 = 9984
orr r2, r3, #0x10 ; 0x10 = 16


is that the fastest way to set 10,000 to r2? And, why can't I simply use a move funtion? Thanks for any info you have.

[edit] :oops: Oops, Just noticed that the title said threas, instead of thread.
The ARM actually saves immediate values as an 8-bit value followed by a bitwise rotate by an even number of bits. (Incidentally, 510 can't be used, but 1021 can, even though they both use 8 bits).

Since 10,000 takes 10 bits to represent (you can use the windows calculator to figure this out), you will need to do some sort of trick.

You can either do this
Code:

mov r2, #16
orr r2, r2, #9984
; the mov and orr could be swapped.

or this
Code:

ldr r2, =10000


Method one is the one the compiler uses.
Method two doesn't use an actual machine instruction, but an assembler pseudo-instruction. The
"ldr" instruction translates to either method one or an access to static program memory stored in the program file. The assembler decides which one to use depending on the (in either case, it's probably better to use number two unless you are in a 256-byte demo competition where you need to know the exact size).
Not related to the topic (well, I guess it is :) ), but you can change the thread topic by editing your original post.

Dan East
Yeah, I know you can change the title, but I always dislike changing a title, becuase then people might not remember that this was a thread they were going to repond to later.

And my next question, which is quicker, or are they both the same

Code:
add r1, r1, r2


or

Code:
add r3, r1, r2
For data processing instructions (like add), they're the same.
According to the ARM reference, the behavior of MUL in the first case is undefined (I have heard that it doesn't matter on most ARM processors, it just isn't required to work for a processor to be ARM compliant, I guess).I read somewhere that MUL is faster if the bigger of the two numbers is first (I'm not sure about this, though).

But for everything besides MUL they are both the same.
is there a speed difference between add and sub in arm asm?

[edit] also what would be quicker

mov r3, #0x27, 24

or

mov r3, #9984
ADD and SUB are equally speedy. With twos-complement math, the following are effectively the same (as far as outcome):
Code:

SUB r2, r0, r1 ; r2=r0-r1

RSB r3, r1, r0 ; r3=r0-r1

NOT r4, r1 ; Not a real ARM instruction; would invert all bytes
ADD r4, r4, #1 ; at this point r4=-r1
ADD r4, r4, r0
; r4=r0-r1

The processor probably subtracts this way internally (albeit with hardwired circuits) as an adder circuit and a twos-complementer (is that an actual word?) is easier to make than an actual subtracter (I don't even know how you would do that. One bit is pretty easy, but I'm not sure how the carry would work)

I think I understand what you are trying to do (though I don't think that's the right syntax), but the assembler will translate the second one to the internal representation when it assembles. Every opcode is 32-bits and with the exception of certain memory access, multiplies, and possibly a couple others, every instruction takes one cycle. Intel should have a datasheet on their website with the timings for each instruction.
It used to be the case that the internals of processors could only subtract. Additions can be represented by subtractions in twos-complement form.


NOT r4, r1 ; Not a real ARM instruction;

Ones complement (bitwise negation):
MVN r4,r1 ; IS a real ARM instruction :)


A MUL using the same destination as one of the source registers is not a good idea - as billcow says, it's "undefined"... he's correct - putting the smaller number on the right hand-side is faster.

i.e.:

LDR R0,=123456
MOV R1,#89

MUL R2,R1,R0 ; <---- Slower

MUL R2,R0,R1 ; <---- Faster


The timings are all in the StrongARM datasheet.

Cheers,

Refractor