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

Am I Jonesing for the Internet?

I’m feeling a little agitated and jittery today. My internet access is down due to some nasty snow and wind. Are the two related? They might be. I know I’m certainly missing my twitter friends and feeling less in touch with the world. How long is this weather going to hold? I can’t look that up. Sure, I could pull out a radio and listen in, if I had one. I might somewhere, but I’m at the mercy of the broadcaster to decide when to report the weather and how much of it to report. Some argue that internet access should be a basic human right. Does this point of view hold water? I suppose it could be argued that since the internet allows us to draw together into a larger community that it is an essential part of improving the human condition. Its use in political organizing and to connect dissidents in repressive regimes can certainly help make the case for it as a basic human right. Is the jitteriness really from not having the internet? My doctor did just increase my dose of modafi...

Piet - an esoteric programming language

There’s a certain group of programmers out there that like to come up with programming languages just for the fun of it. Some of them have profanity as their names , and some are based on internet memes . Whatever the case may be, some individual out there enjoyed thinking up the language, and many of these languages are actually useable. One esoteric language that stands out, for me, at least, is Piet , created by David Morgan-Mar. Based on the idea of making programs that look like abstract art, Piet allows the programmer to express their software in the form of coloured blocks. Numbers are represented by blocks of pixels containing a pixel count equal to the number itself. Operations are performed by changes in hue or darkness. As an example, here is a Piet program I wrote to output the string “Hello World”. This image is in fact the entirety of the program, and can be run in any of the Piet interpreters out there. Other examples of Hello World programs are available on David’s si...

Interactive Fiction - Finding the Mouse

For as long as I’ve been using computers I’ve known of text adventures. Using your imagination and the words on the screen, you construct a world in your mind and then interact with it through the computer by typing in commands. The goal is usually to solve different puzzles in order to come to some sort of winning scenario. Nowadays text adventures have given way to interactive fiction, or IF. Much like text adventures, there is often a goal to reach. However, some can be entirely freeform and offer the reader a variety of scenery and possibilities to explore. They still stick to text as their means of communication and rely on the reader to imagine the scenery. There’s still usually some sort of puzzle to solve, and this can provide a lot of fun for some. The interactive fiction community is still going strong. There are plenty of archives out there for the curious reader to explore; the most prominent being the IF Archive . There’s a newsgroup at rec.arts.int-fiction (Google Groups...