Roller Coaster Tycoon in... Assembly?

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

ThirstyMonkey
Posts: 15
Joined: Wed Aug 24, 2011 11:48 pm UTC

Roller Coaster Tycoon in... Assembly?

Postby ThirstyMonkey » Mon Jul 16, 2012 7:32 pm UTC

Chris Sawyer says that he wrote the majority of code for Roller Coaster Tycoon in assembly, which to me, as a very young programmer, seems absolutely impossible. My only experience with assembly was the classic Binary Bomb assignment, and given that, I have no idea how one would accomplish anything beyond a basic complexity. Obviously large programs have been written in assembly. My question is How?

User avatar
Xanthir
My HERO!!!
Posts: 5400
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Roller Coaster Tycoon in... Assembly?

Postby Xanthir » Mon Jul 16, 2012 7:36 pm UTC

Can you write C? If so, you're already halfway to assembly. Just imagine that every line is replaced by about 5 lines of assembly doing in the same thing, and you're about right.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Roller Coaster Tycoon in... Assembly?

Postby You, sir, name? » Mon Jul 16, 2012 8:29 pm UTC

There's also high level assembly dialects that are somewhere between C and low-level assembly.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Roller Coaster Tycoon in... Assembly?

Postby sourmìlk » Tue Jul 17, 2012 7:43 am UTC

Back in the day everything (including games) were written in assembly. When Blizzard started out, they were three guys writing Rock and Roll Racing in assembly. I don't know what what point C and C++ surpassed assembly in terms of ubiquity for use in games, but I think it was only in the past 20 years or so.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

korona
Posts: 495
Joined: Sun Jul 04, 2010 8:40 pm UTC

Re: Roller Coaster Tycoon in... Assembly?

Postby korona » Tue Jul 17, 2012 10:04 pm UTC

ThirstyMonkey wrote:Chris Sawyer says that he wrote the majority of code for Roller Coaster Tycoon in assembly, which to me, as a very young programmer, seems absolutely impossible. My only experience with assembly was the classic Binary Bomb assignment, and given that, I have no idea how one would accomplish anything beyond a basic complexity. Obviously large programs have been written in assembly. My question is How?

What exactly prevents us from developing large programs in assembly? Sure they are not going to be portable but if you're using a reasonable calling convention for your procedures and you've got enough comments to document what your code actually does it is not too hard to program in assembly. One example of a large program written completely in x86 assembly is http://menuetos.net/. You can download the sources and take a look at the 32 bit kernel and the applications. It is not too hard to understand them after you understood the calling convention.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Roller Coaster Tycoon in... Assembly?

Postby sourmìlk » Tue Jul 17, 2012 10:38 pm UTC

Assembly is just a lot of gruntwork. It's not "hard" in the sense that it's difficult to understand or plan, it's just hard in the sense that there's a lot to do.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

Sagekilla
Posts: 382
Joined: Fri Aug 21, 2009 1:02 am UTC
Location: Long Island, NY

Re: Roller Coaster Tycoon in... Assembly?

Postby Sagekilla » Wed Jul 18, 2012 4:32 am UTC

We don't code in assembly for the very reason why languages like C# and Java are so popular.
Coding in a low level language means you have to do *a lot* of work just to do something.

Let's say I had a list of employees at a store and I wanted to get a list of employees who sold
at least 100 items. In C# this is elegantly implemented using LINQ:

var highSellers = from employee in employees
where employee.sales.Length > 100
select employee;

The equivalent in assembly could be vastly larger. Not only that, but it would be easy to get
caught up in the tedium of the assembly and forget what you were trying to do in the first
place. The equivalent C++ program to the above would look very similar, but would be
written at a lower level:

std::vector<Employee*> highSellers;

for (int i = 0; i < employees.size(); i++)
if (employees[i]->Sales.size() > 100)
highSellers.push_back(employees[i]);

Semantically they do (for all intents and purposes) the same thing. I'd argue the C# version
is much easier to read and understand what you're trying to do. The C++ one isn't bad either,
but it certainly has a bit more going on. Go down to assembly and there's probably a *lot* more
going on.

Once you develop large scale applications, you'll realize that writing these applications in high
level languages makes your life incredibly easier. You can do more in less lines of code, which
translates to less time and effort invested.
http://en.wikipedia.org/wiki/DSV_Alvin#Sinking wrote:Researchers found a cheese sandwich which exhibited no visible signs of decomposition, and was in fact eaten.

User avatar
WarDaft
Posts: 1583
Joined: Thu Jul 30, 2009 3:16 pm UTC

