How do you output assembly with gcc?

Dec 14, 2012 at 10:46am
closed account (iw0XoG1T)
This question really is can you output assembly for a C or C++ program using gcc?

And if so what dialect is it?

If it can be done with gcc could you give me a demonstration of what you enter in the terminal/command prompt?

If it cannot be done how are others looking at the assembly produced by a C or C++ programs? In particularly what are they using and where can I get it?
Dec 14, 2012 at 11:15am
The switch is -S

g++ -S sourceFile.cpp
Dec 14, 2012 at 11:35am
closed account (iw0XoG1T)
@Moschops thankyou

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
	.file	"main.c"
	.def	___main;	.scl	2;	.type	32;	.endef
	.section .rdata,"dr"
LC0:
	.ascii "hello world!\0"
	.text
	.globl	_main
	.def	_main;	.scl	2;	.type	32;	.endef
_main:
LFB6:
	.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
	.cfi_restore 5
	.cfi_def_cfa 4, 4
	ret
	.cfi_endproc
LFE6:
	.def	_printf;	.scl	2;	.type	32;	.endef
 


Would anyone know by looking what dialect of assembly this is?
Dec 14, 2012 at 11:49am
It's x86 assembly, AT&T syntax.

To convert into normal x86 syntax:
1. Remove the % from in front of the registers: movl $LC0, (%esp) => movl $LC0, (esp)
2. Remove the $ from in front of constants: movl $LC0, (esp) => movl LC0, (esp)
3. Change () to []: movl LC0, (esp) => movl LC0, [esp]
4. Reverse the order of operands: movl LC0, [esp] => movl [esp], LC0
5. Convert instruction size suffixes into prefixes: mov [esp], dword LC0.

GNU as uses AT&T syntax for compatibility with the original UNIX as. It can assemble normal (Intel) syntax code if you put the directives
1
2
.intel_syntax
.noprefix

before the code in Intel syntax, but gcc can't produce Intel syntax code, so if you're using embedded assembly in your C code, you either have to write in AT&T syntax, or set it to use AT&T syntax after a block of Intel syntax code with .att_syntax.
Dec 14, 2012 at 12:06pm
closed account (iw0XoG1T)
Thank you chrismane but that is not what I am trying to do.

What I am trying to do is learn to do some simple programming in assembly.

But all I have been able to find for references is either old, or manuals. So I wanted to learn how to create some simple assembly and translate it to nasm. If I can do that I think I will have enough information to use the old references and make them work for me.

This tread is been very useful so far and any additional help would be appreciated.
Last edited on Dec 14, 2012 at 12:07pm
Dec 14, 2012 at 12:45pm
NASM uses x86 assembly as well, it just uses Intel syntax instead of AT&T. I just told you how to convert from AT&T (GNU as) syntax to Intel (NASM) syntax.
Dec 14, 2012 at 12:56pm
closed account (iw0XoG1T)
@Chrisname
Just so you know I am pretty sure you saved me about two hours of googling and reading. So thanks again.
Last edited on Dec 14, 2012 at 1:53pm
Dec 14, 2012 at 1:48pm
closed account (iw0XoG1T)
in the effort to make this thread complete here is my hello world nasm program:

1
2
3
4
5
6
7
8
9
10
11
12
13
global _main
extern _printf
section .data 
    fmt_string: db 'hello, world', 0xA, 0

section .text
    _main:
    sub esp, 4
    lea eax, [fmt_string]
    mov [esp], eax
    call _printf
    add esp, 4
    ret


command prompt
1
2
nasm -felf32 main.asm
gcc main.o -otest2


gcc version 4.7.2 (GCC) (MinGW)
NASM version 2.10.06



Now I have a lot of reading to do.
Last edited on Dec 14, 2012 at 2:10pm
Dec 14, 2012 at 2:26pm
Did it work?
Dec 14, 2012 at 2:29pm
closed account (iw0XoG1T)
yes
Topic archived. No new replies allowed.