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
Thread Post new topic Reply to topic
Coty



Joined: 17 May 2010
Posts: 553
Location: ␀
Coty 30 Oct 2011, 18:17
I'm learning ARM programming for the game boy advance, but I've ran into a snag.

This code points r0 to GBA video memory:
Code:
        mov     r0, 0x6000000
    

So that I can write a pixle, however I started working with interrupts as I thought it would be neat to port Helium-OS to the GBA (Only printing pixles instead of ASCII cars in text mode).

But when I went to point r0 to an interrupt handle I wish to hook:

Code:
        mov     r0, 0x3007FFC    


I get:
Quote:
flat assembler for ARM version 1.69.34 (16384 kilobytes memory)
head.asm [64]:
mov r0, 0x3007FFC
error: Immediate value cannot be encoded.


Am I missing something Question

_________________
http://codercat.org/
Post 30 Oct 2011, 18:17
View user's profile Send private message Send e-mail Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
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    
Post 30 Oct 2011, 18:46
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
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
Post 30 Oct 2011, 19:02
View user's profile Send private message Visit poster's website Reply with quote
Coty



Joined: 17 May 2010
Posts: 553
Location: ␀
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...

_________________
http://codercat.org/
Post 30 Oct 2011, 19:19
View user's profile Send private message Send e-mail Visit poster's website Reply with quote
f2



Joined: 30 May 2010
Posts: 13
Location: France
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
    
Post 01 Nov 2011, 08:30
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 01 Nov 2011, 10:01
f2 wrote:
Code:
macro movi reg, value {
      ldr     reg, [pc]
   b       @f
  dw      value
@@:
}
    
Note that that code only works for ARM mode. If you want to support both THUMB and ARM mode you can use this:
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.
Post 01 Nov 2011, 10:01
View user's profile Send private message Visit poster's website Reply with quote
f2



Joined: 30 May 2010
Posts: 13
Location: France
f2 01 Nov 2011, 13:00
revolution wrote:

BTW: I wouldn't recommend using the @@ syntax inside macros. Other non-macro surrounding code may also use @@ and be broken by the macro.

Oh. I forgot that Confused . Thank you for reporting this Wink .
Post 01 Nov 2011, 13:00
View user's profile Send private message Reply with quote
Coty



Joined: 17 May 2010
Posts: 553
Location: ␀
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 Very Happy

_________________
http://codercat.org/
Post 02 Nov 2011, 02:04
View user's profile Send private message Send e-mail Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.