Performing a left turn using AND, NOT, and ADD

I implement a 16-bit bit shift to rotate the bit left by r . I have access only to AND , NOT and ADD operations. There are 3 condition codes: negative, zero, and positive that are set when using any of these operations.

This is my approach:

  • AND number from 1000 0000 0000 0000 to set the condition codes to positive if the most significant bit is 1 .
  • ADD number with yourself. This shifts the bits to the left by one.
  • If the MSB was 1 , ADD 1 to the result.
  • Repeat steps (1) - (3) r .

Are there any other effective ways I can do this?

+6
assembly
source share
2 answers

Since this is homework, I will help you think about it.

 2 * 2 = 4 4 * 2 = 8 8 * 2 = 16 0010 * 0010 = 00100 0100 * 0010 = 01000 1000 * 0010 = 10000 

The left shift is an [unknown] operation. This [some unknown] operation can be implemented using AND, NOT, and ADD, making ...

+1
source share

Here is the x86 code to get started:

 ; I'm assuming the number you want to rotate is in ax ; I'm assuming the number of bits to rotate is in cx loop_start: add cx, -1 ; Can only use add, so add -1 jo loop_end ; If cx is < 0 mov bx, ax ; Copy ax into bx add ax, ax ; shift ax left 1 bit and bx, 1000000000000000b ; Test the msb of bx jz loop_start ; if the msb is zero, jump add ax, 1 ; if the msb is one, add one to ax jmp loop_start ; Loop loop_end: 
0
source share

All Articles