Shih-Shen Lu

統計

學習紀錄

*本站文章歡迎轉載,但請註明出處。謝謝。

關鍵字(Keyword): embedded system, cognitive radio, partial reconfigurable computing, RFID, Gnu radio, python

筆者按:資訊無限,人腦有限,還是作一下紀錄,幫助自己也可以幫助別人。

[C language] Manhattan Distance Search extension

posted ‎‎23 Nov 2009 06:10‎‎ by Shih-Shen Lu

Extension with the fixed direction to search

Step:
  1. from top to left
  2. from left to bottom
  3. from bottom to right
  4. from right to top
main.c
  1 #include <stdio.h>
  2
  3 #define NOC_X 10
  4 #define NOC_Y 10
  5
  6 struct noc_node
  7 {
  8     int x;
  9     int y;
 10 };
 11
 12 int main()
 13 {
 14     int d = 2;//distance
 15     int p_x = 1;
 16     int p_y = 1;
 17     int i = 0;
 18
 19     int count=0;
 20     int x_tmp, y_tmp;
 21
 22     struct noc_node node_arr[100];
 23     int pf = 0;
 24
 25     x_tmp=p_x+0;
 26     y_tmp=p_y+d;
 27
 28     // initial state
 29     node_arr[pf].x = x_tmp;
 30     node_arr[pf].y = y_tmp;
 31     pf++;
 32
 33     //Second quadrant(-,-)
 34     count = 0;
 35     while(count<d)
 36     {
 37         count++;
 38         node_arr[pf].x = x_tmp-count;
 39         node_arr[pf].y = y_tmp-count;
 40         pf++;
 41     }
 42     x_tmp = x_tmp-count;
 43     y_tmp = y_tmp-count;
 44     //Third quadrant(+,-)
 45     count = 0;
 46     while(count<d)
 47     {
 48         count++;
 49         node_arr[pf].x = x_tmp+count;
 50         node_arr[pf].y = y_tmp-count;
 51         pf++;
 52     }
 53     x_tmp = x_tmp+count;
 54     y_tmp = y_tmp-count;
 55     //Fourth quadrant(+,+)
 56     count = 0;
 57     while(count<d)
 58     {
 59         count++;
 60         node_arr[pf].x = x_tmp+count;
 61         node_arr[pf].y = y_tmp+count;
 62         pf++;
 63     }
 64     x_tmp = x_tmp+count;
 65     y_tmp = y_tmp+count;
 66     //First quadrant(-,+)
 67     count = 0;
 68     while(count<d)
 69     {
 70         count++;
 71         node_arr[pf].x = x_tmp-count;
 72         node_arr[pf].y = y_tmp+count;
 73         pf++;
 74     }
 75     x_tmp = x_tmp-count;
 76     y_tmp = y_tmp+count;
 77     count = 0;
 78     pf--;//because the last node are duplicate.
 79
 80     for(i=0; i<pf;i++)
 81         printf("(x,y)=(%d, %d)\n", node_arr[i].x, node_arr[i].y);
 82     return 0;
 83 }


[C language] Manhattan Distance Search

posted ‎‎21 Nov 2009 22:30‎‎ by Shih-Shen Lu   [ updated ‎‎22 Nov 2009 21:55‎‎ ]

What is Manhattan Distance?
It means |x2-x1|+|y2-y1|.

Now I want to design a program that can return the related coordinates as you input the Manhattan distance.

Example:
input: Manhattan distance=1, coordinates=(0, 0)
output:(1, 0), (0, 1), (-1, 0), (0, -1)

input: Manhattan distance=2, coordinates=(0, 0)
output:(2, 0), (1, 1), (0, 2), (-1, 1), (-2, 0), (-1, -1), (0, -2), (1, -1)

Analysis:
Giving the Manhattan distance is used to calculate the distance from the given coordinates.

main.c
  1 #include <stdio.h>
  2
  3 #define NOC_X 10
  4 #define NOC_Y 10
  5
  6 struct noc_node
  7 {
  8     int x;
  9     int y;
 10 };
 11
 12 int main()
 13 {
 14     int distance = 5;
 15     int p_x = 1;
 16     int p_y = 1;
 17     int i = 0, j = distance, k=0;
 18     int first_arr=0;
 19
 20     struct noc_node node_arr[100];
 21     int pf = 0;
 22
 23     int NOC_arr[NOC_X][NOC_Y];
 24
 25     for(i=1, j=distance, k=0;j>=0;j=j-i, k=k+i)
 26     {
 27         node_arr[pf].x = j;
 28         node_arr[pf].y = k;
 29         pf++;
 30     }
 31     first_arr = pf-1;
 32
 33     for(i=0;i<first_arr;i++)
 34     {
 35         node_arr[pf].x = -node_arr[i].x;
 36         node_arr[pf].y = node_arr[i].y;
 37         pf++;
 38     }
 39     for(i=0;i<first_arr;i++)
 40     {
 41         node_arr[pf].x = -node_arr[i].x;
 42         node_arr[pf].y = -node_arr[i].y;
 43         pf++;
 44     }
 45     for(i=0;i<first_arr;i++)
 46     {
 47         node_arr[pf].x = node_arr[i].x;
 48         node_arr[pf].y = -node_arr[i].y;
 49         pf++;
 50     }
 51
 52     for(i=0; i<pf;i++)
 53         printf("(x,y)=(%d, %d)\n", node_arr[i].x+p_x, node_arr[i].y+p_y);
 54     return 0;
 55 }


