0

I am building a CPU circuit with Logisim. My CPU has only 2 general purpose registers and a 16-byte RAM. I have encoded the following instruction set (Rxy means one of the two registers)

• ADD Rxy, Rxy (add Rxy and Rxy and store result inside the first register) 
• SUB Rxy, Rxy (same but with sub)
• ST Rxy, Address (save value of Rxy into RAM address)
• LD Rxy, Address (load into Rxy value at RAM address)
• BZ Rxy, Address (branch to address if value of Rxy is zero)

I thought I could use decrement the second addend until it reaches 0 and at each step, add the first addend to itself.

For example, 5*3 = 5+5+5 ; 3-1-1-1

But I'm not sure my instruction can permit this program... I only have a branch if Rxy is equal to 0, whereas I would like to branch if not equal to 0.

My program currently looks like this :

Assume R1 is preloaded with second addends (iteration left count)

(A) LD R0, Address      # constant 1
    SUB R1, R0      # decrement iteration left
    ST R1, Address      # save iteration count in memory
    LD R0, Address      # Load first addend
    LD R1, Address      # load current total
    ADD R0, R1      # do addition
    ST R0, Address      # save new current total

BZ R1, (B)          # if iteration is 0, then display and end, else add

(B)
    STOP

Is there a way to loop with my instruction set?

Justin D.
  • 4,946
  • 5
  • 36
  • 69
  • 1
    Is the program counter a general register in your CPU architecture? If so, you could do an unconditional branch by loading the address from memory into that register. Otherwise you could load zero into a spare register and use `BZ`. – Michael Nov 28 '13 at 18:15
  • PC is another register. The control writes to it when the program executes BZ (if R0 == 0, it will put Address inside PC). The main issue is I want to loop if the remaining count is **not** and BZ will loop if it is.. – Justin D. Nov 28 '13 at 18:17
  • Sure, but if you have a way of doing an unconditional branch (lets' call it `B`, even though it may be a combination of instructions in your case) you can simulate a `BNZ` using `BZ skip` / `B loop` / `skip:` – Michael Nov 28 '13 at 18:20
  • Unfortunately, I don't have unconditional branch implemented. I agree it would be much simpler but a lot of factors make that I can't add any new instruction. – Justin D. Nov 28 '13 at 19:40
  • The first comment I made contained suggestions of how to do an unconditional branch. If none of those are possible you might be out of luck. – Michael Nov 28 '13 at 19:53

1 Answers1

1

You can change

BZ R1, (B)

(B)

to

BZ R1, (B)
LD R0, Address      # constant 1
SUB R0, R0
BZ R0, (A)

(B)
Patrik
  • 2,695
  • 1
  • 21
  • 36