简体   繁体   中英

How can learning assembly language help me in Debugging C programs?

I try writing simple applications and whenever any error occurs, will try to debug and understand what is causing the error.

As stated in the title how can learning assembly language help me in debugging C programs? Also what all topics of assembly language would be necessary for the same.

PS : I am not interested to master assembly language but considering to learn the same to understand the process better.

PPS : Links to the topics would be great.

Thanks!

Honestly, it won't. That is, learning assembly will do little to help you to specifically debug your C code (except in a few special cases, like micro controller/embedded systems projects). If you already know assembly, it can help, but even then, in my experience working with both C and assembly, and some really powerful debuggers, it seems to me to be inefficient to debug code that way.

That said, I would absolutely encourage anyone to explore assembly and learn as much as possible about it, and even write some assembly code. It will provide other critical insights about how programs work at a much lower level. And, in some cases, it can even be fun.

As for debugging, tools like gdb for Linux and the visual studio debugger for Windows should be your first line of defense when debugging your C code. I have rarely ever opened my assembly files specifically in an attempt to track down bugs in my C code. I more often open it out of curiosity.

If you are interested in learning about assembly in general, there is plenty of material that is only a short Google search away, and a variety of good books on the subject. One book I used in school (and still use) was Computer Systems: A Programmer's Perspective , which did not cover much assembly, but will teach you about low level computing from the point of view of the programmer. It will explain how we, as programmers, can write code with low level computing in mind, which I think is at the heart of your question.

In a nutshell: I encourage anyone to explore assembly, but I would advise using other tools for debugging before trying to debug C code by looking at assembly.

Having a knowledge of assmebly helps you to undertsand what is going on inside your program. especially bufferoverruns can be hard to track down and in worst case the application breaks somewhere totaly unrelated and you have no idea how it got there. It also helps if the compiler has a bug and you don't know why your coding is not doing what it should.

A simple approach would be to write a small and simple C program and let the compiler generate the assembly file for you. Then you can take a look at the actual code generated without the hassle of getting to understand all the ugly details of segment setup and declaring data.

For example:

 main()
 {
     int a = 3,
     int b = 5;
     int c = 0;
     c = a+b;

     printf("%d\n", c);
 }

A simple program and you know exactly what it should do. Looking at the generated code can give you an understanding on how it works and you can continue on to more complex stuff, ie using pointers or such.

The instructions involved are not so complicated and could be easily understood without a deep knowledge of assembly.

I am not sure what exactly you want.But ya below link might be useful for you

http://cs.smith.edu/~thiebaut/ArtOfAssembly/artofasm.html

I agree that a knowledge of assembler can help you debug c programs. I would advise you to do two things. Feel free to mix and mingle :-)

FIRST: Get an introductory book about x86 (IA-32) assembly language. There are several good books available like:

"Guide to Assembly Language Programing in Linux"

"Assembly Language for x86 Processors"

Buy it or check it out at your library (libgen/bookfi). I read the "Guide to Assembly Language Programing in Linux" and it is a good start. Tough I would prefer to have read "Assembly Language for x86 Processors" in retrospect. Yes, these are quite large books but they also have a long introduction you you may (want to) skip and you don't have to read the books to the end just to get an introduction to assembler.

SECOND: Write simple c programs and compile them to assembler with gcc -S and use gdb to step through them and try to understand what they do. If you cannot figure out what an instruction will do google the instruction and you will find plenty of tutorials and commented code that will help you.

And don't get discouraged if you don't understand everything as fast as you wished. Now, you got an introduction to assembler and it's time to step up (the game/to the plate/ whatever). Try for example to follow (ie run & debug code in brain/machine) these posts:

http://www.phrack.org/issues.html?issue=49&id=14#article This is the widely acknowledged "Smashing The Stack For Fun And Profit" by Aleph One. You will find lots of relics posted on this site relating to this article ;-) but beware: use gcc -fno-stack-protector if you want to try this yourself at home.

https://blogs.oracle.com/ksplice/entry/hello_from_a_libc_free This is not really an article about assembly language but it is right to the point of your question. It uses an understanding of assembler and other tools to create (debug) a piece of code and it is very detailed and it will give you further hints to what skills are useful for debugging.

You will see now that not only a knowledge of assembler is useful but also a deeper understanding of stack and heap mechanics (as well as debugging tools, d'oh!) are necessary to become a good debugger.

I hope this finds you well. And as always --- hack responsibly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM