The task is given: to replace the combination 1011 in the given word with the combination 1101.

I do not fully understand how to do this task:

  1. I copy the data to the register
  2. I select the low 4 bits through AND with the mask 1111
  3. I do CMP c 1011.
  4. Depending on the result, I do JMP
  5. I write the result in another register (I'm trying to collect the result in another register)
  6. I restore the original data in the register (Before applying AND) and make a shift by 1
  7. I perform all the checks again
  8. Now we need to combine the results of this iteration with step 5 ... Here at this step I do not understand what to do ... How can I combine the results of the current iteration with the results of the previous iteration in the result register?

I would be grateful if you tell me how to do this or even better, throw an example on TASM.

  • Hm And what should come out of the word 10111111b ? Depending on whether you are checking from left to right or right to left, there will be different results. - VladD
  • I was told to compare from right to left. - iluxa1810

1 answer 1

Try this:

  1. Current mask = 1111b, current number from data to register, current offset 0
  2. Cycle:
  3. Select addition mask ( not ) to another register
  4. Apply the mask to the number ( and ) and shift to the right by the value of the current shift.
  5. Check result, compare with 1011b
  6. If not equal, go to step 8.
  7. Replace. To do this, we calculate the part of the number outside the mask ( and with the addition of the mask), the new part “under” the mask ( 1101b shifted to the left by the value of the current shift) and connect them ( or )
  8. Shift the mask to the left by 1 bit, increase the current shift by 1
  9. If the current shift is still less than the data width, return to step 2.

I will not paint in assembly language, after all a training task.

Key idea: to divide data into parts under the mask and the rest.

Possible optimization: the cycle can be rotated only as long as the current shift + 3 is less than the data width. Win 3 iterations.

  • And in step 4, we do not accidentally lose the senior level by using and? - iluxa1810
  • @ iluxa1810: Well, the result of the operation from step 4 we put in another register, without overwriting the current number. I hope there are enough registers. - VladD
  • I was able to write such code, but for some reason it does not work correctly: I uploaded the code here: rghost.ru/8yhjMWhSQ as if to insert it into the comment, then the formatting is lost = ( - iluxa1810
  • @ iluxa1810: I did not find the addition of a mask in your code. Did you do as in the answer, or in the question? If as in question, then it is wrong. PS: it is better to spread the code on pastebin.com. - VladD pm