SlideShare a Scribd company logo
2
Most read
3
Most read
5
Most read
Run-Time Stack
• Why we can use C/C++ to write “recursive” program
but not FORTRAN ?
• Why we can use “variable number parameters” in
C/C++ but not in other languages ?
• How the parameter/return value are passed in
function/procedure call ?
• What are difference between local & static variables ?
• Why new “ENTER/LEAVE” instruction since 80386 ?
• How to correctly use the “local variables” ?
Why use “run-time stack” in
languages ?
• Store the following values in function calls:
(communication between caller & callee)
• Machine status: program counter (return
address)、registers、CPU flags.
• The parameters in function/procedure calls.
• The data of local variables.
• Control link: points to the activation record
(or frame) of the caller.
Machine status in “run-time stack”
void interrupt (*old_isr)(...);
void interrupt new_isr(…)
{
(*old_isr)();
}
@new_isr$qve proc far
push ax
push bx
push cx
push dx
push es
push ds
push si
push di
push bp
mov bp,DGROUP
mov ds,bp
mov bp,sp
; {
; (*old_isr)();
pushf
call dword ptr DGROUP:_old_isr
; }
pop bp
pop di
pop si
pop ds
pop es
pop dx
pop cx
pop bx
pop ax
iret
@new_isr$qve endp
n-6
n-2
n
n+2
n+4
n+6
n+8
n+10
n+12
n+14
n+16
n+18
n+22
n+24
CPU flags
Return Address
.
.
.
ax
axbx
cx
dx
es
ds
si
di
bp sp, bp
Parameters in “run-time stack”
_DATA segment word public 'DATA'
_c label byte
db 97
_i label word
db 52,18
_j label word
db 120,86,0,0
_DATA ends
_TEXT segment byte public 'CODE'
;
; void main(void)
;
assume cs:_TEXT
_main proc near
push bp
mov bp,sp
;
; {
; val = func(c, i, j);
;
push word ptr DGROUP:_j+2
push word ptr DGROUP:_j
push word ptr DGROUP:_i
mov al,byte ptr DGROUP:_c
push ax
call near ptr _func
add sp,8
mov word ptr DGROUP:_val,ax
;
; }
;
pop bp
ret
_main endp
_TEXT ends
char c='a';
int i=0x1234;
long j=0x5678;
int val;
int func(char, int, long);
void main(void)
{
val = func(c, i, j);
}
n-16
n-14
n-12
n-10
n-8
n-6
n-4
n-2
n
n+2
n+4
Return Address
.
.
.
low word of j
i
c
bp
high word of j
sp
bp
Local variables in “run-time stack”
_TEXT segment byte public 'CODE'
;
; int func(char cc,int ii,long jj)
;
assume cs:_TEXT
_func proc near
push bp
mov bp,sp
sub sp,4
;
; {
; int k=0, l=1;
;
mov word ptr [bp-2],0
mov word ptr [bp-4],1
;
; return k;
;
mov ax,word ptr [bp+6]
jmp short @1@58
@1@58:
;
; }
;
mov sp,bp
pop bp
ret
_func endp
;
; void main(void)
;
assume cs:_TEXT
_main proc near
...
_main endp
_TEXT ends
char c='a';
int i=0x1234;
long j=0x5678;
int val;
int func(char cc, int ii, long jj)
{
int k=0, l=1;
return ii;
}
void main(void)
{
val = func(c, i, j);
}
n-4
n-2
n
n+2
n+4
n+6
n+8
n+10
n+12
n+14
n+16
Return Address
.
.
.
low word of j =jj
I =ii
c =cc
bp
high word of j =jj
Return Address
sp
bpbp
?? = l
?? = k
Control
Link
bp
ENTER Instruction
LEAVE Instruction

More Related Content