Re: Roller Coaster Tycoon in... Assembly?

Postby WarDaft » Wed Jul 18, 2012 5:44 am UTC

You can't even name things in assembler can you?

Also...
getHighSellers = filter ((>100) . length . sales)
All Shadow priest spells that deal Fire damage now appear green.
Big freaky cereal boxes of death.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Roller Coaster Tycoon in... Assembly?

Postby sourmìlk » Wed Jul 18, 2012 6:44 am UTC

You can name variables and labels and such. You can't really make your own types though*

[i]* - Using C-style object orientation, where you create functions that take pointers to structs as arguments, you can probably get something sort of close to it though.

Also, I don't know that it's necessarily the case that high level languages allow you to write less code than, say, C++. Very often I will agree that's the case, but one of the things about C++ is that it allows you to develop a lot of your own sort of language constructs: the combination of templates, operator overloading, and polymorphism makes it such that you invent types and treat them as primitives. The upshot of that is, after laying not too much base code, you can do some complicated things in C++ in very few lines that would take substantially more time in, say, Java or Python, because you can't creatively utilize templates and operator overloading to hide some of the stuff that's going on.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

User avatar
WarDaft
Posts: 1583
Joined: Thu Jul 30, 2009 3:16 pm UTC

Re: Roller Coaster Tycoon in... Assembly?

Postby WarDaft » Wed Jul 18, 2012 12:35 pm UTC

They are more concise though. They really, really are. Type classes are templates but done right, C++ polymorphism is downright ugly by comparison to some languages, and operator overloading is just one of the things that type classes let you do properly.
All Shadow priest spells that deal Fire damage now appear green.
Big freaky cereal boxes of death.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Roller Coaster Tycoon in... Assembly?

Postby You, sir, name? » Wed Jul 18, 2012 4:43 pm UTC

Sagekilla wrote:
Spoiler:
We don't code in assembly for the very reason why languages like C# and Java are so popular.
Coding in a low level language means you have to do *a lot* of work just to do something.

Let's say I had a list of employees at a store and I wanted to get a list of employees who sold
at least 100 items. In C# this is elegantly implemented using LINQ:

var highSellers = from employee in employees
where employee.sales.Length > 100
select employee;

The equivalent in assembly could be vastly larger. Not only that, but it would be easy to get
caught up in the tedium of the assembly and forget what you were trying to do in the first
place.
The equivalent C++ program to the above would look very similar, but would be
written at a lower level:

std::vector<Employee*> highSellers;

for (int i = 0; i < employees.size(); i++)
if (employees[i]->Sales.size() > 100)
highSellers.push_back(employees[i]);

Semantically they do (for all intents and purposes) the same thing. I'd argue the C# version
is much easier to read and understand what you're trying to do. The C++ one isn't bad either,
but it certainly has a bit more going on. Go down to assembly and there's probably a *lot* more
going on.

Once you develop large scale applications, you'll realize that writing these applications in high
level languages makes your life incredibly easier. You can do more in less lines of code, which
translates to less time and effort invested.


C++11 does go some way towards making life easier for you though. Your example can now just as easily be written

Code: Select all

std::vector<Employee*> highSellers;
copy_if(employees.begin(), employees.end(), back_inserter(highSellers),
     [](Employee* e){ return e->Sales.size() > 100 ;  }
  );



sourmìlk wrote:You can name variables and labels and such. You can't really make your own types though*

[i]* - Using C-style object orientation, where you create functions that take pointers to structs as arguments, you can probably get something sort of close to it though.

Also, I don't know that it's necessarily the case that high level languages allow you to write less code than, say, C++. Very often I will agree that's the case, but one of the things about C++ is that it allows you to develop a lot of your own sort of language constructs: the combination of templates, operator overloading, and polymorphism makes it such that you invent types and treat them as primitives. The upshot of that is, after laying not too much base code, you can do some complicated things in C++ in very few lines that would take substantially more time in, say, Java or Python, because you can't creatively utilize templates and operator overloading to hide some of the stuff that's going on.


I think the reason you'd favor some higher level language to C++ is that it's really easy to shoot yourself in the foot with C++. It's so easy to irrevocably fuck up the state of a C++ program it isn't even funny. You can do it in a language like Java or C# as well, but it takes a lot more effort to put it in such a crazy tailspin the error can't be handled in a well designed system.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

Ben-oni
Posts: 278
Joined: Mon Sep 26, 2011 4:56 am UTC

Re: Roller Coaster Tycoon in... Assembly?