makefile
  1 all:main

execution:
As distance is 5 and coordinates is (1, 1)
[leeraphael@embedded 15:31 manhattan_distance]$./main
(x,y)=(6, 1)
(x,y)=(5, 2)
(x,y)=(4, 3)
(x,y)=(3, 4)
(x,y)=(2, 5)
(x,y)=(1, 6)
(x,y)=(-4, 1)
(x,y)=(-3, 2)
(x,y)=(-2, 3)
(x,y)=(-1, 4)
(x,y)=(0, 5)
(x,y)=(-4, 1)
(x,y)=(-3, 0)
(x,y)=(-2, -1)
(x,y)=(-1, -2)
(x,y)=(0, -3)
(x,y)=(6, 1)
(x,y)=(5, 0)
(x,y)=(4, -1)
(x,y)=(3, -2)
(x,y)=(2, -3)



[C language]Queue 與 Stack

posted ‎‎17 Nov 2009 18:21‎‎ by Shih-Shen Lu   [ updated ‎‎23 Nov 2009 06:18‎‎ ]

前言:讓使用者在一開始輸入要模擬Queue或是Stack,預設QueueStack大小不超過100個元素,當使用者選擇Queue則有EnqueueDequeue選擇Stack則有PushPop,之後使用者可以一直使用這四項功能,一直到使用者輸入字元’q’為止。同時每次使用任四項之一功能時都必須印出整個陣列的狀態。

限制:

1.  陣列大小100

2.  元素範圍int(包含正負)

3.  當選擇功能時,輸入非1,2q,要求重新輸入

4.  當輸入的元素內容超過範圍,要求重新輸入

5.  當沒有內容可以DequeuePop時,讓使用者重新輸入

範例1.

Please select (1)Queue (2)Stack:1

(1)Enquque(2)Dequeue:1

Enqueue element:5

Array:[5]

 

(1)Enquque(2)Dequeue:1

Enqueue element:-188

Array:[5][-188]

(1)Enquque(2)Dequeue:2

Array:[-188]

Dequeue:[5]

(1)Enquque(2)Dequeue:2

Array:

Dequeue:[-188]

(1)Enquque(2)Dequeue:2

Nothing can be dequeue

(1)Enquque(2)Dequeue:q

Thanks for use!

範例2.

Please select (1)Queue (2)Stack:2

(1)Push(2)Pop:1

Push element:20

Array:[20]

(1)Push(2)Pop:1

Push element:-199

Array:[20][-199]

(1)Push(2)Pop:2

Array:[-199]

Pop:[20]

(1)Push(2)Pop:A

No such command!

(1)Push(2)Pop:2

Array:

Pop: [-199]

(1)Push(2)Pop:2

Nothing can be pop!

(1)Push(2)Pop:q

Thanks for use!

加分題:

前言:

加分題附加於前面的基本題,必須讀入一字串,並且能夠依照輸入的內容直接解讀為Queue或是Stack並直接將元素加入該陣列中。將使用者最後的結果算出並直接輸出。

 

限制:

1.  當開頭非SQ(都大寫)時,直接輸出錯誤訊息即可

2.  過程中元素為非數字時,直接輸出錯誤訊息即可

3.  當中間出先非PD時,直接輸出錯誤訊息即可

4.  出現錯誤訊息後,直接回到上一個問題問題即可

 

範例3.

Please select (1)Queue (2)Stack (3)Command: %

Error!

Please select (1)Queue (2)Stack (3)Command: 3

Input string:S 188 -19 37 CCC 39

Error!

Input string:S 188 -19 P 37 46

Result Array:[188][37][46]

 

範例4.

Please select (1)Queue (2)Stack (3)Command: 3

Input string:Q 56 78 -10 34 D D D

Result Array :[34]

code
#include <stdio.h>

#define ARRAY_SIZE 100
#define FULL 1
#define EMPTY 2


int var_stack[ARRAY_SIZE];
int var_queue[ARRAY_SIZE];
int var_top_stack = -1;
int var_head_queue = -1;
int var_tail_queue = -1;

