views:

452

answers:

10

I've been programming for around... 6->8 years, and I've begun to realize that I don't really know what really happens at the low-ish level when I do something like

int i = j%348

The thing is, I know what j%348 does, it divides j by 348 and finds the remainder. What I don't know is HOW the computer does this.

Similarly, I know that

try
{
 blah();
}catch(Exception e){
 blah2();
}

will invoke blah and if blah throws, it will invoke blah2... however, I have no idea how the computer does this instead of err... crashing or ending execution.

And I figure that in order for me to get "better" at programming, I should probably know what my code is really doing. [This would probably also help me optimize and... err... not do stupid things]

I figure that what I'm asking for is probably something huge taught in universities or something, but to be honest, if I could learn a little, I would be happy.

The point of the question is:
What topic/computer-science-course am I asking about? Because in all honesty, I don't know.

Since I don't know what the topic is called, I'm unable to actually find a book or online resource to learn about the topic, so I'm sort of stuck. I'd be eternally thankful if someone helped me =/

+1  A: 

The first part might be considered computer engineering, but the second is just language design.

Ignacio Vazquez-Abrams
A: 

Its a field of Language Recognition..

you should look at the ANTLR to understand the language recoginition.

ANTLR, ANother Tool for Language Recognition, is a language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions containing actions in a variety of target languages. ANTLR provides excellent support for tree construction, tree walking, translation, error recovery, and error reporting.

Ramesh Vel
+11  A: 

I would say the first part is computer architecture, while the second part is programming language.

Some good books on computer architecture, if you are interested in understanding a bit more on how the computer executes a program are:

I'm not sure what to recommend for understanding programming language constructs such as catching exceptions. Probably a good compilers book.

Especially with your second example, different programming languages might be implemented very differently. For example, a language running on a virtual machine such as Java, would have the virtual machine to protect it and throw certain types of exceptions, while in C++ this would be handled differently.

Avi
+1 for The Elements of Computing Systems. That book is amazing!
Dinah
+1  A: 

I think you are looking at how a compiler translates a high-level language code to machine instructions. Have a look at compiler design. This is a classic book.

kgiannakakis
WOAH! I recognize the cover! I know my dad has that book! When I was a kid I used to look at the book's cover and think it was a fairy tale book ._.
ItzWarty
@Itz - +1 for having thought the dragon book was a fairytale :)
Cam
@Itz +1 for having a dad who has the dragon book.
Yaser Sulaiman
+1  A: 

Sounds vaguely like you're talking about compiler construction and language design.

The (most) general "CS thing" that can implement exceptions is probably continuations (as found in, among other things, Scheme). If you haven't read thouhg "Structure and Interpretation of Computer Programs" (SICP, Web page here, including full text) it may be worth giving a quick go-through, it touches lightly on compiler construction.

Vatine
+6  A: 

You should look into assembly first, and then get into compiler design. If you don't know assembly you'll be completely lost with compiler design. I'm personally just starting with assembly, for the exact reason you are - I want to understand what my code is doing at a lower level.

I found this resource, which is very cool: http://en.wikibooks.org/wiki/X86_Disassembly

Basically it's an assembly book that explains some of the concepts of how higher level code is executed in assembly, and has some examples where it shows how functions etc might be generated in assembly by a compiler.

Cam
+1 for assembly, It definitively helps you to give insight about what happens 'behind' all those nifty syntactic sugar your language is providing you.
HeDinges
I have decided that I will follow what you have suggested. I will learn assembly, and after that, I will get into compiler design. Thanks for the suggestion.
ItzWarty
@incrediman: Update: A few months into this and it's going pretty well. Writing a simple interpreter for a simple language. Not writing a compiler yet, but hopefully i'll be doing that in the next year or so.
ItzWarty
@ItzWarty: Cool! Thanks for the followup. So what route did you decide to follow? Have you looked into assembly, or have you concentrated on lexing/parsing? What resources did you use, etc? :)
Cam
@incrediman: Both =P. So now, i'm working on a program to parse a script into a parsetree, which will then we walked/executed... Before that, I learned to work with nasm and some Z80-like asm [gameboy]... Pretty interesting. The interpreter is written in c#, and it's just parsing javascript-like syntax. Has a ton of optimization to go, but i've parsed hello world in 0.0002 seconds >_>[it's only 1 line, JS-like, once again]
ItzWarty
A: 

The actual mechanism used to calculate the modulus is likely to vary between languages and then between the implementation of each language. There some information about the algorithms here.

Maybe Algorithms is the general area you're interested in?

Richard Miskin
+1  A: 

No one is talking about Mathematic for modulo? First semester for media informatics (I'm in the fifth), course: Mathematic:

10 % 3 = ?
x % y = z

Calculation:

  1. 10 / 3 = 3.33333
  2. 3.3333 rounded to 3
  3. 3 * 3 = 9
  4. 10 - 9 = 1

As one formular:
10 - (Math.round(10 / 3) * 3) = 1

With variables:
x % y = x - (Math.round(x / y) * y)

Understanding binary and you will understand, that each math operation is based on addition.

WarrenFaith
A: 

The book mentioned above, "The Elements of Computing Systems: Building a Modern Computer from First Principles", By Noam Nisan and Shimon Schocken, addresses the types of questions that you raise in a holistic way: from architecture to VM to compilers to OS.

anon
A: 

If you don't want to spend any money, there's a great book called "Computer Organization and Design Fundamentals" available here (PDF link). I also recommend "Code" by Charles Petzold, published by Microsoft Press.

justthisguy