2
votes

as a hobby I'm trying to learn ARM assembly after briefly seeing it years ago in College.

I modified a little pseudo Hello World program (found in a book) this way :

 .global _start          

_start:         ldr     R1,=msgtxt      
                mov     R2,#11          
                bl      v_asc           
                mov     R0,#0           
                mov     R7,#1           
                svc     0               


                .text
v_asc:          mov    R0,#1           
                mov    R7,#4           
                svc    0               
                bx      LR              
                .end


                .data
msgtxt:         .ascii  "Yeah Baby!\n"

LD throws the following error :

prog.o: In function v_asc': (.text+0x1c): undefined reference tomsgtxt'

Simply putting the .data section above the .text one makes it work like a charm. But then, _start is still above .data :

 .global _start          

_start:         ldr     R1,=msgtxt      
                mov     R2,#11          
                bl      v_asc           
                mov     R0,#0           
                mov     R7,#1           
                svc     0               

                .data
msgtxt:         .ascii  "Yeah Baby!\n"

                .text
v_asc:          mov    R0,#1           
                mov    R7,#4           
                svc    0               
                bx      LR              
                .end

But this confuses me :

Why is LD pretending the reference is in v_asc while it is in _start? How come the line "ldr R1,=msgtxt" does not throw an undefined reference?

Thanks by advance.

1
Don't put stuff after .end as that will be ignored. The order of your blocks does not matter. As for the misleading message, that's just because = instructs the assembler to place the constant into a literal pool and that happens to be located after v_asc. - Jester

1 Answers

2
votes

I'm answering my question since Jester did it in comment (thanks!).

I made a rookie mistake not understanding the .end was for the whole program.

As for the main question about the undefined reference, it is due to the semantics of '=' which places the constant in the literal pool which, in the object file, is after v_asc (I found out more with this page : http://benno.id.au/blog/2009/01/02/literal-pools).