void Show_stack_array()
{
    int i = 0;
    // The following lines show the array
    printf("Array:");
    for(i = 0; i<=var_top_stack; i++)
        printf("[%d]", var_stack[i]);
    printf("\n");
    
}
void error_report(int type)
{
    if(type==FULL)
        printf("FULL\n");
    else if(type ==EMPTY)
        printf("EMPTY\n");
}
int Stack()
{
    char c;
    int input_val = 0;

start_stack:
    printf("(1)Push(2)Pop:");
    scanf("%s", &c);
    
    if(c=='1')
    {
        if(var_top_stack == ARRAY_SIZE-1)
        {
            error_report(FULL);
            goto start_stack;
        }

        printf("Push element:");
        scanf("%d", &input_val);
        //The following lines are the push action
        ++var_top_stack;
        var_stack[var_top_stack] = input_val;
        Show_stack_array();        
    }
    else if(c=='2')
    {
        if(var_top_stack == -1)
        {
            error_report(EMPTY);
            goto start_stack;
        }    
    
        //The following lines are the pop action
        printf("Pop:[%d]\n", var_stack[var_top_stack]);
        --var_top_stack;
        Show_stack_array();
    }
    else if(c=='q')
    {
        return 0;
    }
    goto start_stack;

}
void Show_queue_array()
{
    int i = 0;
    // The following lines show the array
    printf("Array:");
    for(i = var_tail_queue+1; i<=var_head_queue; i++)
        printf("[%d]", var_queue[i]);
    printf("\n");
    
}
int Queue()
{
    char c;
    int input_val = 0;

start_queue:
    printf("(1)Enquque(2)Dequeue:");
    scanf("%s", &c);
    if(c=='1')
    {
        if((var_head_queue+1)%ARRAY_SIZE == var_tail_queue ||(var_head_queue==ARRAY_SIZE-1&&    var_tail_queue==-1))
        {
            error_report(FULL);
            goto start_queue;
        }

        printf("Enqueue element:");
        scanf("%d", &input_val);
        
        //The following lines are the Enquque action
        var_head_queue = (++var_head_queue)%ARRAY_SIZE;
        var_queue[var_head_queue] = input_val;
        Show_queue_array();        
    }
    else if(c=='2')
    {
        if(var_head_queue == var_tail_queue)
        {
            error_report(EMPTY);
            goto start_queue;
        }    
        var_tail_queue = (++var_tail_queue)%ARRAY_SIZE;
        printf("Dequeue:[%d]\n", var_queue[var_tail_queue]);
        Show_queue_array();        
        
    }
    else if(c=='q')
    {
        return 0;
    }
    goto start_queue;
}

int main()
{
    char c;

start:
    printf("Please select (1)Queue (2)Stack:");
    scanf("%s", &c);
    
    if(c=='1')
        Queue();
    else if(c=='2')
        Stack();
    else if(c=='q')
        return 0;
    else
        goto start;
}

筆者按:很嗐,要嘛!作業自己寫,不然就不要交!!人要有志氣阿!另外那啥鬼助教跟學生說10分鐘就寫完!幹 打code不用時間喔!

[C language] Hacking application by LD_PRELOAD

posted ‎‎13 Nov 2009 18:16‎‎ by Shih-Shen Lu

Objective

Change the function call before load the original function.

Method

main.c
   1 #include <stdio.h>
  2
  3 int main()
  4 {
  5     printf("original printf %d\n", 1);
  6     return 0;
  7 }


newlb.c
   1 //void printf(char * str)
  2 void printf(char * str)
  3 {
  4     write(1, str, strlen(str));
  5 }
  6


We want to replace the printf function by preloaded the new library.
In the main.c we use the original printf function.
In the newlb.c we use to replace the original printf function.
The following steps are the hacking steps.

# 1. compile our new library, newlb
[leeraphael@embedded 09:40 library_reload]$gcc -shared -Wl,-soname,libnewlb.so.1 -o libnewlb.so.1.0.0 newlb.c
newlb.c:2: warning: conflicting types for built-in function 'printf'
newlb.c: In function 'printf':
newlb.c:4: warning: incompatible implicit declaration of built-in function 'strlen'

# 2. compile the main program
[leeraphael@embedded 09:40 library_reload]$gcc -o main ./main.c

# 3. Execute the original main program without preloading another library.
[leeraphael@embedded 09:40 library_reload]$./main
original printf 1

# 4. Add the LD_PRELOAD environment variable before executing the main program. The LD_PRELOAD environment variable is used to preload library before it use the original library.
[leeraphael@embedded 09:42 library_reload]$LD_PRELOAD=/home/leeraphael/C/library_reload/libnewlb.so.1.0.0 ./main
original printf %d

# 5. Analysis
[leeraphael@embedded 09:42 library_reload]$ldd ./main
        linux-gate.so.1 =>  (0x00b90000)
        libc.so.6 => /lib/libc.so.6 (0x00110000)
        /lib/ld-linux.so.2 (0x00b3e000)
[leeraphael@embedded 09:52 library_reload]$LD_PRELOAD=/home/leeraphael/C/library_reload/libnewlb.so.1.0.0 ldd ./main
        linux-gate.so.1 =>  (0x003ba000)
        /home/leeraphael/C/library_reload/libnewlb.so.1.0.0 (0x00720000)
        libc.so.6 => /lib/libc.so.6 (0x00b63000)
        /lib/ld-linux.so.2 (0x00b3e000)

# Do you discover the diffidence between step 3 and step 4? While we preload the new library with new printf function, it show the  "original printf %d". However, the original printf function show the string, "original printf 1".
Because our new printf function only has one parameter, it will not replace the %d with 1.

Reference

[1] Modifying a Dynamic Library Without Changing the Source Code / 在不更動原始程式碼的前提下,修改動態程式庫, http://jserv.sayya.org/kernel/ld_preload-usage.html

[2]LD_PRELOAD 的應用, http://blog.linux.org.tw/~jserv/archives/000782.html

[Embedded System]Create ARM developped environment by QEMU

posted ‎‎11 Nov 2009 05:00‎‎ by Shih-Shen Lu

Official website: QEMU, http://www.nongnu.org/qemu/

QEMU is Installed by yum.
[leeraphael@Shih-Shen ~]# yum install qemu

