顯示具有 C語言 標籤的文章。 顯示所有文章
顯示具有 C語言 標籤的文章。 顯示所有文章

2012年2月7日 星期二

C 語言 macro 應用

C 語言用了十幾年, 卻很少用到macro 來簡化程式, 先記下連結慢慢復習
C language MACRO in depth

2012年2月6日 星期一

assembly call C function

UEFI BIOS在 SEC進PEI階段會由assembly code 進入C code,
除了設定好stack 外, 就是要注意assembly call C function的方法
會先把參數以反順序的方式push到stack, 然後call PEI的主程式
不過這跟一般的assembly call c function不一樣的是, UEFI BIOS
不會再回到assembly mode, 所以不需要再把esp 調整回來!
一般的assembly call C function要把esp加上參數總共size.
範例如下

global  _main

extern  _printf

section .data

text    db      "291 is the best!", 10, 0
strformat db    "%s", 0

section .code

_main
        push    dword text
        push    dword strformat
        call    _printf
        add     esp, 8
        ret

2012年1月11日 星期三

Intrinsic functions

MS provide some intrinsic functions for low level operations (eg. firmware/driver) on their compiler that is very convenience. You don't have to include the any header file to make use of these functions. But you have to specify a compiler option "/Oi".
example:
#progma intrinsic (func1)
#progma intrinsic (func2, func3)

Below are some of intrinsic functions

_disable
_outp
fabs
strcmp
_enable
_outpw
labs
strcpy
_inp
_rotl
memcmp
strlen
_inpw
_rotr
memcpy
_lrotl
_strset
memset
_lrotr
abs
strcat

2011年12月21日 星期三

C/C++中 union 用法

平常因為工作內容的關係, 從沒用過union

今天突然看到, 竟然忘記這資料結構是甚麼 ! 筆記一下大陸人的網頁
#include <stdio.h>
void main()
{
union number
{ /*定义一个联合*/
int i;
struct
{ /*在联合中定义一个结构*/
char first;
char second;
}half;
}num;
num.i=0x4241; /*联合成员赋值*/
printf("%c%c\n", num.half.first, num.half.second);
num.half.first='a'; /*联合中结构成员赋值*/
num.half.second='b';
printf("%x\n", num.i);
getchar();
}
输出结果为: 
AB 
6261