Go Back   AnandTech Forums > Software > Programming

Forums
· Hardware and Technology
· CPUs and Overclocking
· Motherboards
· Video Cards and Graphics
· Memory and Storage
· Power Supplies
· Cases & Cooling
· SFF, Notebooks, Pre-Built/Barebones PCs
· Networking
· Peripherals
· General Hardware
· Highly Technical
· Computer Help
· Home Theater PCs
· Consumer Electronics
· Digital and Video Cameras
· Mobile Devices & Gadgets
· Audio/Video & Home Theater
· Software
· Software for Windows
· All Things Apple
· *nix Software
· Operating Systems
· Programming
· PC Gaming
· Console Gaming
· Distributed Computing
· Security
· Social
· Off Topic
· Politics and News
· Discussion Club
· Love and Relationships
· The Garage
· Health and Fitness
· Merchandise and Shopping
· For Sale/Trade
· Hot Deals with Free Stuff/Contests
· Black Friday 2014
· Forum Issues
· Technical Forum Issues
· Personal Forum Issues
· Suggestion Box
· Moderator Resources
· Moderator Discussions
   

Reply
 
Thread Tools
Old 11-03-2012, 10:24 PM   #1
Danimal1209
Senior Member
 
Join Date: Nov 2011
Posts: 346
Default assembly code

So, I understand registers, base/stack pointers, and the instructions. I can sort of walk through a program and somewhat tell what it is doing. But I have a terrible time translating this in to another language, like C.

Any advice?
Also, I have been using GDB to debug, is there a better debugger that will disassemble?
Danimal1209 is offline   Reply With Quote
Old 11-03-2012, 10:27 PM   #2
Train
Lifer
 
Join Date: Jun 2000
Posts: 13,071
Default

Might be better to start looking at it from the other direction. Write a simple method in C, compile it, and look at it in an Assembler. Sometimes the stuff the compiler comes up with will surprise you, and totally give you a different perspective.

Try a recursive function, and watch how the assembly uses a stack to trace its steps up and down the function calls.
__________________
.
Train is offline   Reply With Quote
Old 11-03-2012, 10:47 PM   #3
degibson
Golden Member
 
degibson's Avatar
 
Join Date: Mar 2008
Posts: 1,389
Default

Write more C; over time, you'll start to see the assembly pretty easily through the veneer of C, but only practice will get you there.
degibson is offline   Reply With Quote
Old 11-03-2012, 11:25 PM   #4
Ken g6
Elite Member
 
Ken g6's Avatar
 
Join Date: Dec 1999
Location: Colorado
Posts: 8,845
Default

Try compiling your C code with gcc -S. That outputs the assembly code equivalent.
__________________
Look for really BIG primes with TeAm AnandTech PrimeGrid!
Next race: November 19-24
Ken g6 is offline   Reply With Quote
Old 11-04-2012, 02:10 AM   #5
Kromis
Diamond Member
 
Kromis's Avatar
 
Join Date: Mar 2006
Location: San Gabriel, California
Posts: 5,211
Default

*Shudder* Assembly...that stuff gave me nightmares, yo! I never learned C either, it was just killing me.
__________________
Rig Heat
Quote:
Originally posted by: Toastedlightly
shes shorter than my c*ck.

Does she work in a circus? Because I've never seen a 2 inch woman before.
AnandTech Moderator
Kromis is offline   Reply With Quote
Old 11-04-2012, 02:50 AM   #6
lambchops511
Senior Member
 
Join Date: Apr 2005
Posts: 659
Default

