Reputation: 409
I am practicing the assembly programming, My doubt is arm-none-eabi-ld -Ttext=0x20 -o add.elf.the labels get the address from 0x20 till 0x2C because of 4 instructions and each instruction is 4 bytes.The program counter contains the address of the instruction to be fetched, here R15 is the PC it contains the value 0x0C. How it holds 0x0C, it should have had the addresss 0x2C. Then what is the use of arm-none-eabi-ld -Ttext=0x20 -o add.elf, placing the text section in 0x20. I am a noobie so it would be helpful if it is explained in simple terms and stepwise.
start:
mov r0, #5
mov r1, #2
add r2, r1, r0
stop:
b stop
arm-none-eabi-ld -Ttext=0x20 -o add.elf.
user@stretch:~/Desktop/Gnu_Toolchain/Adding_Two_Numbers$ arm-none-eabi-nm -n add.elf
U _start
**00000020** t start
0000002c t stop
qemu-system-arm -M connex -pflash flash.bin -nographic -serial /dev/null
info registers
R00=00000005 R01=0000000c R02=00000011 R03=00000000
R04=00000000 R05=00000000 R06=00000000 R07=00000000
R08=00000000 R09=00000000 R10=00000000 R11=00000000
R12=00000000 R13=00000000 R14=00000000 **R15=0000000c**
Upvotes: 1
Views: 148
Reputation: 71536
start:
mov r0, #5
mov r1, #2
add r2, r1, r0
stop:
b stop
arm-none-eabi-as so.s -o so.o
arm-none-eabi-ld -Ttext=0x20 -o add.elf
arm-none-eabi-ld: no input files
arm-none-eabi-ld -Ttext=0x20 so.o -o add.elf
arm-none-eabi-ld: warning: cannot find entry symbol _start; defaulting to 0000000000000020
arm-none-eabi-objdump -d add.elf
add.elf: file format elf32-littlearm
Disassembly of section .text:
00000020 <start>:
20: e3a00005 mov r0, #5
24: e3a01002 mov r1, #2
28: e0812000 add r2, r1, r0
0000002c <stop>:
2c: eafffffe b 2c <stop>
So you left out stuff in your question, you didn't show the definition of _start, your command line was incomplete, etc.
I can't see what dumping registers from nm has to do with anything or why that is even something nm would output. What -Ttext=0x20 means is that you are telling the linker that that code will be placed in the processors memory space at address 0x20 so it needs to link the code for that address. The branch is pc relative so it is position independent so you end up with the same machine code for this example independent of the address where this code will live.
When you execute this code (nm doesn't execute code) and if this code were in memory where you indicated then the branch for example will start execution with PC set to 0x2C+8 = 0x34 and after execution of the branch 0x2C. Understanding that the program counter is a pseudo register, there are essentially multiple program counters one for doing math during execution, one for fetching, one for a possible branch destination, and one that you see when you dump registers in a debugger.
But to see any of this you need to actually run it and use tools that stop execution and examine registers. And to do that properly you need to place it memory at the as linked address and run there. You can't see anything statically from the binary.
Upvotes: 1