110 likes | 219 Views
CS170 Computer Organization and Architecture I. Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 16: 10/29/2002. Outline. Constant or Immediate Operands Pseudoinstrcutions Memory usage and a related pseudoinstruction. Constant Operands 1/6.
E N D
CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 16: 10/29/2002 CS170 Fall 2002
Outline • Constant or Immediate Operands • Pseudoinstrcutions • Memory usage and a related pseudoinstruction CS170 Fall 2002
Constant Operands1/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 CS170 Fall 2002
8 16 16 4 op rs rt immediate Constant Operands2/6 Add Immediate instruction addi $s0,$s0,4 # $s0 $s0 +4 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 CS170 Fall 2002
Contents of register $t0 after lui instruction is executed 0000 0000 1111 1111 0000 0000 0000 0000 Constant Operands3/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. CS170 Fall 2002
Constant Operands4/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 # 6110 = 0000 0000 0011 11012 • Add lower 16 bits whose lower value is 2304 • addi $s0, $s0, 2304 # 230410 = 0000 1001 0000 00002 • 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 CS170 Fall 2002
Constant Operands5/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? CS170 Fall 2002
Constant Operands6/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 # 6110 = 0000 0000 0011 11012 • To represent constant in hex • lui $s0,0x003D # Why 6110 is 003D16? CS170 Fall 2002
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 CS170 Fall 2002
Memory Usage in MIPS • See Figure A.9 (section A.5 in appendix A) • Reserved for Operating System • Program code (“Text”) starts at 0040 000016 (PC is program counter) • Static data (data size known at compile time, and lifetime is the program’s entire execution) starts at 1000 000016 • 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 800016) to make it easy to access data CS170 Fall 2002
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 100000A416 • 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 CS170 Fall 2002