Professional Documents
Culture Documents
8086 Assembler Tutorial For Beginners (Part 7)
8086 Assembler Tutorial For Beginners (Part 7)
unconditional jumps
The basic instruction that transfers control to another point in the program
is JMP.
JMP label
To declare a label in your program, just type its name and add ":"
to the
end, label can be any character combination but it cannot start with
a
number, for example here are 3 legal label definitions:
label1:
label2:
a:
Label can be declared on a separate line or before any other instruction, for
example:
x1:
MOV
AX, 1
org 100h
calc:
stop:
https://jbwyatt.com/253/emu/asm_tutorial_07.html 1/7
5/11/21 15:43 8086 assembler tutorial for beginners (part 7)
As you can see from this example JMP is able to transfer control both
forward and backward.
It
can jump anywhere in current code segment (65,535 bytes).
Unlike JMP instruction that does an unconditional jump, there are instructions
that do a
conditional jumps (jump only when some conditions are in act).
These instructions are divided
in three groups, first group just test single flag, second
compares numbers as signed, and third
compares numbers as unsigned.
Opposite
Instruction Description Condition
Instruction
as you may already notice there are some instructions that do that same thing, that's correct,
they
even are assembled into the same machine code, so it's good to remember that
when you
compile JE instruction - you will get it disassembled as: JZ, JC is assembled the same as JB
etc...
different names are used to make programs easier to understand, to code and most importantly
to remember. very offset dissembler has no clue what the original instruction was look like that's
why it uses the most common name.
if you emulate this code you will see that all instructions are assembled into JNB, the
operational code (opcode) for this instruction is 73h this instruction has fixed length of two
bytes, the second byte is number of bytes to add to the IP register if the condition is true.
because the instruction has only 1 byte to keep the offset it is limited to pass control to -128
bytes back or 127 bytes forward, this value is always signed.
https://jbwyatt.com/253/emu/asm_tutorial_07.html 2/7
5/11/21 15:43 8086 assembler tutorial for beginners (part 7)
jnc a
jnb a
jae a
mov ax, 4
a: mov ax, 5
ret
ZF = 0
Jump if Greater (>).
JG , JNLE and JNG, JLE
Jump if Not Less or Equal (not <=).
SF = OF
ZF = 1
Jump if Less or Equal (<=).
JLE , JNG or JNLE, JG
Jump if Not Greater (not >).
SF <> OF
https://jbwyatt.com/253/emu/asm_tutorial_07.html 3/7
5/11/21 15:43 8086 assembler tutorial for beginners (part 7)
(not >=).
Jump if Carry.
CF = 1
Jump if Below or Equal (<=).
JBE , JNA or JNBE, JA
Jump if Not Above (not >).
ZF = 1
Another example:
it's required to compare 7 and
7,
7-7=0
the result is zero! (Zero Flag
is set to 1 and JZ or JE will do the jump).
include "emu8086.inc"
org 100h
stop:
try the above example with different numbers for AL and BL,
open flags by clicking on flags
button, use single step
and see what happens. you can use F5 hotkey to recompile and reload the
program into the emulator.
loops
https://jbwyatt.com/253/emu/asm_tutorial_07.html 4/7
5/11/21 15:43 8086 assembler tutorial for beginners (part 7)
DEC CX and
LOOP decrease cx, jump to label if cx not zero.
JCXZ
decrease cx, jump to label if cx not zero and not equal (zf
LOOPNE LOOPE
= 0).
OR CX, CX and
JCXZ jump to label if cx is zero.
JNZ
loops are basically the same jumps, it is possible to code loops without using the loop
instruction, by just
using conditional jumps and compare, and this is just what loop does. all
loop instructions use CX register to count steps, as you know CX register has 16 bits and the
maximum value it can hold is 65535 or FFFF, however with some agility it is possible to put
one loop into another, and another into another two, and three and etc... and receive a nice value
of 65535 * 65535 * 65535 ....till infinity.... or the end of ram or stack memory. it is possible
store original value of cx register using push cx instruction and return it to original when the
internal loop ends with pop cx, for example:
org 100h
mov cx, 5
int 10h
push cx
mov cx, 5
int 10h
push cx
mov cx, 5
int 10h
pop cx
pop cx
ret
bx counts total number of steps, by default emulator shows values in hexadecimal, you can
double click the register to see the value in all available bases.
https://jbwyatt.com/253/emu/asm_tutorial_07.html 5/7
5/11/21 15:43 8086 assembler tutorial for beginners (part 7)
just like all other conditional jumps loops have an opposite companion that can help to create
workarounds, when the address of desired location is too far assemble automatically assembles
reverse and long jump instruction, making total of 5 bytes instead of just 2, it can be seen in
disassembler as well.
for more detailed description and examples refer to complete 8086 instruction set
All conditional jumps have one big limitation, unlike JMP instruction
they can only jump 127
bytes forward and 128 bytes backward (note that most
instructions are assembled into 3 or more
bytes).
label_x: - can be any valid label name, but there must not be two or more labels with the same
name.
here's an example:
include "emu8086.inc"
org 100h
mov al, 5
mov bl, 5
jmp equal
not_equal:
add bl, al
sub al, 10
xor al, bl
jmp skip_data
skip_data:
stop:
ret
https://jbwyatt.com/253/emu/asm_tutorial_07.html 6/7
5/11/21 15:43 8086 assembler tutorial for beginners (part 7)
Note: the latest version of the integrated 8086 assembler automatically creates a workaround by
replacing the conditional jump with the opposite, and adding big unconditional jump. To check if you
have the latest version of emu8086 click help-> check for an update from the menu.
org 100h
jmp $3+2
a db 3 ; 1 byte.
b db 4 ; 1 byte.
c db 4 ; 1 byte.
mov bl,9
dec bl ; 2 bytes.
ret
https://jbwyatt.com/253/emu/asm_tutorial_07.html 7/7