Or you can install by source code from http://www.nongnu.org/qemu/download.html

Download the test image file from QEMU website
arm-test-0.2.tar.gz

[leeraphael@Shih-Shen ~]$ ls -l
總計 11428
drwxr-xr-x  2 leeraphael leeraphael    4096 2006-05-05 01:38 arm-test
-rw-rw-r--  1 leeraphael leeraphael 3310162 2009-11-11 20:55 arm-test-0.2.tar.gz
drwxrwxr-x 13 leeraphael leeraphael    4096 2009-11-11 20:22 buildroot
-rw-rw-r--  1 leeraphael leeraphael   22717 2009-11-11 20:00 curt-src-v1.tar.bz2
drwxr-xr-x  8 leeraphael leeraphael    4096 2009-11-11 20:11 CuRT_v1
-rw-rw-r--  1 leeraphael leeraphael 8334927 2009-11-11 20:56 linux-0.2.img.bz2
[leeraphael@Shih-Shen ~]$ cd arm-test
[leeraphael@Shih-Shen arm-test]$ ls -l
總計 3264
-rw-r--r-- 1 leeraphael leeraphael 2180206 2006-05-05 01:38 arm_root.img
-rw-r--r-- 1 leeraphael leeraphael     475 2006-05-05 01:38 README
-rwxr-xr-x 1 leeraphael leeraphael 1146376 2006-05-05 01:38 zImage.integrator
[leeraphael@Shih-Shen arm-test]$ qemu-system-arm -kernel ./zImage.integrator -initrd ./arm_root.img -nographic -append "console=ttyAMA0"
Uncompressing Linux.......................................................................... done, booting the kernel.
Linux version 2.6.17-rc3 (paul@wren) (gcc version 4.1.0 (CodeSourcery ARM)) #53 Thu May 4 15:05:18 BST 2006
CPU: ARM926EJ-Sid(wb) [41069265] revision 5 (ARMv5TEJ)
Machine: ARM-IntegratorCP
Memory policy: ECC disabled, Data cache writeback
CPU0: D VIVT write-through cache
CPU0: I cache: 4096 bytes, associativity 4, 32 byte lines, 32 sets
CPU0: D cache: 65536 bytes, associativity 4, 32 byte lines, 512 sets
Built 1 zonelists
Kernel command line: console=ttyAMA0
PID hash table entries: 1024 (order: 10, 4096 bytes)
Console: colour dummy device 80x30
Dentry cache hash table entries: 16384 (order: 4, 65536 bytes)
Inode-cache hash table entries: 8192 (order: 3, 32768 bytes)
Memory: 128MB = 128MB total
Memory: 125184KB available (1928K code, 388K data, 104K init)
Mount-cache hash table entries: 512
CPU: Testing write buffer coherency: ok
checking if image is initramfs... it is
Freeing initrd memory: 2129K
NET: Registered protocol family 16
NetWinder Floating Point Emulator V0.97 (double precision)
Installing knfsd (copyright (C) 1996 okir@monad.swb.de).
Initializing Cryptographic API
io scheduler noop registered
io scheduler anticipatory registered (default)
io scheduler deadline registered
io scheduler cfq registered
CLCD: Integrator/CP hardware, VGA display
Clock CLCDCLK: setting VCO reg params: S=1 R=39 V=35
Console: switching to colour frame buffer device 80x30
Serial: AMBA PL011 UART driver
mb:16: ttyAMA0 at MMIO 0x16000000 (irq = 1) is a AMBA/PL011
mb:17: ttyAMA1 at MMIO 0x17000000 (irq = 2) is a AMBA/PL011
RAMDISK driver initialized: 16 RAM disks of 8192K size 1024 blocksize
loop: loaded (max 8 devices)
nbd: registered device at major 43
smc91x.c: v1.1, sep 22 2004 by Nicolas Pitre <nico@cam.org>
eth0: SMC91C11xFD (rev 1) at c8814000 IRQ 27 [nowait]
eth0: Ethernet addr: 52:54:00:12:34:56
mice: PS/2 mouse device common for all mice
mmc0: MMCI rev 0 cfg 00 at 0x1c000000 irq 23,24
Green LED off
input: AT Raw Set 2 keyboard as /class/input/input0
input: ImExPS/2 Generic Explorer Mouse as /class/input/input1
NET: Registered protocol family 2
IP route cache hash table entries: 1024 (order: 0, 4096 bytes)
TCP established hash table entries: 4096 (order: 2, 16384 bytes)
TCP bind hash table entries: 2048 (order: 1, 8192 bytes)
TCP: Hash tables configured (established 4096 bind 2048)
TCP reno registered
IPv4 over IPv4 tunneling driver
GRE over IPv4 tunneling driver
TCP bic registered
NET: Registered protocol family 1
NET: Registered protocol family 17
802.1Q VLAN Support v1.8 Ben Greear <greearb@candelatech.com>
All bugs added by David S. Miller <davem@redhat.com>
VFP support v0.3: implementor 41 architecture 1 part 10 variant 9 rev 0
eth0: link up
rdate: Unable to connect to remote host (10.0.2.2): Connection refused

This root FS contains most basic linux utilities (implemented with busybox)
and the Lynx web browser.

Kernel config is available through /proc/config.gz