DOCX
The proj main.asm;; the_proj_main.asm;; created 231
PDF
Basic c++ 11/14 for python programmers
PDF
C++ Programming - 2nd Study
PDF
TLPI - 6 Process
PDF
C++ Programming - 11th Study
PPTX
Understand more about C
PPT
C++ Preprocessor Directives
The proj main.asm;; the_proj_main.asm;; created 231
Basic c++ 11/14 for python programmers
C++ Programming - 2nd Study
TLPI - 6 Process
C++ Programming - 11th Study
Understand more about C
C++ Preprocessor Directives

What's hot (19)

PDF
C++ Programming - 4th Study
PPTX
Merge sort
PDF
C++ Programming - 1st Study
PDF
1 introducing c language
PDF
C++ Programming - 3rd Study
PPTX
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
PPTX
What's new in c#7
DOC
All VLSI programs
PPT
Eecs 317 20010209
PDF
MeCC: Memory Comparison-based Code Clone Detector
PDF
Data Structure - 2nd Study
PDF
7 functions
PPT
Doublylinklist
DOCX
Computer Architecture and Organization lab with matlab
PPT
Lecture05
DOCX
Cmptr ass
PDF
GoでKVSを書けるのか
PDF
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
PDF
Singly Linked List
C++ Programming - 4th Study
Merge sort
C++ Programming - 1st Study
1 introducing c language
C++ Programming - 3rd Study
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
What's new in c#7
All VLSI programs
Eecs 317 20010209
MeCC: Memory Comparison-based Code Clone Detector
Data Structure - 2nd Study
7 functions
Doublylinklist
Computer Architecture and Organization lab with matlab
Lecture05
Cmptr ass
GoでKVSを書けるのか
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
Singly Linked List
Ad

Viewers also liked (20)

PPTX
Presentacion profesional
DOCX
Documento sin título
PPTX
tipos de movimientos - fisica
ODP
Yacimiento arqueológico de Oreto-Zuqueca
PPTX
Museos Campo de Calatrava
PPT
Snowboard
PDF
Tipos de drogas
PPTX
Educación
DOCX
Potential health hazard associated with handling pipe used in oil and gas pro...
DOCX
General safety precautions for maintenance of extinguisher
PPTX
1차시 앱인벤터 소개
PDF
Componentes red informatica natalia garcia
PPTX
Energies renovables
PPTX
презентація Данте
PPTX
English i text set (1)
PDF
Journal of Water Pollution & Purification Research vol 3 issue 3
PDF
Isik Uni Transcript (GQ)
DOCX
Як складати конспект
PDF
Forests-Wood-and-Climate-Change-Fact-Sheet
DOCX
Календарно - тематичне планування 11 клас. Профільний рівень
Presentacion profesional
Documento sin título
tipos de movimientos - fisica
Yacimiento arqueológico de Oreto-Zuqueca
Museos Campo de Calatrava
Snowboard
Tipos de drogas
Educación
Potential health hazard associated with handling pipe used in oil and gas pro...
General safety precautions for maintenance of extinguisher
1차시 앱인벤터 소개
Componentes red informatica natalia garcia
Energies renovables
презентація Данте
English i text set (1)
Journal of Water Pollution & Purification Research vol 3 issue 3
Isik Uni Transcript (GQ)
Як складати конспект
Forests-Wood-and-Climate-Change-Fact-Sheet
Календарно - тематичне планування 11 клас. Профільний рівень
Ad

Similar to runtimestack (20)

PDF
lec15_x86procedure_4up.pdf
PPTX
[ASM]Lab6
PPT
Runtimeenvironment
PPTX
PPTX
PPTX
Reversing malware analysis training part4 assembly programming basics
PDF
Unit 4 assembly language programming
PPTX
Lecture 14 run time environment
PDF
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
PPTX
Microprocessors-based systems (under graduate course) Lecture 7 of 9
PDF
Stack and subroutine
PDF
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
PPT
Assembly language programming_fundamentals 8086
PDF
Basics of building a blackfin application
PDF
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
PPTX
Chapter 4 programming concepts III
PDF
OptimizingARM
PPTX
Procedure.lecture number pptx slide form
PDF
Fix assembly Code new peoblem on call printf gt .pdf
PPTX
Co&al lecture-07
lec15_x86procedure_4up.pdf
[ASM]Lab6
Runtimeenvironment
Reversing malware analysis training part4 assembly programming basics
Unit 4 assembly language programming
Lecture 14 run time environment
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Microprocessors-based systems (under graduate course) Lecture 7 of 9
Stack and subroutine
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
Assembly language programming_fundamentals 8086
Basics of building a blackfin application
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
Chapter 4 programming concepts III
OptimizingARM
Procedure.lecture number pptx slide form
Fix assembly Code new peoblem on call printf gt .pdf
Co&al lecture-07

