680 likes | 837 Views
嵌入式處理器架構與 程式設計. 王建民 中央研究院 資訊所 2008 年 7 月. Contents. Introduction Computer Architecture ARM Architecture Development Tools GNU Development Tools ARM Instruction Set ARM Assembly Language ARM Assembly Programming GNU ARM ToolChain Interrupts and Monitor.
E N D
嵌入式處理器架構與程式設計 王建民 中央研究院 資訊所 2008年 7月
Contents • Introduction • Computer Architecture • ARM Architecture • Development Tools • GNU Development Tools • ARM Instruction Set • ARM Assembly Language • ARM Assembly Programming • GNU ARM ToolChain • Interrupts and Monitor
Outline • Linux/Cygwin • GNU Compiler Collection • GNU Libraries and Linker • GNU Binary Utilities
Cygwin • Cygwin is a Linux-like environment for Windows. It consists of two parts: • A DLL (cygwin1.dll) which acts as a Linux emulation layer providing substantial Linux API functionality. • A collection of tools, which provide Linux look and feel. • http://www.cygwin.com/
Linux Commands1 • cat • concatenate file(s) • ex. $ cat hello.c • ex. $ cat > newfile Hello World CTRL-D
Linux Commands2 • ls • list information about the files (the current directory by default).
Linux Commands3 • cp • copy source to destination • ex. $ cp foo1 other • mv • move/rename source to destination • ex. $ mv foo1 other • rm • delete file(s) • ex. $ rm hello.c
Linux Commands4 • mkdir • create a new directory • ex. $ mkdir exercise • rmdir • remove a empty directory • ex. $ rmdir exercise • remove a directory containing files • ex. $ rm –r exercise
Linux Commands5 • pwd • show current directory • ex. $ pwd /home/user1 • cd • change current directory • ex. $ cd exercise
Linux Commands6 • ps • show processes • ex. $ ps • kill • kill a process • ex. $ kill pid
Linux Commands7 • run a program in the background • add & following the command line • ex. $ ./hello &
Linux Commands8 • fg • run a background program in the foreground • ex. $ fg %job_num $ jobs [1]+ gcc program.c $ fg %1
Linux Commands9 • run the foreground program in the background • ex. $ gcc program.c –o program Ctrl-z [1]+ stopped gcc program.c –o program $ bg [1]+ stopped gcc program.c –o program $
Outline • Linux/Cygwin • GNU Compiler Collection • GNU Libraries and Linker • GNU Binary Utilities
GNU Compiler Collection • GNU Compiler Collection (GCC) • An integrated distribution of compilers for several major programming languages • C, C++, Objective-C, Fortran, Java, and Ada • http://gcc.gnu.org/
The Compilation Process input files Preprocessor output files .c Compiler .s Assembler .s .o .o .a Linker a.out / a.exe
An Example #define GREETING ”Hello, World!\n” int main() { printf(GREETING); } hello.c
Using gcc1 • The simplest way to compile • gcc hello.c • a.exe executable file • If you want to produce a executable file named “hello” • gcc -o hello hello.c • hello.exe executable file
Using gcc2 • To compile with multiple source files • Ex: • gcc -o hello hello.c foo.c • hello.exe executable file hello.c : extern void foo(char []) int main() { char str[]=“hello world!”; foo(str); } foo.c : void foo(char str[]) { printf(“%s\n”,str); }
Using gcc3 • -E: Stop after the preprocessing stage; do not run the compiler proper
Using gcc4 • -S: Stop after the stage of compilation proper; do not assemble. Output assembly code file (*.s)
Using gcc5 • -c : Compile and assemble the source files, but do not link. • gcc –c hello.c • hello.o object file • -o file : Place output in file file • gcc –o hello hello.c • hello.exe
Using gcc6 • Specifying libraries • -llibrary : Search the library named library when linking • library file name : liblibrary.a • ex: foo.o refers to functions in library file “libz.a” • gcc -o foo foo.o -lz
Using gcc7 • Specify directories to search for header files, for libraries and for parts of the compiler • -Idir: Add the directory dir to the head of the list of directories to be searched for header file. • -Ldir : Add directory dir to the list of directories to be searched for -l.
Using gcc8 • Example • A header file in “/tmp/foo.h” • A function foo() in library “/tmp/libfoo.a” • A program: /home/foo/foo.c • gcc –I/tmp/foo.h –L/tmp/libfoo.a foo.c -lfoo #include <foo.h> int main() { … foo(); … }
Debugging Options • -g : Produce debugging information in the operating system's native format • -ggdb : Produce debugging information for use by GDB • -time : Report the CPU time taken by each subprocess in the compilation sequence
Optimization Options1 • -O0 : Do not optimize (the default) • -O or -O1 : Optimize • Tries to reduce code size and execution time, without performing any optimizations that take a great deal of compilation time. • -O2 : Optimize even more • Performs nearly all supported optimizations that do not involve a space-speed tradeoff. • Does not perform loop unrolling or function inlining.
Optimization Options2 • -O3 : Optimize yet more • Turns on all optimizations specified by -O2 and also turns on the -finline-functions • -Os : Optimize for size • Enables all –O2 optimizations that do not typically increase code size • -funroll-loops : Loop unrolling • -finline-functions : Function inlining
Outline • Linux/Cygwin • GNU Compiler Collection • GNU Libraries and Linker • GNU Binary Utilities
How to make library1 • Example: write a function print_str in “foo.c” foo.c: #include <stdio.h> void print_str(char str[]) { printf(“%s\n”, str); }
How to make library2 • First, make the object file “foo.o” • Second, use “ar” to create archive file “libfoo.a”. • Option “c” to crate archive file, “r” to insert or replace members to archive file. $ gcc -c foo.c $ ar cr libfoo.a foo.o
How to use library • Write a header file “foo.h” : • Our program “hello.c” : foo.h : extern void print_str(char []); hello.c: #include <foo.h> main () { print_str(“hello world!”); }
How to link library • Compile with library (assume “foo.h” & “libfoo.a” in “/tmp” directory.) • gcc include “linker” step. • You can also use “ld” to linker objects and libraries. $ gcc -I/tmp -L/tmp hello.c -lfoo $ ./a.out hello world!
Practice #1 • Temperature C = (5/9)(F-32) • Write a subroutine that convert the temperature. • Compile the subroutine into an object file. • Make a temperature library • Write a program that invokes the function to convert a source temperature to a target one
GNU C Library • In Linux, header files is usually in “/usr/include”. • Online manualhttp://www.gnu.org/manual/ • To use GNU C library, you can search the function name to see what header files you need to include, and what library you need to link.
GNU Linker “ld” • ld combines a number of object and archive files, relocates their data and ties up symbol references. • Usually the last step in compiling a program, ex: $ gcc -I/tmp -c hello.c $ ld -o hello -L/tmp /usr/lib/crt1.o /usr/lib/crti.o hello.o -lfoo.a -lc $ ./hello hello world!
Shared Library1 • Use ld to make shared object (shared library). • Compile a shared library • Use “gcc” to make object file with “-fPIC” option (generate position-independent code for use in a shared library). • Use “ld” with “-shared”, “-soname” to make shard object. $ gcc -fPIC -c foo.c $ ld -shared -soname libfoo.so -o libfoo.so.0 -lc foo.o
Shared Library2 • Install shared library • Use “ldconfig” with “-v”, “-n” options. “-v” to show messages. “-n” to process directories specified on the command line. • Not applicable on Cygwin! (why?) $ ldcofnig -v -n . .: libfoo.so -> libfoo.so.0 (changed)
Shared Library3 • Using shared library • Add “/tmp” in LD_LIBRARY_PATH (assume our libfoo.so in “/tmp” directory) • In bash • In tcsh or csh • Compile with library $ export LD_LIBRARY_PATH=/tmp:$LD_LIBRARY_PATH $ setenv LD_LIBRARY_PATH /tmp:$LD_LIBRARY_PATH $ gcc -I/tmp -L/tmp -o hello hello.c –lfoo $ ./hello hello world!
Linker Options1 • -o output : Use output as the name for the program produced by ld. • -larchive : Add archive file archive to the list of files to link. • -Lsearchdir : Add path searchdir to the list of paths that ld will search for archive libraries .
Linker Options2 • -b input-format : to specify the binary format for input object files. ld may be configured to support more than one kind of object file. • -r : generate an output file that can in turn serve as input to ld. This is often called partial linking. • -E : When creating a dynamically linked executable, add all symbols to the dynamic symbol table.
Linker Options3 • When using gcc, you can pass linking options by “-Wl,option”. • Pass one option at once $ gcc –Wl,-L. -Wl,-ohello -I/tmp -L/tmp hello.c -lfoo $ ./hello hello world!