Lecture 5B Code Generation From HLL
Lecture 5B Code Generation From HLL
Introduction
• In this chapter we are supposed to learn instruction set of a real
CS 252: Computer Organization and computer; both in the form written by humans (high-level language)
and in the form read by the machine
Architecture II
• We will go step-by-step from a notation that looks like a restricted
programming language, until we see the real language of the real
computer
Lecture 4B
• Computer designers have the common goal:
Principles of Computer Hardware Design and
—To find a language that makes it easy to build the hardware and
Code Generation from HLL the compiler, while maximizing performance and minimizing cost
• C/C++ programming language will be used as HLL, and the MIPS
machine instruction set will also be used
• We will see the restricted programming, at the same time we will
also see the four design principles of computer hardware
By: Dr. E. A. Kalinga, CSE, CoICT, UDSM 1
Operations (ops) of the computer hardware Operations (ops) of the computer hardware
• Every computer must be able to perform fundamental • In C/C++ programming we will have:
arithmetic operations a = b + c + d + e;
• MIPS assembly language notation: • Due to restrictions mentioned above, the equivalent
add a, b, c //a = b + c
assembly language produced by C compiler will be:
Instructs a computer to add two variables b and c and to put their
sum in a add a, b, c // a = b + c
add a, a, d // a = b + c + d
• Restrictions in this is that:
—Each MIPS arithmetic instruction performs only one operation, add a, a, e // a = b + c + d + e
and • Note:
—Each arithmetic instruction must always have exactly three —Three instructions are used to take the sum of four variables
variables; no more, no less
—Each instruction will have exactly three operands, i.e. the two
• Example: suppose we want to place the sum of four numbers being added together and a place to put the sum
variables b, c, d and e into a.
Operations (ops) of the computer hardware Operations (ops) of the computer hardware
• Requiring every instruction to have exactly three operands, Example 1: Compiling two c assignment statement
no more and no less, conforms to the philosophy of into MIPS
keeping the hardware simple This segment of a C program contains the five variables
• i.e. Hardware for a variable number of operands is more a, b, d, and e:
complicated than hardware for a fixed number of operands a = b + c;
• This gives the first out of four (4) principles of hardware d = a – e;
design: The translation from C to MIPS assembly language is performed
by the compiler. Show the MIPS code produced by a C compiler.
Design Principle 1: Simplicity favors regularity Solution:
In assembly language we will have:
add a, b, c // always three operands
sub d, a, e // always three operands
Instructions for making decisions (Loops) Instructions for making decisions (Loops)
—The next two instructions add A[i] to g and the j to i Solution:
add $s1, $s1, $t0 // g = g + A[i] —1st step is to load save[i] into a temporary register
add $s3, $s3, $s4 // i = i + j Loop: add $t1, $s3, $s3 // 2*i
—The final instruction will be: add $t1, $t1, $t1 // 4*i
bne $s3, $s4, Loop // go to loop if i j add $t1, $t1, $s6 // the address of save[i]
lw $t0, 0($t1)
Example: Compiling a while loop —Next performs the loop test, exiting if save[i] k
while (save[i]==k) bne $t0, $s5, Exit // go to Exit if save[i] k
i = i + j; add $s3, $s3, $s4 // if save[i] = k, i=i+j
Assume i, j and k maps $s3, $s4 and $s5 respectively. Base —The end of the loop branches back to the while test
register to be $s6. What is the MIPS assembly code
j Loop // go to Loop
Exit:
MIPS Instruction Set Covered OP Field Values and Funct Field values of Instructions
Category Instruction Name Format Example Name Format OP Field Funct Field
Add Add R add $s1, $s2, $s3 Value Value
Arithmetic Subtract Sub R sub $s1, $s2, $s3 add R 0 32
Add immediate addi I Addi $s1, $s2, 100 sub R 0 34
Load word lw I Lw $s1, 100($s2) addi I 8 N/A
Data Store word sw I sw $s1, 100($s2) lw I 35 N/A
Transfer sw I 43 N/A
Load upper immediate lui I Lui $s1, 100
Branch on equal beq I beq $s1, $s2, 25 beq I 4 N/A
Conditio- Branch on not equal bne I bne $s1, $s2, 25 bne I 5 N/A
nal branch
Set on less than slt R slt $s1, $s2, $s3 slt R 0 42
Set on less than immediate slti I slti $s1, $s2, 25 j J 2 N/A
Unconditi- Jump j J j 2500 jr R 0 8
onal jump Jump register jr R jr $ra jal J 3 N/A
Jump and link jal J jal 2500