UMBC CMSC 211 |
The comparison is the same for signed and unsigned numbers, but the conditional jumps are different.
The following will teach you how to do a GOTO. I must emphasis that you use a "Jump" only to implement Repetition and Selection, even though you have the power to do things differently!
Since we want the assembler to compute things for us, we will give the target as a label. (You can do it manually, however, if you go back and change the program later, it might change the distance of the jump and if you forget to make the change, you just broke your program! Therefore, always let the assembler worry about it.) Because of the distance limitation, we will call this a shortlabel. Actually, it could be an address, which is simply a number. Remember that when you define a label for code, it has to have a colon afterwards. Labels of pseudo-instructions such as DB, DW, EQU, PROC, ENDP do not have labels
Jumps do not alter the flags.
There is an unconditional jump, or one that is always preformed. The unconditional jump is not not limited to a short jump, and properly coded, can jump anywhere in memory!
The format is:
Instruction | Description |
---|---|
JZ shortlabel | jump if zero |
JNZ shortlabel | jump if not zero |
JS shortlabel | jump if sign |
JNS shortlabel | jump if not sign |
JC shortlabel | jump if carry |
JNC shortlabel | jump if not carry |
JO shortlabel | jump if overflow |
JNO shortlabel | jump if not overflow |
JP shortlabel | jump if parity |
JNP shortlabel | jump if not parity |
JPE shortlabel | jump if parity even |
JPO shortlabel | jump if parity odd |
Instruction | Symbolic | Description |
---|---|---|
JE shortlabel | op1 = op2 | jump if equal |
JNE shortlabel | op1 != op2 | jump if not equal |
JG shortlabel | op1 > op2 | jump if greater than |
JNG shortlabel | !(op1 > op2) | jump if not greater than |
JGE shortlabel | op1 >= op2 | jump if greater than or equal |
JNGE shortlabel | !(op1 >= op2) | jump if not greater than or equal |
JL shortlabel | op1 < op2 | jump if less than |
JNL shortlabel | !(op1 < op2) | jump if not less than |
JLE shortlabel | op1 <= op2 | jump if less than or equal |
JNLE shortlabel | !(op1 <= op2) | jump if not less than or equal |
if (ax < bx) { X = -1; } else { X = 1; }
cmp | ax, bx | ||
jl | axLess | ; go to 'axLess' if ax < bx | |
mov | X, 1 | ; This is the 'else part | |
jmp | Both | ; skip the 'then' part | |
axLess: | mov | X, -1 | ; This is the 'then' part |
Both: | ; Everyone back together here |
cmp | ax, bx | ||
jge | notLess | ; use a jump with the opposite condition | |
jmp | axLess | ; now do a long jump | |
notLess: | mov | X, 1 | ; This is the 'else part |
jmp | Both | ; skip the 'then' part | |
axLess: | mov | X, -1 | ; This is the 'then' part |
Both: | ; Everyone back together here |