Author Topic: Question Regarding How Flags Work  (Read 5434 times)

xXZexxMooreXx

  • Regular Member
  • *
  • Posts: 5
Question Regarding How Flags Work
« on: March 01, 2017, 07:14:55 PM »
Code: [Select]
mov ax, 7FF0h

add al, 10h  ; a) CF = 1, SF = 0, ZF = 1, OF = 0

add ah, 1     ; b) CF = 0, SF = 1, ZF = 0, OF = 1

add ax, 2     ; c) CF = ?, SF = ?, ZF = ?, OF = ?

a) F0 + 10 = 00 so the the ZF throws, but why the Carry (unsigned) over the the Overflow(signed)?   I'd like to guess that it's because the result of "00" isn't signed given the high bit is 0 and not 1. 

b) 7F + 01 = 80, and that's -128 signed, so why does it Overflow if it's within bounds of a signed byte? 

Flags are probably so easy that asking this question could be considered an embarrassment. 

jj2007

  • Member
  • *****
  • Posts: 13946
  • Assembly is fun ;-)
    • MasmBasic
Re: Question Regarding How Flags Work
« Reply #1 on: March 01, 2017, 07:57:42 PM »
Look at the results, perhaps that makes it easier (uppercase=flag set):
Code: [Select]
A
flags:          CZso
x:al            00   ; new result is zero, and to reach it, al had to go beyond 255 = carry!

B
flags:          czSO
x:ah            80   ; not beyond 255, but result is negative

C
flags:          czSo
x:ax            8002  ; again, not beyond 65535, but result is negative

Source:

include \masm32\MasmBasic\MasmBasic.inc      ; download
  Init
  mov ax, 7FF0h
  add al, 10h            ; a) CF = 1, SF = 0, ZF = 1, OF = 0
  deb 4, "A", flags, x:al
  add ah, 1            ; b) CF = 0, SF = 1, ZF = 0, OF = 1
  deb 4, "B", flags, x:ah
  add ax, 2            ; c) CF = ?, SF = ?, ZF = ?, OF = ?
  deb 4, "C", flags, x:ax
  Inkey
EndOfCode


Re overflow flag: the signed two's-complement result would not fit in the number of bits used for the operation or (much more detailed) http://teaching.idallen.com/dat2343/10f/notes/040_overflow.txt

xXZexxMooreXx

  • Regular Member
  • *
  • Posts: 5
Re: Question Regarding How Flags Work
« Reply #2 on: March 01, 2017, 08:55:01 PM »
Re overflow flag: the signed two's-complement result would not fit in the number of bits used for the operation or (much more detailed) http://teaching.idallen.com/dat2343/10f/notes/040_overflow.txt

After reading the text file you linked I now understand.  Thank you for your reply jj2007. 

xXZexxMooreXx

  • Regular Member
  • *
  • Posts: 5
Re: Question Regarding How Flags Work
« Reply #3 on: March 02, 2017, 02:32:10 AM »
[deleted] 
« Last Edit: March 02, 2017, 02:53:11 PM by xXZexxMooreXx »