Postby Ben-oni » Wed Jul 18, 2012 7:54 pm UTC

You, sir, name? wrote:I think the reason you'd favor some higher level language to C++ is that it's really easy to shoot yourself in the foot with C++. It's so easy to irrevocably fuck up the state of a C++ program it isn't even funny. You can do it in a language like Java or C# as well, but it takes a lot more effort to put it in such a crazy tailspin the error can't be handled in a well designed system.

No. Wrong. This is as far from the truth as it's possible to be without being nonsensical. We do not prefer high level languages because we might mess up otherwise. We prefer them because they help us think in the abstract.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Roller Coaster Tycoon in... Assembly?

Postby You, sir, name? » Wed Jul 18, 2012 8:15 pm UTC

Ben-oni wrote:
You, sir, name? wrote:I think the reason you'd favor some higher level language to C++ is that it's really easy to shoot yourself in the foot with C++. It's so easy to irrevocably fuck up the state of a C++ program it isn't even funny. You can do it in a language like Java or C# as well, but it takes a lot more effort to put it in such a crazy tailspin the error can't be handled in a well designed system.

No. Wrong. This is as far from the truth as it's possible to be without being nonsensical. We do not prefer high level languages because we might mess up otherwise. We prefer them because they help us think in the abstract.


That may be true in general (when comparing for example C and Java), but not when comparing C++ (a high level language) with Java (a slightly higher level language; higher level than C++ specifically because it protects from the things I mentioned).
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
WarDaft
Posts: 1583
Joined: Thu Jul 30, 2009 3:16 pm UTC

Re: Roller Coaster Tycoon in... Assembly?

Postby WarDaft » Wed Jul 18, 2012 8:46 pm UTC

Ben-oni wrote:
You, sir, name? wrote:I think the reason you'd favor some higher level language to C++ is that it's really easy to shoot yourself in the foot with C++. It's so easy to irrevocably fuck up the state of a C++ program it isn't even funny. You can do it in a language like Java or C# as well, but it takes a lot more effort to put it in such a crazy tailspin the error can't be handled in a well designed system.

No. Wrong. This is as far from the truth as it's possible to be without being nonsensical. We do not prefer high level languages because we might mess up otherwise. We prefer them because they help us think in the abstract.


... I'm pretty sure compile time guarantees of correctness are generally considered a pretty desirable aspect.
All Shadow priest spells that deal Fire damage now appear green.
Big freaky cereal boxes of death.

korona
Posts: 495
Joined: Sun Jul 04, 2010 8:40 pm UTC

Re: Roller Coaster Tycoon in... Assembly?

Postby korona » Wed Jul 18, 2012 8:50 pm UTC

Java has some advantages over C++:
  • larger standard library. Includes GUI, networking, database access, cryptography, ...
  • garbage collection: You can't run into buffer overflows. You need to worry about different types of smart pointers or object life cycles. Java garbage collection is faster than C++ smart pointers
    and can take care of cyclic dependencies.
  • compile-once-run-everywhere. A big deal for some people, not that important for others.

C++ has some advantages over Java:
  • you can easily predict how much a call/line of code will cost
  • less overhead: In java all objects contain additional words that identify the object's class. Every object can be locked. Every java call is virtual unless the java JIT turns it into a non-virtual one.
  • more flexibility: You want to allocate some buffers on the stack or have custom memory allocators? C++ allows you to do that. It also allows you to control the size of your data structures.
  • meta-programming. In some situations this saves a lot of code without having a runtime cost.
If you're writing high performance applications C++ will be a better choice. If you're writing a server component that will run on many different machines Java will often be a better choice.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Roller Coaster Tycoon in... Assembly?

Postby You, sir, name? » Wed Jul 18, 2012 9:57 pm UTC

korona wrote:Java garbage collection is faster than C++ smart pointers and can take care of cyclic dependencies.


Borderline OT, but this point is kinda moot. If performance is important, you avoid short lived (heap) objects in both C++ and Java. In fact, in timing critical high throughput systems, you don't want the GC to run at all. Because GC:ing 250 Gb of data takes ages no matter how fast your system is.

Because C++ can aggregate member objects within the same memory chunk, this gets a bit easier. But you get basically the same performance in Java, even if you occasionally have to resort to some wonky techniques (e.g. string internalization voodoo).
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

korona
Posts: 495
Joined: Sun Jul 04, 2010 8:40 pm UTC

Re: Roller Coaster Tycoon in... Assembly?

Postby korona » Wed Jul 18, 2012 10:08 pm UTC

