Week 2 p2
Week 2 p2
Elaboration: MIPS software tries to keep the stack aligned to word addresses,
allowing the program to always use lw and sw (which must be aligned) to access the
stack. This convention means that a char variable allocated on the stack occupies 4
bytes, even though it needs less. However, a C string variable or an array of bytes will
pack 4 bytes per word, and a Java string variable or array of shorts packs 2 halfwords
per word.
Elaboration: Reflecting the international nature of the web, most web pages today
use Unicode instead of ASCII.
I. Which of the following statements about characters and strings in C and Check
Java are true? Yourself
1. A string in C takes about half the memory as the same string in Java.
2. Strings are just an informal name for single-dimension arrays of
characters in C and Java.
3. Strings in C and Java use null (0) to mark the end of a string.
4. Operations on strings, like length, are faster in C than in Java.
II. Which type of variable that can contain 1,000,000,000ten takes the most
memory space?
1. int in C
2. string in C
3. string in Java
First, we would load the upper 16 bits, which is 61 in decimal, using lui:
ANSWER
lui $s0, 61 # 61 decimal = 0000 0000 0011 1101 binary
The value of register $s0 afterward is
ori $s0, $s0, 2304 # 2304 decimal = 0000 1001 0000 0000
The final value in register $s0 is the desired value:
FIGURE 2.17 The effect of the lui instruction. The instruction lui transfers the 16-bit immediate constant field value into the
leftmost 16 bits of the register, filling the lower 16 bits with 0s.
2.10 MIPS Addressing for 32-bit Immediates and Addresses 113
Either the compiler or the assembler must break large constants into pieces and Hardware/
then reassemble them into a register. As you might expect, the immediate field’s
size restriction may be a problem for memory addresses in loads and stores as
Software
well as for constants in immediate instructions. If this job falls to the assembler, Interface
as it does for MIPS software, then the assembler must have a temporary register
available in which to create the long values. This need is a reason for the register
$at (assembler temporary), which is reserved for the assembler.
Hence, the symbolic representation of the MIPS machine language is no longer
limited by the hardware, but by whatever the creator of an assembler chooses to
include (see Section 2.12). We stick close to the hardware to explain the architecture
of the computer, noting when we use the enhanced language of the assembler that
is not found in the processor.
Elaboration: Creating 32-bit constants needs care. The instruction addi copies the
left-most bit of the 16-bit immediate field of the instruction into the upper 16 bits of a
word. Logical or immediate from Section 2.6 loads 0s into the upper 16 bits and hence
is used by the assembler in conjunction with lui to create 32-bit constants.
2 10000
6 bits 26 bits
where the value of the jump opcode is 2 and the jump address is 10000.
Unlike the jump instruction, 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:
5 16 17 Exit
If addresses of the program had to fit in this 16-bit field, it would mean that no
program could be bigger than 216, which is far too small to be a realistic option
today. An alternative would be to specify a register that would always be added
to the branch address, so that a branch instruction would calculate the following:
This sum allows the program to be as large as 232 and still be able to use
conditional branches, solving the branch address size problem. Then the question
is, which register?
The answer comes from seeing how conditional branches are used. Conditional
branches are found in loops and in if statements, so they tend to branch to a
nearby instruction. For example, about half of all conditional branches in SPEC
benchmarks go to locations less than 16 instructions away. Since the program
counter (PC) contains the address of the current instruction, we can branch within
215 words of the current instruction if we use the PC as the register to be added
to the address. Almost all loops and if statements are much smaller than 216 words,
so the PC is the ideal choice.
PC-relative This form of branch addressing is called PC-relative addressing. As we shall see
addressing An in Chapter 4, it is convenient for the hardware to increment the PC early to point
addressing regime to the next instruction. Hence, the MIPS address is actually relative to the address
in which the address
of the following instruction (PC 4) as opposed to the current instruction (PC).
is the sum of the
program counter (PC) It is yet another example of making the common case fast, which in this case is
and a constant in the addressing nearby instructions.
instruction. Like most recent computers, MIPS uses PC-relative addressing for all conditional
branches, because the destination of these instructions is likely to be close to the
branch. On the other hand, jump-and-link instructions invoke procedures that
have no reason to be near the call, so they normally use other forms of addressing.
Hence, the MIPS architecture offers long addresses for procedure calls by using the
J-type format for both jump and jump-and-link instructions.
Since all MIPS instructions are 4 bytes long, MIPS stretches the distance of the
branch by having PC-relative addressing refer to the number of words to the next
instruction instead of the number of bytes. Thus, the 16-bit field can branch four
times as far by interpreting the field as a relative word address rather than as a
relative byte address. Similarly, the 26-bit field in jump instructions is also a word
address, meaning that it represents a 28-bit byte address.
Elaboration: Since the PC is 32 bits, 4 bits must come from somewhere else for
jumps. The MIPS jump instruction replaces only the lower 28 bits of the PC, leaving
the upper 4 bits of the PC unchanged. The loader and linker (Section 2.12) must be
careful to avoid placing a program across an address boundary of 256 MB (64 million
instructions); otherwise, a jump must be replaced by a jump register instruction preceded
by other instructions to load the full 32-bit address into a register.
2.10 MIPS Addressing for 32-bit Immediates and Addresses 115
Hardware/ Most conditional branches are to a nearby location, but occasionally they branch
far away, farther than can be represented in the 16 bits of the conditional branch
Software instruction. The assembler comes to the rescue just as it did with large addresses
Interface or constants: it inserts an unconditional jump to the branch target, and inverts the
condition so that the branch decides whether to skip the jump.
1. Immediate addressing
op rs rt Immediate
2. Register addressing
op rs rt rd . . . funct Registers
Register
3. Base addressing
op rs rt Address Memory
4. PC-relative addressing
op rs rt Address Memory
PC + Word
5. Pseudodirect addressing
op Address Memory
PC Word
FIGURE 2.18 Illustration of the five MIPS addressing modes. The operands are shaded in color.
The operand of mode 3 is in memory, whereas the operand for mode 2 is a register. Note that versions of
load and store access bytes, halfwords, or words. For mode 1, the operand is 16 bits of the instruction itself.
Modes 4 and 5 address instructions in memory, with mode 4 adding a 16-bit address shifted left 2 bits to the
PC and mode 5 concatenating a 26-bit address shifted left 2 bits with the 4 upper bits of the PC. Note that a
single operation can use more than one addressing mode. Add, for example, uses both immediate (addi)
and register (add) addressing.
Although we show MIPS as having 32-bit addresses, nearly all microprocessors Hardware/
(including MIPS) have 64-bit address extensions (see Appendix E and Section
2.18). These extensions were in response to the needs of software for larger
Software
programs. The process of instruction set extension allows architectures to expand in Interface
such a way that is able to move software compatibly upward to the next generation
of architecture.
118 Chapter 2 Instructions: Language of the Computer
op(31:26)
28–26 0(000) 1(001) 2(010) 3(011) 4(100) 5(101) 6(110) 7(111)
31–29
0(000) R-format Bltz/gez jump jump & link branch eq branch blez bgtz
ne
1(001) add addiu set less set less andi ori xori load upper
immediate than imm. than imm. immediate
unsigned
2(010) TLB FlPt
3(011)
4(100) load byte load half lwl load word load byte load lwr
unsigned half
unsigned
5(101) store byte store half swl store word swr
6(110) load linked lwc1
word
7(111) store cond. swc1
word
25–24
0(00) mfc0 cfc0 mtc0 ctc0
1(01)
2(10)
3(11)
FIGURE 2.19 MIPS instruction encoding. This notation gives the value of a field by row and by column. For example, the top portion
of the figure shows load word in row number 4 (100two for bits 31–29 of the instruction) and column number 3 (011two for bits 28–26 of the
instruction), so the corresponding value of the op field (bits 31–26) is 100011two. Underscore means the field is used elsewhere. For example,
R-format in row 0 and column 0 (op 000000two) is defined in the bottom part of the figure. Hence, subtract in row 4 and column
2 of the bottom section means that the funct field (bits 5–0) of the instruction is 100010two and the op field (bits 31–26) is 000000two. The
floating point value in row 2, column 1 is defined in Figure 3.18 in Chapter 3. Bltz/gez is the opcode for four instructions found
in Appendix A: bltz, bgez, bltzal, and bgezal. This chapter describes instructions given in full name using color, while Chapter 3
describes instructions given in mnemonics using color. Appendix A covers all instructions.
120 Chapter 2 Instructions: Language of the Computer
Figure 2.20 shows all the MIPS instruction formats. Figure 2.1 on page 64 shows
the MIPS assembly language revealed in this chapter. The remaining hidden portion
of MIPS instructions deals mainly with arithmetic and real numbers, which are
covered in the next chapter.
Check I. What is the range of addresses for conditional branches in MIPS (K 1024)?
Yourself 1. Addresses between 0 and 64K 1
2. Addresses between 0 and 256K 1
3. Addresses up to about 32K before the branch to about 32K after
4. Addresses up to about 128K before the branch to about 128K after
II. What is the range of addresses for jump and jump and link in MIPS
(M 1024K)?
1. Addresses between 0 and 64M 1
2. Addresses between 0 and 256M 1
3. Addresses up to about 32M before the branch to about 32M after
4. Addresses up to about 128M before the branch to about 128M after
5. Anywhere within a block of 64M addresses where the PC supplies the
upper 6 bits
6. Anywhere within a block of 256M addresses where the PC supplies the
upper 4 bits
III. What is the MIPS assembly language instruction corresponding to the
machine instruction with the value 0000 0000hex?
1. j
2. R-format
3. addi
4. sll
5. mfc0
6. Undefined opcode: there is no legal instruction that corresponds to 0