Learning assembly is useful for specific optimization (think SSE or other vector instructions if you don't like x86-64). If you need to deal with base/stack pointers you are likely coding in the wrong decade.
lambchops511 is offline   Reply With Quote
Old 11-04-2012, 10:28 AM   #7
degibson
Golden Member
 
degibson's Avatar
 
Join Date: Mar 2008
Posts: 1,389
Default

Quote:
Originally Posted by lambchops511 View Post
Learning assembly is useful for specific optimization (think SSE or other vector instructions if you don't like x86-64). If you need to deal with base/stack pointers you are likely coding in the wrong decade.
Don't let the naive dissuade you, OP:
- There's a huge value in understanding the machine at the level of base and stack pointers;
- Similarly large value in understanding how high-level languages map to assembly-level constructs;
- Not all platforms afford the luxury of high-level execution environments;
- Programmers write compilers, don't they?
degibson is offline   Reply With Quote
Old 11-04-2012, 06:54 PM   #8
ObscureCaucasian
Diamond Member
 
ObscureCaucasian's Avatar
 
Join Date: Jul 2006
Location: Raleigh, NC
Posts: 3,934
Default

Quote:
Originally Posted by degibson View Post
Don't let the naive dissuade you, OP:
- There's a huge value in understanding the machine at the level of base and stack pointers;
- Similarly large value in understanding how high-level languages map to assembly-level constructs;
- Not all platforms afford the luxury of high-level execution environments;
- Programmers write compilers, don't they?
I very much agree, I personally think that a knowledge of how the code you write will be translated into assembly (at least at some level) is critical. Then again the higher-level language you work in, the less critical this may be, however I love embedded/systems programming, so I can't imagine _not_ understanding code at this level.
ObscureCaucasian is offline   Reply With Quote
Old 11-04-2012, 08:58 PM   #9
exdeath
Lifer
 
exdeath's Avatar
 
Join Date: Jan 2004
Posts: 13,473
Default

Use a IDE that shows interleaved C and corresponding assembly output during debug tracing/stepping. Disable optimizations. Have fun discovering that there are really only a handful of constructs that keep occurring over and over again in assembly (conditionals, for loops, do/while loops, etc).

When you work in pure assembly, you'll naturally learn to comment everything and all the comments end up actually looking like an entire pseudo C program.

My current platform of interest is the 6502 / NES. Very unique and challenging platform.

Some excerpts:

http://forums.anandtech.com/showthread.php?t=2271963

Last edited by exdeath; 11-04-2012 at 09:03 PM.
exdeath is offline   Reply With Quote
Old 11-04-2012, 09:06 PM   #10
exdeath
Lifer
 
exdeath's Avatar
 
Join Date: Jan 2004
Posts: 13,473
Default

Quote:
Originally Posted by degibson View Post
Don't let the naive dissuade you, OP:
- There's a huge value in understanding the machine at the level of base and stack pointers;
- Similarly large value in understanding how high-level languages map to assembly-level constructs;
- Not all platforms afford the luxury of high-level execution environments;
- Programmers write compilers, don't they?
WHAT? You mean I can't have Java on a system with 2k of RAM?! What do you mean I need 2 GB RAM just to write "Hello World!" ?
exdeath is offline   Reply With Quote
Old 11-05-2012, 07:24 PM   #11
degibson
Golden Member
 
degibson's Avatar
 
Join Date: Mar 2008
Posts: 1,389
Default

2k will get you just to your HelloWorldFactory
But in 4k, you can do this: http://www.nullterminator.net/java/sub4k/

Last edited by degibson; 11-05-2012 at 08:05 PM.
degibson is offline   Reply With Quote
Old 11-10-2012, 06:18 PM   #12
Markbnj
Moderator
Programming
 
Markbnj's Avatar
 
Join Date: Sep 2005
Posts: 12,887
Default

Quote:
Originally Posted by degibson View Post
Don't let the naive dissuade you, OP:
- There's a huge value in understanding the machine at the level of base and stack pointers;
- Similarly large value in understanding how high-level languages map to assembly-level constructs;
- Not all platforms afford the luxury of high-level execution environments;
- Programmers write compilers, don't they?
Major-league agree with this.
__________________
Everytime I try to tell you, the words just come out wrong

**
Some meaningless scribbling of no account

The 4th Realm

Arts and Letters Daily - Get some culture
Markbnj is offline   Reply With Quote
Old 11-11-2012, 08:06 AM   #13
Cogman
Diamond Member
 
Cogman's Avatar
 
Join Date: Sep 2000
Location: A nomadic herd of wild fainting goats
Posts: 9,806
Default

Quote:
Originally Posted by exdeath View Post
WHAT? You mean I can't have Java on a system with 2k of RAM?! What do you mean I need 2 GB RAM just to write "Hello World!" ?
Come now, Java isn't THAT bad. I mean, you should have a system with at least 64mb of memory before trying to do anything with it... but it isn't THAT bad. (Though, come to think of it, our current java solution will eat up around 8gb of ram with everything running and doing nothing... Yeah, not great, but at least I can delude myself into believing that it is a huge system).
__________________
CogBlog - Random Babblings of Cogman mainly focused on software.
Cogman is offline   Reply With Quote
Old 11-11-2012, 08:40 PM   #14
postmortemIA
Diamond Member
 
postmortemIA's Avatar
 
Join Date: Jul 2006
Location: Midwest USA
Posts: 6,510
Default

I wouldn't want to read assembly code of i386 CPUs, compile it with gcc for PowerPC, which has much simpler instruction set.
__________________
D1. Win7 x64 i7-3770 on Z77, HD7850, 2707WFP, 840, X-Fi D2. Win7 x64 E8400 on P35
L1. OSX 10.9 rMBP 13 L2. Vista x86 E1505
M. Galaxy S4

postmortemIA is online now   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 06:52 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.