Architecture / ISA
Beginner
MIPS32 / RISC-V RV32I
MIPS vs RISC-V 핵심 어셈블리 명령어 1:1 대응 비교 치트시트MIPS vs. RISC-V Core Assembly Instruction 1:1 Comparison Cheat Sheet
산술(Add/Sub), 논리(And/Or/Xor), 메모리 로드/스토어(Lw/Sw), 조건 분기 및 점프(Branch/Jump) 명령어의 MIPS와 RISC-V 문법 차이를 1:1로 비교합니다.A 1:1 syntax and semantics comparison between MIPS and RISC-V across arithmetic, bitwise logic, memory load/store, and conditional branch/jump instructions.
#MIPS #RISC-V #Assembly #Cheat Sheet #Computer Architecture
핵심 개념MIPS와 RISC-V는 모두 로드-스토어(Load-Store) 기반의 32개 범용 레지스터 구조를 채택한 RISC 아키텍처입니다. 그러나 피연산자 표기법, 시프트 즉시값 접미사, 대소 비교 조건 분기 방식, 분기 지연 슬롯(Delay Slot) 유무에서 차이가 있으므로 두 아키텍처 간 코드를 변환할 때 정확한 대응 규칙을 확인해야 합니다.
Key ConceptBoth MIPS and RISC-V are load-store RISC architectures built around 32 general-purpose registers. However, they differ in register naming prefixes, shift-immediate suffixes, relational branch instructions, and the presence of branch delay slots. Reviewing these differences prevents subtle bugs when porting assembly code between the two ISAs.
1. 산술 및 논리 연산 명령어 비교1. Arithmetic and Bitwise Logic Instruction Comparison
좌우로 밀어서 표 전체 내용을 확인하세요
| 동작 설명 | MIPS 문법 | RISC-V 문법 | 차이점 / 비고 |
|---|---|---|---|
| 레지스터 덧셈 | add $d, $s, $t | add rd, rs1, rs2 | 동일 (rd = rs1 + rs2) |
| 상수 덧셈 | addi $t, $s, imm | addi rd, rs1, imm | MIPS는 오버플로우 예외 발생, RISC-V는 무시 |
| 레지스터 뺄셈 | sub $d, $s, $t | sub rd, rs1, rs2 | 동일 (rd = rs1 - rs2) |
| 비트 AND / OR | and $d, $s, $t / or | and rd, rs1, rs2 / or | 동일 |
| 비트 XOR | xor $d, $s, $t | xor rd, rs1, rs2 | 동일 |
| 좌측 논리 시프트 | sll $d, $t, shamt | slli rd, rs1, shamt | RISC-V는 즉시값 시프트에 'i' 접미사 사용 |
| 우측 산술 시프트 | sra $d, $t, shamt | srai rd, rs1, shamt | 부호 비트 보존 시프트 |
Swipe horizontally to view the full table
| Operation | MIPS Syntax | RISC-V Syntax | Differences / Notes |
|---|---|---|---|
| Register Addition | add $d, $s, $t | add rd, rs1, rs2 | Identical operand order (rd = rs1 + rs2) |
| Immediate Addition | addi $t, $s, imm | addi rd, rs1, imm | MIPS traps on signed overflow; RISC-V wraps silently |
| Register Subtraction | sub $d, $s, $t | sub rd, rs1, rs2 | Identical (rd = rs1 - rs2) |
| Bitwise AND / OR | and $d, $s, $t / or | and rd, rs1, rs2 / or | Identical |
| Bitwise XOR | xor $d, $s, $t | xor rd, rs1, rs2 | Identical |
| Logical Left Shift | sll $d, $t, shamt | slli rd, rs1, shamt | RISC-V appends 'i' suffix for immediate shifts |
| Arithmetic Right Shift | sra $d, $t, shamt | srai rd, rs1, shamt | Preserves sign bit |
2. 메모리 접근(Load / Store) 비교2. Memory Access (Load / Store) Comparison
좌우로 밀어서 표 전체 내용을 확인하세요
| 동작 설명 | MIPS 문법 | RISC-V 문법 | 차이점 / 비고 |
|---|---|---|---|
| 4바이트 워드 로드 | lw $t0, 8($s0) | lw t0, 8(s0) | 동일한 문법 (목적지, 오프셋(베이스)) |
| 4바이트 워드 스토어 | sw $t0, 8($s0) | sw t0, 8(s0) | 동일한 문법 (소스, 오프셋(베이스)) |
| 1바이트 부호 로드 | lb $t0, 0($s0) | lb t0, 0(s0) | 동일 (부호 확장) |
| 1바이트 부호없는 로드 | lbu $t0, 0($s0) | lbu t0, 0(s0) | 동일 (0 확장) |
Swipe horizontally to view the full table
| Operation | MIPS Syntax | RISC-V Syntax | Differences / Notes |
|---|---|---|---|
| Load 4-Byte Word | lw $t0, 8($s0) | lw t0, 8(s0) | Identical addressing syntax: dest, offset(base) |
| Store 4-Byte Word | sw $t0, 8($s0) | sw t0, 8(s0) | Identical addressing syntax: src, offset(base) |
| Load Signed Byte | lb $t0, 0($s0) | lb t0, 0(s0) | Identical (sign-extended to 32 bits) |
| Load Unsigned Byte | lbu $t0, 0($s0) | lbu t0, 0(s0) | Identical (zero-extended to 32 bits) |
3. 조건 분기 및 점프 비교3. Conditional Branch and Jump Comparison
좌우로 밀어서 표 전체 내용을 확인하세요
| 동작 설명 | MIPS 문법 (Delay Slot 있음) | RISC-V 문법 (Delay Slot 없음) | 주요 차이점 |
|---|---|---|---|
| 값이 같으면 분기 | beq $t0, $t1, label | beq t0, t1, label | RISC-V는 다음 행을 무조건 실행하지 않음 |
| 값이 다르면 분기 | bne $t0, $t1, label | bne t0, t1, label | 동일 |
| 미만 비교 분기 (<) | slt $at, $t0, $t1 bne $at, $0, label | blt t0, t1, label | RISC-V는 1개 명령어로 직접 대소 분기 가능 |
| 이상 비교 분기 (>=) | slt $at, $t0, $t1 beq $at, $0, label | bge t0, t1, label | MIPS는 slt 플래그 검사 필요, RISC-V는 직접 분기 |
| 함수 호출 (점프 & 링크) | jal target | jal ra, target | MIPS는 암시적으로 $ra, RISC-V는 명시적 링크 레지스터 |
| 함수 복귀 (레지스터 점프) | jr $ra | ret (jalr zero, ra, 0) | RISC-V는 ret 의사 명령어로 제공 |
Swipe horizontally to view the full table
| Operation | MIPS Syntax (With Delay Slot) | RISC-V Syntax (No Delay Slot) | Key Differences |
|---|---|---|---|
| Branch if Equal | beq $t0, $t1, label | beq t0, t1, label | RISC-V does not execute the following slot on taken branch |
| Branch if Not Equal | bne $t0, $t1, label | bne t0, t1, label | Identical operand order |
| Branch if Less Than (<) | slt $at, $t0, $t1 bne $at, $0, label | blt t0, t1, label | RISC-V performs comparison and branch in a single instruction |
| Branch if Greater/Equal (>=) | slt $at, $t0, $t1 beq $at, $0, label | bge t0, t1, label | MIPS requires slt + branch; RISC-V branches directly |
| Subroutine Call (Jump & Link) | jal target | jal ra, target | MIPS implicitly writes $ra; RISC-V specifies link register |
| Subroutine Return | jr $ra | ret (jalr zero, ra, 0) | RISC-V provides the ret pseudo-instruction |
4. 실전 반복문 어셈블리 1:1 비교4. Side-by-Side Loop Assembly Comparison
# C 코드: while (i < n) { sum += i; i++; }
# --- [MIPS 어셈블리] ---
loop_mips:
slt $t2, $t0, $t1 # $t2 = (i < n)
beq $t2, $zero, exit_mips
nop # <- 분기 지연 슬롯 (Branch Delay Slot)
add $s0, $s0, $t0 # sum += i
addi $t0, $t0, 1 # i++
j loop_mips
nop # <- 점프 지연 슬롯
exit_mips:
# --- [RISC-V 어셈블리] ---
loop_riscv:
bge t0, t1, exit_riscv # i >= n 이면 탈출 (지연 슬롯 없음)
add s0, s0, t0 # sum += i
addi t0, t0, 1 # i++
j loop_riscv # jal zero, loop_riscv
exit_riscv:
# C code: while (i < n) { sum += i; i++; }
# --- [MIPS Assembly] ---
loop_mips:
slt $t2, $t0, $t1 # $t2 = (i < n)
beq $t2, $zero, exit_mips
nop # <- Branch Delay Slot
add $s0, $s0, $t0 # sum += i
addi $t0, $t0, 1 # i++
j loop_mips
nop # <- Jump Delay Slot
exit_mips:
# --- [RISC-V Assembly] ---
loop_riscv:
bge t0, t1, exit_riscv # Exit if i >= n (no delay slot)
add s0, s0, t0 # sum += i
addi t0, t0, 1 # i++
j loop_riscv # jal zero, loop_riscv
exit_riscv:
5. 실무 적용 및 주의사항5. Practical Considerations and Caveats
- 산술 오버플로우 예외 동작 차이: MIPS의
add/addi는 부호 있는 오버플로우 발생 시 하드웨어 예외(Trap)를 발생시키므로 예외를 피하려면addu/addiu를 써야 하지만, RISC-V의add/addi는 오버플로우 예외를 발생시키지 않고 하위 32비트 결과만 기록합니다. - 즉시값(Immediate) 부호 확장 규칙: MIPS의 논리 즉시값 연산(
andi,ori,xori)은 16비트 상수를 0 확장(Zero-extend)하지만, RISC-V의 모든 12비트 즉시값 연산(andi,ori,xori포함)은 항상 부호 확장(Sign-extend)으로 동작합니다. - 분기 지연 슬롯 이식 주의: 하드웨어 지연 슬롯이 활성화된 MIPS 코드를 RISC-V로 옮길 때는 분기 명령어 바로 다음 줄의 명령어가 분기 성립 여부와 무관하게 실행되던 로직인지 반드시 확인하고 순서를 재배치해야 합니다.
- Arithmetic Overflow Trap Behavior: MIPS
add/additrigger a hardware exception on signed overflow (requiringaddu/addiufor wraparound arithmetic), whereas RISC-Vadd/addinever trap on overflow. - Immediate Sign-Extension Rules: MIPS bitwise immediate instructions (
andi,ori,xori) zero-extend their 16-bit immediate, while all RISC-V 12-bit immediate instructions (includingandi,ori,xori) sign-extend. - Porting Branch Delay Slots: When translating MIPS assembly written for hardware with enabled delay slots to RISC-V, relocate any instruction placed in the delay slot above the branch so it executes before the control transfer.
MIPS와 RISC-V는 기본적인 3-피연산자 레지스터 구조가 유사하므로, 조건 분기 명령어 통합 방식과 즉시값 부호 확장 규칙의 차이만 숙지하면 손쉽게 상호 포팅할 수 있습니다.
Because MIPS and RISC-V share a 3-operand register-to-register model, understanding fused relational branches and immediate sign-extension rules makes translating between the two straightforward.