Page 1 of 1

C > ASM

PostPosted: Sat Dec 27, 2014 4:22 pm
by RatkinHHK
http://gcc.godbolt.org/

Just stumbled across it. You might know about it. Might be pointless. I find it useful. It is a C to Assembly converter.
Over and out!

Re: C > ASM

PostPosted: Sat Dec 27, 2014 6:34 pm
by WallShadow
Code: Select all
#include <string>
#include <iostream>

using std::string;
using std::cout;
using std::endl;

int main() {
  string s = "hello world!";
  cout << s << endl;
  return 0;
}

Code: Select all
<No output: generated assembly was too large (762804 > 500000 bytes)>

T.T

still needs a little work

Re: C > ASM

PostPosted: Sat Dec 27, 2014 6:47 pm
by RatkinHHK
This makes me wonder, how the hell you program something entirely in assembly. If your job is to program something in assembly, wont you starve to death until you get your pay?

I seems strange though. You just print a short string. How is the assembly to big?


EDIT:

Works if you change the compiler. Try with g++ 4.4 for example. The default one is "Ubuntu clang 3.0.6" which perhaps is a C compiler. And as far as I know, what you did was C++. I need someone to back me up or tell me to shut the f**k up. Whichever seems more intuitive or morally correct.

Also, are we allowed to swear around here. I am not vulgar by nature, but sometimes a couple of f**ks here and there make it seem like you mean it.

Re: C > ASM

PostPosted: Sat Dec 27, 2014 7:24 pm
by QtDevl
RatkinHHK wrote:Works if you change the compiler.

You are correct, he should use gcc ( the latest or w/e )
Both cout AND std ( the standard library ) are c++.
Pure c would be something like
Code: Select all
#include <stdio.h>

int main() {
printf("Hello World");
}


EDIT:
RatkinHHK wrote:This makes me wonder, how the hell you program something entirely in assembly.

I've programmed in asm for more than 5 years. I really liked the freedom and flexibility you had. I didn't know any other languages at the time, I've just started writing in codecaves of existing programs, then I've moved on to tasm ( which is almost pure asm ), then to MASM which is a tad more higher level than pure assembly. I didn't have any problems nor did I feel restricted at any time while programing in asm, and managed to do pretty much everything I'd set myself to. Moving on, for a while I've tried writing C, compiling it and use that when I wanted to inject some code or something ( mainly stuff for unpackers) but while a bit faster it wasn't all that super great, I still pretty much prefer my ollydbg scripts. And if I don't do that I'll simply just write what I need in a higher level language, so that's that. I don't think you'd program in pure asm anything as of now, apart when programing a PLC or something( maybe shell code too, but most of the times they are simply compiled c code ), I don't think there is the need for pure ASM.

Re: C > ASM

PostPosted: Sun Dec 28, 2014 2:29 am
by e3cb
RatkinHHK wrote:This makes me wonder, how the hell you program something entirely in assembly.


Writing any 'meaningful' program in assembly is pure autism. It's generally only done where there are major restraints on the environment, but even then, C is very often used due to the fact that compilers have become fucking brilliant since their inception. The only thing I 'write' in ASM is shellcode, and typically only the first stage, second/third stage shit is still gonna be in C or some derivative. (Look at meterpreter if you're interested, the DLL is written in C.) Hell, even working in realtime systems I never use ASM. Why? Because one prefers portability over a few cycles saved. Hell, most optimizations done in inline asm can be done with clever C code. So, long story short, no real need to write it well, just a need to read it really well.

QtDevl wrote:ollydbg scripts


Come on mang, move on to immunity or IDA!

Re: C > ASM

PostPosted: Sun Dec 28, 2014 6:50 am
by QtDevl
e3cb wrote:Come on mang, move on to immunity or IDA!

I use IDA for analysis and mostly when writing keygens, but if you work with packers in IDA you're gonna have a bad time.

Re: C > ASM

PostPosted: Sun Dec 28, 2014 7:44 am
by ghost107
RatkinHHK wrote:This makes me wonder, how the hell you program something entirely in assembly. If your job is to program something in assembly, wont you starve to death until you get your pay?

Antivirus companies, one of their requirements is knowing Assembly.

ASM is used to access certain parts of the Hardware where you don't have full access. For most MCU platforms(PIC, AVR, ARM, etc), it is hard to find a high level language compiler(MicroC, has most of the support for PIC).

For instance in game programming, before Shader language was invented, developers wrote shades in ASM(Shaders are programs that run on the GPU).
Code: Select all
vertexshader vs = asm { /*assembly instructions go here */ };
pixelshader ps = asm { /*assembly instructions go here */ };


RatkinHHK wrote:I seems strange though. You just print a short string. How is the assembly to big?

To print something you only need the address(offset) of the function that prints, and to push to the stack the parameters of that function;

A simple example of a method in ASM:
Code: Select all
void PrintFunction(const char* param){
   printf(param);
}

In 32 bit asm would be(I use Intel Syntax for AT&T just reverse the values and put a % before, for 64 bit instead of E use R, and the address size is 64bit not 32bit) :
push EBP            ;save base pointer
mov EBP, ESP     ; make ESP the new base pointer
;Code goes here
push [EBP-4]      ; push parameters to stack
call printf          ; call the function to print
; Code ends here, if the method does not return nothing EAX must be 0, else the return value
mov ESP, EBP     ;restore the Stack pointer
pop EBP             ; restore the base pointer
ret

But the code can be optimized for more speed, by removing unnecessary instruction lines.

Re: C > ASM

PostPosted: Sun Dec 28, 2014 7:16 pm
by tgoe
fyi RatkinHHK, you can do this yourself locally of course.
Code: Select all
$ cc -S prog.c


---
Yikes, just DoSed myself looking for a vuln. Lookout for that auto processed code from localstorage. :)