One should also consider that even if GC is faster than smart pointers, you usually do not manage all your objects using smart pointers. In java every object is managed by the GC.
From my experience java 1.6 is ~3 times slower than C++ in high throughput situations (i.e. gigabytes of data per second).
EDIT: but in that situation we were already using custom memory allocators in C++ that allowed us to free tens of thousands of objects at the same time; we did not manage each object's life span individually.

User avatar
Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: Roller Coaster Tycoon in... Assembly?

Postby Sc4Freak » Wed Jul 18, 2012 11:39 pm UTC

The performance of smart pointers in C++ depends on which one you use. unique_ptr is basically free. But because shared_ptr is reference-counted, there is some overhead in keeping track of the refcount. Each increment/decrement is the cost of an interlocked operation, which is a fairly expensive operation (a hundred clock cycles or so).

But it also depends on your application. For real-time applications like video/audio playback or games, the mark-and-sweep GCs used in Java/C#/etc are often unacceptable unless strictly controlled. Even if a GC gives better average performance, the intermittent (and unpredictable) cost of garbage collection is a nightmare if you're trying to keep a consistent framerate. If you're aiming for 60fps, the user is going to notice a 30ms skip much much more than a consistent 55fps.

Ben-oni
Posts: 278
Joined: Mon Sep 26, 2011 4:56 am UTC

Re: Roller Coaster Tycoon in... Assembly?

Postby Ben-oni » Thu Jul 19, 2012 12:12 am UTC

WarDaft wrote:... I'm pretty sure compile time guarantees of correctness are generally considered a pretty desirable aspect.

While this proposition would be true, since the antecedent isn't, this is a moot point. Turing Complete languages don't guarantee correctness. Even when the language does include proof mechanisms, it still doesn't guarantee that the correct thing is being proven. While guarantees of type safety, guarantees of functional behavior, etc. are nice to have, there are more important reasons for using high level languages.


Sc4Freak wrote:But it also depends on your application. For real-time applications like video/audio playback or games, the mark-and-sweep GCs used in Java/C#/etc are often unacceptable unless strictly controlled. Even if a GC gives better average performance, the intermittent (and unpredictable) cost of garbage collection is a nightmare if you're trying to keep a consistent framerate. If you're aiming for 60fps, the user is going to notice a 30ms skip much much more than a consistent 55fps.

Then don't use a process interrupting GC. There are nice concurrent ones out there that work very well.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Roller Coaster Tycoon in... Assembly?

Postby sourmìlk » Thu Jul 19, 2012 5:36 am UTC

I'm not trying to say that C++ is a perfect tool (though I like to use it for more than it's probably objectively fit for), just that it often doesn't really require more code than a higher level language because of how the language features allow you to make your code very concise.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Roller Coaster Tycoon in... Assembly?

Postby EvanED » Thu Jul 19, 2012 8:00 am UTC

sourmìlk wrote:Assembly is just a lot of gruntwork. It's not "hard" in the sense that it's difficult to understand or plan, it's just hard in the sense that there's a lot to do.

And also that it's easy in assembly to miss a detail and screw up your program. I mean, moreso than normal. :-) (No more compiler errors for mismatched types and stuff. Call a function with the wrong parameters -- or change it later and miss a call site? -- Ha ha, sucks to be you.)

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Roller Coaster Tycoon in... Assembly?

Postby You, sir, name? » Thu Jul 19, 2012 8:14 am UTC

Ben-oni wrote:
Sc4Freak wrote:But it also depends on your application. For real-time applications like video/audio playback or games, the mark-and-sweep GCs used in Java/C#/etc are often unacceptable unless strictly controlled. Even if a GC gives better average performance, the intermittent (and unpredictable) cost of garbage collection is a nightmare if you're trying to keep a consistent framerate. If you're aiming for 60fps, the user is going to notice a 30ms skip much much more than a consistent 55fps.

Then don't use a process interrupting GC. There are nice concurrent ones out there that work very well.


The stop-the-world GC is fast. The concurrent GC tanks performance a hell of a lot more.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Roller Coaster Tycoon in... Assembly?

Postby sourmìlk » Thu Jul 19, 2012 8:55 am UTC

Why is that?
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Roller Coaster Tycoon in... Assembly?

Postby You, sir, name? » Thu Jul 19, 2012 9:27 am UTC

I haven't a faintest. Slower algorithm, I suppose?
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Roller Coaster Tycoon in... Assembly?

Postby sourmìlk » Thu Jul 19, 2012 10:01 am UTC

That's not a very satisfying explanation :-|
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

