Skip to main content

Emulator Deep Dive - The Instruction Set: Part 4

This is the fourth part in this series, and will cover flow control instructions.

You can find the first post here, the second here, and the third here.

Prerequisites

None of the flow control operations use the data stack, and only two make use of the return address stack, so knowing about stacks is less important in this post than in the others. Knowledge of other assembly language variants would definitely be an asset, as these follow the same patterns found in other processors.

Last Time

The last post covered arithmetic and logic unit (ALU) instructions, which modify the condition code (CC) register. Many of the flow control instructions make use of the CC flags to determine whether or not to move execution flow elsewhere.

Jumps

Jump (jmp, jsr, rts) instructions change the program counter (PC) to an absolute address in memory. They are all unconditional, that is, the change in the PC happens regardless of the system's state.

jmp changes the PC to an address specified as immediate operand to the instruction without side effects.

jsr changes the PC, but pushes the address of the next instruction after jsr to the return address stack. This is paired with rts which pulls the PC from the top of the return address stack. These allow for subroutine calls and returns and allow for more structured programming than jmp or any of the branching instructions.

Branches

Branch instructions use a single byte, twos-complement signed address offset as their operand. This allows for faster changes to the PC, as a single byte can be loaded faster by the CPU. Additionally, except for the unconditional b branch, each branch instruction will check against some CC flag or flags.

Conditional Branches

  • beq - branch on equal
    • branches when the zero flag on the CC is set
  • blt - branch on less-than
    •  branches when the negative flag on the CC is set
  • ble - branch on less-than-or-equal
    •  branches when either the zero or negative CC flags are set
  • bcr - branch on carry
    •  branches when the carry flag on the CC is set
  • bov - branch on overflow
    •  branches when the overflow flag on the CC is set
  • bdiv0 - branch on divde-by-zero
    •  branches when the divide-by-zero flag on the CC is set

At the moment there is no branch-on-underflow instruction, or any greater-than branches. There's space to put them, but I haven't gotten around to it yet.

The End?

That concludes the instruction set portion of these deep dives. Next up is the assembler itself. Included will be the well loved Hello World! example, among others. Full knowledge of all the system calls is not necessary, each one used will be explained as it's used.

Comments

Popular posts from this blog

Losing a loved one, as an atheist

When I was around 11 or 12, I started to question the received wisdom that there was a deity. I came to the conclusion that all signs pointed to no. Do I outright, unequivocally and without reservation deny the existence of such an entity? No. However, I don’t see it as a likely scenario, and until I’m presented with hard evidence, I have enough reason to say that there isn’t. Some people take comfort in their religious beliefs, especially their belief in an afterlife. I have no interest in an afterlife, either for myself of my loved ones. In the past five years I’ve lost both my grandmother and mother, both of whom I loved dearly. No amount of belief in an afterlife would soothe my pain. I mourn at the fact that they are lost from my life, right here, right now. I imagine it’s the same even for those who believe that the dead pass on to somewhere else. You can’t escape the fact that their tangible presence is forever gone from your life. Unless you believe in ghosts, but that’s a ...

If I Grow Up

I’ve figured out what I want to be if I grow up: a writer. In fact, by writing this I’m living the dream, aren’t I? I guess things have been building up to this my whole life; I’ve been reading since I could, and writing even when I didn’t have to. Whatever my mood has been, as long as it wasn’t too severe, I’ve always written. When I was down, I wrote depressing poetry or prose. When I was up, I wrote whatever popped into my head. Now that I’m stable, I can take the time to write coherent, sensible articles. What are you going to write? It’s a good question, and I like it quite a bit. I’m going to write articles here, like the ones I’ve written so far. I’m going to write for HandmadeNews.org , with my first article there out just recently. I’ll always be writing emails. Sometimes I’ll write things down in my notebooks. If I feel like my writing is worthy of it, I might write a screenplay Josh Olson would enjoy reading. I wont bring it to him to read, though. If it’s good enough ...

SpaceRat - an exercise in game programming

Almost entirely throughout my life, I’ve had access to computers. In most of that time I’ve wanted to write computer games. Most of my attempts have been aborted early on, before even getting code written. Stacks and stacks of paper have been used to sketch characters and scenery. One day in 2001 I decided to put together a small Space Invaders-like game named SpaceRat . SpaceRat began as a set of graphics I had created for a friend’s clone of Space Invaders. I had drawn a “hero” space ship and some enemy ships. There were a number of enemy sprites that I did not use in SpaceRat, simple because they did not fit the look I was going for. Starting off with pre-made graphics gave me the push I needed and gave me the opportunity to jump straight into programming the game. I was still in university, but was then employed in a co-operative education placement for discreet, a division of AutoDesk . Health problems prevented me from actually working at the time, and so to judge my ability ...