##sub_addsub_imm_execute
bits(datasize) result;
bits(datasize) operand1 = if n == 31 then SP[] else X[n];
bits(datasize) operand2 = imm;
bits(4) nzcv;
bit carry_in;

if sub_op then
    operand2 = NOT(operand2);
    carry_in = 1;
else
    carry_in = 0;
end

(result, nzcv) = AddWithCarry(operand1, operand2, carry_in);

if setflags then 
    PSTATE.<N,Z,C,V> = nzcv;
end

if d == 31 && !setflags then
    SP[] = result;
else
    X[d] = result;
end
@@
