2

I am reading the book for learning ARM and ARM assembly.. examples in book is based on armcc compiler but I am using arm-none-eabi-gcc. so how can change this peace of code to gnu assembler??

IMPORT |Lib$$Request$$armlib|,WEAK. 

whole example is :

    AREA |.text|, CODE , READONLY
    EXPORT main
    IMPORT |Lib$$Request$$armlib|,WEAK
    IMPORT __main ;C library entry
    IMPORT printf ; prints to stdout
i   RN 4
    ;int main(void)
main
    STMFD sp!,{i,lr}
    MOV i,#0
loop
   ADR r0, print_string
   MOV r1,i
   MUL r2,i,i
   BL printf
   ADD i,i,#1
   CMP i,#10
   BLT loop
   LDMFD sp!,{i,pc}
print_string
   DCB "Square of %d is %d\n",0
   END

so I converted it to

 .section .text
    .weak Lib$$Request$$armlib
    .global main
    i .req r4
main:
    STMFD sp!,{i,lr}
    MOV i,#0
loop:
    ADR r0,print_string
    MOV r1,i
    MUL r2,i,i
    BL printf
    ADD i,i,#1
    CMP i,#10
    BLT loop
    LDMFD sp!,{i,pc}
print_string:
    .ascii "Square of %d is %d\n"
    .end

I am using ARMSim for simulating ... but I get some error:

Undefined symbol printf address is not in text section so how can include "stdio.h" for using printf or what is wrong generally??

fuz
  • 88,405
  • 25
  • 200
  • 352
  • Do you know which library provides the `printf` function? And as you aren't programming in C, I'm not sure what you want to do with the `stdio.h` header file. – fuz Mar 31 '17 at 11:34
  • If you're using `ARMSim`, it is possible that `printf` isn't available at all. – fuz Mar 31 '17 at 11:39
  • I am confusing here, and if you are kind enough, please help me to find out and please describe me ever single line of code and translation to gnu assembler. I will appropriate this kindness. – peyman khalili Mar 31 '17 at 11:40
  • I believe your translation is correct (though, this `.weak Lib$$Request$$armlib` won't do anything). It's just that you haven't linked against a library that provides `printf`. As I've never worked with ARMSim, I cannot say if there is a library providing this function. – fuz Mar 31 '17 at 11:55
  • If you want to have your code explained, please ask a separate question. – fuz Mar 31 '17 at 11:55
  • thank you, it was enough to me. – peyman khalili Mar 31 '17 at 12:13

1 Answers1

4

In the GNU assembler, you do not need to declare symbols you use. If you use a symbol and that symbol isn't defined anywhere, it will be entered as an undefined symbol into your object file.

To define a symbol as a weak symbol, write:

.weak sym

where sym is the name of the symbol.

fuz
  • 88,405
  • 25
  • 200
  • 352