Ben-oni
Posts: 278
Joined: Mon Sep 26, 2011 4:56 am UTC

Re: Roller Coaster Tycoon in... Assembly?

Postby Ben-oni » Thu Jul 19, 2012 10:18 am UTC

You, sir, name? wrote:I haven't a faintest. Slower algorithm, I suppose?

It's the compaction step, I think. You can't move memory that's currently in use. Of course, collectors for pure languages have an advantage: they can just copy memory and update the references as they find them. Though it never works that way in practice, as in-place mutations are needed for efficiency. I remember a flaw with some Haskell arrays (I don't know if it's been addressed) that caused various operations to take O(n) time instead of O(1) because the GC had to touch the whole array, so it's worth noting that there are hidden costs associated with GC. But it's an area of active research, so it's worth remembering that what we think we know about GC may not be true a couple years from now.

Carnildo
Posts: 2023
Joined: Fri Jul 18, 2008 8:43 am UTC

Re: Roller Coaster Tycoon in... Assembly?

Postby Carnildo » Fri Jul 20, 2012 4:36 am UTC

WarDaft wrote:
Ben-oni wrote:
You, sir, name? wrote:I think the reason you'd favor some higher level language to C++ is that it's really easy to shoot yourself in the foot with C++. It's so easy to irrevocably fuck up the state of a C++ program it isn't even funny. You can do it in a language like Java or C# as well, but it takes a lot more effort to put it in such a crazy tailspin the error can't be handled in a well designed system.

No. Wrong. This is as far from the truth as it's possible to be without being nonsensical. We do not prefer high level languages because we might mess up otherwise. We prefer them because they help us think in the abstract.


... I'm pretty sure compile time guarantees of correctness are generally considered a pretty desirable aspect.

The usefulness depends entirely on what's being guaranteed. For example, TECO guarantees that whatever you typed is a valid TECO program, but that's because every character string is a valid TECO program.

RarrRaptor
Posts: 28
Joined: Mon Nov 19, 2012 3:51 am UTC

Re: Roller Coaster Tycoon in... Assembly?

Postby RarrRaptor » Mon Nov 19, 2012 4:45 pm UTC

ThirstyMonkey wrote:I have no idea how one would accomplish anything beyond a basic complexity. Obviously large programs have been written in assembly. My question is How?


If you're an object oriented programmer, assembly languages have procedures (like methods) that you can call. So you can design a data structure and the procedures to initialize one and do all the common operations with it. This is quite similar to object oriented programming: you have the instance variables and the methods to construct and operate on them. You can then build up from that just like in any OOP language. It can be a bit tricky to make the hardware do what you want, but you can write 'classes' to deal with that, too. The only slightly tricky thing is dealing with addresses, which are like pointers in C++.

If you're a functional programmer, similar reasoning applies. You have procedures that are like functions. You can call them. You can create new data rather than modifying anything existing if that's your style. And so on.

If you're an old style procedural programmer, well, assembly language pretty much is that, so you're at home.

nharding
Posts: 6
Joined: Fri Nov 23, 2012 4:19 pm UTC

Re: Roller Coaster Tycoon in... Assembly?

Postby nharding » Fri Nov 23, 2012 5:59 pm UTC

I've been programming games for over 30 years, when I started programming commercial games, they were entirely in assembly language. The average development for a game was 16 weeks (for Atari ST), and then I ported to Amiga (another 4 weeks, to take advantage of some of the Amiga hardware and improve the game). I actually prefer programming in 68000 to C. I used to write object orientated code (although I didn't know the term at the time). I'd define a structure such as a bullet, which has x & y position and velocities, size, image definition, damage (this field would be health on enemy or player) then an enemy would have all the same fields plus logic routine pointer, animation pointer, score value (how much it is worth, or for a player, how many points they have), the player may have a few additional fields such as state variables like jumping, ducking, etc. You then call move routine, with a0 pointing to the memory for the variable, and it would add xVel to xPos, yVel to yPos, check if it is colliding with anything etc. So it would be like a method, that works on bullet, enemy, or player class. You'd then call animate on all the enemies and players (I used to have a complex data structure for animation, that allowed me to nest code inside the animation which is hard to do in a language like C). My animation routine had
dc.w frameNumber, .... if frameNumber was negative then it meant you had 15 bits for control structures, so bit 1 = call routine, bit 2 = move animation pointer, bit 3 = check variable, bit 4 = modify variable (I had an animation that pulsed a palette value, so I had red warning light, that changed color whilst it was on screen all handled automatically in the animation routine), and so on.

