# Using clang to learn ASM?



## sgeos (Dec 26, 2013)

Years ago when I was trying to learn ARM ASM, I tried to use gcc as a learning tool. The ASM syntax generated by gcc was really weird so I gave up. Looking back, giving up on gcc may have been premature.

In the modern world, could clang to be used to learn and compile ASM? Preliminary testing indicates that the following works:

Makefile

```
test : test.s
        clang -o test test.s

test.s : test.c
        clang -S test.c

clean :
        rm -f test.s test
```

test.c

```
#include "stdio.h"

int main(int argc, char **argv)
{
  printf("Hello world!\n");
  return 0;
}
```

I have never used x86 ASM, so I can not tell if the generated .s file is readable or not.


----------



## Beastie (Dec 26, 2013)

sgeos said:
			
		

> The ASM syntax generated by gcc was really weird so I gave up. Looking back, giving up on gcc may have been premature.


The AT&T syntax is atrocious!

Good thing you can generate Intel syntax assembly with both compilers:

`gcc -S -masm=intel helloworld.c`
`clang -S -mllvm --x86-asm-syntax=intel helloworld.c`
:beer


----------



## sgeos (Dec 28, 2013)

Beastie said:
			
		

> Good thing you can generate Intel syntax assembly with both compilers:
> 
> `gcc -S -masm=intel helloworld.c`
> `clang -S -mllvm --x86-asm-syntax=intel helloworld.c`
> :beer


How can I compile the generated Intel syntax .s file into an .o file or an executable? I get errors like "unexpected token type" when I try. Are there similar options to generate ARM ASM in a syntax that is not atrocious?


----------



## sossego (Dec 28, 2013)

@Beastie, how does one go about compiling code that will run commands or an interactive program?


----------



## sossego (Dec 28, 2013)

I want to run a program within a .c or .cpp file while generating code. The purpose would be to have the program check the syntax of the code and auto-correct it by referencing to local libraries that are standard for the given system.


----------



## lyuts (Jan 22, 2014)

sossego said:
			
		

> I want to run a program within a .c or .cpp file while generating code. The purpose would be to have the program check the syntax of the code and auto-correct it by referencing to local libraries that are standard for the given system.



Can you elaborate on your use case? I think it is confusing right now(at least for me).



> program check the syntax of the code



By the time the program is ran there is no source code, the program itself is binary.



> auto-correct it by referencing to local libraries that are standard for the given system


This is usually acomplished by the build tool you are using to compile your program, not by the program itself. For instance, let's say your program uses xml library, which on system A is called libxml and on system B is called libQtXml, then in order to run your program on system B you compile your program specifying that it needs to link against libQtXml.

But your clarifications would be useful to help you further.


----------



## throAU (Jan 22, 2014)

2c - learning assembler by reading the intermediate code generated by a C compiler will be like learning Greek via Google Translate.

Would suggest getting a copy of NASM or some other assembler, a few books on x86 assembly, and learning it that way.


----------