Log in as root with no password.
qemu login: root
login[724]: root login  on `ttyAMA0'



BusyBox v1.1.2 (2006.05.04-15:30+0000) Built-in shell (ash)
Enter 'help' for a list of built-in commands.

#


-----------------------------------------------------

It's too powerful.




[RC project]Using TGFF to generate random task graph

posted ‎‎10 Nov 2009 22:59‎‎ by Shih-Shen Lu   [ updated ‎‎17 Nov 2009 19:17‎‎ ]

To generate the random task graphs.

Official website: TGFF, http://ziyang.eecs.umich.edu/~dickrp/tgff/

Download source code from http://ziyang.eecs.umich.edu/~dickrp/tgff/download.html


[leeraphael@embedded 10:55 examples]$vi ./simple.tgffopt
   1 tg_cnt 5    #the number of graph
  2 task_cnt 20 5    #the average of graph task, the relative error
  3 task_degree 3 2    #in/out
  4 period_laxity 1     #execution time == period == 1
  5 period_mul 1, 0.5, 2    #array refer document
  6 tg_write    #generate the type file
  7 eps_write    #generate the type file
  8 vcg_write    #generate the type file
  9
 10 table_label COMMUN    # table name
 11 table_cnt 3    
 12 table_attrib price 80 20
 13 type_attrib exec_time 50 20
 14 trans_write

/*
From TGFF doc.http://ziyang.eecs.umich.edu/~dickrp/tgff/manual.pdf
•  tg_cnt <int>: Sets the number of task graphs to generate.
•  tg_label <string>: Sets the (case dependent) label used for task graphs.
•  tg_offset <string> <int>: Sets the start index for named task graph (default 0).
•  task_cnt <int> <int>: Sets the minimum number of tasks per task graph (average, multiplier).
•  trans_type_cnt <int>: Sets the number of possible transmit types.
•  period_mul <list(<int>)>: Sets the multipliers for periods in multirate systems.
–  Allows the user to specify the periods relative to each other.
–  The multipliers are randomly selected from this list.
•  prob_periodic <flt>: Sets the probability that a graph is periodic (default 1.0).
6
–  Allows simultaneous generation of aperiodic and periodic task graphs.
•  prob_multi_start_nodes <flt>: Sets the probability that a graph has more than one start node (default 0.0).
•  start_node <int> <int>: Sets the number of start nodes for graphs which have multiple start nodes (average,
multiplier).
–  If prob_multi_start_nodes is zero, this option is ignored.
*/

The following steps are the installing.

[leeraphael@embedded 15:13 Benchmark]$wget http://ziyang.eecs.umich.edu/~dickrp/tgff/tgff-3_5.tgz
[leeraphael@embedded 15:20 Benchmark]$tar zxvf tgff-3_5.tgz
.
.
.
[leeraphael@embedded 15:20 Benchmark]$cd tgff-3.5
[leeraphael@embedded 15:22 tgff-3.5]$make
***** Compiling -> RGen.o
***** Compiling -> Epsilon.o
***** Compiling -> TGraph.o
***** Compiling -> DBase.o
***** Compiling -> TG.o
***** Compiling -> ArgPack.o
***** Compiling -> RMath.o
***** Compiling -> psprint.o
***** Compiling -> RStd.o
***** Compiling -> main.o
***** Compiling -> RString.o
***** Compiling -> PGraph.o
***** Compiling -> Interface.o
***** Compiling -> Graph.o
***** Linking -> tgff

[leeraphael@embedded 15:22 tgff-3.5]$
[leeraphael@embedded 15:22 tgff-3.5]$ls -l ./examples/
總計 160
-rw-r----- 1 leeraphael esl305   136 2008-08-24 09:22 bus.tgffopt
-rw-r----- 1 leeraphael esl305  1457 2008-08-24 09:22 creds1.tgffopt
-rw-r----- 1 leeraphael esl305  2514 2008-08-24 09:22 kbasic_tables.tgffopt
-rw-r----- 1 leeraphael esl305  1018 2008-08-24 09:22 kbasic_task.tgffopt
-rw-r----- 1 leeraphael esl305   998 2008-08-24 09:22 kextended.tgffopt
-rw-r----- 1 leeraphael esl305 56638 2008-08-24 09:22 kseries_parallel.eps
-rw-r----- 1 leeraphael esl305 14181 2008-08-24 09:22 kseries_parallel.tgff
-rw-r----- 1 leeraphael esl305  6774 2008-08-24 09:22 kseries_parallel.tgff.dot
-rw-r----- 1 leeraphael esl305  1040 2008-08-24 09:22 kseries_parallel.tgffopt
-rw-r----- 1 leeraphael esl305 22310 2008-08-24 09:22 kseries_parallel.vcg
-rw-r----- 1 leeraphael esl305  1295 2008-08-24 09:22 kseries_parallel_xover.tgffopt
-rw-r----- 1 leeraphael esl305   439 2008-08-24 09:22 packed.tgffopt
-rw-r----- 1 leeraphael esl305   258 2008-08-24 09:22 packets.tgffopt
-rw-r----- 1 leeraphael esl305  1295 2008-08-24 09:22 robtst.tgffopt
-rw-r----- 1 leeraphael esl305   201 2008-08-24 09:22 RunMe
-rw-r----- 1 leeraphael esl305   387 2008-08-24 09:22 RunMe.view
-rw-r----- 1 leeraphael esl305   203 2008-08-24 09:22 simple.tgffopt
-rw-r----- 1 leeraphael esl305   143 2008-08-24 09:22 sp_rand.tgffopt

[leeraphael@embedded 15:23 tgff-3.5]$./tgff ./examples/simple
[leeraphael@embedded 15:23 tgff-3.5]$ls -l ./examples/
總計 224
-rw-r----- 1 leeraphael esl305   136 2008-08-24 09:22 bus.tgffopt
-rw-r----- 1 leeraphael esl305  1457 2008-08-24 09:22 creds1.tgffopt
-rw-r----- 1 leeraphael esl305  2514 2008-08-24 09:22 kbasic_tables.tgffopt
-rw-r----- 1 leeraphael esl305  1018 2008-08-24 09:22 kbasic_task.tgffopt
-rw-r----- 1 leeraphael esl305   998 2008-08-24 09:22 kextended.tgffopt
-rw-r----- 1 leeraphael esl305 56638 2008-08-24 09:22 kseries_parallel.eps
-rw-r----- 1 leeraphael esl305 14181 2008-08-24 09:22 kseries_parallel.tgff
-rw-r----- 1 leeraphael esl305  6774 2008-08-24 09:22 kseries_parallel.tgff.dot
-rw-r----- 1 leeraphael esl305  1040 2008-08-24 09:22 kseries_parallel.tgffopt
-rw-r----- 1 leeraphael esl305 22310 2008-08-24 09:22 kseries_parallel.vcg
-rw-r----- 1 leeraphael esl305  1295 2008-08-24 09:22 kseries_parallel_xover.tgffopt
-rw-r----- 1 leeraphael esl305   439 2008-08-24 09:22 packed.tgffopt
-rw-r----- 1 leeraphael esl305   258 2008-08-24 09:22 packets.tgffopt
-rw-r----- 1 leeraphael esl305  1295 2008-08-24 09:22 robtst.tgffopt
-rw-r----- 1 leeraphael esl305   201 2008-08-24 09:22 RunMe
-rw-r----- 1 leeraphael esl305   387 2008-08-24 09:22 RunMe.view
-rw-r--r-- 1 leeraphael esl305 35997 2009-11-11 15:23 simple.eps
-rw-r--r-- 1 leeraphael esl305 10802 2009-11-11 15:23 simple.tgff
-rw-r----- 1 leeraphael esl305   203 2008-08-24 09:22 simple.tgffopt
-rw-r--r-- 1 leeraphael esl305 14573 2009-11-11 15:23 simple.vcg
-rw-r----- 1 leeraphael esl305   143 2008-08-24 09:22 sp_rand.tgffopt
[leeraphael@embedded 15:23 tgff-3.5]$

The blue words are the new files generated by tgff.


[C language] Predefined Names

posted ‎‎8 Nov 2009 08:19‎‎ by Shih-Shen Lu

__LINES__ : 顯示現在所在行數
__FILE__:顯示該檔案名稱
__TIME__:顯示編譯時間,"hh:mm:ss"
__DATE__:顯示編譯日期,"Mmm dd yyyy"
__STDC__:當compiler符合ANSI C時,顯示1


[C language]resolve swap(&a, b&)

posted ‎‎7 Nov 2009 06:14‎‎ by Shih-Shen Lu

To understand the x86 assembly language.


main program
      1 int swap(int *a, int *b)
      2 {
      3         int c;
      4         c = *a;
      5         *a = *b;
      6         *b = c;
      7 }
      8 int main()
      9 {
     10         int a, b;
     11         a = 20;
     12         b = 40;
     13         swap(&a, &b);
     14         return (a-b);
     15 }



assembly code generated by gcc
compiling command:$ gcc -S -oswap.s ./main.c

      1         .file   "main.c"
      2         .text
      3 .globl swap
      4         .type   swap, @function
      5 swap:
      6         pushl   %ebp
      7         movl    %esp, %ebp
      8         subl    $20, %esp       #宣告c的空間
      9         movl    8(%ebp), %eax   #將a的位置取出來放到eax變數
     10         movl    (%eax), %eax    #將eax位置的值抓出來放到eax
     11         movl    %eax, -4(%ebp)  #將eax的值放到c的空間
     12         movl    12(%ebp), %eax  #將b的位置取出來放到eax變數
     13         movl    (%eax), %edx    #將eax位置的值抓出來放到edx
     14         movl    8(%ebp), %eax   #將a的位置取出來放到eax變數
     15         movl    %edx, (%eax)    #將b的值放到a位置的value
     16         movl    12(%ebp), %edx  #將b的位置取出來放到edx變數
     17         movl    -4(%ebp), %eax  #將c位置的value放到eax變數
     18         movl    %eax, (%edx)    #將eax值存到edx位置的value
     19         leave                   #restore the ebp, esp
     20         ret
     21         .size   swap, .-swap
     22 .globl main
     23         .type   main, @function
     24 main:
     25         leal    4(%esp), %ecx
     26         andl    $-16, %esp
     27         pushl   -4(%ecx)
     28         pushl   %ebp            #保留ebp到stack中
     29         movl    %esp, %ebp      #將目前esp存到ebp中
     30         pushl   %ecx            #將ecx也存起來
     31         subl    $24, %esp       #將esp向下移24bytes
     32         movl    $20, -8(%ebp)   #將20存到a中
     33         movl    $40, -12(%ebp)  #將40存到b中
     34         leal    -12(%ebp), %eax #將b的位置放到eax中
     35         movl    %eax, 4(%esp)   #將b的位置放進swap的堆疊中
     36         leal    -8(%ebp), %eax  #將a的位置放到eax中
     37         movl    %eax, (%esp)    #將a的位置放進swap的堆疊中
     38         call    swap
     39         movl    -8(%ebp), %edx
     40         movl    -12(%ebp), %eax
     41         movl    %edx, %ecx
     42         subl    %eax, %ecx
     43         movl    %ecx, %eax
     44         addl    $24, %esp
     45         popl    %ecx
     46         popl    %ebp
     47         leal    -4(%ecx), %esp
     48         ret
     49         .size   main, .-main
     50         .ident  "GCC: (GNU) 4.3.2 20081105 (Red Hat 4.3.2-7)"
     51         .section        .note.GNU-stack,"",@progbits



2009電信奧斯卡 入圍

posted ‎‎5 Nov 2009 06:08‎‎ by Shih-Shen Lu

打一下廣告

我們是
------------------------------
隊伍 C5 - 關懷你我他
作品 RFID 3G
------------------------------
人氣投票區 http://chtoscar.amego.com.tw/list.php?site=3

投票可以到時可以抽獎!

到時11/25要上台北參加頒獎,期待一下!冠軍!

[C language]buliding library

posted ‎‎13 Oct 2009 06:37‎‎ by Shih-Shen Lu   [ updated ‎‎13 Oct 2009 07:50‎‎ ]

c的函式庫分兩種
  1. Static library
  2. Share library
差異在於當程式使用共用或是不共用時,程式檔案大小會有差。
以下介紹如何自己製作函式庫(library)
P.S.以下例子有打包在附件[buliding_library.tbz]

Static library

以下透過例子來介紹。
[leeraphael@embedded 21:41 C]$vi ./sum.c
--------S-----------------sum.c-------------------------------------
  1 int sum(int x, int y)
  2 {
  3     return x+y;
  4 }
--------E-----------------sum.c-------------------------------------
[leeraphael@embedded 21:41 C]$vi ./multiply.c
--------S-----------------multiply.c-------------------------------------
  1 int multiply(int x, int y)
  2 {
  3     return x*y;
  4 }
--------E-----------------multiply.c-------------------------------------
[leeraphael@embedded 21:41 C]$vi ./math_header.h
--------S-----------------math_header.c-------------------------------------
   1 int sum(int, int);
  2 int multiply(int, int);
--------E-----------------math_header.c-------------------------------------
[leeraphael@embedded 21:46 C]$gcc -c ./sum.c
[leeraphael@embedded 21:46 C]$gcc -c ./multiply.c
[leeraphael@embedded 21:46 C]$ar rcs libmath.a sum.o multiply.o
[leeraphael@embedded 21:46 C]$vi main.c
--------S-----------------main.c-------------------------------------
  1 #include <stdio.h>
  2 #include "math_header.h"
  3
  4 int main()
  5 {
  6     int x = 10;
  7     int y = 10;
  8
  9     printf("x*y = %d\n", multiply(x,y));
 10     printf("x+y = %d\n", sum(x,y));
 11
 12     return 0;
 13
 14 }
--------E-----------------main.c-------------------------------------
[leeraphael@embedded 21:48 C]$gcc -o main main.c -L. -lmath
-L:搜尋函式庫要找得路徑。以這裡來說就是搜尋當前目錄。
-l:使用你所給的lib name。以這裡來說就是找尋libmath,因為-lmath會自動將lib加上math所以是尋找libmath,因為lib有規定如果要用-l找得到的話,函式庫檔暗明稱開頭就得要是"lib"。
[leeraphael@embedded 21:48 C]$gcc main.c libmath.a -o main
這裡的編譯方式跟上一個結果是一樣的,只是這裡直接給library file
[leeraphael@embedded 21:54 C]$./main
x*y = 100
x+y = 20
[leeraphael@embedded 21:54 C]$
跟我們要得結果一樣。

Share library

[leeraphael@embedded 21:56 C]$gcc -c -fPIC sum.c multiply.c
加上-fPIC是因為我們要做share library所加的參數。
 -shared
           Produce a shared object which can then be linked with other objects to form
           an executable.  Not all systems support this option.  For predictable
           results, you must also specify the same set of options that were used to
           generate code (-fpic, -fPIC, or model suboptions) when you specify this
           option.[1]
-mlarge-data
           When -mexplicit-relocs is in effect, static data is accessed via gp-relative
           relocations.  When -msmall-data is used, objects 8 bytes long or smaller are
           placed in a small data area (the ".sdata" and ".sbss" sections) and are
           accessed via 16-bit relocations off of the $gp register.  This limits the
           size of the small data area to 64KB, but allows the variables to be directly
           accessed via a single instruction.

           The default is -mlarge-data.  With this option the data area is limited to
           just below 2GB.  Programs that require more than 2GB of data must use
           "malloc" or "mmap" to allocate the data in the heap instead of in the
           program’s data segment.

           When generating code for shared libraries, -fpic implies -msmall-data and
           -fPIC implies -mlarge-data.
細節有興趣的可以在找男人。

[leeraphael@embedded 21:57 C]$gcc -shared -Wl,-soname,libmath.so.1 -o libmath.so.1.0.0 sum.o multiply.o
[leeraphael@embedded 22:06 C]$ln -s libmath.so.1.0.0 libmath.so
[leeraphael@embedded 22:06 C]$ln -s libmath.so.1.0.0 libmath.so.1
以上兩行是為了產生兩個連結用的ln
[leeraphael@embedded 22:07 C]$ls -l
總計 44
-rw-r--r-- 1 leeraphael esl305 1588 2009-10-13 21:31 libmath.a
lrwxrwxrwx 1 leeraphael esl305   16 2009-10-13 22:05 libmath.so -> libmath.so.1.0.0
lrwxrwxrwx 1 leeraphael esl305   16 2009-10-13 22:06 libmath.so.1 -> libmath.so.1.0.0
-rwxr-xr-x 1 leeraphael esl305 4162 2009-10-13 21:58 libmath.so.1.0.0
-rwxr-xr-x 1 leeraphael esl305 5158 2009-10-13 21:54 main
-rw-r--r-- 1 leeraphael esl305  173 2009-10-13 21:35 main.c
-rw-r--r-- 1 leeraphael esl305   44 2009-10-13 21:28 math_header.h
-rw-r--r-- 1 leeraphael esl305   44 2009-10-13 21:27 multiply.c
-rw-r--r-- 1 leeraphael esl305  689 2009-10-13 21:57 multiply.o
-rw-r--r-- 1 leeraphael esl305   39 2009-10-13 21:26 sum.c
-rw-r--r-- 1 leeraphael esl305  683 2009-10-13 21:57 sum.o
以上是目前所有的檔案
[leeraphael@embedded 22:08 C]$gcc -o main main.c libmath.so
編譯main,sum及multiply function則由共享函式庫提供
[leeraphael@embedded 22:11 C]$./main
./main: error while loading shared libraries: libmath.so.1: cannot open shared object file: No such file or directory
[leeraphael@embedded 22:11 C]$ldd ./main
        linux-gate.so.1 =>  (0x00852000)
        libmath.so.1 => not found
        libc.so.6 => /lib/libc.so.6 (0x00b63000)
        /lib/ld-linux.so.2 (0x00b3e000)
不能執行,因為他需要 libmath.so.1但卻找不到,所以我們需要動點手腳。將共享函式庫搜尋路徑加上我們現在libmath.so.1的位址給它。
[leeraphael@embedded 22:13 C]$export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
[leeraphael@embedded 22:12 C]$ldd ./main
        linux-gate.so.1 =>  (0x00add000)
        libmath.so.1 => ./libmath.so.1 (0x00e8b000)
        libc.so.6 => /lib/libc.so.6 (0x00b63000)
        /lib/ld-linux.so.2 (0x00b3e000)
[leeraphael@embedded 22:12 C]$./main
x*y = 100
x+y = 20
可以執行摟!

比較

以上兩種介紹完了,現在來比較一下兩者差異。
[leeraphael@embedded 22:19 C]$gcc -o static_main -static ./main.c ./libmath.a
[leeraphael@embedded 22:20 C]$gcc -o share_main ./main.c libmath.so
[leeraphael@embedded 22:22 C]$ls -l
總計 656
-rw-r--r-- 1 leeraphael esl305   1588 2009-10-13 21:31 libmath.a
lrwxrwxrwx 1 leeraphael esl305     16 2009-10-13 22:05 libmath.so -> libmath.so.1.0.0
lrwxrwxrwx 1 leeraphael esl305     16 2009-10-13 22:06 libmath.so.1 -> libmath.so.1.0.0
-rwxr-xr-x 1 leeraphael esl305   4162 2009-10-13 21:58 libmath.so.1.0.0
-rw-r--r-- 1 leeraphael esl305    173 2009-10-13 21:35 main.c
-rw-r--r-- 1 leeraphael esl305     44 2009-10-13 21:28 math_header.h
-rw-r--r-- 1 leeraphael esl305     44 2009-10-13 21:27 multiply.c
-rw-r--r-- 1 leeraphael esl305    689 2009-10-13 21:57 multiply.o
-rwxr-xr-x 1 leeraphael esl305   5293 2009-10-13 22:22 share_main
-rwxr-xr-x 1 leeraphael esl305 623412 2009-10-13 22:20 static_main
-rw-r--r-- 1 leeraphael esl305     39 2009-10-13 21:26 sum.c
-rw-r--r-- 1 leeraphael esl305    683 2009-10-13 21:57 sum.o

檔案大小比較
share_main遠小於static_main,因為share_main使用共享,所以一些函式都是需要時才link進來,反之static則將所有會用到的函式都包進來。比方說x86的平台的execution file不能放到arm上面run。
差異在於使用static方式編譯,由於不需要外在的函式庫存在,所以可以任意放在相同架構上的平台run。反之,由於share需要外在函式庫存在,所以只buliding_library.tbz要有缺一個library就會不能執行。

‹ Prev    1-10 of 54    Next ›