One advantage of assembly compared with C/C++ was that you could make more efficient use of the machine, my first game I left the operating system in and it didn't work on the next release of the operating system, since there was less free memory available (fortunately the OS release happened whilst I was developing the game, so I managed to reduce the memory requirements enough to ensure it would work). After that I kicked out the OS entirely, and had my own disk loader (provided by one of the programmers at the company) so I could use 511K on a 512K machine (the 1st 1K is reserved for interrupt vectors) and the code would run fine even if the OS changed. In fact the Amiga version ran on the high end Amiga that came out a few years later with a 68030 processor on it.

Learning assembly language is still useful for times when you need performance, even if you are programming in C. Since C code is turned into assembly language, there are certain constructs that will be more efficient a*++ = b*++ with a 0 terminated value is pretty efficient. If you are using a for loop, then for (i = 0; i < 10; i++) is less efficient than for (i = 10; i > 0; i--). These tricks are not needed on a modern PC during the vast majority of code, although if something is executed a lot in your code, then it may be worth implementing, but if you are writing code for an embedded device, or mobile phone then it may be worth using since the code not only executes faster it takes up less memory as well.

Neil Harding

Derek
Posts: 2180
Joined: Wed Aug 18, 2010 4:15 am UTC

Re: Roller Coaster Tycoon in... Assembly?

Postby Derek » Sun Nov 25, 2012 6:27 pm UTC

nharding wrote:If you are using a for loop, then for (i = 0; i < 10; i++) is less efficient than for (i = 10; i > 0; i--).

Would you mind providing an explanation for this? I have trouble believing this except in some case-by-case cache situations.

User avatar
Shivahn
Posts: 2200
Joined: Tue Jan 06, 2009 6:17 am UTC

Re: Roller Coaster Tycoon in... Assembly?

Postby Shivahn » Sun Nov 25, 2012 6:30 pm UTC

I'm pretty sure that a good optimizing compiler will turn them into the same assembly. That's basically their entire job.

Not to say that you can't optimize by dropping down to assembly, of course, but I think usually the examples are slightly more complex and less common than for loops.

DaveInsurgent
Posts: 207
Joined: Thu May 19, 2011 4:28 pm UTC
Location: Waterloo, Ontario

Re: Roller Coaster Tycoon in... Assembly?

Postby DaveInsurgent » Sun Nov 25, 2012 6:51 pm UTC

but if you are writing code for an embedded device, or mobile phone then it may be worth using since the code not only executes faster it takes up less memory as well.


I work for a company that produces mobile devices, and I can say that I've never seen a single person touch assembly. It simply isn't worth it. Devices have 1-2GB of RAM and at least dual if not quad core processors. The productivity loss in working with assembly compared to writing services in C and apps in C++ (with say, Qt and so on) is unfathomable to a business. We're at the point where the thing in your hand superseeds anything you had on your desktop not too long ago.

Now, embedded... sure, I can see that maybe, maybe in certain circumtances it might be an issue, but I'd imagine even a modern vending machine being designed would have a few hundred megabytes of RAM. Hell, we had a coffee machine that had a full blown GUI on a touch screen. The thing ran Java, and not out of an intent to be clever.

There are optimizations that may be more easily performed at a high level, certain facets of the structure of the program are easier to see and it's easier to refactor - not to mention JIT compilation - also, the type safety and expressiveness of languages go far beyond just "this saves me time" in writing. It's about balancing communicating as much as possible with performance. I'd actually argue that commoditized things like embedded and mobile are less likely to benefit from the use of assembly, because they are more time-to-maket sensitive and the people who make them jump in and out quickly (hence the need for a higher level language and common patterns/paradigms). Based on something I heard, but can't substantiate, I think the places that you might see using assembly are the ones that are shifting towards C++ away from Java for one reason: energy savings. The cost of executing Java code is apparently more of a concern than the cost of writing the software. So maybe assembly would benfit even further. I heard this was a trend in big data places.

it's just hard in the sense that there's a lot to do.


And that increases the odds of errors/mistakes. You can't just say, oh, assembly takes 5x as long to write than C++, so we need to hire 5x as many people. Statements regarding turing complete languages not being formally provable only apply to a small, incredibly small - like 1% - of the developer population. A lot of the academic and science side is really not useful in the real world. (It's amusing to see how many people - recent graduates - get caught up with data structures and algorithms but can't deliver a useful product on time). Most developers are writing code to save a business money by either making someone elses' job redundant, cheaper/easier to do, or making something that a human couldn't do (such as data aggregation/reporting) possible. Working in assembly just goes directly opposite of the reason most developers would ever be hired by a business. That's why we don't do it anymore. Not because it isn't possible.

