Aca R20 Unit-2
Aca R20 Unit-2
Aca R20 Unit-2
Answer: A MIPS instruction operates on two source operands and places the result in one
destination operand. Hence, the two simple statements above compile directly into these two
MIPS assembly language instructions:
add a, b, c
sub d, a, e
Answer: The compiler must break this statement into several assembly instructions, since
only one operation is performed per MIPS instruction.
The first MIPS instruction calculates the sum of g and h. We must place the result
somewhere, so the compiler creates a temporary variable, called t0:
add t0,g,h # temporary variable t0 contains g + h
The second instruction places the sum of i and j in another temporary variable created by the
compiler, called t1:
add t1,i,j # temporary variable t1 contains i + j
Finally, the subtract instruction subtracts the second sum from the first and places the
difference in the variable f, completing the compiled code:
sub f,t0,t1 # f gets t0 – t1, which is (g + h) – (i + j)
2
Memory Operands
Data transfer instruction: A command that moves data between memory and registers.
Address: A value used to define the location of a specific data element within a memory
array. To access a word in memory, the instruction must supply the memory address.
Memory is just a large, single-dimensional array, with the address acting as the index to
that array, starting at 0.
For example, in Figure, the address of the third data element is 2, and the value of Memory
[2] is 10.
The data transfer instruction that copies data from memory to a register is traditionally
called load. The actual MIPS name for this instruction is lw, standing for load word.
lw $s1,20($s2) # $s1=Memory[$s2+20]
The instruction complementary to load is traditionally called store; it copies data from a
register to memory. The actual MIPS name is sw, standing for store word.
sw $s1,20($s2) # Memory[$s2+20]=$s1
For example, Figure shows the actual MIPS addresses for the words in Figure the byte
address of the third word is 8.
Alignment restriction: A requirement that data be aligned in memory on natural
boundaries.
In MIPS, words must start at address that is multiples of 4. This requirement is called an
alignment restriction.
Fig: Actual MIPS memory addresses and contents of memory for those words.
The arithmetic instructions in which one operand is a constant. This quick add instruction
with one constant operand is called add immediate or addi. To add 4 to register $s3, we just
write
addi $s3,$s3,4 # $s3 = $s3 + 4
For example, add instruction where one operand is zero. Hence, MIPS dedicates a register
$zero to be hard-wired to the value zero.
add $t2, $s1, $zero # $t2 gets $s1
Generalizing the point, in any number base, the value of ith digit d is
d ×Basei
We subscript decimal numbers with ten and binary numbers with two. For example,1011two
represents
(1 x 23) + (0 x 22) + (1 x 21) + (1 x 20)ten
= (1 x 8) + (0 x 4) + (1 x 2) + (1 x 1)ten
= 8 + 0 + 2 + 1ten
= 11ten
We number the bits 0, 1, 2, 3, . . . from right to left in a word. The drawing below shows the
numbering of bits within a MIPS word and the placement of the number 1011two:
Least Significant Bit: The Rightmost Bit in a MIPS word. Most Significant Bit: The
Left most bit in a MIPS word.
Hence, the phrase least significant bit is used to refer to the rightmost bit (bit 0 above)
and most significant bit to the left most bit (bit 31).
The MIPS word is 32 bits long, so we can represent 2 32 different 32-bit patterns. It is
natural to let these combinations represent the numbers from 0 to 232-1(4,294,967,295ten).
4
That is, 32-bit binary numbers can be represented in terms of the bit value times a power of 2
(here xi means the ith bit of x):
Signed Numbers
Computer programs calculate both positive and negative numbers, so we need a
representation that distinguishes the positive from the negative.
The most obvious solution is to add a separate sign, which conveniently can be represented in
a single bit; the name for this representation is sign and magnitude.
Leading 0s mean positive, and
Leading 1s mean negative.
This convention for representing signed binary numbers is called two’s complement
representation.
The positive half of the numbers, from 0 to 2,147,483,647 ten (231-1), use the same
representation as before. The following bit pattern (1000 . . . 0000 two) represents the most
negative number -2,147,483,648ten(-231).
By recognizing the role of the sign bit, we can represent positive and negative 32-bit numbers
in terms of the bit value times a power of 2:
The sign bit is multiplied by -2 31, and the rest of the bits are then multiplied by positive
versions of their respective base values.
Answer: Substituting the number’s bit values into the formula above:
Two useful shortcuts when working with two’s complement numbers. The first shortcut is
a quick way to negate a two’s complement binary number. Simply invert every 0 to 1 and
every 1 to 0, then add one to the result.
5
Negation Shortcut
Negate 2ten, and then check the result by negating -2ten.
2ten = 0000 0000 0000 0000 0000 0000 0000 0010two
Negating this number by inverting the bits and adding one,
Example: Binary to Hexadecimal and Back Convert the following hexadecimal and binary
numbers into the other base:
eca8 6420hex
0001 0011 0101 0111 1001 1011 1101 1111two
Answer:
6
MIPS Fields
R-type (for register) or R-format
I-type (for immediate) or I-format: Second type of instruction format is called I-type
(for immediate) or I-format.
It is used by the immediate and data transfer instructions. The fields of I-format are
Figure shows the numbers used in each field for the MIPS instructions covered so far.
7
In the table above, “reg” means a register number between 0 and 31, “address” means a 16-
bit address, and “n.a.” (not applicable) means this field does not appear in this format.
Note that add and sub instructions have the same value in the op field; the hardware uses the
funct field to decide the variant of the operation: add (32) or subtract (34).
Example: We can now take an example all the way from what the programmer writes to
what the computer executes. If $t1 has the base of the array A and $s2 corresponds to h, the
assignment statement
A[3000] = h + A[300];
Compiled MIPS code:
lw $t0, 1200($t1) # $t0 gets A[300]
add $t0, $s2, $t0 # $t0 get h + h + A[300]
sw $t0, 1200($t1) # stores h + A[300] back to A[300]
5. Logical Operations
Logical Operations are instructions for bitwise manipulation. Useful for extracting and
inserting groups of bits in a word
Figure show logical operations in C, Java, and MIPS
Fig: C and Java logical operators and their corresponding MIPS instructions.
MIPS implements NOT using a NOR with one operand being zero.
Shift Operations
They move all the bits in a word to the left or right, filling the emptied bits with 0s.
The actual name of the two MIPS shift instructions are called shift left logical (sll) and shift
right logical (srl).
8
shamt: it stands for shift amount.It defines how many positions to shift.
o Shift left logical
Shift left and fill with 0 bits
sll by i bits multiplies by 2i
o Shift right logical
Shift right and fill with 0 bits
srl by i bits divides by 2i (unsigned only)
Example: Assuming that the original value was in register $s0 and the result should go in
register $t2:
sll $t2,$s0,4 # reg $t2 = reg $s0 << 4 bits
AND Operation
AND is a bit-by-bit operation that leaves a 1 in the result only if both bits of the operands are
1.
For example, if register $t2 contains
0000 0000 0000 0000 0000 1101 1100 0000two
and register $t1 contains
0000 0000 0000 0000 0011 1100 0000 0000two
then, after executing the MIPS instruction
and $t0,$t1,$t2 # reg $t0 = reg $t1 & reg $t2
the value of register $t0 would be
0000 0000 0000 0000 0000 1100 0000 0000two
OR Operations
It is a bit-by-bit operation that places a 1 in the result if either operand bit is a 1.
For example, if register $t2 contains
0000 0000 0000 0000 0000 1101 1100 0000two
and register $t1 contains
0000 0000 0000 0000 0011 1100 0000 0000two
then, after executing the MIPS instruction
or $t0,$t1,$t2 # reg $t0 = reg $t1 | reg $t2
the value of register $t0 would be
0000 0000 0000 0000 0011 1101 1100 0000two
NOT Operation
NOT takes one operand and places a 1 in the result if one operand bit is a 0, and vice versa.
In three-operand format, the designers of MIPS decided to include the instruction NOR (NOT
OR) instead of NOT.
If one operand is zero, then it is equivalent to NOT: A NOR 0 = NOT (A OR 0) = NOT (A).
Example: If the register $t1 is
0000 0000 0000 0000 0011 1100 0000 0000two
and register $t3 has the value 0, the result of the MIPS instruction
nor $t0,$t1,$t3 # reg $t0 = ~ (reg $t1 | reg $t3)
is this value in register $t0:
1111 1111 1111 1111 1100 0011 1111 1111two
9
beq register1, register2, L1
This instruction means go to the statement labeled L1 if the value in register1 equals the
value in register2. The mnemonic beq stands for branch if equal.
Conditional Branch: An instruction that requires the comparison of two values and that
allows for a subsequent transfer of control to a new address in the program based on the
outcome of the comparison.
Unconditional Branch:The MIPS name for this type of instruction is jump, abbreviated
as j
j L1 # Unconditional jump to instruction labeled L1
Loops
Decisions are important both for choosing between two alternatives—found in if statements
—and for iterating a computation—found in loops.
MIPS Code:
Loop: sll $t1,$s3,2 # Temp reg $t1 = i * 4
add $t1,$t1,$s6 # $t1 = address of save[i]
Lw $t0,0($t1) # Temp reg $t0 = save[i]
bne $t0,$s5, Exit # go to Exit if save[i] ≠ k
addi $s3,$s3,1 #i=i+1
j Loop # go to Loop
10
Exit:
set on less than comparisons
MIPS offer two versions of the set on less than comparison to handle signed and
unsigned numbers.
o Set on less than (slt) and set on less than immediate (slti) work signed integers.
o Unsigned integers are compared using set on less than unsigned (sltu) and set on less than
immediate unsigned (sltiu)
slt rd, rs, rt # if (rs< rt) rd = 1; else rd = 0;
slti rt, rs, constant # if (rs< constant) rt = 1; else rt = 0;
o Use in combination with beq, bne
slt $t0, $s1, $s2 # if ($s1 < $s2)
bne $t0, $zero, L # branch to L
Case/Switch Statement
A case or switch statement that allows the programmer to select one of many alternatives
depending on a single value. The simplest way to implement switch is via a sequence of
conditional tests, turning the switch statement into a chain of if-then-else statements.
Similarly, in the execution of a procedure, the program must follow these six steps:
1. Put parameters in a place where the procedure can access them.
2. Transfer control to the procedure.
3. Acquire the storage resources needed for the procedure.
4. Perform the desired task.
5. Put the result value in a place where the calling program can access it.
6. Return control to the point of origin, since a procedure can be called from several points in
a program.
MIPS software follows the following convention for procedure calling in allocating its
32 registers:
■ $a0–$a3: four argument registers in which to pass parameters
■ $v0–$v1: two value registers in which to return values
■ $ra: one return address register to return to the point of origin
Jal ProcedureAddress
Return address: A link to the calling site that allows a procedure to return to the proper
address; in MIPS it is stored in register $ra.
MIPS use jump register instruction (jr) to allow the procedure to return to the proper
address. An unconditional jump to the address specified in a register:
11
Jr $ra
The jump register instruction jumps to the address stored in register $ra— which is just what
we want.
Caller: The program that instigates a procedure and provides the necessary parameter values.
Callee: A procedure that executes a series of stored instructions based on parameters
provided by the caller and then returns control to the caller.
Program counter (PC): The register containing the address of the instruction in the program
being executed.
The calling program, or caller, puts the parameter values in $a0–$a3 and uses jal X to
jump to procedure X (sometimes named the callee). The callee then performs the
calculations, places the results in $v0 and $v1, and returns control to the caller using jr $ra.
The jal instruction actually saves PC + 4 in register $ra to link to the following
instruction to set up the procedure return.
Nested Procedures
Procedure that do not calls others are called leaf procedure. Nested Procedures call other
procedures.
For nested call, caller needs to save on the stack:
o Its return address $ra
o Any arguments ($a0 - $a3) and temporaries ($t0 =$t9) needed after the call
o Restore from the stack after the call
13
Figure summaries that is preserved across a procedure call.
Figure shows the state of the stack before, during, and after the procedure call.
Some MIPS software uses a frame pointer ($fp) to point to the first word of the frame of
a procedure.
Frame pointer: A value denoting the location of the saved registers and local variables for a
given procedure.
A stack pointer ($sp) might change during the procedure, and so references to a local
variable in memory might have different offset depending on where they are in the procedure.
Fig: Illustration of the stack allocation (a) before, (b) during, and (c) after the procedure call.
14
Fig: The MIPS memory allocation for program and data.
A series of instructions can extract a byte from a word, MIPS provides instructions to move
bytes.
Load byte (lb) loads a byte from memory, placing it in the rightmost 8 bits of a register.
Store byte (sb) takes a byte from the rightmost 8 bits of a register and writes it to memory.
Thus, we copy a byte with the sequence.
lb $t0,0($sp) # Read byte from source
sb $t0,0($gp) # Write byte to destination
Characters are normally combined into strings, which have a variable number of characters.
The MIPS instruction set has explicit instructions to load and store such 16- bit quantities,
called halfwords.
Load half (lh) loads a halfword from memory, placing it in the rightmost 16 bits of a
register. Load halfword unsigned (lhu) works with unsigned integers.
Store half (sh) takes a halfword from the rightmost 16 bits of a register and writes it to
memory. We copy a halfword with the sequence
lhu $t0,0($sp) # Read halfword (16 bits) from source
sh $t0,0($gp) # Write halfword (16 bits) to destination
First, we would load the upper 16 bits, which is 61 in decimal, using lui:
lui $s0, 61 # 61 decimal = 0000 0000 0011 1101 binary
The next step is to insert the lower 16 bits, whose decimal value is 2304:
ori $s0, $s0, 2304 # 2304 decimal = 0000 1001 0000 0000
The instruction lui transfers the 16-bit immediate constant field value into the left most 16
bits of the register, filling the lower 16 bits with 0s.
16
Addressing in Branches and Jumps
The MIPS jump instructions have the simplest addressing. They use the final MIPS
instruction format, called the J-type, which consists of 6 bits for the operation field and the
rest of the bits for the address field. Thus,
j 10000 # go to location 10000
where the value of the jump opcode is 2 and the jump address is 10000.
Target address = PC31…28 : (address × 4)
The conditional branch instruction must specify two operands in addition to the branch
address. Thus,
bne $s0,$s1,Exit # go to Exit if $s0 ≠ $s1
is assembled into this instruction, leaving only 16 bits for the branch address:
17
Fig: Illustration of the five MIPS addressing modes.
To know when a task is finished writing so that it is safe for another to read, the tasks
need to synchronize. If they don’t synchronize, there is a danger of a data race.
Data Race: Two memory accesses form a data race if they are from different threads to
same location, at least one is a write, and they occur one after another.
We focus on the implementation of lock and unlock synchronization operations. Lock and
unlock can be used straightforwardly to create regions where only a single processor can
operate, called a mutual exclusion.
We require to implement synchronization in a multiprocessor is a set of hardware
primitives with the ability to atomically read and modify a memory location.
One such hardware primitive and show how it can be used to build a basic
synchronization primitive. One typical operation for building synchronization operations is
the atomic exchange or atomic swap, which inter-changes a value in a register for a value in
memory.
To build a basic synchronization primitive, assume that we want to build a simple lock,
where the value 0 is used to indicate that the lock is free and 1 is used to indicate that the lock
is unavailable.
For example, consider two processors that each try to do the exchange simultaneously:
this race is broken, since exactly one of the processors will perform the exchange first,
returning 0, and the second processor will return 1 when it does the exchange.
In MIPS this pair of instructions includes a special load called a load linked and a special
store called a store conditional.
18
These instructions are used in sequence: if the contents of the memory location specified
by the load linked are changed before the store conditional to the same address occurs, then
the store conditional fails.
The store conditional is defined to both store the value of a register in memory and to
change the value of that register to a 1 if it succeeds and to a 0 if it fails.
The following sequence implements an atomic exchange on the memory location
specified by the contents of $s1:
again: addi $t0,$zero,1 ;copy locked value
ll $t1,0($s1) ; load linked
sc $t0,0($s1) ; store conditional
beq $t0,$zero,again ;branch if store fails
add $s4,$zero,$t1 ;put load value in $s4
i) Compiler: The compiler transforms the C program into an assembly language program, a
symbolic form of what the machine understands. High-level language programs take many
fewer lines of code than assembly language, so programmer productivity is much higher.
Assembly Language: A symbolic language that can be translated into binary machine
language
ii) Assembler: The assembler converts this assembly language instruction into the machine
language.
Pseudoinstruction: A common variation of assembly language instruction often treated as if it
were an instruction in its own right.
Thus, the MIPS assembler accepts the instruction even though it is not found in the MIPS
architecture:
move $t0,$t1 # register $t0 gets register $t1
The assembler converts this assembly language instruction into the machine language
equivalent of the following instruction:
add $t0,$zero,$t1 # register $t0 gets 0 + register $t1
The MIPS assembler also converts blt (branch on less than) into the two instructions slt and
bne.
blt $t0, $t1, L → slt $at, $t0, $t1
bne $at, $zero, L
Pseudoinstructions give MIPS a richer set of assembly language instructions than those
implemented by the hardware.
19
The primary task of an assembler is assembly into machine code. The assembler turns the
assembly language program into an object file, which is a combination of machine language
instructions, data, and information needed to place instructions properly in memory.
To produce the binary version of each instruction in the assembly language program, the
assembler must determine the addresses corresponding to all labels. Assemblers keep track of
labels used in branches and data transfer instructions in a symbol table.
Symbol Table: A table that matches names of labels to the addresses of the memory words
that instructions occupy.
iii) Linker: Also called link editor. A systems program that combines independently
assembled machine language programs and resolves all undefined labels into an executable
file.
Executable file: A functional program in the format of an object file that contains no
unresolved references. It can contain symbol tables and debugging information.
There are three steps for the linker:
1. Place code and data modules symbolically in memory.
2. Determine the addresses of data and instruction labels.
3. Patch both the internal and external references.
iv) Loader: A systems program that places an object program in main memory so that it is
ready to execute.
The executable file is on disk the operating system reads it to memory and start it.
The Java compiler checks the types of data and produces the proper operation for each
type.
Java programs are distributed in the binary version of these bytecodes. A software
interpreter, called a Java Virtual Machine (JVM), can execute Java bytecodes. An
interpreter is a program that simulates an instruction set architecture.
Just In Time compiler (JIT): The name commonly given to a compiler that operates at
runtime, translating the interpreted code segments into the native code of the computer.
20
12.A C Sort Example to Put It All Together
The MIPS code from two procedures written in C: one to swap array elements and one to sort
them.
The Full swap Procedure: MIPS assembly code of the procedure swap
21
}
}
}
22
MIPS Assembly Language:
Let’s start with the array version clear1. We assume that the two parameters array and size
are found in the registers $a0 and $a1, and that i is allocated to register $t0.
clear1:
move $t0,$zero #i=0
loop1: sll $t1,$t0,2 # $t1 = i * 4
add $t2,$a0,$t1 # $t2 = address of array[i]
sw $zero, 0($t2) # array[i] = 0
addi $t0,$t0,1 #i=i+1
slt $t3,$t0,$a1 # $t3 = (i< size)
bne $t3,$zero,loop1 # if (i< size) go to loop1
There is a similar core of instruction sets for arithmetic-logical and data transfer instructions
for MIPS and ARM, as Figure shows.
23
Fig: ARM register-register and data transfer instructions equivalent to MIPS core.
Addressing Modes
Figure shows the data addressing modes supported by ARM. Unlike MIPS, ARM does not
reserve a register to contain 0. Although MIPS has just three simple data addressing modes,
ARM has nine.
Figure shows the instruction formats for ARM and MIPS. The principal differences are the
4-bit conditional execution field in every instruction and the smaller register field, because
ARM has half the number of registers.
15.x86 Instructions
x86 (also known as 80x86 or the 8086 family) is a family of complex instruction set
computer (CISC) instruction set architectures[a] initially developed by Intel based on the Intel
8086 microprocessor and its 8088 variant.
x86 started out as a 16-bit instruction set for 16-bit processors (the 8086 and 8088
processors), then was extended to a 32-bit instruction set for 32-bit processors (80386 and
80486), and now has been extended to a 64-bit instruction set for 64-bit processors.
Starting with the 80386, the top eight registers were extended to 32 bits and could also be
used as general-purpose registers.
25
x86instructions
Figure shows the arithmetic, logical, and data transfer instructions are two-operand
instructions.
There are two important differences here.
The x86 arithmetic and logical instructions must have one operand act as both a
source and a destination; ARMv7 and MIPS allow separate registers for source and
destination.
The second important difference is that one of the operands can be in memory. Thus,
virtually any instruction may have one operand in memory, unlike ARMv7 and MIPS.
Fig: Instruction types for the arithmetic, logical, and data transfer instructions.
Fig: x86 32-bit addressing modes and the equivalent MIPS code.
x86 Integer Operations
The 8086 provides support for both 8-bit (byte) and 16-bit (word) data types. Th e 80386
adds 32-bit addresses and data (double words) in the x86. (AMD64 adds 64-bit addresses and
data, called quad words).
The x86 integer operations can be divided into four major classes:
1. Data movement instructions, including move, push, and pop
2. Arithmetic and logic instructions, including test, integer, and decimal arithmetic operations
3. Control flow, including conditional branches, unconditional jumps, calls, and returns
4. String instructions, including string move and string compare
26
x86 Instruction Encoding
The encoding of instructions in the 80386 is complex, with many different instruction
formats. Instructions for the 80386 may vary from 1 byte up to 15 bytes.