runtimestack

  • 1. Run-Time Stack • Why we can use C/C++ to write “recursive” program but not FORTRAN ? • Why we can use “variable number parameters” in C/C++ but not in other languages ? • How the parameter/return value are passed in function/procedure call ? • What are difference between local & static variables ? • Why new “ENTER/LEAVE” instruction since 80386 ? • How to correctly use the “local variables” ?
  • 2. Why use “run-time stack” in languages ? • Store the following values in function calls: (communication between caller & callee) • Machine status: program counter (return address)、registers、CPU flags. • The parameters in function/procedure calls. • The data of local variables. • Control link: points to the activation record (or frame) of the caller.
  • 3. Machine status in “run-time stack” void interrupt (*old_isr)(...); void interrupt new_isr(…) { (*old_isr)(); } @new_isr$qve proc far push ax push bx push cx push dx push es push ds push si push di push bp mov bp,DGROUP mov ds,bp mov bp,sp ; { ; (*old_isr)(); pushf call dword ptr DGROUP:_old_isr ; } pop bp pop di pop si pop ds pop es pop dx pop cx pop bx pop ax iret @new_isr$qve endp n-6 n-2 n n+2 n+4 n+6 n+8 n+10 n+12 n+14 n+16 n+18 n+22 n+24 CPU flags Return Address . . . ax axbx cx dx es ds si di bp sp, bp
  • 4. Parameters in “run-time stack” _DATA segment word public 'DATA' _c label byte db 97 _i label word db 52,18 _j label word db 120,86,0,0 _DATA ends _TEXT segment byte public 'CODE' ; ; void main(void) ; assume cs:_TEXT _main proc near push bp mov bp,sp ; ; { ; val = func(c, i, j); ; push word ptr DGROUP:_j+2 push word ptr DGROUP:_j push word ptr DGROUP:_i mov al,byte ptr DGROUP:_c push ax call near ptr _func add sp,8 mov word ptr DGROUP:_val,ax ; ; } ; pop bp ret _main endp _TEXT ends char c='a'; int i=0x1234; long j=0x5678; int val; int func(char, int, long); void main(void) { val = func(c, i, j); } n-16 n-14 n-12 n-10 n-8 n-6 n-4 n-2 n n+2 n+4 Return Address . . . low word of j i c bp high word of j sp bp
  • 5. Local variables in “run-time stack” _TEXT segment byte public 'CODE' ; ; int func(char cc,int ii,long jj) ; assume cs:_TEXT _func proc near push bp mov bp,sp sub sp,4 ; ; { ; int k=0, l=1; ; mov word ptr [bp-2],0 mov word ptr [bp-4],1 ; ; return k; ; mov ax,word ptr [bp+6] jmp short @1@58 @1@58: ; ; } ; mov sp,bp pop bp ret _func endp ; ; void main(void) ; assume cs:_TEXT _main proc near ... _main endp _TEXT ends char c='a'; int i=0x1234; long j=0x5678; int val; int func(char cc, int ii, long jj) { int k=0, l=1; return ii; } void main(void) { val = func(c, i, j); } n-4 n-2 n n+2 n+4 n+6 n+8 n+10 n+12 n+14 n+16 Return Address . . . low word of j =jj I =ii c =cc bp high word of j =jj Return Address sp bpbp ?? = l ?? = k Control Link bp ENTER Instruction LEAVE Instruction