I'm trying to write a hello world program in assembly in visual studio. 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?
With inline assembly you can use the _emit
pseudo instruction, like this (here for 32-bit code):
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
.
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. Not sure if it's happy to jump to a label in a different __asm section tho.]
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'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.