Author Topic: Not Quite Op2 Coding  (Read 3314 times)

Offline Simpsonboy77

  • Full Member
  • ***
  • Posts: 168
Not Quite Op2 Coding
« on: March 12, 2009, 09:08:02 AM »
I know a small amount of assembler and I was wondering if anyone knew of any that helped you learn it. I'm particularly interested in how the compiler will make small optimizations. Or would a good way to learn is just write simple programs and see how they are compiled?

Sorry I haven't been back in a while I had stuff going on in my life, school mostly.

Thanks
My tutorials
Part 1
Part 2
Part 3

Offline Spikerocks101

  • Hero Member
  • *****
  • Posts: 711
Not Quite Op2 Coding
« Reply #1 on: March 12, 2009, 10:52:02 AM »
never feel sorry, for we all hate school (takes us away from op2)

Hooman could give you codes, but if you want to learn, google the type of code you wan to learn, an put totorial after it, i helped me learn quite a bit, buit then i got lazy and never haven't been teaching my self for months..
I AM YOUR PET ROCK!!!!!!

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Not Quite Op2 Coding
« Reply #2 on: March 13, 2009, 02:13:00 AM »
I probably learned most of my assembly from trying to reverse engineer Outpost 2. I had a little bit of prior knowledge, but not much experience before I started on that.


There were a few ways that I learned. First, you probably want to try a little bit, just to get your feet wet. And maybe stumble around getting error messages to find out that you can't do certain things, that might otherwise seem obvious to do. (Try MOV [addr1], [addr2]).

Next a spent a lot of time reading assembly to see what was done and how it was done. This was largely reading Outpost 2 code. Some of it was just reading code that was hand crafted in assembly. Perhaps some speed sensitive code, or some processor specific feature, or CPUID type of code.

After that, I found I was trying to reverse engineer some of these things into a higher level language such as C/C++. Initially it was just the general concept, such as rewriting the algorithm so it would do the same thing. Then after a while I started wondering more about exactness. Not just an algorithm that would do the same thing, but one that was written in such a way that when compiled it would produce the same or similar assembly code. You'll sometimes find yourself puzzling over certain instruction sequences, and how they came to be. Sometimes things will compile to much less efficient code than you might have written yourself if starting from assembly. You'll definately start learning something about compilers at about that point.

There is also writing simple programs and compiling them to see what sort of assembly is produced. Try changing the source code in minor ways. See what extra declarative keywords do. (__stdcall, __cdecl, virtual, volatile, containment vs. inheritance of classes) Definately instructive as to how compilers work. You'll begin to understand why those keywords and there a lot better, and also how to use them properly.

You can also learn how the assembly mnemonics map to machine code. (Think of the actual binary image/hex byte dump, as opposed to text based source code). Then you'll really start to understand certain things about why assembly is the way it is. Like why you can't do MOV [addr1], [addr2]. (Assuming x86 of course). You also get to learn about certain efficiency aspects, like using 16 bit values in 32 bit code. (Those instructions need an operand size prefix, which adds to the instruction length). There are also weird effects you might not expect, like changing the register used for the same instruciton might lead to a different sized encoding, or a completely different encoding altogether. You'll also get to see how the architecture has changed over the years (legacy encodings, vs newer more "orthogonal" encodings). You can also see how the same instruction mnemonic is found in different places throughout the opcode tables for the different operand formats it supports (such as JMP and CALL).

By this point, you might consider trying to write a few "assembly" programs directly in machine code. Try writing a simple "Hello World" program by writing raw bytes to disk with a hex editor, or for extra fun using:
Code: [Select]
COPY CON > test.com
The "CON" stands for console. This will copy what is entered at the keyboard directly into the file. The .com files are binary executables with no header, so they'd be much easier to write this way than an exe file, which has a rather large and fairly complex header (at least if you're trying to write one by hand). There are of course certain restrictions on the values you can enter that way, as some keys are processed specially, such as backspace. You can use the Alt+Numpad trick to enter in all the non restricted values, which gets you wroughly 240? of the 256 possible bytes you can enter. I believe Ctrl+Z denotes end of file (completes the copy) under DOS? It might be Ctrl+D or something under Unix.

You can also write a program to format the boot sector of the harddrive this way. I saw an example of this online somewhere. Probably won't work under Windows though, so unless you're using DOS, and the BIOS isn't set to protect the boot sector, this probably won't do anything.

After you've done that, you can go on to writing a hex editor to make future work easier. ;)


Oh, and at some point, you'll want to try writing a few serious algorithms for yourself without reference assembly code to guide you.


If that's still not enough to satisfy you, you can try poking around with different architectures. Perhaps there's an old NES game that you liked? Well, FCEUltra is an emulator with a nice built in debugger. Just grab a book or online article on 6502 assembly, and away you go. (I recommend the book, as there is no doubt an ancient copy collecting dust on a public library shelf somewhere, and it probably of higher value than many online documents). I've heard the SNES also uses a processor from the same line, and in many ways, the CPU was backwards compatible (although the supporting hardware likely wasn't). And just to horrify all you old nintendo lovers, which all too often have rather strong feelings about a certain line of computers, guess what else the 6502 was used in?  ;)
 

