發表文章

自己動手做OS, micro kernel develop on Beagleboard-XM(ARMv7) lab1

This is a tutorial on bare-metal [OS] development on the Beagleboard-XM. I will show you how to implement a Helloworld image in this article. 在這篇文章中,我會介紹如何寫一個Helloworld的image.  1. From BeagleBoard-xM System Reference Manual, we can get, "A single RS232 port is provided on the BeagleBoard and provides access to the TX and RX lines of UART3 on the processor." 2. From DM3730 Technical Reference Manual- we can get "THR_REG W UART3 base address is 0x49020000"  - 首先我們可以從BeagleBoard-xM System Reference Manual,查到板上子上的RS-232是接在UART3上,再從DM3730 Technical Reference Manual,得知,UART3的THR_REG的位址是0x49020000. I've already added  comments in source file, please refer to source code(github) for details. 我把註解加入到檔案中了,請到github下載source。 1.boot.asm #UART3 THR_REG register address .equ UART3_THR_REG, 0x49020000 .arm _start: ldr r0,=UART3_THR_REG adr r1,.L0 bl helloworld .L1: b .L1 .align 2 .L0: .ascii "helloworld\n\0" 2.Helloworld.c int helloworld(unsigned int...

Assembly - moving data 組合語言,移動資料

圖片
從記憶體中讀資料 load date from memory into register 把暫存器中存資料存進記憶體 store register data into memory                Figure 1. integer registes,                 source:https://www.cs.cmu.edu/~fp/courses/15213-s07/misc/asm64-handout.pdf mov operand combinations(mov的運算整理) mov imm to register, mov $0x11,%eax -> var_a=0x11; mov imm to mem, mov $0x39,(%eax) -> *p=0x39; mov reg to reg, mov %edx,%eax -> var_a=var_b; mov reg to mem, mov %eax,(%edx) -> *p=var_a; mov mem to reg, mov (%eax),%eax -> var_a=*p; *can not do memory to memory transfer in single instruction. Here is an simple swap code: void swap(int *xp, int *yp) {    int a=*xp;    int b=*yp;    *xp=b;    *yp=a; } Let's see how above code done by assembly. Variables store in which memory place and registers is assigned...

C語言,大數運算,階層筆記

如果今天要用C語言計算50!,int都是不夠用的。 要解這樣的問題,必需要用陣列。  32位元系統unsigned int 最大 4,294,967,295 (10位數字) 64位元 uint64_t 18,446,744,073,709,551,615 (20 位數字) /*從array的高位元開始存值*/ void Big_factorial_iteration(int n) { int i=1; /*init array*/ int a[SIZE_A]= {0}; int tail=SIZE_A-1; a[tail]=1; for(i=1; i <= n ; i++) { int j; /* multiply the value */ for(j= tail ; j >= 0 ; j--) { a[j]=a[j]*i; } /*進位留下個位數,其餘的加點前一位 */ for(j= tail ; j >= 0 ; j--) { if (a[j] >= 10) a[j-1]=a[j-1]+(a[j]/10); a[j]=a[j]%10; } } for(i=0; i< SIZE_A ; i++) { printf("%d",a[i]); } printf("\n"); } int main() { printf(" %d \n",factorial_recursion(10)); printf(" %d \n",factorial_iteration(10)); Big_factorial_iteration(20); 計算20階層 } result: [carlos@localhost c_examples]$ ./factorial_2  3628800  3628800  000000000002432902008176640000  原始碼:   https://github.com/tzuCarlo...

安裝Cowday到你的linux歡迎訊息,message of day linux

圖片
1.先安裝 sudo apt-get install fortune cowsay 2接著加入以下指令到 bash.bashrc中,這樣每次開啟terminal 或 ssh 就會有隻牛跟你說話囉 sudo vim    / etc / bash.bashrc   # Spicing up Terminal fortune | cowsay -n echo source: http://xtremediary.blogspot.tw/2010/04/spice-up-ubuntulinux-terminal-with.html

C語言階層的遞迴與迴圈 - example factorial recursion and iteration.

筆記一下C語言階層的遞迴與迴圈寫法,這個範例不考慮溢位的問題。 #include /* * example for recursion and iteration * I know it should return error if n < 0, but this just a simple example for study. * Do not care about overflow issues this example. */ unsigned int factorial_recursion(int n) { /* return n>=1 ? n * factorial_recursion(n-1) : 1; */ if(n==0) return 1; /*Base case */ else return n * factorial_recursion(n-1); /*General case */ } unsigned int factorial_iteration(int n) { int i=1; int result=1; for(i=1; i <= n ; i++) { /*result *= i */ result=result * i; } return result; } int main() { printf(" %d \n",factorial_recursion(10)); printf(" %d \n",factorial_iteration(10)); } 原始碼: https://github.com/tzuCarlos/linux_C/blob/master/c_examples/factorial.c

Strace cross compile PowerPC

今天遇到一個bug Web daemon隨機會segmentation fault 想用Strace來debug看看 在這抓 - http://sourceforge.net/projects/strace/files/strace/ ./configure --host=powerpc-linux CC=powerpc-linux-gcc LD=powerpc-linux-ld  make之後就可以放上機器上囉。 在機器上執行:Strace -f -o web.trace webs 神奇的事發生了,這時候我的web daemon就不會掛掉了。 只好,慢慢trace code了,最後是發現有人做這樣的行為,寫C時要小心,別亂copy亂指的。 UCHAR old_pass[32]; UCHAR *passphrase; strcpy(passphrase, old_pass);

objdump gcc Assembly debug筆記

gcc -s xxxx 可以篇成組合語言 gcc -g xxxx 之後可以用 objdump -S a.out 看到組語、C code 遇到要debug kernel panic時很好用! :) example: [carlos@localhost testC]$ objdump -S ./a.out ./a.out:     file format elf32-i386 080483d4 : int func1() {  80483d4:       55                      push   %ebp  80483d5:       89 e5                   mov    %esp,%ebp  80483d7:       83 ec 08                sub    $0x8,%esp    printf("WAT WAT WAT \n");  80483da:       c7 04 24 10 85 04 08    movl   $0x8048510,(%esp)  80483e1:       e8 02 ff ff ff          call   80482e8 }  80483e6:       c9                      leav...