Pergunta

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?

Foi útil?

Solução

You can change

BZ R1, (B)

(B)

to

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

(B)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top