This question is an exact duplicate of:
I have C++ code with asm input for working with string. How can I use them from main program in assembly?
#include <iostream>
#include <fstream>
#include <clocale>
#include <curses.h>
#define MAX_LENGTH 80
using namespace std;
void printWelcomeStr();
int main()
{
char input_string[MAX_LENGTH];
char output_string[MAX_LENGTH];
cin.getline(input_string, MAX_LENGTH, '\n');
asm
(
"lea eax, input_string\n""mov esi, %eax\n""lea eax, output_string\n""mov %edi, %eax\n""BEGINCYCLE:\n""lodsb\n""test al, al\n""je EXIT\n""cmp al, 41h\n" // >= 'A'"jnbe LatinLetterGeneral\n""jmp NEXT\n""LatinLetterGeneral:\n""cmp %al, 5Ah\n" // <= 'Z'"jnae LatinLetterFinal\n""cmp al, 61h\n" // >= 'a'"jnbe LatinLetterFinal\n""jmp NEXT\n""LatinLetterFinal:\n""cmp al, 7Ah\n" // > 'z'"ja NEXT\n""cmp ax, al\n""sub ax, 30h\n""push cx\n""mov cx, 2\n""rol dl, 4\n""mov ax, 300fh\n""and al,dl\n""aaa\n""aad 11h\n""pop cx\n""mov al, di\n""jmp NEXT\n""NEXT:\n""stosb\n""jmp BEGINCYCLE\n""EXIT:\n""stosb\n"
);
cout << "\n\nResult:"<< endl;
ofstream file_output("output.txt");
int i = 0;
while (output_string[i] != '\0')
{
cout << output_string[i];
file_output << output_string[i];
i++;
}
cout << endl;
file_output << endl;
file_output.close();
getch();
system("pause");
return 0;
}
I compile it with:
g++ -masm=intel -m32 -o main main.cpp
And get this errors:
/usr/bin/ld: /tmp/ccoToLty.o: in function `main':
main.cpp:(.text+0x110): undefined reference to `input_string'
/usr/bin/ld: lr4_comments.cpp:(.text+0x118): undefined reference to `output_string'
collect2: error: ld returned 1 exit status
Adding .intel_syntax noprefix
doesn't help, only adds some more errors. What have I do it this way? Is it connect with some correct arrangement of "%" and "[]? What's totally correct way to use data from asm?