assembly - ASM8086: mul, imul, carry flag and overflow flag -
i understood logic of carry flag , overflow flag. but, when read program (wrote in masm 8086) , got perplexed bit.
the program intent tell if quadratic equation has 2 distincts solutions, 2 equals solutions or no solutions @ all.
.model small .stack .data aa dw 2 bb dw 4 cc dw 2 sol_msg db "there exist 2 real solutions", cr, nl no_sol_msg db "no real solutions! ", cr, nl sol_coinc db "the 2 solutions coincide! ", cr, nl .code .startup mov ax, bb imul bb jc overflow ; decided work @ 16-bit numbers push ax mov ax, aa imul cc jc overflow mov bx, 4 imul bx jc overflow pop bx sub bx, ax jo overflow js mess2 jz mess3 lea si, sol_msg jmp next mess2: lea si, no_sol_msg jmp next mess3: lea si, sol_coinc next: mov bx, lung_msg mov ah, 2 loop1: mov dl, [si] int 21h inc si dec bx jnz loop1 jmp end1 overflow: nop end1: .exit end
now, doubt is: why first 3 checks has been tested carry flag , last 1 overflow flag?
since in last one, subtraction between 2 signed numbers (as think those), have check overflow flag see if there's overflow (i.e. if numbers goes beyond interval[-2^15,2^15-1]
). first one, example, (bb)^2
imul
.
so think them 16-bit signed numbers (so -2^15 <= bb <= 2^15-1
) and, multiplication set cf
/of
bit on if @ least 1 sum (in multiplication algorithm) cf
/of
bit on.
but since treat signed numbers, shouldn't check overflow flag ?
also noticed that, since 2^15-1=32767
, if set bb
190
(190^2=36100
) cf=0
; if bb
equals 200
(200^2=40000
) cf=1
.
why that? explain me in detailed way, please?
p.s.: i'm using emu8086.
if have @ pseudo algorithm of imul, you'll see
if operandsize = 16 tmp_xp ← ax ∗ src (* signed multiplication; tmp_xp signed integer @ twice width of src *) dx:ax ← tmp_xp[31:0]; sf ← tmp_xp[15]; if signextend(tmp_xp[15:0]) = tmp_xp cf ← 0; of ← 0; else cf ← 1; of ← 1; fi; fi;
cf
, of
either both set, or both cleared. so, can check either flag after imul
Comments
Post a Comment