Derek
Posts: 2180
Joined: Wed Aug 18, 2010 4:15 am UTC

Re: Roller Coaster Tycoon in... Assembly?

Postby Derek » Sun Nov 25, 2012 9:34 pm UTC

Shivahn wrote:I'm pretty sure that a good optimizing compiler will turn them into the same assembly. That's basically their entire job.

If there is real performance to be gained, yeah, I would expect a compiler to make the optimization if it could. But I'm seriously questioning whether there is actually anything to gain. I guess the only thing I can see is that one is doing a comparison with 10, and the other is doing a comparison with 0, and the comparison with 0 might be faster. But I suspect that in most cases both take a single clock cycle.

DaveInsurgent
Posts: 207
Joined: Thu May 19, 2011 4:28 pm UTC
Location: Waterloo, Ontario

Re: Roller Coaster Tycoon in... Assembly?

Postby DaveInsurgent » Sun Nov 25, 2012 11:26 pm UTC

But I suspect that in most cases both take a single clock cycle.


Yes but that is the nature of premature optimization-oriented statements like "use countdown loops". Instead of reactively changing the code based on profiling, and using higher level language constructs to revisit the structure of the program, a lot of people unfortunately take one example that shows (as you say, a very, very small increase in performance) and can't "think outside the fast" -- you hear it all the time: "Virtual methods are slow". Yeah, sure, after a few million (or even a few more orders of magnitude) iterations... what the fuck does that matter at the application or interface layer? Instead, people decide to always write their loops countdown style because hey why not. Then you get a bug because they used the loop counter for array access. There's a huge, gigantic gap between the academic side of the engineering/business/pratical side in computer software that, unfortunately only seems to be shrinking in relative size: with each wave of web developer "I r programmr" influx, the gap appears smaller, but only because the field is so diluted with people who can't write code for shit. The distance between the sciency-mathy types who know the fuck out of computing and the engineers who know the discipline side of the profession stays the same. </rant>

nharding
Posts: 6
Joined: Fri Nov 23, 2012 4:19 pm UTC

Re: Roller Coaster Tycoon in... Assembly?

Postby nharding » Thu Nov 29, 2012 8:57 pm UTC

When writing code for PC's I never use the backward counting loop, unless the logic counts for it (say you are trying to reverse a string) since it is harder to read, and therefore more likely to have an error. The backward count loop, is efficient because of the removal of an instruction, so it no only makes the code faster, it makes it smaller but you can normally gain a lot more by unrolling a loop if performance is a problem.

Even on the Gameboy Advance, I wrote most of the code in C++, but I did use small assembly language routines for fixed point arithmetic. I wrote F14 Tomcat using floats during development, then I switched to 16.16 fixed point which meant the game could run in a frame (16.16 fixed point was over 100 times faster for additions, and 20 times faster for the multiplication, these numbers may be off but are the right order of magnitude).

