简体   繁体   中英

How to detect overflow flag?

In the following question, i wonder why OVERFLOW flag is set to 1. I know that overflow can only happen when adding two numbers of the same sign and getting a different sign. Is this is a mistake in the solution of the book or what?

在此处输入图片说明

Those look like 8-bit, unsigned numbers. The result is 9 bits, which would overflow an 8-bit register (hard to find, these days). If these were signed numbers, they would, indeed have different signs (-1 and 1). However, the image shows a nine-bit sum, not simply 0 (or even 00000000). So those are unsigned 8-bit or there's an error in "the book". Notice the half-carry bit is set. This suggests 1/2 of a 16-bit register is being used. That happens all the time.

The statement about the signs being different making an overflow impossible -- is just restating the simple relationship about adding 2 numbers of the same bit-size, one of which is positive, the other negative. The largest 8-bit positive number is 127. The largest negative 8-bit number is -128. It's relatively easy to do the math. Start with 127, and add every possible negative number... 127+-1=126, 127+-2=125, 127+-3=124, ... 127+-126=1, 127+-127=0, 127+-128=-1. None of the sums is either greater than 127 or less than -128, so no overflow occurs. You can finish the proof by adding every positive number to -128.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM