[英]Simple x86-64 C++ Inline assembly “Hello World” example
I am trying to find a very basic C++ inline x86-64 assembly example, similar to this: 我试图找到一个非常基本的C ++内联x86-64汇编示例,类似于此:
a Simple "Hello World" Inline Assembly language Program in C/C++ C / C ++中的简单“ Hello World”内联汇编语言程序
char msg[] = "Hello, world";
asm {
mov ax,4 // (I/O Func.)
mov bx,1 // (Output func)
lds cx, msg // (address of the string)
mov dx,6 // (lenght of the string)
int 0x21 // system call
}
which would work with the Intel Compiler. 可以与Intel编译器一起使用。 Could someone please help with this?
有人可以帮忙吗?
EDIT Regarding OS I have the ICC on Windows and Linux- lets say Linux! 关于操作系统,我在Windows和Linux上都有ICC,可以说Linux!
The code you have posted is 16-bit code. 您发布的代码是16位代码。 64-bit Windows (or any version of Linux) doesn't support 16-bit code.
64位Windows(或任何版本的Linux)不支持16位代码。 [And it's a bit buggy, since it sets the length to 6, when the actual length of the string is 12...]
[这有点bug,因为当字符串的实际长度为12时,它将长度设置为6 ...]
You could possibly figure out how to do the same thing in Windows code, but I fail to see the point of learning how to make system calls in Windows from assembler. 您可能会想出如何在Windows代码中执行相同的操作,但是我看不到学习如何从汇编程序在Windows中进行系统调用的意义。 Write some code that does something that you can actually apply in real life, such as counting the number of characters in a string.
编写一些可以在现实生活中实际应用的代码,例如计算字符串中的字符数。
Of course, you will need to use either Intel or GCC compilers, since the Microsoft compiler doesn't allow inline assembler in 64-bit mode. 当然,您将需要使用Intel或GCC编译器,因为Microsoft编译器不允许在64位模式下进行内联汇编程序。
Here's a little sample of inline assembler using the "read timestamp counter" instruction, which will work with the gcc compiler (and by compatibility should work with the Intel compiler too). 这是使用“读取时间戳记计数器”指令的内联汇编程序的一个小示例,该指令将与gcc编译器一起工作(并且兼容性也应与Intel编译器一起工作)。
static __inline__ unsigned long long rdtsc(void)
{
unsigned hi, lo;
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}
AT&T vs. Intel assembly format. AT&T与Intel汇编格式。
at&t noprefix intel
mov eax, -4(ebp,edx,4) mov DWORD PTR[-4 +ebp +edx *4], eax
mov eax, -4(ebp) mov DWORD PTR[-4 +ebp], eax
mov edx, (ecx) mov DWORD PTR[ecx], edx
lea ( ,eax,4), eax lea eax, DWORD PTR[8 + eax*4]
lea (eax,eax,2), eax lea eax, DWORD PTR[eax*2+eax]
Or this. 或这个。
asm(".intel_syntax noprefix");
asm("mov eax, ebx");
asm(".att_syntax prefix");
asm("mov %ebx, %eax");
AT&T syntax. AT&T语法。
int main(int argc, char *argv[])
{
int x=1, f=2, fa=3;
asm("int $0x3");
asm("mov 4%0,%%eax"::"m"(x));
asm("movss 4%0,%%xmm1"::"m"(f));
asm("fld 4%0"::"m"(fa));
return 0;
}
Note that one difference is the use of '%' and the direction of assignment to a register. 注意,一个区别是使用“%”和分配给寄存器的方向。
Some more discussion here and here . 这里和这里还有更多讨论。 Much of that has to do with debugging.
其中大部分与调试有关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.