how use Assembly on C++ for print

i'm trying(for now) using Assembly on C++, but without success.
for start i need ask: how can i print a string and a number?
What exactly are you trying? Assembly is different for every CPU family (x86, x64, a64, etc), and the way you do output from an assembly program is different for every OS (Linux, FreeBSD, Windows, etc), some OSs have multiple ways. Since you're talking about combining assembly and C++, you could also place calls from your assembly files into C or C++ standard library for portable output.
my CPU it's x64, maybe accept, i think, x86.
i'm using Windows.
i don't know these introductions. but, for now, i need learn how to print a string and a number.
i'm trying:
1
2
3
4
5
6
7
8
9
10
11
int main()
{

asm ( "mov eax,4\n"
      "mov ebx,1\n"
      "mov ecx,\"hello world\"\n"
      "mov edx,11\n"
      "int 80h"
      );
    return 0;
}

but i'm getting several errors:
4 errors on: "too many memory references for `mov'"
and:
"junk `h' after expression"
"operand size mismatch for `int'"
"int 80h" is the old (pre-VDSO) 32bit Linux syscall instruction, so that piece of assembly simply won't work on Windows even if you get the syntax right (except perhaps within WSL). You'll need a different example.

Based on the fragments of the error messages you quoted, you're not using Visual Studio (which doesn't support inline assembly at all on x64: assembly source has to be added to the project as a separate file), so you'd need to find out the inline assembly syntax on your specific compiler, too.
Last edited on
its GNU\GCC, but seems hard to find something :(
some tutorials\code aren't compatible :(
"GCC" is not specific enough. Windows has multiple GCC forks with multiple distributions.
Here, I'll show you how that demo works with GCC on x64 Linux, using one of the many online compilers that use that combination:

1
2
3
4
5
6
7
8
9
const char* p = "hello world";
int main()
{
asm ("movq $1, %rax\n"  // _NR_write
     "movq $1, %rdi\n"  // fd
     "movq p,  %rsi\n"  // buf
     "movq $11,%rdx\n" // size
     "syscall");
}

live demo: https://wandbox.org/permlink/NZwaTKuse4bFc90t

but you're really better off calling back into C (or C++) for output:
1
2
3
4
5
6
const char* p = "hello world";
int main()
{
asm ("movq p, %rdi\n"
     "call puts");
}

live demo: https://wandbox.org/permlink/CAtVnNSnYcrFjVro

Personally, I find Linux to be a more developer-friendly environment for these kind of things.
Last edited on
now i know that the code works, because i test it on internet. but do i need link a library or some options on compiler?
on the 1st isn't printed, and on 2nd i get a memory leak.
maybe i miss something on options, that i don't know
do i need link a library or some options on compiler?

no. You need to be using gcc on a 64-bit Linux. Here's how I compiled them:
$ g++ -o test test.cc
$ ./test
hello world


on the 1st isn't printed, and on 2nd i get a memory leak.

If you tried to compile and run these on Windows, there is no way to tell what could possibly happen. They are 64-bit Linux programs. They are not compatible with Windows.
understood. theres no Assembly ANSI? right?
theres no Assembly ANSI? right?


That's right. Assembler language is specific to the chip (and according to @Cubbi, to the operating system, too). The C++ compiler is responsible for translating the common C++ syntax into specific assembler instructions for the specific chip/OS. So, higher level languages (like C++) provide a layer of abstraction to shield the user of the specifics of the underlying hardware/OS.

When you dip your toes into embedded assembler, you are peeling back that layer of abstraction and sending native instructions to the chip. Since each family of chips uses a different language, there can be no standard for this.
doug4 wrote:
according to @Cubbi, to the operating system, too

to be fair, the language is the same, only the way you use it differs: in the Linux demo above when I call puts() from assembly, I put the pointer into %rdi, but on Windows, it would have to go into %rdx
Just a minor correction to what doug4 said: An Assembly program is an arbitrary human-readable representation of machine code. An assembler (a program that translates Assembly to machine code) can have any given syntax while targeting a given architecture. For example, MASM, NASM, and GAS all have incompatible syntaxes, yet they all target x86. Yet another syntax is shown in this thread: inline Assembly.
What makes this particular program incompatible with Windows is that an operating system is free to use any of various methods to make system calls (transfer control from from user code to kernel code). The methods used by Windows and by Linux are mutually incompatible. Even if you were to rewrite that code in, say, MASM Assembly, it would still not work on Windows.
Right. Visual used to support a VERY casual inline assembly that understood some C-like things that would have been more explict in a MASM file.

actual machine code looks a lot like a stream of integers. In our assembly class for x86 we fired up an ancient program called simply 'debug' and jacked commands to the CPU from there directly to toy with some actual machine code in machine code format. I don't think 'debug' even exists anymore, you could use it to hack stuff back in 286 era.
Topic archived. No new replies allowed.