Skip to main content

Emulator Deep Dive - The Instruction Set Part 3

This is the third post in this series and will cover arithmetic and logic unit (ALU) instructions. The operations are pretty straightforward, and there are few caveats, if any. You can find a table of these instructions at the end of the post.

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

Prerequisites

There are a couple of prerequisites to fully understanding this series of posts. It will assume you know what a stack is and that you have some knowledge of assembly language. But the content could still be interesting even without that knowledge. I'll leave it to you to decide.

Last Time

In the last post I covered stack and memory manipulation instructions. Those instructions allowed for manipulation of the values on the stack; shuffling items around, adding items, and removing them. They can be combined with ALU instructions to produce a number of effects.

ALU instructions

These instructions apply mathematical operators to values from the stack. ALU operations operate exclusively on values from the top of the stack, replacing them with the results of the operation. Each of the instructions can modify one of more flags from the condition code (CC) register.

The current shortcomings are the lack of logical and/or, and the lack of any xor instructions. This will be remedied in a future version of the emulator.

Like stack instructions, ALU instructions have single and double byte variants. The double byte versions are formed by adding a w to the instruction mnemonic.

CC register

The CC register contains the following flags:

  • overflow - the most recent multiplication operation would require more bits than are available for the current operation size
  • negative - the result of the last operation was negative
  • carry - the result of the last operation had a carry bit left over
  • zero - the result of the last operation was zero
  • underflow - the most recent division operation generated a result lower than 1, higher than -1, but not 0
  • divide by 0 - the most recent division instruction attempted to divide a number by 0

Next Time

For the next post I'll cover the branching instructions. I'm guessing those will take the most explanation, so prepare for a longer post!

Instruction table

Instruction

Stack effect

Description

CC

add

(a b -- a+b)

Add a and b

ZCN

sub

(a b -- a-b)

Subtract b from a

ZCN

mul

(a b -- a*b)

Multiply a and b

ZCNO

div

(a b - a/b)

Integer division of a by b

ZNDU

or (bitwise)

(a b -- a|b)

Bitwise or a and b

ZN

and (bitwise)

(a b -- a&b)

Bitwise and a and b

ZN

shl

(a b -- a shl b)

a is shifted left b bits, filling the vacated bits with 0

ZC

shr

(a b -- a shr b)

a is shifted right b bits, filling the vacated bits with 0

ZC

inc

(a -- a+1)

Increments the contents of the top item on the stack

ZCN

dec

(a -- a-1)

Decrements the contents of the top item on the stack

ZCN

cmp

(a b -- )

Sets:

  • Z flag of CC to 1, N to 0 if identical,
  • Z and N to 0 if a > b
  • N flag to 1 and Z to 0, if b > a

ZN

mula

(a b c -- a*b+c)

Multiplies a by b, then adds c

ZCNO

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...