flat assembler
Message board for the users of flat assembler.
Index
> Non-x86 architectures > [ARM] How do I load a large constant into a register? |
Author |
|
revolution 30 Oct 2011, 18:46
You can't encode that value in a single instruction (see the ARM manual about immediate constant encoding).
Depending upon which version of CPU you have you can encode it like this: Code: ;for v6 and below mov r0,0x3000000 orr r0,0x0007f00 orr r0,0x00000fc ;for v6T2 and above movw r0,0x0007ffc movt r0,0x0300 |
|||
30 Oct 2011, 18:46 |
|
revolution 30 Oct 2011, 19:02
Perhaps I should add that if you need to keep the constant as a 32 bit value (for relocation maybe) then you can do this:
Code: ldr r0,[.myValue] ;... .myValue dw 0x03007ffc Last edited by revolution on 02 Jun 2012, 04:57; edited 1 time in total |
|||
30 Oct 2011, 19:02 |
|
Coty 30 Oct 2011, 19:19
Ah, I see! Hmm, I think I'll just use LDR for now, I'll probably be less likely to make mistakes that way...
|
|||
30 Oct 2011, 19:19 |
|
f2 01 Nov 2011, 08:30
Indeed, using LDR for loading an immediate value in a register is the best way. Others ARM assemblers support the 'LDR' pseudo-instruction (http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0041c/Babbfdih.html) for that:
Code: ldr r0, =0x3007FFC In this example, the 'LDR' pseudo instruction loads the value 0x3007FFC into R0, which is what you wanted to do with 'mov'. FASMARM does not support this pseudo-instruction, but you can "implement" it as a macro: Code: macro movi reg, value { ldr reg, [pc] b @f dw value @@: } This will allow you to do this: Code: movi r0, 0x3007FFC |
|||
01 Nov 2011, 08:30 |
|
revolution 01 Nov 2011, 10:01
f2 wrote:
Code: macro movlit reg,const { local .const,.skip ldr reg,[.const] b .skip align 4 .const: dw const .skip: } BTW: I wouldn't recommend using the @@ syntax inside macros. Other non-macro surrounding code may also use @@ and be broken by the macro. |
|||
01 Nov 2011, 10:01 |
|
f2 01 Nov 2011, 13:00
revolution wrote:
Oh. I forgot that . Thank you for reporting this . |
|||
01 Nov 2011, 13:00 |
|
Coty 02 Nov 2011, 02:04
Yeah, I just can't justify the extra clockticks it would take to make my life 6% easier though, even if all my code will do is print pink and blue dots
|
|||
02 Nov 2011, 02:04 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.