Lecture 16: 10/29/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 16: 10/29/2002
CS170 Fall Outline Constant or Immediate Operands Pseudoinstrcutions Memory usage and a related pseudoinstruction
Lecture 16: 10/29/2002CS170 Fall 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
Lecture 16: 10/29/2002CS170 Fall Constant Operands 2/6 Add Immediate instruction addi $s0,$s0,4# $s0 $s 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
Lecture 16: 10/29/2002CS170 Fall 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 Transfer 16 bit immediate constant into leftmost 16 bits of register and fill lower 16 bits with 0s Contents of register $t0 after lui instruction is executed
Lecture 16: 10/29/2002CS170 Fall Constant Operands 4/6 What is the MIPS assembly code to load the following 32-bit constant into register $s0? Load upper 16 bits using lui into $s0 lui $s0, 61# = Add lower 16 bits whose lower value is 2304 addi $s0, $s0, 2304# = 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
Lecture 16: 10/29/2002CS170 Fall 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?
Lecture 16: 10/29/2002CS170 Fall 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# = To represent constant in hex lui $s0,0x003D # Why is 003D 16 ?
Lecture 16: 10/29/2002CS170 Fall 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
Lecture 16: 10/29/2002CS170 Fall Memory Usage in MIPS See Figure A.9 (section A.5 in appendix A) Reserved for Operating System Program code (“Text”) starts at (PC is program counter) Static data (data size known at compile time, and lifetime is the program’s entire execution) starts at 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 ( ) to make it easy to access data
Lecture 16: 10/29/2002CS170 Fall 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 A4 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