leziwn.cs 2023. 9. 27. 06:34
Logical Operations
  • Instructions for bitwise manipulation.

Logical Operations

 

Shift Operations: <<, >>

  • immed: how many positions to shift

slli, srli

 

AND Operations: &
and  x9, x10, x11   // x9 = x10 & x11

AND Operations: &

 

OR Operations: |
or  x9, x10, x11   // x9 = x10 | x11

 

XOR Operations: ^ (NOT Operation)
  • XOR(exclusive OR) = NOT
xor  x9, x10, x11   // NOT Operation

XOR Operations: ^ (NOT Operation)

 


Conditional Operations
  • beq: Branch if EQual
  • bnq: Branch if Not eQual
beq  rs1, rs2, L1   // if(rs1 == rs2), branch to L1

bne  rs1, rs2, L1   // if(rs1 != rs2), branch to L1

 

Example: Conditional Operations
if(i == j)
  f = g + h;
else
  f = g - h;
// f~j == x19~x23

bnq  x22, x23, Else   // if(i!=j), branch to Else
add  x19, x20, x21    // f = g + h
beq  x0, x0, Exit     // Exit
Else:
  sub x19, x20, x21   // Else: f = g - h
Exit:

 

Compiling a While Loop
while (save[i] == k)
   i += 1;
// i: x22, k: x24, save: x25

Loop: slli  x10, x22, 2    // x10 = 4*i
      add   x10, x10, x25  // x10 = save[i]
      lw    x9, 0(x10)     // x9 = x10
      bne   x9, x24, Exit  // if(save[i] != k), branch to Exit
      addi  x22, x22, 1    // i += 1
      beq   x0, x0, Loop   // Loop
Exit:

 

Basic Block

Basic Block

 

More Conditional Operations
  • blt: Branch if Less Than
  • bge: Branch if Greater Than
  • bltu: Branch if Less Than, Unsigned
  • bgeu: Branch if Greater Than, Unsigned

 


Supporting Procedures in Computer Hardware
  1. parameter를 register에 넣는다.
  2. function으로 go!
  3. Acquire the storage resources needed for the procedure.
  4. Pefrom the desired task.
  5. result value 저장한다.
  6. return address로 돌아온다.

 

Registers
  • x10~x17: parameter registers --> Pass parameters or return values.
  • x1: return address --> Saves the return address for PC.

 


Two More Unconditional Jump Instructions
jal  x1, ProcedureAddress   // jump to ProcedureAddress and store the return address in x1(PC+4)
jalr x0, 0(x1)              // return to x1(PC+4)

: PC를 강제로 ProcedureAddress로 바꿨다가, (PC+4)로 돌아온다.

 

Comments
  • x1: return address --> PC+4

▶ caller

  1. Puts the value in x10~x17.
  2. jal  x1, X: Branch to procedure X

▶ callee

  1. Performs the calculations.
  2. Places the rsults in the same parameter registers.
  3. jalr  x0, 0(x1): Returns control to the caller. (PC값을 PC+4로 변경해서 돌아온다.

 

Some Terminalogies
  • Return address (x1): 돌아와야 할 주소 --> PC + 4
  • Program Counter (PC): The register containing the address of the instruction.
  • Caller
  • Callee