Assembly Language¶
This page won’t describe the syntax of Assembly Language, but lists commonly used commands for quick reference.
Flags¶
There are four flags in CPU, Zero, Overflow, Carry, Sign.
Zero Flag (ZF)¶
Zero flag is set iff the result is 0, and is unset iff the result is not 0
Overflow Flag (IF)¶
Overflow flag is set iff
both operands are of the same sign,
the operation causes the sign bits to change.
E.g.
0100 + 0100 = 1000 (overflow=1, because the first bit, the sign bit, changes from 0 to 1)
1000 + 1001 = 0001 (overflow=1, because the first bit, the sign bit, changes from 1 to 0)
But the following four operation will turn off the overflow flag.
E.g.
0100 + 0001 = 0101 (overflow=0, because both numbers are not of the same sign)
0110 + 1001 = 1111 (overflow=0, because both numbers are not of the same sign)
1000 + 0001 = 1001 (overflow=0, because both numbers are not of the same sign)
1100 + 1100 = 1000 (overflow=0, even though both numbers are of the same sign, but the new sign is the same as the original sign)
Carry Flag (CF)¶
Carry flag is set iff either of the following two cases happens:
The addition of two numbers causes a carry out of the most significant (leftmost) bits added.
E.g. 1111 + 0001 = 0000 (carry=1)
2. the subtraction of two numbers requires a borrow into the most significant (leftmost) bits subtracted. E.g. 0000 - 0001 = 1111 (carry=1)
Otherwise, carry flag is unset: E.g.
0111 + 0001 = 1000 (carry=0)
1000 - 0001 = 0111 (carry=0)
In unsigned arithmetic, watch the carry flag to detect errors, since the carry flag is set to 1 may infer the flipping of sign. In signed arithmetic, the carry flag tells you nothing interesting.
Sign Flag (SF)¶
Sign flag is set iff the result is negative, and is unset iff the result is positive. When the result is 0, it’s not changed.
Commands¶
MOV¶
Assign the first operand with the second operand.
MOV AX,0 ; AX=0
MOV [678],123 ; memory location [DS:678]=123
CMP¶
Compare the first operand with the second operand.
CMP DX, 0 ; compare DX with 0. Set flags
JMP¶
Unconditional jump to the destination
JMP 0x8200 ; jump to address of 0x8200
Easy Jumps¶
Comparing to JMP, easy jump is triggered only if certain flags are set/unset
JE/JZ and JNE/JNZ¶
JE/JZ: Jump if equal <=> Jump if ZF is set
JNE/JNZ: Jump if not equal <=> Jump if ZF is not set
JC and JNC¶
JC: Jump if CF is set
JNC: Jump if CF is unset
JS/JNS¶
JS: Jump if SF is set
JNS: Jump is SF is not set
JO/JNO¶
JO: Jump if OF is set
JNO: Jump if OF is not set
Unsigned Jumps (Above/Below)¶
JAE/JNB¶
Jump if Above or Equal / Not Below (>=)
JA/JNBE¶
Jump if Above / Not Below or Equal (>) (CF=0 && ZF=0)
JB/JNAE¶
Jump if Below / Not Above or Equal (<)
JBE/JNA¶
Jump if Below or Equal / Not Above (<=)
Signed Jumps (Less/Greater)¶
JG/JNLE¶
Jump if Greater. (SF==OF && ZF==0)
JL/JNGE¶
Jump if Less / Not Greater or Equal (SF != OF)
JLE/JNG¶
Jump if Less or Equal / Not Greater (SF != OF && ZF=1)
JCXZ/JECXZ¶
JCXZ: Jump if CX register equals zero
JECXZ: Jump if ECX register equals zero