1

I am trying to call printf in ARM M4 assembly and meet some problems. The purpose is to dump content in R1. The code is like the following

    .data
    .balign 4
output_string:
    dcb "content in R1 is 0x%x\n", 0
....
    .text
....
    push {r0, r1}
    mov r1, r0
    ldr r0, =output_string
    bl printf
    pop {r0, r1}

The problem I meet is that, when put "output_string" address into R0, the value is added with a extra 1. For example, if the symbol "output_string" have a value of 0x2000, R0 will get the value 0x2001.

I feel this has something to do with THUMB/ARM mode. But I have declare "output_string" in data section, why the assembler still translate it as an instruction address?

Or is there some more formal way to do such in-assembly function calling?

artless noise
  • 21,212
  • 6
  • 68
  • 105
Eric Sun
  • 777
  • 6
  • 20

1 Answers1

0

I think you should use:

ldr r0, =output_string

The = prefix is an assembler shorthand to make it load an arbitrary 32-bit constant. See this ARM Information Center page.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • Do you think it is a bug that `ldr r0, output_string` produces an odd address? – auselen Mar 26 '14 at 09:27
  • Thanks for point it out, actually this is a typo when I describe the problem. I acturally use "ldr r0, =output_string" in my code, but the address loaded into r0 is "output_string + 1". I have correct it in the original post. – Eric Sun Mar 26 '14 at 09:30