joey rohan
joey rohan

Reputation: 3566

Division of two binary numbers

So here I am facing a problem.I have only 25 instructions in my instruction set.(no multiply,divide,subtract, increment instruction). now, I am trying to preform a binary division.My problem is, how can I know if the divisor is smaller then the dividend in order to subtract them both (in 2's compliment form)?

INSTRUCTION SET: enter image description here

Upvotes: 0

Views: 1660

Answers (2)

Seva Alekseyev
Seva Alekseyev

Reputation: 61378

I'm assuming AC is a register (accumulator). So what you need is a SPA command while AC has the result of the subtraction of the two numbers. Or a SNA, depends on what you're subtracting from what.

Again, you don't have subtraction. Replace it with addition of one number and a negative of another. Negative is complement plus two, like Ferruccio said.

EDIT: SPA/SNA works by skipping the next command if the AC is positive/negative. So if you want to have an if statement with a nontrivial body, you'd want to put an Sxx followed by a BUN (branch unconditionally). If the condition is true, the branch is skipped, if the condition is false - branch is executed.

Note that it's an inversion of the conventional logic of assembly. Normally, it's "branch if condition is true"; on this machine, it's "branch if the condition is false".

For the record, your instruction set is deliberately stunted. Real life CPUs are more programmer friendly than that, even the RISC kind.

Upvotes: 2

Ferruccio
Ferruccio

Reputation: 100668

Many instructions can be decomposed into simpler instructions.

It seems like you need a compare instruction.

If you don't have a compare instruction, you can simulate it using a subtract and checking the status of the processor flags after the subtract.

You don't have a subtract instruction, so you can simulate it by adding the negative of the subtracted value.

If you don't have a negate instruction, you can simulate it in two's complement arithmetic by flipping the values of all the bits and adding 1.

Upvotes: 2

Related Questions