Skip to content

Commit

Permalink
update post
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueHorn07 committed Nov 3, 2024
1 parent bfbaaaf commit f7f2610
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions _posts/computer-science/operating-system/2024-10-23-stack-frame.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,13 @@ Base PointerλŠ” μž¬λ°ŒλŠ” νŠΉμ„±μ„ 가지고 μžˆλŠ”λ°, BP κΈ°μ€€μœΌλ‘œ μœ„μ—
mov sp, bp ; BP 값을 SP둜 볡사해 μŠ€νƒμ„ 정리
```
<br/>
이제 BP 값을 caller의 BP κ°’μœΌλ‘œ λ³΅μ›ν•œλ‹€. 이것도 μ•„λž˜μ˜ λͺ…λ Ήμ–΄λ‘œ μˆ˜ν–‰ν•  수 μžˆλ‹€.
```
pop bp
```
μœ„μ˜ 과정을 톡해 BPκ°€ κ°€λ¦¬ν‚€λ˜ 곳에 μ €μž₯λ˜μ–΄ 있던 caller BP 값이 BP λ ˆμ§€μŠ€ν„°μ— μ €μž₯λœλ‹€. `pop`이 ν˜ΈμΆœλ˜μ—ˆκΈ° λ•Œλ¬Έμ—, SPλŠ” μžλ™μœΌλ‘œ SP += 4 처리 λœλ‹€ γ…Žγ…Ž
μœ„μ˜ 과정을 톡해 BPκ°€ κ°€λ¦¬ν‚€λ˜ 곳에 μ €μž₯λ˜μ–΄ 있던 caller BP 값이 BP λ ˆμ§€μŠ€ν„°μ— μ €μž₯λœλ‹€. `pop`이 ν˜ΈμΆœλ˜μ—ˆκΈ° λ•Œλ¬Έμ—, SPλŠ” μžλ™μœΌλ‘œ SP += 4 처리 λœλ‹€ γ…Žγ…Ž μ–΄μ…ˆλΈ”λ¦¬μ—μ„œλŠ” μš” `mov`와 `pop` 과정을 ν•©μ³μ„œ `leave`λΌλŠ” λͺ…λ Ήμ–΄λ‘œ μ œκ³΅ν•˜κΈ°λ„ ν•œλ‹€.
<br/>
Expand Down Expand Up @@ -195,16 +193,29 @@ sub sp, N
leave
ret

; Caller Termination
; Caller Termination (remove args)
pop
```
`ret`을 μ‹€ν–‰ν•΄μ„œ CallerλŠ” ν•¨μˆ˜ ν˜ΈμΆœμ—μ„œ λŒμ•„μ˜¨ ν›„μ—λŠ”, `pop`으둜 μŠ€νƒμ— λ„£μ–΄λ’€λ˜ ν•¨μˆ˜ argsλ₯Ό λΉΌμ£ΌλŠ” 과정이 μΌμ–΄λ‚œλ‹€!
## ν•¨μˆ˜ 리턴값을 μ²˜λ¦¬ν•˜λŠ” 법에 λŒ€ν•΄
## ν•¨μˆ˜ 리턴값은 eax λ ˆμ§€μŠ€ν„°μ— μ €μž₯λœλ‹€.
λ§Œμ•½ ν•¨μˆ˜μ— 리턴값이 μ‘΄μž¬ν•œλ‹€λ©΄, κ·Έ 값은 `eax` λ ˆμ§€μŠ€ν„°μ— μ €μž₯λœλ‹€.
```
; Callee Termination
mov eax, 42
leave
ret

; Caller Termination (remove args)
pop
```
TDB...
ν•¨μˆ˜κ°€ `ret`으둜 μ’…λ£Œλ˜κΈ° 전에 `mov eax, xxx`λ₯Ό 톡해 `eax` λ ˆμ§€μŠ€ν„°μ— λ‹΄κΈ΄λ‹€. 그러면 콜러 ν•¨μˆ˜λŠ” `eax`에 μ €μž₯된 리턴 값을 μ‚¬μš©ν•˜κ±°λ‚˜, 콜러 ν•¨μˆ˜μ˜ μŠ€νƒμ— μ–΄λ–€ λ³€μˆ˜λ‘œ μ €μž₯ν•œλ‹€.
리턴 값은 λ‹€λ₯Έ λ ˆμ§€μŠ€ν„°κ°€ μ•„λ‹ˆλΌ 항상 `eax`에 μ €μž₯λ˜λŠ”λ°, 이것은 x86 μ•„ν‚€ν…μ²˜μ—μ„œ "Calling Convention"이기 λ•Œλ¬Έμ΄λ‹€. ν•΄λ‹Ή κ·œμ•½μ—μ„œλŠ” ν•¨μˆ˜ 리턴 값은 `eax` λ ˆμ§€μŠ€ν„°μ— 담도둝 μ •μ˜ν•˜κ³  μžˆλ‹€. 사싀 `eax` λ ˆμ§€μŠ€ν„°λŠ” "λˆ„μ‚°κΈ°(Accumulator) λ ˆμ§€μŠ€ν„°"λΌλŠ” μ΄λ¦„μœΌλ‘œ, μ‚°μˆ  μ—°μ‚°μ˜ κ²°κ³Όλ₯Ό λ‹΄κΈ° μœ„ν•΄ λ””μžμΈλœ λ ˆμ§€μŠ€ν„°μ΄μ§€λ§Œ, μš” ν•¨μˆ˜ 리턴 값을 μ €μž₯ν•˜λŠ” μš©λ„λ‘œλ„ μ‚¬μš©λ˜λŠ” 것이닀.
# Overall ASM Code
Expand Down

0 comments on commit f7f2610

Please sign in to comment.