Offline Simpsonboy77

  • Full Member
  • ***
  • Posts: 168
Not Quite Op2 Coding
« Reply #3 on: March 17, 2009, 08:33:17 PM »
Wow thanks Hooman. I started poking around with a simple program and testing out different things like volatile static etc. I'm not quite to the point where I want to look at multiple classes at once, I don't want to scare myself yet :). I noticed that if i have a long chain of if if else .... vs a case statement, the compiler makes a jumptable rather than the normal JE/JNE. I tried floating point numbers, so I'm just going to play with integer math for now.

What I have really found interesting is writing a small program, then compile it with and without optimizations. Sometimes the changes it makes make the flow very hard to follow even though I got the C code right next to me. This is harder that I initially thought. Its kind of humbling to think this is how computers were programmed at one time. For the Apollo missions they wired the ROM, loop the wire this way for a 0 and another way for a 1.


Speaking of different architectures, in high school I was captain of the robotics team my senior year, as well as the self titled lead programmer ( was the only one). I programmed on a PIC18F8722, and a decent portion was in assembly. It was mostly the start up sequence since there were some bugs in the processor. I know that better than x86.


I hope I don't get to spend as much time here, even though it's awesome, since that would mean I got an internship at Lockheed Martin. But I'll be back.
« Last Edit: March 17, 2009, 08:34:05 PM by Simpsonboy77 »
My tutorials
Part 1
Part 2
Part 3

Offline Spikerocks101

  • Hero Member
  • *****
  • Posts: 711
Not Quite Op2 Coding
« Reply #4 on: March 18, 2009, 05:28:40 PM »
no thanks spikerocks... no one ever thanks spiker :(
I AM YOUR PET ROCK!!!!!!

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3238
Not Quite Op2 Coding
« Reply #5 on: March 18, 2009, 08:34:47 PM »
Quote
no thanks spikerocks... no one ever thanks spiker :(
Quote
never feel sorry, for we all hate school (takes us away from op2)

Ah yes, such valuable advice.  I loved the part where you told him to Google it.  You certainly added to this discussion, Spiken00b.
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline Spikerocks101

  • Hero Member
  • *****
  • Posts: 711
Not Quite Op2 Coding
« Reply #6 on: March 19, 2009, 02:40:27 AM »
*cries then goes play op2.... ON EASY!*
I AM YOUR PET ROCK!!!!!!

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Not Quite Op2 Coding
« Reply #7 on: March 21, 2009, 02:14:26 PM »
I personally find the code easier to read with optimizations turned on. I find it produces a much more natural instruction sequence. With optimizations off, it just looks too mechanical, and well, just plain sick. There's usually many more instructions when optimizations are off, which means more to remember and figure out, and they're often pointless (like writes to memory locations which are never read later on). Plus, the inefficiency makes me want to stop reading it, or perhaps to rewrite some of it.


PICs sound interesting, but I've never had reason to use one. The only actual hardware project I worked on was small enough to design from the gate level, and build with probably only five 7400 series chips or so.

I also got to design a CPU from the gate level, and then program it, all in a simulator. Did that twice. The first was more of a CISC CPU, and the second was more of a RISC CPU. There was no actual hardware implementation of either of those projects though.
 

Offline Simpsonboy77

  • Full Member
  • ***
  • Posts: 168
Not Quite Op2 Coding
« Reply #8 on: March 26, 2009, 11:04:58 AM »
I'm sorry Spikerocks, I didn't know you expected to be thanked for such a lengthy post ;). So here is your thanks. Than... eh its not worth finishing.

The PIC was for a robotics club, and we competed in the New York and the New Jersey regional. We won the entire New York regional. The controller was based on a PIC.

I'm glad im not the only one who had trouble reading unoptimized code.

I got plenty to tinker with and I got a few big projects, then a IEEE conference at the end of April, so you might not hear from me for a while.
 
My tutorials
Part 1
Part 2
Part 3

Offline Simpsonboy77

  • Full Member
  • ***
  • Posts: 168
Not Quite Op2 Coding
« Reply #9 on: July 28, 2009, 08:08:28 PM »
Hey, thought I'd drop by and give an update.

I got the internship at Lockheed, and I've been working there since May. I tried doing some assembler work, but it's really not my thing. I like writing small efficient code, but I don't like reverse engineering. Even though I don't like it, I think its a great thing to know.

I think I may find it more interesting if you give me a mini project that would actually help the development. Is there a list of things to do?
« Last Edit: July 28, 2009, 08:09:35 PM by Simpsonboy77 »
My tutorials
Part 1
Part 2
Part 3