[CA] Chapter 3-1: Logical Operations
[CA] Chapter 3-1: Logical Operations
⚙ Computer Architecture 공부
Logical Operations
Shift Operations
📚 Shifting all the bits in a word to the left or right, with the emptied bits filled with 0s Shift instruction:
sll(Shift Left Logical)
: bits를 왼쪽으로 이동srl(Shift Right Logical)
: bits를 오른쪽으로 이동
예시:
- 값 9를 4bits만큼 shift하는 경우:
- 9 = 0000 0000 0000 0000 0000 0000 0000 1001
- (Shifting left)
9 << 4
= 0000 0000 0000 0000 0000 0000 1001 0000 = 144 (10진수) - (Shifting right)
9 >> 4
= 0000 0000 0000 0000 0000 0000 0000 0000 = 0 (10진수)
sll $t2, $s0, 4
# $t2 = $s0 « 4비트op
= 0 (R-type)rs
= 0 (미사용)rt
= 10000 ($s0=16)rd
= 01010 ($t2=10)shamt
= 00100 (shift bits=4)funct
= 0 (sll 연산)
And/Or Operations
AND operation
은 두 비트가 모두 1인 경우에만 결과가 1이 되는 연산 OR operation
은 두 비트 중 하나라도 1이면 결과가 1이 되는 연산
- and/or 예시:
1
2
3
4
5
6
7
8
$t1 = 0000 0000 0000 0000 0011 1100 0000 0000 (15,360)
$t2 = 0000 0000 0000 0000 0000 1101 1100 0000 (3,520)
and $t0, $t1, $t2 # $t0 = $t1 & $t2
결과: $t0 = 0000 0000 0000 0000 0000 1100 0000 0000 (3,072)
or $t0, $t1, $t2 # $t0 = $t1 | $t2
결과: $t0 = 0000 0000 0000 0000 0011 1101 1100 0000 (15,808)
MIPS는 AND와 OR operation의 immediate
버전도 지원
andi
(AND immediate): 레지스터와 즉시값 사이의 AND 연산ori
(OR immediate): 레지스터와 즉시값 사이의 OR 연산
NOR Operation
NOR operation
은 두 비트를 OR 연산한 후 그 결과를 반전(NOT)시키는 연산
- 예시:
1
nor $t0, $t1, $t3 # $t0 = ~($t1 | $t3)
$t1
= 0000 0000 0000 0000 0011 1100 0000 0000 = 15,360 (demical)$t3
= 0000 0000 0000 0000 0000 0000 0000 0000 = 0 (demical)$t1
or$t3
= 0000 0000 0000 0000 0011 1100 0000 0000 = 15,360 (demical)- NOT(
$t1
or$t3
) = 1111 1111 1111 1111 1100 0011 1111 1111 = 0 (demical) = NOT$t1
위의 예시에서 알 수 있듯이 만약 한 피연산자가 0이라면, NOR 연산은 다른 피연산자의 NOT 연산과 동일
- MIPS에는 별도의 NOT 연산자가 없지만, NOR 연산을 활용하여 NOT 연산을 구현할 수 있음
DECISION-MAKING INSTRUCTIONS
Branch Instructions
Conditional Branches:
beq
(Branch if Equal): 두 레지스터 값이 같으면 지정된 label로 분기
1
beq $s0, $s1, Label # $s0 == $s1이면 Label로 이동
bne
(Branch if Not Equal): 두 레지스터 값이 다르면 지정된 label로 분기
1
bne $s0, $s1, Label # $s0 != $s1이면 Label로 이동
Unconditional Branche:
j
(Jump): 항상 지정된 label로 분기
1
j Label # 항상 Label로 이동
if-else Instructions
if-else
구문은 MIPS 어셈블리에서 branch intruction을 사용하여 구현가능.
예시:
C 언어 코드:
1
2
3
4
if (i == j)
f = g + h;
else
f = g - h;
MIPS 어셈블리 코드 (f=$s0
, g=$s1
, h=$s2
, i=$s3
, j=$s4
):
1
2
3
4
5
6
bne $s3, $s4, Else # i != j이면 Else로 이동
add $s0, $s1, $s2 # f = g + h (i == j인 경우)
j Exit # Exit로 이동
Else: # Else 레이블
sub $s0, $s1, $s2 # f = g - h (i != j인 경우)
Exit: # Exit 레이블
j Exit
명령어가 없으면add
와sub
이 모두 실행될 수 있어서 주의할 것- 일반적으로 반대 조건을 테스트하고 then 부분을 건너뛰는 것이 효율적
while
while loop
도 branch intruction을 사용하여 구현
예시:
C 언어 코드:
1
2
while (save[i] == k)
i += 1;
MIPS 어셈블리 코드 (i=$s3
, k=$s5
, save
배열의 기본 주소는 $s6):
1
2
3
4
5
6
7
8
Loop:
sll $t1, $s3, 2 # $t1 = i * 4 (바이트 단위 인덱스 계산)
add $t1, $t1, $s6 # $t1 = save[i]의 주소
lw $t0, 0($t1) # $t0 = save[i]의 값
bne $t0, $s5, Exit # save[i] != k이면 Exit로 이동
addi $s3, $s3, 1 # i = i + 1
j Loop # Loop로 이동
Exit:
Comparison Instructions
slt
(Set Less Than): 첫 번째 레지스터가 두 번째 레지스터보다 작으면 목적 레지스터를 1로 설정, 그렇지 않으면 0으로 설정1
slt $t0, $s1, $s2 # $s1 < $s2이면 $t0 = 1, 그렇지 않으면 $t0 = 0
slti
(Set Less Than Immediate): 레지스터가 즉시값보다 작으면 목적 레지스터를 1로 설정1
slti $t0, $s1, 100 # $s1 < 100이면 $t0 = 1, 그렇지 않으면 $t0 = 0
for
예시:
C 언어 코드:
1
2
3
for (i = 0; i < 4; i++) {
// 무언가 수행
}
MIPS 어셈블리 코드 (i=$t0
):
1
2
3
4
5
6
7
8
add $t0, $zero, $zero # i = 0
Loop:
slti $t1, $t0, 4 # $t1 = (i < 4 ? 1 : 0)
beq $t1, $zero, Exit # $t1 == 0이면 (i >= 4) Exit로 이동
# 여기서 무언가 수행
addi $t0, $t0, 1 # i++
j Loop # Loop로 이동
Exit:
- 여기서
$zero
레지스터는 항상 0 값을 가지는 특수 레지스터 - 이를 활용하여 레지스터를 0으로 초기화 가능
This post is licensed under CC BY 4.0 by the author.