Mips Instructions
Mips Instructions
Mips Instructions
Introduction to computer
architecture
Branch instructios
● Conditional branches
– Execute if some condition holds
– Transfer the program control to a different point
based on a condition
● Unconditional branch
– Take the branch without checking any condtion
– Transfer the program flow to a different point
– Also called as jumps
Branch equal
● Syntax: beq r1, r2, <target>
● Sematics:
– Transfer the program control to the given target address of r1
== r2
– Target is a label in the code (which will later be replaced with
an address)
● Examples
beq r1, r2, if
…..
if:
Branch NOT equal
● Syntax: bne r1, r2, <target>
● Sematics:
– Transfer the program control to the given target address of r1
!= r2
– Target is a label in the code (which will later be replaced with
an address)
● Examples
bne r1, r2, if
…..
if:
Other banch instructions
● branch greater than zero
– Syntax: bgtz r1, <target>
– Sematics: take the branch if r1 > 0
● Branch greater than or equal to zero
– Syntax: bgez r1, <target>
– Sematics: take the branch if r1 >= 0
Other banch instructions ...
● branch less than zero
– Syntax: bltz r1, <target>
– Sematics: take the branch if r1 < 0
● Branch less than or equal to zero
– Syntax: blez r1, <target>
– Sematics: take the branch if r1 <= 0
Converting to instructions
● Some of the conditions provided by the higher
level language are much more expressive than
this
● Compiler will convert then to suitable
operations and use these hardware instructions
to implement them.
● Example: if(x + 1 > y)
Thinking time …
● How to convert the following C-code into
assembly
if(x+1 < y && y > 10)
z ++;
else
z –;
Note on the target
● The taget given in the assembly code in encoded using
16bits
– The address field in the j-type instructions
● There is a limit to the target address
– It should be within the 216 byte limit
● If the actual function is outside of this limit another
instruction will be placed there to make the real branch.
● The label will be replaced with the actual address at the link
time
– Assembel → link → final executable
Address encoding
● Size aligned address:
– Almost all computers requires you to align data to its size
– That is, if the data item is 4bytes its address should be divisible
by 4 (or 2 LSBs should be zero)
● Since all instructions are 32bit and they are stored aligned
to the size the last two bits of any instruction should be
zero
– So, we do not include the last 2bits in the encoding (or divide the
address by 4)
– When taking the branch, recompute the address by adding
2more bits (or multiply with 4)
Jump instruction
● In addition to conditional branches MIPS also provide
jump which transfer the control always
● Two type of jump
– Syntax: j <taget address>
– Sematics: jump to the given address. Address has a limited
range (you cannot go to any place in the address space)
– Synaxt: jr r1
– Sematics: jump to the address given in the register. No limit on
the range. However, need to use an additional register for this.
Thinking time …
● Are these enogh?
– Can we implement all branches provided in a
typical programming language