Subtracting a large unsigned binary number from a smaller one

前端 未结 2 1121
独厮守ぢ
独厮守ぢ 2021-02-19 22:22

I\'m taking a computer organization and assembly language course. The written part of our lab this week has a question on it that has me stumped. The question reads...

相关标签:
2条回答
  • 2021-02-19 22:36

    simply subtract the two binary numbers as they are, then take the 2's complement of the result. voila!

    0 讨论(0)
  • 2021-02-19 22:40

    You do it the same way irrespective of which number is bigger and which is smaller.

     bb b      bb   <- borrows
     0101 0111 1101 (1405)
    -1110 1011 0110 (3766)
     --------------
     0110 1100 0111 (1735?)
    

    Now, if you want a proper difference, you need to take into account the overflow since the above result doesn't include the sign bit:

     b bb b      bb   <- borrows
     0 0101 0111 1101 (1405)
    -0 1110 1011 0110 (3766)
     ----------------
     1 0110 1100 0111 (-2361 signed 2's complement)
    

    Really, the CPU doesn't care what gets subtracted from what. It uses the same algorithm for integer addition/subtraction, moreover, this algorithm is the same for signed and unsigned integers. You only have to correctly interpret the result and the carry and overflow flags. That's all.

    0 讨论(0)
提交回复
热议问题