0

I would like to do bit-rotation in HACK assembly (nand2tetris). For example:

11110000 becomes: 11100001

How will I do this? I saw that normal assembly has the rol syntax that does that but I cant find one for HACK assembly

Belly
  • 33
  • 5
  • think about how you would do it in C or some other language or some other ISA without rotate but using shifts. – old_timer May 20 '21 at 11:58

2 Answers2

4

This answer is going to be somewhat Socratic in nature as the point of Nand2Tetris is to learn things, and you'll learn much more if you figure this out yourself.

There is no ROL instruction in HACK, so you have to build it from what you have available. Here are some questions to ask yourself:

  • How can I do a normal shift-left operation in HACK?
  • When is shift-left the same as rotate-left, and when is it different?
  • How can I test to see if just a shift-left will do the trick?
  • What do I do when shift-left alone isn't enough?

If you need to do a ROL on less than a full word (16-bit) value, I suggest you solve the problem for full words, then adapt it to handle the bit length you need.

  • Extra credit: if the bit length is relatively small (8 bits or less, say) and you're going to be doing this operation a lot, is there a way to make the operation much faster at the cost of other resources you have available to you?
MadOverlord
  • 1,034
  • 6
  • 11
0

First check the msb. Nand2Tetris use 2's complement if a < 0, msb = 1, else (a >= 0) msb = 0
Keep the msb 0 or 1

And, left shift means multiply by 2, a * 2 -> D = D + D or D = D + M Discard overfloww

finally, add keeping msb value...

Gyu
  • 1