川山甲

追求内心的非常平静!瞬间清空所有的杂念,达到物我两忘!

  博客园  :: 首页  ::  :: 联系 :: 订阅 订阅  :: 管理
 
介绍
 
 
  现在C语言跨的领域非常之多,如游戏、嵌入式、智能电器等。为什么不直接用汇编或机器语言直接写呢?原因是汇编和机器语言受到计算机体系结构的影响。直接用某种体系结构的汇编或机器指令写出来的程序只能在这种体系结构的计算机上运行
 
  C语言的好处是各种体系结构的计算机都有各自的C编译器,可以把C程序编译成各种不同体系结构的机器指令,这意味着用C语言写的程序只需稍加修改甚至不用修改就可以在各种不同的计算机上编译运行
 

  
hello,world
 
  我们从简单hello,world开始——有人说了,又来了,这个“hello,world”,好多博友都写这种东西烦不烦?呵呵,那我写的也许跟他们说的不一样呢?或者理解的跟别人有不一样的地方。
 
  先写个程序我们看看。
  
源程序(或者源文件):是程序员通过文本编辑器创建并保存的文本文件。
源程序实际就是由0和1组成的位序列,这些位8个一组,成为字节,每个字节通过ASCII字符集(大部分是)对应一个文本字符。
                                 下面是上面程序对应的16进制数字表示

                                  在终端输入man ascii看看ascii表

操作步骤:

             

             查看16进制格式

             回复到文本格式

 

                                 我们看看对应的二进制是什么样的?——这个也是在磁盘中的存储状态。

 

 
总结:
  系统中所有的信息——包括磁盘文件、存储器中的程序,存储器中存放的用户数据以及网络上传送的数据,都是由一串比特表示的。
 
思考:
  同样的一套字节序列可能在不同的上下文中表示一个整数、浮点数、字符串或者机器指令。——这取决于数据对象的上下文。
 
我们该做什么?
  做为程序员,我们需要了解数字的机器表示方式,因为它们与常见的整数和实数是不同的。
 
使用参考:
 

 
翻译
 
  
 
  源程序是能够被人读懂的,为了能在系统上运行,我们需要把C语句通过其他程序转化为一系列的低级机器语言指令。然后这些指令按照一种称为“可执行目标程序“的格式打包好,并以二进制磁盘文件的形式存放起来。
 
  在Linux系统上,从源文件到目标文件(可执行目标程序文件)的转化是由编译器驱动程序完成的。如下图:

