Click here to Skip to main content
15,880,364 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<stdio.h>

int main(){
	printf("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}

i tried to compile above C++ code under windows using g++ -S test1.cpp -o test1.o to get the assembly. but it didn't look like an assembly like i used to use in emu8086. it looked like this:
XML
	.file	"test1.cpp"
	.def	___main;	.scl	2;	.type	32;	.endef
	.section .rdata,"dr"
LC0:
	.ascii "ABCDEFGHIJKLMNOPQRSTUVWXYZ\0"
	.text
	.globl	_main
	.def	_main;	.scl	2;	.type	32;	.endef
_main:
LFB7:
	.cfi_startproc
	pushl	%ebp
	.cfi_def_cfa_offset 8
	.cfi_offset 5, -8
	movl	%esp, %ebp
	.cfi_def_cfa_register 5
	andl	$-16, %esp
	subl	$16, %esp
	call	___main
	movl	$LC0, (%esp)
	call	_printf
	movl	$0, %eax
	leave
...................................blablablabla


I used to write assembly in emu8086 like this:

ORG 100H
TDATA: JMP PROCESS
        KAL DB 'TEXTTEXTTEXTTEXT',10,13,'$'
PROCESS: MOV CX,11H
        XOR BX,BX
        LEA DX,KAL
PRINT: MOV AH,09H
       INT 21H 
       INC BX
       CMP BX,CX
       LOOP PRINT
       INT 20H
END TDATA     


any idea will be appreciated.
Thanks
--Rudy
Posted
Updated 10-Dec-21 23:47pm

That's because it's compiler output - and it doesn't know what the code means! So it uses it's "generic" parameter passing mechanism - via the stack - to hand the const string to a function which can do the printing instead of "knowing" what you are trying to do and just producing code for that.

To be honest compiler output is not a good start for any assembly project, it's never as good as a competent hand coder. I wouldn't use it for learning material at all! :laugh:
 
Share this answer
 
Comments
rudy-peto 26-Apr-15 10:41am    
So, The point is I have to code it manually? There's no other way?

Anyway, Thanks
--Rudy
OriginalGriff 26-Apr-15 11:10am    
I would - you won't get "good code" from a compiler!
Frankie-C 26-Apr-15 13:15pm    
While is not a real option to use a compiler to generate assembler to reuse the point is that you need a DOS compiler you can't use GCC!
See my other solution...
EMU8086 is an emulator, assembler,...etc. for 8086 16bits code only. It emulates old 16 bits DOS OS. Specifically the code you used starting with an ORiGin metainstruction denotes the writing of a binary '.com' executable (by default located at adrress 0x100). To print out characters you use DOS interrupt calls (INT 21H).
Actual Os's are 32/64 bits they use a different memory layout, and are normally located at 0x40000, but can be reorganized by linker. In actual code the program will start normally from runtime library (i.e. MSVCrt.lib/MSVCrt.dll) that then call your entry point (main, wmain, WinMain, etc. depending on subsystem choosed in the linker). In actual programs 'printf' is a library function, not a a DOS call, so the compiler generates code to call it.
In modern OS's there is a standard sequence of instructions to save actual stack pointer and allocate space for local variables (on top of stack) called 'preamble'. The preamble is what seems so strange to you...
Why I made this so complicated? Because nowadays is it a little bit more complicate than the old DOS days :(.
The assembler you got from gcc is for a 32bit PE executable, not for 8086 16bits old DOS :).
Moreover your assembly (as supported by MS MASM) is INTEL syntax, while GCC use AT&T assembly syntax that is the deafult GAS (GNU ASsembler) format. See here[^] for AT&T syntax.
Look here[^], you'll find a llot of tutorials. Just after reading the first you'll get surprisingly a better understood of the assembler you got from GCC ;).

P.S. Try adding '-masm=intel' to you g++ command line it should emit assembler in INTEL syntax.
 
Share this answer
 
v7
If you really want use a compiler to generate assembler listing to reuse in your code you have to understand that you need a DOS C compiler, not a 32/64bits compiler like GCC.
If you really want go on download the free version of TURBOC 2.01 that is widly available around. You'll find it here[^], or here[^], or here[^]...
For C++ you can download TurboC++ 1.00 or TurboC++ 3.00 from here[^].
You can also use a Delorie derived (DJGPP) or a Digital Mars or a Watcom old version (just google around)...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900