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

What Kind of Games?

I started programming when I was young, with the hopes of writing video games. I think a lot of kids start that way. When you like something, or someone, you try to emulate what you’re seeing. But how has that early dream turned out? They tell writers to write what they know. It’s good advice. How can you write about life in the Serengeti without have someone to give you a first hand account or having been there yourself? You can always use your imagination, and that’s all you can really do when writing fantasy or science fiction. It works for writing video games. How can you expect to write a genre you don’t immerse yourself in? These days I spend most of my gaming time playing casual games. I’m busy doing other things, and don’t want to spend long stretches just sitting at the console or computer. Recently I read an article about the kind of video games the most people tend to flock to. Typically they’re games that are relatively simple and involve sorting things in some way. It

KnitPicker - a knitting stitch counter programming project

The art of knitting lace is an intricate process; it takes great skill with knitting and the ability to keep track of complex knitting instructions. One day, my wife Sophie was knitting a particularly complex pattern, and she complained about not being able to keep track of how many stitches she should have for a particular row or how many should be left in the next row. Enter the programmer! I’ve been throwing together little utilities for myself for ages. Sometimes I want to calculate some value that would take too long by hand; sometimes I want to sort a list in some particular way. Other times I have trouble deciding what to have for dinner, so I’ll write up a list of options and then pop in a random number to pick from it. I have one such script from ages ago that I used regularly when living in Montreal. It was clear to me that Sophie needed software to handle her problem. With instructions spanning up to a hundred stitches, it’s the sort of thing that’s hard to keep track of