Computer Architecture/컴퓨터구조[05]
[CA] Lecture #09
leziwn.cs
2023. 9. 27. 06:34
Logical Operations
- Instructions for bitwise manipulation.
Shift Operations: <<, >>
- immed: how many positions to shift
AND Operations: &
and x9, x10, x11 // x9 = x10 & x11
OR Operations: |
or x9, x10, x11 // x9 = x10 | x11
XOR Operations: ^ (NOT Operation)
- XOR(exclusive OR) = NOT
xor x9, x10, x11 // 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
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
- parameter를 register에 넣는다.
- function으로 go!
- Acquire the storage resources needed for the procedure.
- Pefrom the desired task.
- result value 저장한다.
- 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
- Puts the value in x10~x17.
- jal x1, X: Branch to procedure X
▶ callee
- Performs the calculations.
- Places the rsults in the same parameter registers.
- 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