GNU Arm Assembly--(ii) compilation of compilation links and operations

Source: Internet
Author: User

The GNU assembler is part of the GNU tools and can be used for ARM assembly language source code to be compiled into binary files. An introduction to the GNU Assembler can be found in the GNU Assembler Manual. Here we are just making a brief introduction, Have a general idea of the GNU assembler, and take a look at the GNU arm Assembly in two examples.

Give a template file:

    . text                                               ; Executable code follows
_start:. Global _start                                  ; "_start" is required by the linker
    . Global main                                        ; "Main" is my main program
    b main                                              ; Start running the main program
main:                                                   ; Entry to the function "main"
; Insert your code here
    mov PC,LR                                           ; Return to the caller
    .
Use of assembler:

One assembler is Arm-elf-as, one is Arm-linux-as, and the other is subtle. But in general, the semiconductor manufacturer will provide a specific compiler, and the compiler should be correct, and the optimization effect should be optimal, After all, it is provided by the chip company. They know the architecture best, and are clear about how to optimize it. And our average developer can understand the architecture of the processor and the characteristics of the system of the embedded system to optimize the assembly code and C code.

Compile process:

Arm-elf-as-marm7tdmi--gdwarf2-o FILENAME.O FILENAME.S

-MARM7TDMI is the designation CPU,ARM7TDMI belongs to the armv4t, generally armv4t should be compatible.

--gdwarf2 is a representation containing the debug information.

Link process:

Arm-elf-ld-o filename.elf FILENAME.O

As with UNIX system programming, we can write makefile according to the steps above and make it.

Specific arm of the instruction set, pseudo instructions will not write, a lot of data.

Here are two examples of arm assembly, one is the buzzer under the bare metal (simple control gpio only, than the water lamp is also simple), one is under Arm Linux "Hello World" (using system calls to achieve).

The buzzer example is as follows:

Beep.lds beep. S Makefile start. S

Start. S

. Text
. Global _start
_start:
	LDR     R3, =0x53000000     @ watchdog register address
	mov     r4, #0x0                     
	str   R4, [R3]              @ Write 0, prohibit watchdog, otherwise the CPU will continue to reboot

	Ldr     sp, =1024*2         @ set Stack, note: Can not be greater than 4k, because the available memory is now only 4 K
                                        @ The code in NAND Flash is moved to internal RAM after the reset, which has only 4 K
	bl      _main                @ jump to main function
Halt_loop:
	b       Halt_loop

Beep. S

. equ    Gpbcon,   0x56000010  
equ    gpbdat,   0x56000014  

. Global _main  
_main: 
	LDR r0,= Gpbcon

	LDR r1,=0x1
	str R1, [R0]
loop: Ldr r2,=gpbdat ldr r1,=0x1
	str r1,[r2]
	BL Delay

	Ldr r2,=gpbdat
	ldr r1,=0x0
	str r1,[r2]
	bl delay

	b loop

delay:
	LDR r3,= 0X4FFFFFF

delay1:
	Sub R3,r3, #1

	CMP R3, #0x0

	bne delay1

	mov pc,lr
. End

Beep.lds
Output_format ("Elf32-littlearm", "Elf32-littlearm", "Elf32-littlearm")
Output_arch (ARM)
ENTRY (_start)

sections{
    . = 0x33000000;
    . Text: {
        * (. Text)
        * (. rodata)
    }

    . Data ALIGN (4): {
        * (. data)
    }

    . BSS ALIGN (4): {
        * (. BSS)
    }
}

Makefile

CROSS =  arm-linux-
cflags =-nostdlib

beep.bin:start. S beep. S
	${CROSS}GCC $ (cflags)-c-o start.o start. S
	${CROSS}GCC $ (cflags)-c-o beep.o beep. S
	${cross}ld-tbeep.lds start.o beep.o-o beep.elf ${cross}objcopy-o binary-s beep.elf beep.bin rm-f  * . O Clean


:
	rm-f *.elf *.o
	rm-f beep.bin

After compiling the Beep.bin file into the dram, you can hear the sound. Although it can be run, there are still two questions:

1.lds Compile link file writing and techniques/follow-up to keep chasing

2.elf file Format//elf format is a newer executable file format that is currently used on many OS. This format can be run directly with the operating system, but for bare-metal situations, the Elf file must be Do objcopy and follow up on the chase.


The examples of Hello world are as follows:

HelloWorld. S

. Data
	msg:  . Asciz  "Hello, world\n"

. Text
       . Align  2
       . Global _start
_start:
       LDR     R1, =msg         @ address
       mov     r0, #1          @ stdout
       mov     r2, #13         @ length
       swi     # 0x900004       @ sys_write
       mov     r0, #0
       swi     #0x900001       @ sys_exit
       . Align  2

Makefile

All:
	arm-linux-as HelloWorld. S-o helloworld.o
	arm-linux-ld  helloworld.o-o HelloWorld

Put the elf files on the ARM board running Linux and run to output Hello World. You can also qemu-arm HelloWorld simulations in Ubuntu.

Compare x86 with the same system tuned to output the Hello World program:

. Data
	msg:. String "hello\n"
	len =.-Msg
. Text
. Global _start

_start:
	nop
	movl $len,%edx
	movl $msg,%ecx
	movl $,%ebx
	movl $,%eax
	int $0x80

	MOVL $,%ebx
	movl $,%eax
	int $0x 80

They differ in a few ways:

1.arm is to use the SWI instruction to carry on the soft interrupt, falls into the kernel state to realize the system call. and x86 is using int $0x80

The 2.x86 system call number is represented by the EAX register, which is the first parameter. and arm SWI directly with the system call number, 0x900004 is 0x900000+4, where 0x900000 is base.


According to Google, the above summary, the GNU ARM Assembly has been recognized, and the system calls soft interrupt, interrupt processing, uboot anomaly vector table, etc. have the desire to explore the elf format and compile links have interest, according to their own direction and energy, Follow up on the content to do one or deep or shallow learning.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.