In: Computer Science
The language is Assembly Language. Thank you!
Assignment 3:
Run the following code.
Record what each of the flag changes are for each command.
INCLUDE Irvine32.inc
.data
.code
main PROC
mov al,255
add al,1
call DumpRegs
sub al,1
call DumpRegs
sub al,1
call DumpRegs
exit
main ENDP
END main
INCLUDE Irvine32.inc
.data
.code
main PROC
comment: - adding 1 to 255 rolls AL over to zero:
mov al,255;
add al,1; AL=0, CF=1( unsigned overflow)
call DumpRegs
subtracting larger number from smaller:
sub al,1
call DumpRegs
sub al,1; -- AL=255, CF=1
call DumpRegs
exit
main ENDP
END main
comments:--
There are two flags used to track when a value overflows.
One is the unsigned overflow, CF, and the other is the signed overflow OF.
The CF flag is set when you do an addition that goes over the maximum value a register can hold. In your case any addition that goes over 255.
For subtractions, same thing, the other way around. If you subtract and it goes under 0, then you get a carry.
The CF flag allows you to do additions on very large numbers without the need for specialized code:
add eax, ebx
adc ecx, edx
This adds two 32 bit numbers together (eax and ebx) and then another set of 32 bit numbers (ecx, edx) taking the carry in account. The result is ebx/edx representing a 64 bit number. You can use that for numbers of any size (i.e. you could write code to add two numbers of 1024 bits each.)
If your number is unsigned, you could add a jc overflow at the end, if it jumps, then you have an overflow (your number requires 65 bits after the addition.)
The OF flag is always set, however, it is generally only used on the last addition to know whether you had an overflow in the event your number is signed. If your number is always unsigned, then OF is never used in your code (obviously, the processor always set OF no matter what since it does not know whether you are working with a signed or unsigned number.)
So in our previous addition, you would do:
add eax, ebx
adc ecx, edx
jo overflow
When the jump to overflow happens, your 64 bit addition does not fit a 64 bit number. You would need 65 bits. That can be used to generate an exception and let the user know that his math is incorrect. The OF flag, of course, would work on AL in which case that value is viewed as a number between -128 and +127. If an add generates a number larger than 127, or a subtract a number smaller than -128, then OF gets set.