简体   繁体   中英

Carry flag of the operation 0 - 1 in 8 bit register

I tried the following code in emu 80x86 IDE:

mov al,0h
sub al,1h

In the output it displays in the emulator al=FF and I did understand it, but what I didn't understand is why in the flags section cf=1

Carry flag CF is used when CPU works with unsigned integer numbers (in 8bit registers they can have value between 00h and FFh).

When addition is performed and the result exceeds the maximal value FFh, CF signalizes that it happened, and that number 1 should be added to the register with higher order ( ah ).

When subtraction is performed and the result is below the minimal value 00h, CF signalizes that it happened and that number 1 should be borrowed (subtracted) from ah .

In your example code the result of subtracting 1h from the 0h in al is below the allowed minimal value, that's why CF is set.

Setting cf=1 CPU indicates unsigned subtraction underflow.

If you look at those numbers as signed integers , their allowed range is -128 to +127 (80h to 7F) and overflow or underflow is signalized with different flag of . In your code the result (treated as signed number) is -1 (FFh) and this is within the allowed range (no overflow), so you should see in emulator that of=0 .

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