Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 16: 10/29/2002CS170 Fall 20021 CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.

Similar presentations


Presentation on theme: "Lecture 16: 10/29/2002CS170 Fall 20021 CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University."— Presentation transcript:

1 Lecture 16: 10/29/2002CS170 Fall 20021 CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 16: 10/29/2002

2 CS170 Fall 20022 Outline Constant or Immediate Operands Pseudoinstrcutions Memory usage and a related pseudoinstruction

3 Lecture 16: 10/29/2002CS170 Fall 20023 Constant Operands 1/6 In the C compiler gcc, 52% of arithmetic operations involve constants. In circuit simulation program spice it is 69% x = x +4; where x is associated with register $s0, the constant 4 is stored in memory at memory address ADDR_4 What is the MIPS assembly code? lw $t0, ADDR_4($zero)# $t0  the constant 4 add $s0,$s0,$t0# $s0  $s0 + 4 This solution requires memory access to load a constant from memory (not efficient) Offer versions of arithmetic instructions in which operand can be a constant Use I-format (Immediate-format) for such instructions (2 registers and a constant) The MIPS field containing the constant is 16 bits long

4 Lecture 16: 10/29/2002CS170 Fall 20024 Constant Operands 2/6 Add Immediate instruction addi $s0,$s0,4# $s0  $s0 +4 8 16 4 op rs rt immediate Immediate version of slt slti $t0,$s2,10# $t0 = 1 if $s2 < 10 Design principle: Make the common case fast Constant operands occur frequently, make constants part of arithmetic instructions. Much faster than a memory load

5 Lecture 16: 10/29/2002CS170 Fall 20025 Constant Operands 3/6 How about constants bigger than 16 bits? Set upper 16 bits of a constant in a register using lui instruction A subsequent instruction can specify the lower 16 bits of the constant lui $t0, 255#255 is 0000 0000 1111 1111 Transfer 16 bit immediate constant into leftmost 16 bits of register and fill lower 16 bits with 0s. 0000 0000 1111 11110000 0000 Contents of register $t0 after lui instruction is executed

6 Lecture 16: 10/29/2002CS170 Fall 20026 Constant Operands 4/6 What is the MIPS assembly code to load the following 32-bit constant into register $s0? 0000 0000 0011 1101 0000 1001 0000 0000 Load upper 16 bits using lui into $s0 lui $s0, 61# 61 10 = 0000 0000 0011 1101 2 Add lower 16 bits whose lower value is 2304 addi $s0, $s0, 2304# 2304 10 = 0000 1001 0000 0000 2 Compiler or assembler break large constants into pieces and then reassemble into a register If assembler performs such job, it needs a temporary register available in which to create the long values Register $at is reserved for assembler

7 Lecture 16: 10/29/2002CS170 Fall 20027 Constant Operands 5/6 A word of caution addi sign extends 16-bit immediate field before performing addition Copy leftmost bit (sign bit) of 16-bit immediate field of instruction into upper 16 bits of a word before performing addition An alternative to addi is to use ori instruction (logical OR immediate) to create 32-bit constants in conjunction with lui (especially useful for base memory addresses) ori $s0, $s0,2304 ori loads 0s into the upper 16 bits (zero-extend immediate field) Why it is OK to use ori after lui?

8 Lecture 16: 10/29/2002CS170 Fall 20028 Constant Operands 6/6 Any constant value can be expressed in decimal, binary, or hexadecimal By default the value is in decimal in instructions To represent in hex use 0x before constant value lui $s0, 61# 61 10 = 0000 0000 0011 1101 2 To represent constant in hex lui $s0,0x003D # Why 61 10 is 003D 16 ?

9 Lecture 16: 10/29/2002CS170 Fall 20029 Pseudoinstructions Some instructions are provided to simplify assembly language programming and translation, and give a richer set of instructions than those implemented by the hardware The assembler reads such instructions and replaces by a sequence of real machine instructions that have the same effect Example move $t0,$t1# pseudoinstruction $t0  $t1 Assembler converts this instruction into machine language equivalent add $t0, $zero,$t1# $t0  0 + $t1 Other pseudoinstructions blt, bgt, bge, and ble You can use pseudoinstructions to simplify programming, but attempt to write your programs in terms of REAL MIPS instructions

10 Lecture 16: 10/29/2002CS170 Fall 200210 Memory Usage in MIPS See Figure A.9 (section A.5 in appendix A) Reserved for Operating System Program code (“Text”) starts at 0040 0000 16 (PC is program counter) Static data (data size known at compile time, and lifetime is the program’s entire execution) starts at 1000 0000 16 Dynamic data (allocation occurs during execution) is next and grows up towards the stack Stack is dynamic (will see function when we look at procedure calls) The register $gp is the global pointer and is set to an address (1000 8000 16 ) to make it easy to access data

11 Lecture 16: 10/29/2002CS170 Fall 200211 A related pseudoinstruction How does an array address get loaded into a register? Array is allocated in the static data portion of the data segment We can use load address pseudoinstruction la rdest, symbolic address la $s3, Address At compile time Address is known. Assume Address is 100000A4 16 We need to put this value in $s3 using real instructions Immediate field is only 16 bits (4 hex digits)? In this case, la gets implemented using two instructions lui $s3, 0x1000 addi $s3,$s3,0x00A4


Download ppt "Lecture 16: 10/29/2002CS170 Fall 20021 CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University."

Similar presentations


Ads by Google