Lecture4-Language2
Lecture4-Language2
Lecture 4:
Language of the computer
(II)
Dr. Hakem Beitollahi
1/70
Outline
Logical operations
4/70
© by Hakem Beitollahi
Shift Operations
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
6/70
© by Hakem Beitollahi
OR Operations
Useful to include bits in a word
Set some bits to 1, leave others unchanged
or $t0, $t1, $t2 #t0 = t1 || t2
7/70
© by Hakem Beitollahi
NOT Operations
Useful to invert bits in a word
Change 0 to 1, and 1 to 0
MIPS has NOR 3-operand instruction
a NOR b == NOT ( a OR b )
nor $t0, $t1, $zero Register 0: always
read as zero
8/70
© by Hakem Beitollahi
Format of AND, OR
and NOR
OP = 000000
9/70
© by Hakem Beitollahi
Immediate OR and
AND
11/70
© by Hakem Beitollahi
Outline
Logical operations
12/70
© by Hakem Beitollahi
Conditional Operations
13/70
© by Hakem Beitollahi
Compiling If
Statements
C code:
i ==j
if (i==j) f = g+h;
else f = g-h; true false
15/70
© by Hakem Beitollahi
Basic Blocks
A basic block is a sequence of
instructions with
No embedded branches (except at end)
No branch targets (except at beginning)
18/70
© by Hakem Beitollahi
Check Yourself
20/70
© by Hakem Beitollahi
Supporting procedures in
computer hardware
21/70
© by Hakem Beitollahi
Procedure Calling
Steps required
1. Place parameters in registers such that
procedure can access them
2. Transfer control to the procedure
3. Acquire the storage resources needed for
procedure
4. Perform procedure’s operations
5. Place result in register for caller
6. Return to place of call
22/70
© by Hakem Beitollahi
Register Usage
C code:
int leaf_example (int g, int h, int i,int j)
{ int f;
f = (g + h) - (i + j);
return f;
}
Arguments g, …, j in $a0, …, $a3
f in $s0 (hence, need to save $s0 on stack)
Result in $v0
28/70
© by Hakem Beitollahi
MIPS code:
leaf_example:
CPU
$s0
addi $sp, $sp, -4
Save $s0 on stack
sw $s0, 0($sp)
jr $ra Return
29/70
© by Hakem Beitollahi
Nested Procedures
30/70
© by Hakem Beitollahi
Nested procedures
(I)
Procedures that do not call others are leaf
procedures
Non-leaf procedures are the procedures that call
procedures on their body
Just as a spy might employ other spies as part of
a mission, who in turn might use even more
spies, so do procedures invoke other procedures.
Recursive procedures even invoke “clones” of
themselves.
We need to be careful when using registers in
nested procedures
31/70
© by Hakem Beitollahi
Nested procedures
(II)
Suppose that the main program calls procedure A
with an argument of 3, by placing the value 3
into register $a0 and then using jal A.
Then suppose that procedure A calls procedure B via
jal B with an argument of 7, also placed in $a0.
• Problem 1: Since A hasn’t finished its task yet, there is a
conflict over the use of register $a0.
• Problem 2: There is a conflict over the return address in
register $ra, since it now has the return address for B.
For nested call, caller needs to save on the stack:
Its return address
Any arguments and temporaries needed after the call
Restore from the stack after the© call
32/70
by Hakem Beitollahi
Non-Leaf Procedure
Example
C code:
int fact (int n)
{
if (n < 1) return 1;
else return n * fact(n - 1);
}
Argument n in $a0
Result in $v0
33/70
© by Hakem Beitollahi
Non-Leaf
Procedure Example
MIPS code:
fact:
addi $sp, $sp, -8 # adjust stack for 2 items
sw $ra, 4($sp) # save return address
sw $a0, 0($sp) # save argument
slti $t0, $a0, 1 # test for n < 1
beq $t0, $zero, ELSE
addi $v0, $zero, 1 # if so, result is 1
addi $sp, $sp, 8 # pop 2 items from stack
jr $ra # and return to after jal
ELSE: addi $a0, $a0, -1 # n >= 1: argument gets (n – 1)
jal fact # call fact with (n – 1)
lw $a0, 0($sp) # restore original n
Where
fact
lw $ra, 4($sp) # and return address
returns addi $sp, $sp, 8 # pop 2 items from stack
mul $v0, $a0, $v0 # multiply to get result
jr $ra # and return to the caller
34/70
© by Hakem Beitollahi
Memory Layout
35/70
© by Hakem Beitollahi
Memory Layout
The stack starts in the high end of memory
and grows down.
The first part of the low end of memory is
reserved, followed by the home of the MIPS
machine code (text segment)
The static data segment: The place for
constants and other static variables (e.g.,
array).
Dynamic data: Data structures like linked
lists tend to grow and shrink during their
lifetimes.
The segment is traditionally called the heap.
This allocation allows the stack and heap to
grow toward each other, thereby allowing
the efficient use of memory as the two
segments wax and wane.
36/70
© by Hakem Beitollahi
Registers Number
37/70
© by Hakem Beitollahi
Check Yourself
38/70
© by Hakem Beitollahi
Outline
Logical operations
39/70
© by Hakem Beitollahi
Communicating
with People
Example
void strcpy (char x[], char y[])
{
int i;
i = 0;
while ((x[i] = y[i]) != ‘\0’) /* copy & test byte */
i += 1;
}
43/70
© by Hakem Beitollahi
Strcpy Example
MIPS code:
strcpy:
addi $sp, $sp, -4 # adjust stack for 1 item
sw $s0, 0($sp) # save S0 register
add $s0, $zero, $zero # i= 0 + 0
L1: add, $t1, $s0, $a1 # address of y[i] in $t1
lb $t2, 0($t1) # $t2= y[i]
add $t3, $s0, $a0 # address of x[i] in $t3
sb $t2, 0($t3) # x[i] = y[i]
beq $t2, $zero, L2 # if y[i] == 0, go to L2
addi $s0, $s0, 1 # i = i + 1
j L1 # go to L1
L2: lw $s0, 0($sp) # y[i] == 0: end of string;
# restore old $s0
addi $sp, $sp, 4 # pop 1 item from stack
jr $ra # return to the caller
44/70
© by Hakem Beitollahi
Outline
Logical operations
Byte/Halfword Operations
45/70
© by Hakem Beitollahi
Large constant (32 bits)
op rs rt constant or address
6 bits 5 bits 5 bits 16 bits
PC-relative addressing
Target address = PC + offset × 4
PC already incremented by 4 by this time
48/70
© by Hakem Beitollahi
Jump Addressing
Jump (j and jal) targets could be
anywhere in text segment
Encode full address in instruction
op address
6 bits 26 bits
49/70
© by Hakem Beitollahi
Target Addressing
Example
Loop code from earlier example
Assume Loop at location 80000
50/70
© by Hakem Beitollahi
Addressing Mode
Summary
51/70
© by Hakem Beitollahi
Check Yourself
52/70
© by Hakem Beitollahi
Check Yourself
Answer:
Bne $s0, $s1, L2
J L1
L2:
53/70
© by Hakem Beitollahi
Outline
Logical operations
Byte/Halfword Operations
Static linking
55/
Compilers
56/70
© by Hakem Beitollahi
Producing an Object
Module (Assembler)
60/70
© by Hakem Beitollahi
Linker (I)
62/70
© by Hakem Beitollahi
Loader
63/70
© by Hakem Beitollahi
Check Yourself
64/70
© by Hakem Beitollahi
The Procedure Swap
65/70
© by Hakem Beitollahi