Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- mysql
- Java
- function
- Class
- DATAPATH
- architecture
- for
- php
- Linux
- instruction
- html
- CSS
- computer
- Algorithm
- DoM
- react
- DS
- control
- github
- MIPS
- while
- data structure
- MacOS
- system
- Pipelining
- javascript
- DB
- web
- python
- XML
Archives
- Today
- Total
YYYEJI
[MIPS] Array에서 값을 가져오고 저장하는 Assembly code 본문
728x90
** C code를 기준 **
array에서 값을 가져오고 싶을 때 a = A[1]라고 작성을 하고,
array에 저장하고 싶을 땐 A[1] = a라고 작성을 하게 됩니다.
c 코드를 assembly code로 변환을 해보겠습니다.
A[4] = A[8] + 4
lw $t0, 32($s0) # t0 = A[8]
addi $t0, $t0, 4 # t0 = t0 + 4 = A[8] + 4
sw $t0, 16($s0) # A[4] = t0
$s0는 A[0]를 가르킵니다.
한 줄씩 살펴보면
lw $t0, 32($s0) # t0 = A[8]
$t0에 A[8]의 값을 가져와서 저장하는데 32 값은 4*8입니다.
(1 word가 4bytes이기 때문입니다.)
addi $t0, $t0, 4 # t0 = t0 + 4 = A[8] + 4
$t0에 앞서 가져왔던 A[8]의 값과 4를 더해서 $t0에 다시 넣는 코드입니다.
sw $t0, 16($s0) # A[4] = t0
$t0에 있는 값을 A[4]에 저장하라는 의미인데 16 값은 4*4입니다.
(lw와 동일한 이유로 1 word가 4bytes 때문에 4를 곱합니다.)
. | . | . | . | . | . |
. | . | . | . | . | . |
A[4] | A + 19 | A + 18 | A + 17 | A + 16 | A+16 번째 |
A[3] | A + 15 | A + 14 | A + 13 | A + 12 | A+12 번째 |
A[2] | A + 11 | A + 10 | A + 9 | A + 8 | A+8 번째 |
A[1] | A + 7 | A + 6 | A + 5 | A + 4 | A+4 번째 |
A[0] | A + 3 | A + 2 | A + 1 | A + 0 | A+0 번째 |
✓ 왼쪽부터 1byte가 할당됩니다.
◡̈
'Computer architectures' 카테고리의 다른 글
[MIPS] While문 assembly (0) | 2022.10.19 |
---|---|
[MIPS] Switch문 assembly code (0) | 2022.10.19 |
[MIPS] Bit-wise AND(&)로 Logical AND(&&) 구하기 (2) | 2022.10.19 |
[CA] Simple Memory Layout (0) | 2022.10.13 |
[MIPS] Procedure Call using Stack (0) | 2022.10.10 |