http://www.ibm.com/developerworks/linux/library/l-ia.html is a great introduction to inline assembly code in C. But its code examples are not ready to run. So I write the following code to help me understand the article.
#include <stdio.h>
#include <unistd.h>
#define rdtscll(val) \
__asm__ __volatile__ ("rdtsc" : "=A" (val))
void test_rdtsc() {
unsigned long long abc;
abc = 0;
rdtscll(abc);
printf("%#llX\n", abc);
}
void test_r_constraint() {
int x = 10, y;
asm ("movl %1, %%eax;"
"movl %%eax, %0;"
:[output] "=r"(y) /* y is output operand */
:[input] "r"(x) /* x is input operand */
:"%eax"); /* %eax is clobbered register */
printf("x=%d, y=%d\n", x, y);
}
void test_symbolic_name() {
int x = 10, y;
asm ("movl %[input], %[output];"
:[output] "=r"(y) /* y is output operand */
:[input] "r"(x) /* x is input operand */
:"%eax"); /* %eax is clobbered register */
printf("x=%d, y=%d\n", x, y);
}
void test_a() {
int var = 100;
asm ("incl %0" :"=a"(var):"0"(var));
printf("var: %d\n", var);
}
void test_m() {
int x = 100;
asm("incl %0\n" : :"m"(x));
__asm__("incl %0\n" : :"m"(x));
printf("x=%d\n", x);
}
void test_controlName() {
static int foo asm("myfoo") = 2;
}
void test_read_write() {
int foo, bar;
foo = 1;
bar = 2;
asm("movl %1, %%eax;"
"movl %2, %%ebx;"
"addl %%ebx, %%eax;"
"movl %%eax, %0;"
:"=r" (foo)
:"0" (foo), "g" (bar)
:"%eax", "%ebx");
printf("sum: %d\n", foo);
}
void test_cpuid() {
unsigned op;
unsigned _eax;
unsigned _ebx;
unsigned _ecx;
unsigned _edx;
op = 0;
asm("cpuid"
: "=a" (_eax), "=b" (_ebx), "=c" (_ecx), "=d" (_edx)
: "a" (op));
printf("eax=%#X, ebx=%#X, ecx=%#X, edx=%#X\n", _eax, _ebx, _ecx, _edx);
}
void asm_strcpy(char *dest, char *src, int count) {
asm("cld\n\t"
"rep movsb"
:
: "S" (src), "D" (dest), "c" (count));
}
void test_asm_strcpy() {
char *src = "abc";
char dest[10];
asm_strcpy(dest, src, 4);
printf("dest: %s\n", dest);
}
void test_syscall() {
int sys_number = 1; /* syscall number for exit */
int status = 100;
asm("int $0x80"
:
: "a" (sys_number), "b" (status));
}
void test_asm_memcpy() {
char *src = "123";
char dst[4];
int num = 4;
asm("movl %[count], %%ecx;\n"
"up: lodsl;\n\t"
"stosl;\n\t"
"loop up;"
:
: "S" (src), "D" (dst), [count] "r" (num)
: "%ecx", "%eax");
printf("dst: %s\n", dst);
}
int main(void) {
test_rdtsc();
test_r_constraint();
test_symbolic_name();
test_a();
test_m();
test_controlName();
test_read_write();
test_asm_strcpy();
test_cpuid();
test_syscall();
test_asm_memcpy();
}
分享到:
相关推荐
The book's structure and sample code are designed to help the reader quickly understand x86 assembly language programming and the computational capabilities of the x86 platform. Major topics of the ...
easy sample code for minimum snap trajectory planning in MATLAB.zip
MSP430F22x2 sample code (Assembly IAR).rar
Sample code in MATLABOctave for Kalman Filter for Beginners.zip
MSP430F22x2 sample code (Assembly CCE).rar
Android SQLite Sample Code
Moving to Microsoft Visual Studio 2010 sample code.zip
tines in the C program to ensure that the compiler generates the appropriate assembly language code for the routine. Chapter 14, “Calling Assembly Libraries,” demonstrates how assembly language ...
ios应用源码之感恩---奉上大量samplecode(第一部分) samplecode1
Sensirion I2C Humidity Sensor SHT21 Sample Code 盛世瑞恩 湿度芯片
Linux Multi-thread C sample code
This book introduces ... A companion web site, http://www.rayseyfarth.com, has a collection of PDF slides which instructors can use for in-class presentations and source code for sample programs.
VB.NET Clipboard sample code
windows socket TCP server client sample code
package sample code
Sample code for ASP.NET 2.0 Web 2.0 Blog Tags RSS NAS XML Ajax Mail
Renesas Starter Kit Sample Code for IAR Toolchain
Sample Archive FilesSample ZIP File
MATLABSimulink sample code suite for Autoware.zip
基于Beckhoff EL9800平台的主站代码 经测试IO功能均可实现 对于自己搭建EtherCAT的朋友非常具有代表性。