일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- stl
- Flexbox
- 알고리즘
- 에라토스테네스의 체
- margin
- CSS
- align-items
- c
- Prefix Sums
- 소수
- 통신사할인
- series
- skt membership
- SK바이오사이언스
- dataframe
- 상태
- c++
- 포토샵
- grid
- pandas
- Javascript
- Gap
- 백준
- 강화학습
- 수학
- spring
- Design Pattern
- Codility
- 확률
- Photoshop
Archives
- Today
- Total
sliver__
2.13 A C Sort Exampleto Put It All Together 본문
728x90
Swap C code를 assembly code로 바꾸기 !
void swap(int v[], int k)
{
int temp;
temp = v[k];
v[k] = v[k+1];
v[k+1] = temp;
}
The Procedure swap
Register Allocation for swap
v : $a0, k : $a1
temp : $t0
Code for the Body of the Procedure swap
v의 array에 접근하기 전 ! int는 4bytes 이므로 k * 4를 수행해 접근해야한다 !
sll $t1, $a1,2 # reg $t1 = k * 4
add $t1, $a0,$t1 # reg $t1 = v + (k * 4)
# reg $t1 has the address of v[k]
lw $t0, 0($t1) # reg $t0 (temp) = v[k]
lw $t2, 4($t1) # reg $t2 = v[k + 1]
# refers to next element of v
sw $t2, 0($t1) # v[k] = reg $t2
sw $t0, 4($t1) # v[k+1] = reg $t0 (temp)
The Full Swap Procedure
swap: sll $t1, $a1, 2
add $t1, $a0, $t1
lw $t0, 0($t1)
lw $t2, 4($t1)
sw $t2, 0($t1)
sw $t0, 4($t1)
jr $ra
The Procedure sort
void sort (int v[], int n)
{
int i, j;
for (i = 0; i < n; i += 1) {
for (j = i – 1; j >= 0 && v[j] > v[j + 1]; j =1) {
swap(v,j);
}
}
}
Register Allocation for sort
v : $a0, n : $a1
i : $s0, j : $s1
The Full Procedure Sort
728x90
'CS > 컴퓨터 구조' 카테고리의 다른 글
2.16 Real Stuff: ARMv7 (32-bit) Instructions (0) | 2023.10.23 |
---|---|
2.14 Arrray versus Pointers (1) | 2023.10.22 |
2.12 Translating and Starting a Program (0) | 2023.10.21 |
2.11 Parallelism and Instructions : Synchronization (0) | 2023.10.21 |
2.10 MIPS Addressing for 32-bit immediates and Addresses (1) | 2023.10.21 |
Comments