380 likes | 624 Views
WRAMP Instructions. Review so far...any questions?. Assembly language instructions generally specify a single operation; that is, there is a 1:1 relationship between the assembly language instruction and the binary code generated.
E N D
Review so far...any questions? • Assembly language instructions generally specify a single operation; that is, there is a 1:1 relationship between the assembly language instruction and the binary code generated. • Most WRAMP instructions read|write their operands from a 16 word memory called a register file • Format: <opcode> <operand 1>, <operand 2> , <operand 3> • e.g. add $3, $4, $5 • some instructions have only two operands • WRAMP is a word-addressable processor • Word-size is 32 bits. • Most other processors are byte addressable
Registers Reminder 31 0 $0 0 $1 Always 0 $2 • WRAMP has 16 registers named $0 to $15 • All registers are 32 bits • Registers $1 - $13 may be used for general purposes • $0 is always zero • $sp is used as stack pointer – see later lectures on stack operation • $ra is used as a return address register, used for subroutines. $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $sp ($14) Stack pointer $ra ($15) Return address
Types of Instructions • Arithmetic • Bitwise (Logic) • Test • Branch and Jump • Memory • Special
WRAMP Arithmetic Instructions • There are the full set of Arithmetic instructions in WRAMP: add, subtract, multiply, divide, remainder. • For each of these, there are both signed and unsigned versions. • There is also an immediate form, where one operand is a constant. Available in either signed or unsigned versions.
Arithmetic Instructions WRAMP C add x, y, z x = y + z; sub x, y, z x = y - z; mult x, y, z x = y * z; div x, y, z x = y / z; rem x, y, z x = y % z; • These instructions each have four possible variations, e.g. add • add $3, $4, $5 • addi $3, $4, 0x8765 # 3rd operand stored in instruction • addu $3, $4, $5 # both nums treated as +ve • addui $3, $4, 0x8765 # both of the above x,y,z as stored in some register
Example B = 10; A = B; C = B + A; A = A - 3; D = A * B + C; addi $2, $0, 10 add $1, $2, $0 add $3, $2, $1 subi $1, $1, 3 mult $4, $1, $2 add $4, $4, $3 A => $1 B => $2 C => $3 D => $4
WRAMP Memory Operations • RISC-based architecture often implies that memory operations are limited to simple LOAD (retrieve from memory) and STORE (save in memory) operations. • In WRAMP, the ability to use a form of indirect addressing is included for completeness. • In WRAMP, two registers are involved, as usual… Rs and Rd (source and destination), plus an offset is added to Rs to form an effective address.
Memory InstructioNS • Load word • Format: lw Rd, Offset(Rs) • Function: Add the contents of register Rs and the sign-extended offset to give an effective address. Load the contents of the memory location specified by this address into register Rd. • Store word • Format: sw Rd, Offset(Rs) • Function: Add the contents of register Rs and the sign-extended offset to give an effective address. Store the contents of register Rd into the memory location specified by this effective address.
Bitwise (Logic) Instructions • Arithmetic instructions studied so far apply to the entire word, signed or unsigned. • There exists a set of logical instructions which allow Boolean operations to be performed. These include: • and • or • xor (exclusive OR) • shift (right or left)
Bitwise instructions C equivalent • All of these instructions also have immediate versions • for example, and • andi $3, $4, 0xAAAA • 16-bit Immediate value is zero extended and Rd, Rs, Rt or Rd, Rs, Rt xor Rd, Rs, Rt sll Rd, Rs, Rt srl Rd, Rs, Rt sra Rd, Rs, Rt d = s & t; d = s | t; d = s ^ t; d = s << t; d = s >> t; d = s >> t;
If packed BCD values are stored in register $6, how to select the third value? andi $2, $6, 0x0f00 will cause only the third value to be placed in $2; then it must be shifted 8 places right srli $3, $2, 0x0008 will place the value into the rightmost 4 bits of $3. An example two BCD digits 31 0 third value
Moving bits within a register • Sometimes it is useful to move the bits within a register. This is called shifting • Shifting the contents of a register one bit to the left is equivalent to multiplying by 2 (for integers) • Shifting the contents of a register one bit to the right is equivalent to dividing by 2 (for integers) • There are several WRAMP instructions for shifting the contents of a register
Logical shift • sll <target>, <source>, offset • The sll (shift left, logical) instruction shifts the contents of the <source> register <offset> bits to the left, storing the result in <target> • The right-most <offset> bits of target are all 0's • <offset> is a number between 0 and 31, inclusive • srl <target>, <source>, offset • Is right shift, analogous to sll
Logical shift example • SLL $1, $2, 5 before $1 $2 after $1 $2
Arithmetic shift • Sometimes shifting is used as a fast way to multiply or divide by 2 • On a real chip, shifting is much faster than division • However, negative 2's complement integers become positive if 0's are shifted in from the right • Arithmetic shifting must preserve sign • SRA (shift right, arithmetic) shifts the register contents to the right: the high order <offset> bits of the <target> are the same as the highest order bits of the <source>
Arithmetic shift example • SRA $1, $2, 5 before $1 $2 after $1 $2
Test Instructions • Test instructions are used to perform comparisons. • The result of the comparison (true=1, false=0) is stored in the target register • These tests are often used with branch instructions to perform conditional execution. • For Example: slt $3, $1, $2 beqz $3, endloop while a < b do:
Test instructions • Test instructions exist to perform the following comparisons slt < less than sgt > greater than sle <= less than or equal to sge >= greater than or equal to seq = equal to sne not equal to • Each of these instructions also has immediate, unsigned and unsigned immediate forms
Jump and Branch Instructions • Branch instructions allow non-linear program execution. • Unconditional instructions are jump instructions • Conditional instructions are branch instructions
Jump Instructions • Jump instructions move to a specific address • j address (jump) jumps to the address given in the instructions • jr $r (jump to register) jumps to the address stored in the target register. • Jump and link instructions have the same two forms jal address and jalr $r and do the same thing, but also store the next instruction address in $ra. This is used for calling subroutines
Branch Instructions • Branch instructions test the value of a register and only jump if the test is met. • Branch instructions add an offset value to the next instruction to be executed. • beqz $r branches if the register is zero • bnez $r branches if the register is not zero
Branch and Jump • j 8 jumps to instruction number 8 • absolute value • beqz $r, 8 branches 8 instructions forward (if register is equal to zero) • relative value • Normally the programmer does not have to calculate the values of the instruction number or the offset.
Branch and Jump Instructions : Example Example usage: loop control addi $5, $0, 7 loop: beqz $5, endlop subi $5, $5, 1 … j loop endlop: … More instructions go here
slt $13, $1, $2 beqz $13, endif addi $3, $3, 1 . . . endif: … slt $13, $1, $2 sgt $12, $4, $3 or $13, $13, $12 beqz $13, endif addi $4, $4, 1 endif: … If Statement implementation using test and branch instructions. If (a < b) c = c + 1; . . . If ((a < b) || (d > c)) d = d + 1; NOTE: quantities to be compared, etc. must already be stored in registers!!
slt $13, $1, $2 beqz $13, else addi $3, $3, 1 j endif else: addi $4, $4, 1 endif: … If-else Statement as implemented using branch and jump instructions If (a < b) c = c + 1; else d = d + 1; NOTE: quantities to be compared, etc. must already be stored in registers!!
addi $1, $0, 1 add $2, $0, $0 loop: slt $13, $2, $3 beqz $13, endlop mult $1, $1, $4 addi $2, $2, 1 j loop endlop: While Loop $1 contains result $2 contains count $3 contains exponent $4 contains base $13 flag register Result = 1; count = 0; while (count < exponent) { result = result * base; count++; }
Special Instructions • The WRAMP processor has some special instructions to control the operation of the processor • Most of these are associated with the operation of exceptions (interrupts). • As well as special instructions there are special (extra) registers to control the operation of the processor • We will cover these later
WRAMP Instruction Formats • There is a one-to-one correspondence between assembly language instructions and machine code instructions. • This is unlike high level languages such as C, where a few lines of code can generate hundreds of lines of machine code. • All instructions are encoded into a single word (32 bits) • Different formats are used for the instruction word depending on the type of operands
Instruction Formats • Three instruction formats used • R type – for instructions with three operands specified in registers (e.g. add $3, $5, $6) • I type – for instructions where one operand is an immediate value (16 bits) • J type – for instructions where one operand is an address such as Jump instructions or memory instructions.
R - Type instruction 4 bits 4 bits 4 bits 4 bits 12 bits 4 bits OPcode Rd Rs Func 0000 0000 0000 Rt 0000 0011 0110 0000 0000 0000 0000 1001 Example: R-type instruction add $3, $6, $9 Operation: Add the contents of Rs and add contents of Rt and put the result into Rd. Op code and function are 0000 for add
Immediate 0001 0001 Rd Rs Example: I Instruction • All instructions except branching and memory instructions have an immediate form, where a constant is an operand. For instance, when it is desired to add a constant or fixed value to the contents of a register, addui Rd, Rs, Immediate places the sum of register Rs and the zero-extended immediate into register Rd.
Example: J-type instruction beqz $3, endlop 1010 0000 0011 0000 0000 0000 0010 1111 Operation: When the contents of Rs is equal to zero, the branch is taken. Often used to control a loop. testsum: beqz $3, endlop #some instructions # may go here j testsum endlop: sw $4, 24($5)
1210 = 11002 1000 0011 1001 0000 0000 0000 0000 1100 Example: lw instruction (J type) lw $3, 12($9) $3 $9
Instructions Summary • WRAMP is a load/store (RISC) processor so load and store are used to access memory - all other instructions work on registers. • WRAMP has a full complement of Arithmetic and Logic instructions • Test Instructions and Branch Instructions can be used together to form conditional execution structures
Instructions Summary • Most Instructions have unsigned and immediate forms. The I format is used for immediate forms instructions • J format is used for instructions that require a complete address such as Jump instructions and Memory instructions • Special Instructions and Registers control the operation of the WRAMP processor, particularly with respect to exceptions