0

I am trying to convert a sample c++ code to .asm (intel fornat for x86) in windows with g++(installed by MinGW). My C++ code is

#include<iostream>  
using namespace std; 
int main() 
{ 
    cout<<"Hello World";
    return 0; 
}

The following prints hello world in emu8086(intel based x86 processor is been used) and this file is saved in .asm format

; print "hello world"
; first byte is ascii code, second byte is color code.
mov [02h], 'H'
mov [04h], 'e'
mov [06h], 'l'
mov [08h], 'l'
mov [0ah], 'o'
mov [0ch], ','
mov [0eh], 'W'
mov [10h], 'o'
mov [12h], 'r'
mov [14h], 'l'
mov [16h], 'd'
mov [18h], '!'
 
; wait for any key press:
mov ah, 0
int 16h
ret

i want to generate a code similar to the above(in a .asm file) with g++,I am using windows10 with minGW for g++.

What i tried

g++ -S hello.cpp

but this generates a .s file, is there any way to convert the hello.cpp file to .asm using g++ on windows.

EDIT: More Things I have Tried that didn't work

g++ -o abc.asm hello. cpp

This generated a file abc.asm which i can run from cmd with command abc.asm but the file has no human readable code/text in it(i want the code/text in intel syntax assembly)

I also tried

g++ -S -masm=intel hello.cpp

but this also didn't work.

I am a beginner to g++ so please ignore any thing wrong i did.

huzaifa99
  • 41
  • 7
  • Does this answer your question? [How do you get assembler output from C/C++ source in gcc?](https://stackoverflow.com/questions/137038/how-do-you-get-assembler-output-from-c-c-source-in-gcc) – ChrisMM Jul 24 '20 at 14:01
  • I guess `g++` emits [`gas` code](https://en.wikipedia.org/wiki/GNU_Assembler). If you prefer MASM then try with VC++. FYI: [SO: How to view the assembly behind the code using Visual C++?](https://stackoverflow.com/a/1020515/7478597) – Scheff's Cat Jul 24 '20 at 14:02
  • 1
    `.s` is just a file extension. Use `-o` to specify the output file. – ChrisMM Jul 24 '20 at 14:02
  • @ChrisMM i followed up to your suggestion of using - o, i used it like this ```g++ -o abc.asm hello.cpp``` this gave me abc.asm file with weird symbols which are not required. I also followed up to your suggested link but that also didn't solve my problem, any help is appreciated. – huzaifa99 Jul 24 '20 at 15:45
  • What do you mean by "weird symbols" – ChrisMM Jul 24 '20 at 15:53
  • @ChrisMM like these ```L ( .text å , à 0`.data ``` but i want the code for x86 to be generated in .asm file format,i will edit the question as such. – huzaifa99 Jul 24 '20 at 17:02
  • The format you're looking for, you're not going to get anywhere, unfortunately. No compiler would ever generate the example you gave, since its horribly inefficient. Also, interrupts aren't used like that. Instead, simple calls to library functions are used instead. – ChrisMM Jul 24 '20 at 19:36

0 Answers0