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

Living with Bipolar Disorder

Feeling down when something bad happens is normal. Feeling elated when something good happens is great. Bipolar disorder is neither. Previously referred to as manic depressive disorder, bipolar disorder consists of mood episodes ranging from deep, dark depression to the sense of invincibility and superiority that is characteristic of a manic phase. It disrupts the lives of those who suffer from it and all those around them. Luckily, however, with the right course of treatment it can be kept under control. Bipolar disorder has been found to be linked genetically and physiologically to schizophrenia. A person suffering from bipolar disorder can experience the same sort of hallucinations and delusions as someone with schizophrenia. Both have a genetic component, with a number of genes interacting to create a pre-disposition to these disorders. The families of those afflicted often include others with one of these disorders, mood disorders or alcoholism. Depression is a common symptom ...

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