Saint Louis University School of Engineering and Architecture Department of Electrical Engineering

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Saint Louis University

School of Engineering and Architecture


Department of Electrical Engineering

Jump Instructions

Gole Cruz, Naomi Aira D

Date Submitted: May 4, 2020


Jump Instructions are used for changing the flow of execution of instructions in
the processor. If we want jump to any instruction in between the code, then this
can be achieved by these instructions.
There are two types of Jump instructions:
1. Unconditional Jump Instructions
2. Conditional Jump Instructions

A. Jump (unconditional branch) instructions


These instructions are used to jump on a particular location
unconditionally, i.e. there is no need to satisfy any condition for the jump to
take place. There are three types of procedures used for unconditional jump.
Unconditional branch instructions force the CPU to continue execution with an
instruction at a memory address that could be far away from the current
instruction, without testing any condition. So, the program “jumps” from one
line to a (potentially very) different line.

j label ⇐⇒ jump to label


jr $ra ⇐⇒ jump to ra

While j (jump) makes the program jump to the instruction stored at


label, jr (jump register) makes the program jump to the instruction stored at
the 32-bit memory address that is stored in the register $ra. jr is
represented using the R-type instruction format. To translate a j instruction
into machine language, the J-type instruction format is used, defined as
follows:

J-type instruction format


FIELD Meaning # of bits
OP (opcode) Basic operation 6
Address Address 26

Although a memory address is 32 bits long, the length of the


address field is only 26 bits in the J-type instruction format. Even more, for
conditional branch instructions, translated according to the I-type
instruction format, there are only 16 bits available to encode the branching
address. We will see later how this is resolved using absolute respectively
relative addressing.
Example: We would like to translate the following C code into assembly
language:
if (i==j) f=g+h;
else f=g-h;

Let’s assume that the variables are stored in the following registers:

Register Content
$0 f
$1 g
$2 h
$3 i
$4 j

A possible translation into assembly code goes as follows:

bne $3, $4, subtract # if i!=j then go to "subtract"


add $0, $1, $2 # f = g + h (skipped if i != j)
j end # continue with "end"
subtract: sub $0, $1, $2 # f = g - h (skipped if i == j)
end:
B. Conditional Jump Instructions

In these types of instructions, the processor must check for the particular
condition. If it is true, then only the jump takes place else the normal flow in
the execution of the statements is maintained.

The ALU operations set flags in the status word (Flag register). The
conditional jump statements tests the flag and jump is the flag is set.

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.
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.

jnc a
jnb a
jae a

mov ax, 4
a: mov ax, 5
ret
Generally, when it is required to compare numeric
values CMP instruction is used (it does the same as SUB (subtract)
instruction, but does not keep the result, just affects the flags).

The logic is very simple, for example:


it's required to compare 5 and 2,
5-2=3
the result is not zero (Zero Flag is set to 0).

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).

here's an example of CMP instruction and conditional jump:

include "emu8086.inc"

org 100h

mov al, 25 ; set al to 25.


mov bl, 10 ; set bl to 10.

cmp al, bl ; compare al - bl.

je equal ; jump if al = bl (zf = 1).

putc 'n' ; if it gets here, then al <> bl,


jmp stop ; so print 'n', and jump to stop.

equal: ; if gets here,


putc 'y' ; then al = bl, so print 'y'.

stop:

ret ; gets here no matter what.


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 bx, 0 ; total step counter.

mov cx, 5
k1: add bx, 1
mov al, '1'
mov ah, 0eh
int 10h
push cx
mov cx, 5
k2: add bx, 1
mov al, '2'
mov ah, 0eh
int 10h
push cx
mov cx, 5
k3: add bx, 1
mov al, '3'
mov ah, 0eh
int 10h
loop k3 ; internal in internal loop.
pop cx
loop k2 ; internal loop.
pop cx
loop k1 ; external loop.

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.

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.

References: https://jbwyatt.com/253/emu/asm_tutorial_07.html
https://www.includehelp.com/embedded-system/jump-
instructions-in-8086- microprocessor.aspx
http://eceweb.ucsd.edu/~gert/ece30/CN2.pdf

You might also like