[英]VS embed string in assembly opcodes
I'm trying to write a hello world program in assembly in visual studio.我正在尝试在 Visual Studio 中用汇编编写一个 hello world 程序。 I would like to have the string saved as opcodes between instructions like this
我想将字符串保存为这样的指令之间的操作码
call label1
"hello world"
label1:
pop esi
push esi
call print
How can I do this in Visual Studio?如何在 Visual Studio 中执行此操作?
With inline assembly you can use the _emit
pseudo instruction, like this (here for 32-bit code):使用内联汇编,您可以使用
_emit
伪指令,如下所示(此处为 32 位代码):
auto foo()
-> char const*
{
__asm
{
mov eax, offset my_data
jmp epilogue
my_data:
_emit 'H'
_emit 'e'
_emit 'l'
_emit 'l'
_emit 'o'
_emit '!'
_emit 0
}
epilogue: ;
}
#include <iostream>
using namespace std;
auto main() -> int
{
wcout << foo() << endl;
}
I don't know of any way to write the strings as strings with inline assembly.我不知道有什么方法可以用内联汇编将字符串写成字符串。
I recommend using proper full assembler instead.我建议改用适当的完整汇编程序。 If you're using Visual Studio then you already have it installed, it's
ml.exe
.如果您使用的是 Visual Studio,那么您已经安装了它,它是
ml.exe
。
In inline assembler in Microsofts compiler, you can't do the obvious solution of在微软编译器的内联汇编器中,你不能做明显的解决方案
db "hello world"
so you have to actually generate the instruction sequence that produces the right bytes:所以你必须实际生成产生正确字节的指令序列:
From my hand-disassembly, this should do it - I have not CHECKED that this gives the right sequence从我的手动拆卸来看,应该这样做 - 我没有检查这给出了正确的顺序
[It could perhaps be possible to do: [也许可以这样做:
__asm
{
call label1
_emit(0x68)
_emit(0x6f)
... // rest of "hello world"
label1:
pop esi
push esi
call print
}
I haven't got a Visual studio compiler to try it on.我还没有 Visual Studio 编译器来试用它。 Not sure if it's happy to jump to a label in a different __asm section tho.]
不确定跳到不同 __asm 部分中的标签是否高兴。]
PUSH 6f6c6c65h ; push = 'h', 6f6c6c64 'ello'
AND al,al ; space
JA 6fh ; ja = 77 = 'w', 6f = 'o'
JB 6ch ; JB = 72 = 'r', 6c = 'l'
FS: ADD AL,AL ; fs = 64 = d, ADD AL, AL = 0
This is obviously very unpractical, and I would not do this for anything that needs maintenance... [And I have probably got something wrong, and I expect that JA constant
and JB constant
isn't valid in inline assembler, you have to jump to a label... Which means you need to have a label about 100-110 bytes forwards to make 'l'
and 'd'
这显然是非常不切实际的,我不会为任何需要维护的东西这样做...... [而且我可能搞错了什么,我希望
JA constant
和JB constant
在内联汇编器中无效,你必须跳转到标签...这意味着您需要有一个大约 100-110 字节的标签才能使'l'
和'd'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.