I also wrote Java bytecode directly, since the Java compiler produces bloated code when I worked at Digital Bridges, I had a library of support code for games which had to run on Nokia phones which had a 64K jar limit, for code and data. It was possible to reduce code size by around 20%, by mixing Java and hand coded bytecode but I would only recommend that sort of effort on library routines that will be reused multiple times. The java compiler is/(was? I've not checked it for a while) a joke, it produces code like C compilers used to do back in the mid 80's. A modern C/C++ compiler generates pretty good code now, so the gains in going to assembly language not as great as they used to be. The java compiler would not do simple optimizations, so public static final int X = 0; y = player+X actually adds 0 rather than be optimized away, so I had an optimizer that performed simple peephole optimizations, such as that, multiplications by powers of 2, were converted to shifts, the constant pool was reordered to make the program smaller and I also allowed float variables on CLDC 1.0, they were converted into 20.12 fixed point (to be compatible with 3D supported EL GS device).

Neil Harding

Derek
Posts: 2180
Joined: Wed Aug 18, 2010 4:15 am UTC

Re: Roller Coaster Tycoon in... Assembly?

Postby Derek » Thu Nov 29, 2012 10:27 pm UTC

nharding wrote:When writing code for PC's I never use the backward counting loop, unless the logic counts for it (say you are trying to reverse a string) since it is harder to read, and therefore more likely to have an error. The backward count loop, is efficient because of the removal of an instruction, so it no only makes the code faster, it makes it smaller but you can normally gain a lot more by unrolling a loop if performance is a problem.

What operation does it remove? This is how I would imagine the compiled code:

Code: Select all

mov 0, eax (or xor eax, eax)
loop:
...
inc eax
cmp eax, 10
jle loop


Code: Select all

mov 9, eax
loop:
...
dec eax
cmp eax
jge loop


I can't imagine either being faster than the other. If you want to count from 10 to 1 instead of 9 to 0, you could use test instead of cmp in the second, but I don't think it would make a difference. I also know that there is a loop instruction that decrements ecx, compares it to zero, and jumps if not equal, but I likewise suspect that just gets converted to the same micro ops in the processor.

I also wrote Java bytecode directly, since the Java compiler produces bloated code when I worked at Digital Bridges, I had a library of support code for games which had to run on Nokia phones which had a 64K jar limit, for code and data. It was possible to reduce code size by around 20%, by mixing Java and hand coded bytecode but I would only recommend that sort of effort on library routines that will be reused multiple times. The java compiler is/(was? I've not checked it for a while) a joke, it produces code like C compilers used to do back in the mid 80's. A modern C/C++ compiler generates pretty good code now, so the gains in going to assembly language not as great as they used to be. The java compiler would not do simple optimizations, so public static final int X = 0; y = player+X actually adds 0 rather than be optimized away, so I had an optimizer that performed simple peephole optimizations, such as that, multiplications by powers of 2, were converted to shifts, the constant pool was reordered to make the program smaller and I also allowed float variables on CLDC 1.0, they were converted into 20.12 fixed point (to be compatible with 3D supported EL GS device).

I think the Java compiler is much better these days, though I can't provide any details.

yedidyak
Posts: 954
Joined: Mon Apr 12, 2010 10:24 pm UTC
Location: Israel

Re: Roller Coaster Tycoon in... Assembly?

Postby yedidyak » Thu Nov 29, 2012 11:17 pm UTC

Well I had my first day of coding assembly today, and dear God I hope I never have to write anything like that.

User avatar
DavidH
Posts: 80
Joined: Mon Aug 03, 2009 3:53 am UTC

Re: Roller Coaster Tycoon in... Assembly?

Postby DavidH » Fri Nov 30, 2012 1:10 am UTC

Derek wrote:
nharding wrote:When writing code for PC's I never use the backward counting loop, unless the logic counts for it (say you are trying to reverse a string) since it is harder to read, and therefore more likely to have an error. The backward count loop, is efficient because of the removal of an instruction, so it no only makes the code faster, it makes it smaller but you can normally gain a lot more by unrolling a loop if performance is a problem.

What operation does it remove? This is how I would imagine the compiled code:

Code: Select all

mov 0, eax (or xor eax, eax)
loop:
...
inc eax
cmp eax, 10
jle loop


Code: Select all

mov 9, eax
loop:
...
dec eax
cmp eax
jge loop



It used to be that comparing a number to zero was a single instruction (which makes sense if you've ever designed a CPU in hardware), and comparing a number to any other number would require loading both numbers into registers, subtracting one from the other, and seeing if it was zero.

In fact, I wrote code for a microcontroller two years ago that was constrained this way (you get what you pay for, heh).

Anyway, the performance gains can be big, but I'm sure they're optimized away in C/C++ now.

Edit: It still is that way. Your assembly instructions map to multiple machine instructions, and hence clock cycles. Guess you're still too high level :P

nharding
Posts: 6
Joined: Fri Nov 23, 2012 4:19 pm UTC

Re: Roller Coaster Tycoon in... Assembly?

Postby nharding » Sat Dec 08, 2012 12:11 am UTC

DavidH wrote:

Code: Select all

mov 0, eax (or xor eax, eax)
loop:
...
inc eax
cmp eax, 10
jle loop


Code: Select all

mov 9, eax
loop:
...
dec eax
cmp eax
jge loop



The cmp eax instruction is not needed in some architectures (the dec instructions sets the condition code variable), so you eliminate an instruction. It is still worth knowing these sort of things if you ever have to deal with microcontrollers like an Arduino.

User avatar
styrofoam
Posts: 256
Joined: Sat May 08, 2010 3:28 am UTC

Re: Roller Coaster Tycoon in... Assembly?

Postby styrofoam » Mon Dec 10, 2012 5:29 pm UTC

The amoun of optimizations ProGuard can do is proof the Java compiler is a
still a joke. (ProGuard is a Java bytecode optimizer that also happens to make it harder to read :p)
aadams wrote:I am a very nice whatever it is I am.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 10 guests