OldSchool
OldSchool

Reputation: 2183

How Jump instruction is executed based on value of Out- The Alu Output

j1, j2 and j3 are bits for the jump instruction and comp instructions are the computation instructions like add etc.

Figure from The Elements of Computer System (Nand2Tetris)

Have a look at the scenario where

 j1 = 1 (out < 0 )
 j2 = 0 (out = 0 )
 j3 = 1 (out > 0 )

How this scenario is possible as out < 0 is true as well as out > 0 but out = 0 is false. How out can have both positive and negative values at the same time?

In other words when JNE instruction is going to execute although it theoretically seems possible to me but practically its not?

Upvotes: 1

Views: 1001

Answers (2)

Peter Cordes
Peter Cordes

Reputation: 364160

The mnemonic makes sense if those are match-any conditions, not match-all. i.e. jump if the difference is greater or less than zero, but not if it is zero.

Specifically, sub x, y / jne target works the usual way: it jumps if x and y were equal before the subtraction. (So the subtraction result is zero). This is what the if(out!=0) jump in the Effect column is talking about.

IDK the syntax for Nand2Tetris, but hopefully the idea is clear.


BTW, on x86 JNZ is a synonym for JNE, so you can use whichever one is semantically relevant. JNE only really makes sense after something that works as a compare, even though most operations set ZF based on whether the result is zero or not.

Upvotes: 1

Yuval Filmus
Yuval Filmus

Reputation: 413

If out < 0, the jump is executed if j1 = 1.

If out = 0, the jump is executed if j2 = 1.

If out > 0, the jump is executed if j3 = 1.

Hopefully now you can understand the table better. In particular, JNE is executed if out is non-zero, and is skipped if out is zero.

Upvotes: 3

Related Questions