by Bjorn Chambless
Introduction
The following is designed familiarize the reader with programming in x86 (AT&T
style, that produced by gcc) assembly under Linux and how to interface assembly
and higher-level language code (i.e. C). The tutorial will also briefly cover
debugging your assembly using GDB.
This tutorial requires the following:
an i386 family PC running Linux
GCC, the GNU C-compiler
GDB, the GNU debugger command line debugger
The tutorial was developed on and tested with GCC version 2.95.4 and GDB 19990928 under Linux kernel 2.4.9
I highly recommend working through this tutorial with
"as"
and "gdb"
documentation close at hand.
Source Code
The process begins with a source code program. For example, hello.c
/* hello.c */
#include
main()
{
printf("hello\n").
}
which, when compiled and executed, prints a message
linuxbox> gcc -o hello hello.c
linuxbox> ./hello
hello
The actual compilation proce ......
²Ù×÷ϵͳµÄÒ»¸ö¾µäÎÊÌâÊÇ"Éú²úÕß-Ïû·ÑÕß"ÎÊÌâ, ÕâÉæ¼°Í¬²½ÐźÅÁ¿ºÍ»¥³âÐźÅÁ¿µÄÓ¦ÓÃ, ÔÚÕâÀï,ÎÒÓÃÏ̵߳Äͬ²½ºÍ»¥³âÀ´ÊµÏÖ.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#define N 2 // Ïû·ÑÕß»òÕßÉú²úÕßµÄÊýÄ¿
#define M 10 // »º³åÊýÄ¿
int in = 0; // Éú²úÕß·ÅÖòúÆ·µÄλÖÃ
int out = 0; // Ïû·ÑÕßÈ¡²úÆ·µÄλÖÃ
int buff[M] = {0}; // »º³å³õʼ»¯Îª0£¬ ¿ªÊ¼Ê±Ã»ÓвúÆ·
sem_t empty_sem; // ͬ²½ÐźÅÁ¿£¬ µ±ÂúÁËʱ×èÖ¹Éú²úÕ߷ŲúÆ·
sem_t full_sem; // ͬ²½ÐźÅÁ¿£¬ µ±Ã»²úƷʱ×èÖ¹Ïû·ÑÕßÏû·Ñ
pthread_mutex_t mutex; // »¥³âÐźÅÁ¿£¬ Ò»´ÎÖ»ÓÐÒ»¸öÏ̷߳ÃÎÊ»º³å
int product_id = 0; //Éú²úÕßid
int prochase_id = 0; //Ïû·ÑÕßid
/* ´òÓ¡»º³åÇé¿ö */
void print()
{
int i;
for(i = 0; i < M; i++)
printf("%d ", buff[i]);
printf("\n");
}
/* Éú²úÕß·½·¨ */
void *product()
{
int id = ++product_id;
while(1)
{
// Ó ......
²Ù×÷ϵͳµÄÒ»¸ö¾µäÎÊÌâÊÇ"Éú²úÕß-Ïû·ÑÕß"ÎÊÌâ, ÕâÉæ¼°Í¬²½ÐźÅÁ¿ºÍ»¥³âÐźÅÁ¿µÄÓ¦ÓÃ, ÔÚÕâÀï,ÎÒÓÃÏ̵߳Äͬ²½ºÍ»¥³âÀ´ÊµÏÖ.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#define N 2 // Ïû·ÑÕß»òÕßÉú²úÕßµÄÊýÄ¿
#define M 10 // »º³åÊýÄ¿
int in = 0; // Éú²úÕß·ÅÖòúÆ·µÄλÖÃ
int out = 0; // Ïû·ÑÕßÈ¡²úÆ·µÄλÖÃ
int buff[M] = {0}; // »º³å³õʼ»¯Îª0£¬ ¿ªÊ¼Ê±Ã»ÓвúÆ·
sem_t empty_sem; // ͬ²½ÐźÅÁ¿£¬ µ±ÂúÁËʱ×èÖ¹Éú²úÕ߷ŲúÆ·
sem_t full_sem; // ͬ²½ÐźÅÁ¿£¬ µ±Ã»²úƷʱ×èÖ¹Ïû·ÑÕßÏû·Ñ
pthread_mutex_t mutex; // »¥³âÐźÅÁ¿£¬ Ò»´ÎÖ»ÓÐÒ»¸öÏ̷߳ÃÎÊ»º³å
int product_id = 0; //Éú²úÕßid
int prochase_id = 0; //Ïû·ÑÕßid
/* ´òÓ¡»º³åÇé¿ö */
void print()
{
int i;
for(i = 0; i < M; i++)
printf("%d ", buff[i]);
printf("\n");
}
/* Éú²úÕß·½·¨ */
void *product()
{
int id = ++product_id;
while(1)
{
// Ó ......
ronÊÇÒ»¸ölinuxÏµĶ¨Ê±Ö´Ðй¤¾ß£¬¿ÉÒÔÔÚÎÞÐèÈ˹¤¸ÉÔ¤µÄÇé¿öÏÂÔËÐÐ×÷Òµ¡£ÓÉÓÚCron ÊÇLinuxµÄÄÚÖ÷þÎñ£¬µ«Ëü²»×Ô¶¯ÆðÀ´£¬¿ÉÒÔÓÃÒÔÏµķ½·¨Æô¶¯¡¢¹Ø±ÕÕâ¸ö·þÎñ:
¡¡¡¡/sbin/service crond start //Æô¶¯·þÎñ
¡¡¡¡/sbin/service crond stop //¹Ø±Õ·þÎñ
¡¡¡¡/sbin/service crond restart //ÖØÆô·þÎñ
¡¡¡¡/sbin/service crond reload //ÖØÐÂÔØÈëÅäÖÃ
¡¡¡¡ÄãÒ²¿ÉÒÔ½«Õâ¸ö·þÎñÔÚϵͳÆô¶¯µÄʱºò×Ô¶¯Æô¶¯:
¡¡¡¡ÔÚ/etc/rc.d/rc.localÕâ¸ö½Å±¾µÄĩβ¼ÓÉÏ:
¡¡¡¡/sbin/service crond start
¡¡¡¡ÏÖÔÚCronÕâ¸ö·þÎñÒѾÔÚ½ø³ÌÀïÃæÁË£¬ÎÒÃǾͿÉÒÔÓÃÕâ¸ö·þÎñÁË£¬Cron·þÎñÌṩÒÔϼ¸ÖÖ½Ó¿Ú¹©´ó¼ÒʹÓÃ:
¡¡¡¡1.Ö±½ÓÓÃcrontabÃüÁî±à¼
¡¡¡¡cron·þÎñÌṩcrontabÃüÁîÀ´É趨cron·þÎñµÄ£¬ÒÔÏÂÊÇÕâ¸öÃüÁîµÄһЩ²ÎÊýÓë˵Ã÷:
¡¡¡¡crontab -u //É趨ij¸öÓû§µÄcron·þÎñ£¬Ò»°ãrootÓû§ÔÚÖ´ÐÐÕâ¸öÃüÁîµÄʱºòÐèÒª´Ë²ÎÊý
¡¡¡¡crontab -l //Áгöij¸öÓû§cron·þÎñµÄÏêϸÄÚÈÝ
¡¡¡¡crontab -r //ɾ³ýû¸öÓû§µÄcron·þÎñ
¡¡¡¡crontab -e //±à¼Ä³¸öÓû§µÄcron·þÎñ
¡¡¡¡±ÈÈç˵root²é¿´×Ô¼ºµÄcronÉèÖÃ:crontab -u root -l
¡¡¡¡ÔÙÀýÈ磬rootÏëɾ³ýfredµÄcronÉèÖÃ:crontab -u fred -r
¡¡¡¡ÔÚ±à¼cron·þÎñʱ ......
Ãû³Æ ĬÈ϶¯×÷ ˵Ã÷
SIGHUP ÖÕÖ¹½ø³Ì ÖÕ¶ËÏß·¹Ò¶Ï
SIGINT ÖÕÖ¹½ø³Ì ÖжϽø³Ì
SIGQUIT ½¨Á¢COREÎļþ ÖÕÖ¹½ø³Ì£¬²¢ÇÒÉú³ÉcoreÎļþ
SIGILL ½¨Á¢COREÎļþ ·Ç·¨Ö¸Áî
SIGTRAP ½¨Á¢COREÎļþ ¸ú×Ù×ÔÏÝ
SIGBUS ½¨Á¢COREÎļþ ×ÜÏß´íÎó
SIGSEGV ½¨Á¢COREÎļþ ¶Î·Ç·¨´íÎó
SIGFPE ½¨Á¢COREÎļþ ¸¡µãÒì³£
SIGIOT ½¨Á¢COREÎļþ Ö´ÐÐI/O×ÔÏÝ
SIGKILL ÖÕÖ¹½ø³Ì ɱËÀ½ø³Ì
SIGPIPE ÖÕÖ¹½ø³Ì ÏòÒ»¸öûÓжÁ½ø³ÌµÄ¹ÜµÀдÊý¾Ý
SIGALARM ÖÕÖ¹½ø³Ì ¼ÆÊ±Æ÷µ½Ê±
SIGTERM ÖÕÖ¹½ø³Ì Èí¼þÖÕÖ¹ÐźÅ
SIGSTOP Í£Ö¹½ø³Ì ·ÇÖÕ¶ËÀ´µÄÍ£Ö¹ÐźÅ
SIGTSTP Í£Ö¹½ø³Ì ÖÕ¶ËÀ´µÄÍ£Ö¹ÐźÅ
SIGCONT ºöÂÔÐźŠ& ......
ËùÓÐÏ̶߳¼ÓÐÒ»¸öÏ̺߳ţ¬Ò²¾ÍÊÇ
Thread ID
¡£ÆäÀàÐÍΪ
pthread_t
¡£Í¨¹ýµ÷ÓÃ
pthread_self()
º¯Êý¿ÉÒÔ»ñµÃ×ÔÉíµÄÏ̺߳š£
ÏÂÃæËµÒ»ÏÂÈçºÎ´´½¨Ò»¸öÏ̡߳£
ͨ¹ý´´½¨Ị̈߳¬Ï߳̽«»áÖ´ÐÐÒ»¸öÏ̺߳¯Êý£¬¸ÃÏ̸߳ñʽ±ØÐë°´ÕÕÏÂÃæÀ´ÉùÃ÷£º
void * Thread_Function(void *)
´´½¨Ï̵߳ĺ¯ÊýÈçÏ£º
int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);
ÏÂÃæËµÃ÷һϸ÷¸ö²ÎÊýµÄº¬Ò壺
thread
£ºËù´´½¨µÄÏ̺߳š£
attr
£ºËù´´½¨µÄÏß³ÌÊôÐÔ£¬Õâ¸ö½«ÔÚºóÃæÏêϸ˵Ã÷¡£
start_routine
£º¼´½«ÔËÐеÄÏ̺߳¯Êý¡£
art
£º´«µÝ¸øÏ̺߳¯ÊýµÄ²ÎÊý¡£
ÏÂÃæÊÇÒ»¸ö¼òµ¥µÄ´´½¨Ïß³ÌÀý×Ó£º
#include <pthread.h>
#include <stdio.h>
/* Prints x’s to stderr. The parameter is unused. Does not return. */
vo ......
Load Bios
Read MBR's config to find out the os
Load the kernel of the os
init Process starts
execute /etc/rc.d /sysinit
start other modules (etc/modules.conf)
execute the run level scripts
execute /etc/rc.d/rc.local
execute /bin/login
shell started
......