![[←]](../icons/screen-return.gif)
![[]](../icons/screen-cursor.gif)
情報システム概論 I システム情報工学研究科CS専攻、電子・情報工学系 新城 靖 <yas@is.tsukuba.ac.jp>
このページは、次の URL にあります。
http://www.coins.tsukuba.ac.jp/~yas/coins/compsys1-2006/2007-01-29
あるいは、次のページから手繰っていくこともできます。
http://www.coins.tsukuba.ac.jp/~yas/
http://www.cs.tsukuba.ac.jp/~yas/
言語には文法や語彙がある。
# # write-hello.s -- 画面に hello と表示するプログラム # .text .align 2 .globl _main _main: li r3,1 addis r4,0,ha16(hellonl) addi r4,0,lo16(hellonl) li r5,6 li r0,4 sc li r0,1 sc hellonl: .byte 104 .byte 101 .byte 108 .byte 108 .byte 111 .byte 10
% cc write-hello.sアセンブリ言語について詳しくは、2年生の「機械語序論」で学ぶ。 システムコールについて詳しくは、3年生の「システムプログラム」で学ぶ。% ./a.out
hello %
![]()
アセンブリ言語(機械語)は、機械にはわかりやすいが 人間にはわかりにくい。
注意:「ライブラリ」にはいくつかの意味がある。オペレーティング・システ ムの文脈では、システムコールとライブラリを区別する。
図? ソース・プログラムらプロセスまで
% cat -n today.c1 #define TODAY "Monday" 2 main() 3 { 4 printf("Today is %s.\n",TODAY ); 5 } % cc today.c
% ./a.out
Today is Monday. %
![]()
% cc -v today.c% gcc -v today.c
Using built-in specs. Target: powerpc-apple-darwin8 Configured with: /private/var/tmp/gcc/gcc-5247.obj~4/src/configure --d isable-checking -enable-werror --prefix=/usr --mandir=/share/man --ena ble-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]* $/s/$/-4.0/ --with-gxx-include-dir=/include/c++/4.0.0 --build=powerpc- apple-darwin8 --host=powerpc-apple-darwin8 --target=powerpc-apple-darw in8 Thread model: posix gcc version 4.0.1 (Apple Computer, Inc. build 5247) /usr/libexec/gcc/powerpc-apple-darwin8/4.0.1/cc1 -quiet -v -D__DYNAMI C__ today.c -fPIC -quiet -dumpbase today.c -auxbase today -version -o /var/tmp//cc1bbrh4.s ignoring nonexistent directory "/usr/lib/gcc/powerpc-apple-darwin8/4.0 .1/../../../../powerpc-apple-darwin8/include" #include "..." search starts here: #include <...> search starts here: /usr/local/include /usr/lib/gcc/powerpc-apple-darwin8/4.0.1/include /usr/include /System/Library/Frameworks /Library/Frameworks End of search list. GNU C version 4.0.1 (Apple Computer, Inc. build 5247) (powerpc-apple-darwin8) compiled by GNU C version 4.0.1 (Apple Computer, Inc. build 5247). GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: 293c7bb043389dbe08b10e2d17bba0c4 today.c: In function 'main': today.c:4: warning: incompatible implicit declaration of built-in func tion 'printf' as -arch ppc -o /var/tmp//ccAQE1HQ.o /var/tmp//cc1bbrh4.s /usr/libexec/gcc/powerpc-apple-darwin8/4.0.1/collect2 -dynamic -arch ppc -weak_reference_mismatches non-weak -o a.out -lcrt1.o /usr/lib/gcc /powerpc-apple-darwin8/4.0.1/crt2.o -L/usr/lib/gcc/powerpc-apple-darwi n8/4.0.1 -L/usr/lib/gcc/powerpc-apple-darwin8/4.0.1 -L/usr/lib/gcc/pow erpc-apple-darwin8/4.0.1/../../.. /var/tmp//ccAQE1HQ.o -lgcc -lSystemS tubs -lSystem %
![]()
図? ccコマンドの背後で動いているプログラム
% cat today.cC言語のプリプロセッサは、C言語そのものではなく、マクロプロセッサ。#define TODAY "Monday" main() { printf("Today is %s.\n",TODAY ); } % cc -E today.c > today.i
% cat today.i
# 1 "today.c" # 1 "
" # 1 " " # 1 "today.c" main() { printf("Today is %s.\n","Monday" ); } % ![]()
% cpp today.c# 1 "today.c" # 1 "
" # 1 " " # 1 "today.c" main() { printf("Today is %s.\n","Monday" ); } % ![]()
% cppCプリプロセッサの機能#define apple orange
apple computer
^D # 1 "
" # 1 " " # 1 " " # 1 " " orange computer % ![]()
% cat macro.c#include
#define begin { #define end } #define ever ;; #define OR || main() begin int c ; for(ever) begin if( (c=getchar()) == 'X' OR c == EOF ) break; putchar(toupper(c)); end end % cc macro.c -o macro % ./macro
aaa
AAA X
%
![]()
% cat today.c#define TODAY "Monday" main() { printf("Today is %s.\n",TODAY ); } % cc -S today.c
today.c: In function 'main': today.c:4: warning: incompatible implicit declaration of built-in function 'printf' % cat today.s
.section __TEXT,__text,regular,pure_instructions .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32 .machine ppc .cstring .align 2 LC0: .ascii "Today is %s.\12\0" .align 2 LC1: .ascii "Monday\0" .text .align 2 .globl _main _main: mflr r0 stmw r30,-8(r1) stw r0,8(r1) stwu r1,-80(r1) mr r30,r1 bcl 20,31,"L00000000001$pb" "L00000000001$pb": mflr r31 addis r2,r31,ha16(LC0-"L00000000001$pb") la r3,lo16(LC0-"L00000000001$pb")(r2) addis r2,r31,ha16(LC1-"L00000000001$pb") la r4,lo16(LC1-"L00000000001$pb")(r2) bl L_printf$stub lwz r1,0(r1) lwz r0,8(r1) mtlr r0 lmw r30,-8(r1) blr .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32 .align 5 L_printf$stub: .indirect_symbol _printf mflr r0 bcl 20,31,"L00000000001$spb" "L00000000001$spb": mflr r11 addis r11,r11,ha16(L_printf$lazy_ptr-"L00000000001$spb") mtlr r0 lwzu r12,lo16(L_printf$lazy_ptr-"L00000000001$spb")(r11) mtctr r12 bctr .lazy_symbol_pointer L_printf$lazy_ptr: .indirect_symbol _printf .long dyld_stub_binding_helper .subsections_via_symbols %
![]()
% ls today.*today.c % cc -c today.c
% ls today.*
today.c today.o %
![]()
% cc -v today.o -o a.out様々なライブラリら実行時ルーチンが参照されている。Using built-in specs. Target: powerpc-apple-darwin8 Configured with: /private/var/tmp/gcc/gcc-5247.obj~4/src/configure --d isable-checking -enable-werror --prefix=/usr --mandir=/share/man --ena ble-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]* $/s/$/-4.0/ --with-gxx-include-dir=/include/c++/4.0.0 --build=powerpc- apple-darwin8 --host=powerpc-apple-darwin8 --target=powerpc-apple-darw in8 Thread model: posix gcc version 4.0.1 (Apple Computer, Inc. build 5247) /usr/libexec/gcc/powerpc-apple-darwin8/4.0.1/collect2 -dynamic -arch ppc -weak_reference_mismatches non-weak -o a.out -lcrt1.o /usr/lib/gcc /powerpc-apple-darwin8/4.0.1/crt2.o -L/usr/lib/gcc/powerpc-apple-darwi n8/4.0.1 -L/usr/lib/gcc/powerpc-apple-darwin8/4.0.1 -L/usr/lib/gcc/pow erpc-apple-darwin8/4.0.1/../../.. today.o -lgcc -lSystemStubs -lSystem %
![]()
逆アセンブラは、機械語をアセンブリ言語に戻す。 gdb (GNU Debugger) には、逆アセンブルの機能がある。
% cc -c today.ctoday.c: In function 'main': today.c:4: warning: incompatible implicit declaration of built-in function 'printf' % ls -l today.o
-rw-r--r-- 1 yas prof 812 Jan 25 23:32 today.o % gdb today.o
GNU gdb 6.1-20040303 (Apple version gdb-434) (Wed Nov 2 17:28:16 GMT 2005) Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "powerpc-apple-darwin"... warning: Can't find LC_SEGMENT.__DATA.__data section in symbol file (gdb) x/8x main
0x0
: 0x7c0802a6 0xbfc1fff8 0x90010008 0x9421ffb0 0x10 : 0x7c3e0b78 0x429f0005 0x7fe802a6 0x3c5f0000 (gdb) x/10i main 0x0
: mflr r0 0x4 : stmw r30,-8(r1) 0x8 : stw r0,8(r1) 0xc : stwu r1,-80(r1) 0x10 : mr r30,r1 0x14 : bcl- 20,4*cr7+so,0x18 0x18 : mflr r31 0x1c : addis r2,r31,0 0x20 : addi r3,r2,104 0x24 : addis r2,r31,0 (gdb) ![]()
以下は、Lisp の一種、Scheme での実行例。
% open "/Applications/PLT Scheme v301/DrScheme.app"%
![]()
図? DrSchemeの画面(MacOSX)
プログラミング言語の2種類の動作方法の違い
図? javac コマンドと java コマンド
% cat PrintHello.javajavaコマンドは、「Javaバイトコード」を解釈実行する変りに、機械語にコン パイルして実行することもある。このコンパイラは、Just in time (JIT) コ ンパイラと呼ばれる。class PrintHello { public static void main(String args[]) { System.out.println("hello"); } } % javac PrintHello.java
% ls PrintHello.*
PrintHello.class PrintHello.java % java PrintHello
hello %
![]()
2種類の仮想計算機
Java仮想計算機を使うと、さまざまな種類ハードウェア(CPU)やオペレーティ
ング・システムで共通にバイトコードが使えるようになる。
図? 共通のバイトコードが使える
インタプリタ(仮想計算機)の大部分は、C言語で記述されている。 インタプリタ自身は、それぞのハードウェアの機械語にコンパイルされている。
% ls PrintHello.classPrintHello.class % javap -c PrintHello
Compiled from "PrintHello.java" class PrintHello extends java.lang.Object{ PrintHello(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."
":()V 4: return public static void main(java.lang.String[]); Code: 0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3; //String hello 5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: return } % ![]()
% gs1 に 2 を足し、3を掛けたものを表示。AFPL Ghostscript 8.54 (2006-05-17) Copyright (C) 2005 artofcode LLC, Benicia, CA. All rights reserved. This software comes with NO WARRANTY: see the file PUBLIC for details. GS>1 2 add 3 mul =
9 GS>^D
![]()
% cat box.ps%! newpath 20 20 moveto 20 80 lineto 80 80 lineto 80 20 lineto closepath stroke showpage % gs -g200x200 box.ps
AFPL Ghostscript 8.54 (2006-05-17) Copyright (C) 2005 artofcode LLC, Benicia, CA. All rights reserved. This software comes with NO WARRANTY: see the file PUBLIC for details. >>showpage, press
to continue<< GS>^D
%
![]()
図? GhostScript による PostScript プログラムの実行(画面表示)