编译系统

  我们用GCC工具观察每个阶段。(GCCGNU Compiler CollectionGNU编译器套装
 
先赠送一张gcc命令选项图
 

 
预处理阶段

                                 查看main.i的部分源码

总结:预处理器(cpp)根据以字符#开头的命令,修改原始C程序,结果得到了另一个C程序(还是C程序),通常以i做为文件扩展名。

 编译阶段

 

                                 查看main.s的源码

 

总结:将C语言文本文件翻译成汇编语言文本文件。——汇编语言是非常有用的,因为它为不同高级语言的不同编译器提供了通用的输出语言。

 

 汇编阶段
 

                                 打开源代码,注意此时为二进制磁盘文件,还有注意操作步骤

                                 此时为乱码,需要进行:%!xxd操作

                                 查看指令

 

总结:汇编器(as)将main.s翻译成机器语言指令,把这些指令打包成为一种“可重定位目标程序“的格式,并将结果保存到main.o文件中,main.o是二进制文件,它的字节编码是机器语言指令而不是字符(所以用文本编译器看会出现乱码)。

 

 链接阶段
 

                                 按照汇编阶段操作,查看部分指令码

 

总结:程序中调用了printf函数,它是C标准库的一个函数,printf函数存在于一个名为printf.o的单独的预编译目标文件中。连接器(ld)负责把printf.o的预编译目标文件进行并入,结果就得到a.out文件。

NOTE:a.out是可执行的目标文件,而main.o是可重定位目标程序文件。——可执行目标文件加载到系统存储器后,由系统负责执行。而可重定位目标程序文件是提供给链接器使用,执行并入操作。

 
总结
 
  这一篇主要是学习gcc编译器的编译过程,对整个过程有所熟悉。如果有好的知识点,望大家赐教。
 
  要知后事如何,且听下回分解。
 
推荐
 
 
vim配置分享
" An example for a vimrc file.
"
" Maintainer:   Bram Moolenaar <Bram@vim.org>
" Last change:  2006 Nov 16
"
" To use it, copy it to
"     for Unix and OS/2:  ~/.vimrc
"         for Amiga:  s:.vimrc
"  for MS-DOS and Win32:  $VIM\_vimrc
"       for OpenVMS:  sys$login:.vimrc

" When started as "evim", evim.vim will already have done these settings.
if v:progname =~? "evim"
  finish
endif

" Use Vim settings, rather then Vi settings (much better!).
" This must be first, because it changes other options as a side effect.
set nocompatible

" allow backspacing over everything in insert mode
set backspace=indent,eol,start

if has("vms")
  set nobackup      " do not keep a backup file, use versions instead
else
  set nobackup      " keep [NO] backup file
endif
set history=250     " keep 250 lines of command line history
set ruler       " show the cursor position all the time
set showcmd     " display incomplete commands
set incsearch      " do incremental searching
set nu

" For Win32 GUI: remove 't' flag from 'guioptions': no tearoff menu entries
" let &guioptions = substitute(&guioptions, "t", "", "g")

" Don't use Ex mode, use Q for formatting
map Q gq

" In many terminal emulators the mouse works just fine, thus enable it.
set mouse=a

" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
  syntax on
  set hlsearch
endif

" Only do this part when compiled with support for autocommands.
if has("autocmd")

  " Enable file type detection.
  " Use the default filetype settings, so that mail gets 'tw' set to 72,
  " 'cindent' is on in C files, etc.
  " Also load indent files, to automatically do language-dependent indenting.
  filetype plugin indent on

  " Put these in an autocmd group, so that we can delete them easily.
  augroup vimrcEx
  au!

  " For all text files set 'textwidth' to 78 characters.
  autocmd FileType text setlocal textwidth=78

  " When editing a file, always jump to the last known cursor position.
  " Don't do it when the position is invalid or when inside an event handler
  " (happens when dropping a file on gvim).
  autocmd BufReadPost *
    \ if line("'\"") > 0 && line("'\"") <= line("$") |
    \   exe "normal! g`\"" |
    \ endif

  autocmd VimLeave *
    \ if has("mksession") && exists("v:this_session") && v:this_session != "" |
    \   exec "mksession!" v:this_session |
    \ endif

  augroup END

else

  set autoindent        " always set autoindenting on

endif " has("autocmd")

" Convenient command to see the difference between the current buffer and the
" file it was loaded from, thus the changes you made.
command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
        \ | wincmd p | diffthis

" Add by Journeylee to make things more easily
set softtabstop=4
set shiftwidth=4
set tabstop=4
set noexpandtab
set smarttab
set showmatch
set foldenable
set autoindent
set smartindent
set cindent
set foldmethod=marker
set background=dark
set nobackup

set encoding=utf-8
language en_US.utf8
set fileencodings=ucs-bom,utf-8,gbk,latin1
set fileformats=unix,dos,mac
set fileencoding=utf-8
set fileformat=unix

set fileformat=unix
set matchtime=15

" Make shift-insert work like in Xterm
map <S-Insert> <MiddleMouse>
map! <S-Insert> <MiddleMouse>

" let xterm16_brightness = 'default'     " Change if needed
" let xterm16_colormap = 'allblue'       " Change if needed
" colo xterm16 


"不使用swap文件

map <F5> :!php -l %<CR>

"set runtimepath+=/home/zhoubc/opt/vim/doc
"autocmd BufNewFile,Bufread *.ros,*.inc,*.php set keywordprg="help"

let g:winManagerWindowLayout='TagList|FileExplorer'
let g:winManagerWidth=30
nmap  wm :WMToggle

 

posted on 2012-06-23 14:21  川山甲  阅读(3116)  评论(18编辑  收藏  举报