C > ASM

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

C > ASM

Post by RatkinHHK on Sat Dec 27, 2014 4:22 pm
([msg=86043]see C > ASM[/msg])

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!
"You have enemies? Good. That means you've stood up for something, sometime in your life." --- Winston Churchill
User avatar
RatkinHHK
Experienced User
Experienced User
 
Posts: 72
Joined: Tue Nov 11, 2014 6:06 pm
Blog: View Blog (0)


Re: C > ASM

Post by WallShadow on Sat Dec 27, 2014 6:34 pm
([msg=86045]see Re: C > ASM[/msg])

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
User avatar
WallShadow
Contributor
Contributor
 
Posts: 686
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: C > ASM

Post by RatkinHHK on Sat Dec 27, 2014 6:47 pm
([msg=86047]see Re: C > ASM[/msg])

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.
"You have enemies? Good. That means you've stood up for something, sometime in your life." --- Winston Churchill
User avatar
RatkinHHK
Experienced User
Experienced User
 
Posts: 72
Joined: Tue Nov 11, 2014 6:06 pm
Blog: View Blog (0)


Re: C > ASM

Post by QtDevl on Sat Dec 27, 2014 7:24 pm
([msg=86048]see Re: C > ASM[/msg])

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.
If freedom is outlawed, only outlaws will have freedom...
QtDevl
New User
New User
 
Posts: 40
Joined: Sat May 17, 2008 3:50 pm
Location: my own world
Blog: View Blog (0)


Re: C > ASM

Post by e3cb on Sun Dec 28, 2014 2:29 am
([msg=86055]see Re: C > ASM[/msg])

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!
<3 FF E4 <3
Do you even asm bruh?
User avatar
e3cb
Poster
Poster
 
Posts: 104
Joined: Fri Feb 15, 2013 11:32 pm
Location: Orange County
Blog: View Blog (0)


Re: C > ASM

Post by QtDevl on Sun Dec 28, 2014 6:50 am
([msg=86062]see Re: C > ASM[/msg])

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.
If freedom is outlawed, only outlaws will have freedom...
QtDevl
New User
New User
 
Posts: 40
Joined: Sat May 17, 2008 3:50 pm
Location: my own world
Blog: View Blog (0)


Re: C > ASM

Post by ghost107 on Sun Dec 28, 2014 7:44 am
([msg=86064]see Re: C > ASM[/msg])

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.
User avatar
ghost107
Poster
Poster
 
Posts: 321
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)


Re: C > ASM

Post by tgoe on Sun Dec 28, 2014 7:16 pm
([msg=86076]see Re: C > ASM[/msg])

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. :)
User avatar
tgoe
Contributor
Contributor
 
Posts: 718
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests