Jump to content

  • Log In with Google      Sign In   
  • Create Account


I need to analyze assembly code...


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
20 replies to this topic

#1 lride   Members   -  Reputation: 633

Like
0Likes
Like

Posted 06 October 2012 - 06:20 PM

I must analyze some assembly code..

Is there some kind of "assembly code analyzer"?
Or do i need to learn assembly? Posted Image

Edited by lride, 06 October 2012 - 06:21 PM.

An invisible text.

Sponsor:

#2 Erik Rufelt   Crossbones+   -  Reputation: 3210

Like
0Likes
Like

Posted 06 October 2012 - 06:25 PM

Kinda need to learn it.. what is the code and what is the purpose of the analysis?

#3 Bacterius   Crossbones+   -  Reputation: 8363

Like
3Likes
Like

Posted 06 October 2012 - 06:33 PM

Depends when you mean by "analyze", if you mean find out what it does, then yes, you probably need to understand the code and the computer won't help you do that. If you mean how fast it'll be, how to optimize it, etc, then there are tools available for this kind of stuff (but it doesn't hurt to understand what the assembly is doing, either).

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#4 lride   Members   -  Reputation: 633

Like
0Likes
Like

Posted 06 October 2012 - 06:45 PM

I need to know how conditional statements, loops and etc translate into assembly.
Where can I learn some assembly?
An invisible text.

#5 SiCrane   Moderators   -  Reputation: 9502

Like
0Likes
Like

Posted 06 October 2012 - 06:55 PM

If you just want to see how code in one language turns into assembly, most compilers give you an option to see generated assembly. For gcc you can use the -S switch and with MSVC you can use the /FA switch.

#6 Álvaro   Crossbones+   -  Reputation: 12580

Like
4Likes
Like

Posted 06 October 2012 - 06:55 PM

That part is not too hard. The first hit on Google for `assembly x86 tutorial' was this page, which seems reasonable. You can then ask your C or C++ compiler to generate assembly output for some simple programs and try to analyze what it's doing. If you have optimizations turned off, it should be pretty straight forward most of the time.

If you get far enough to understand how conditional statements and loops are implemented, you should make a little extra effort and understand how function calling and local variables work, since that's very informative for any programmer.

#7 lride   Members   -  Reputation: 633

Like
0Likes
Like

Posted 06 October 2012 - 07:01 PM

If you have optimizations turned off, it should be pretty straight forward most of the time.


Well, I must anaylze a code with full optimization especially in a loop, so I know what kind of loop optimization(such as interchanging, unrolling) is happening.
Is optimized assembly harder to understand?
An invisible text.

#8 Álvaro   Crossbones+   -  Reputation: 12580

Like
3Likes
Like

Posted 06 October 2012 - 07:08 PM

Start with unoptimized code until everything makes sense. Reading optimized code can be a bit of a challenge, and you should probably start with the easiest code you can get your hands on.

#9 kuramayoko10   Members   -  Reputation: 386

Like
3Likes
Like

Posted 06 October 2012 - 07:09 PM

I found the following free e-book: Programming From the Ground-Up
I believe it will cover the intel syntax, which is easier to learn.
Try to find resources on the intel developer zone as well.

I like to use objdump + gcc on Linux to have the assembly code with the C code commented in between.
> gcc -g test.c -o test.o
> objdump -dS test.o > test.asm

On Windows, Visual Studio gives you assembly code pretty easily as well: right-click on your code and select "Show Dissasembly"

Edited by kuramayoko10, 06 October 2012 - 07:09 PM.

Programming is an art. Game programming is a masterpiece!

#10 lride   Members   -  Reputation: 633

Like
0Likes
Like

Posted 06 October 2012 - 07:49 PM

I like to use objdump + gcc on Linux to have the assembly code with the C code commented in between.
> gcc -g test.c -o test.o> objdump -dS test.o > test.asm
On Windows, Visual Studio gives you assembly code pretty easily as well: right-click on your code and select "Show Dissasembly"


I did

g++ -O3 main.cpp -o main.o
objdump -dS main.o >main.asm

but I don't get c++ code commented in between.
Instead I get assembly like below instead

(excerpt)
[source lang="plain"]main.o: file format pei-i386main.o: file format pei-i386Disassembly of section .text:00401000 <___mingw_CRTStartup>: 401000: 53 push %ebx 401001: 83 ec 38 sub $0x38,%esp 401004: a1 70 40 40 00 mov 0x404070,%eax 401009: 85 c0 test %eax,%eax 40100b: 74 1c je 401029 <___mingw_CRTStartup+0x29> 40100d: c7 44 24 08 00 00 00 movl $0x0,0x8(%esp) 401014: 00 401015: c7 44 24 04 02 00 00 movl $0x2,0x4(%esp)[/source]

by the way "go to disassembly" works excellently in VC++

Edited by lride, 06 October 2012 - 07:59 PM.

An invisible text.

#11 Bacterius   Crossbones+   -  Reputation: 8363

Like
3Likes
Like

Posted 06 October 2012 - 08:00 PM

1 C++ line is not 1 assembly line, especially with optimizations enabled. There is no simple one-to-one translation between a lower level and a higher level language. You will just need to learn basic assembly, there is no way around that. You can't learn how to ride a bike by driving a car!

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#12 Álvaro   Crossbones+   -  Reputation: 12580

Like
1Likes
Like

Posted 06 October 2012 - 08:17 PM

You can get the original code interleaved with the generated assembly with this command: g++ test.cpp -c -g -O3 -Wa,-ahl=test.lst

Give it a try!

#13 lride   Members   -  Reputation: 633

Like
0Likes
Like

Posted 06 October 2012 - 08:25 PM

Thanks, it works.

I was wondering if assembly code could show if branch prediction is taking place.
An invisible text.

#14 Álvaro   Crossbones+   -  Reputation: 12580

Like
2Likes
Like

Posted 06 October 2012 - 08:27 PM

I was wondering if assembly code could show if branch prediction is taking place.


No, that doesn't make any sense. Branch prediction is a feature of the CPU, which tries to execute the code as fast as possible, but the assembly is not instrumented in any way to enable it: The CPU will do it automatically everywhere.

#15 lride   Members   -  Reputation: 633

Like
0Likes
Like

Posted 06 October 2012 - 08:33 PM

Thanks to everyone who answered my question!

so much to know for a science fair project...
An invisible text.

#16 Álvaro   Crossbones+   -  Reputation: 12580

Like
0Likes
Like

Posted 06 October 2012 - 08:39 PM

The science fair project is so you learn a lot of things, not the other way around. :)

#17 Ohforf sake   Members   -  Reputation: 1716

Like
3Likes
Like

Posted 07 October 2012 - 03:48 AM


I was wondering if assembly code could show if branch prediction is taking place.


No, that doesn't make any sense. Branch prediction is a feature of the CPU, which tries to execute the code as fast as possible, but the assembly is not instrumented in any way to enable it: The CPU will do it automatically everywhere.


Actually, this is not true for all instruction sets. Some don't have complex branch prediction mechanisms but rely on branch hinting where a special branch hint instruction has to be issued a few cycles before the branch. In those cases you can actually check in the assembly if the branch hint instruction is present and located in the right spot.

For x86 (or x86_64) however, this is not the case, as alvaro already pointed out. But all modern intel and amd CPUs have hardware counters that can be used by profilers to tell you, where branch mispredictions occure. See oProfile (Linux) or vTune and CodeAnalyst (Windows).

#18 radioteeth   Prime Members   -  Reputation: 966

Like
0Likes
Like

Posted 07 October 2012 - 12:46 PM

My two cents: grab a disassembler - a program which shows you the assembly of a compiled EXE or DLL file.. I used to modify all sorts of programs (while simultaneously gaining a bare-minimum awareness of assembly itself) by disassembling them, and editing their code directly using a hex editor - overwriting small bits of existing code by surmising the actual byte opcodes of the asm instructions I desired and writing them in by hand.. Also, writing programs that would do it all in memory (WriteProcessMemoryEx) so that the original file could be left alone while effecting the equivalent change in functionality once the target was running. That may or may not help you.

#19 Heath   Members   -  Reputation: 344

Like
0Likes
Like

Posted 07 October 2012 - 05:14 PM

I know it's not the same architecture (6502 is not x86), but I just wanted to throw this out there: Machine Language for Beginners.

Edited by Heath, 07 October 2012 - 05:15 PM.


#20 slmgc   Members   -  Reputation: 168

Like
0Likes
Like

Posted 08 October 2012 - 04:16 AM

I need to know how conditional statements, loops and etc translate into assembly.
Where can I learn some assembly?


You should definitely read Code Optimization: Effective Memory Usage by well-known code-hacker Kris Kaspersky. One of the best books on the subject you want to know.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS