[PDF] [PDF] Procedures and the Call Stack





Previous PDF Next PDF





x64 Cheat Sheet

x64 assembly code uses sixteen 64-bit registers. Register %rsp is used as the stack pointer a pointer to the topmost element in the stack.



Functions in MIPS

Oops in Strings/pointers (example from last time) Stack Pointer Saved ... Assembly language is untyped—there is no distinction between integers.



M6800 Assembly Language Programming

Program Counter (PC). Stack Pointer (SP). Index Register (X). Condition Code Register (CCR). Figure 3.2 - Registers of the MC6802 Microrprocessor. Page 3. 3.



Assembly Programming (III)

In AVR a stack is implemented as a block of consecutive bytes in the SRAM memory. ? A stack has at least two parameters: ? Bottom. ? Stack pointer.



Lecture 9: Assembly Programming Part 2

How would you convert this into assembly? int n = 10; Memory Instructions in MIPS assembly ... Special register $sp stores the stack pointer.



A Tiny Guide to Programming in 32-bit x86 Assembly Language

special purposes—the stack pointer (ESP) and the base pointer (EBP). In some cases namely EAX



A problem with parameter passing via stack Pointers and Arrays

– A function's activation record defines its “scope”. – We've seen examples of how to do this in Assembly. ?. Pointer. – Address of a variable in memory. – 



Procedure Stack operations

Computer Organization and Assembly Languages A push operation decrements the stack pointer by 2 or ... location pointed to by the stack pointer.



Y86 Assembly

Set up Stack pointer irmovl Stack %ebp. # Set up base pointer jmp Main. # Execute main program. # Array of 4 elements .align 4 array: .long 0xd .long 0xc0.



[PDF] The stack and the stack pointer

A reserved area of memory used to keep track of a program's internal operations including functions return addresses passed parameters etc A stack is



[PDF] The stack and the stack pointer

-Reverse Assembly Stack and Stack Pointer -What happens to stack and SP for instructions (e g PSHX JSR) -How the SP is used in getting to and leaving 



[PDF] Procedure Stack operations

A push operation decrements the stack pointer by 2 or 4 (depending on operands) and copies a value into the location pointed to by the stack pointer 0FF0



[PDF] Stack Operations Runtime Stack

The runtime stack is a memory array managed directly by the CPU using the ESP register known as the stack pointer register The ESP register holds a 32-bit



[PDF] Assembly Programming (III)

In AVR a stack is implemented as a block of consecutive bytes in the SRAM memory ? A stack has at least two parameters: ? Bottom ? Stack pointer



[PDF] Chapter 8 – Stacks

Initialize stack pointer at beginning of program STACK equ A subroutine is “called” in assembly using the MSP430 Single operand instructions:



[PDF] Quick Guide to Assembly in 161 0xbfffffff 0xbfffffc0 “top of the stack”

instruction pointer (EIP): register containing the address of the instruction to be executed Other examples: EAX (return value) etc Instructions



[PDF] Procedures and the Call Stack

Procedure/stack instructions • Calling conventions Stack Pointer: esp stack grows Pointer: ebp Stack frames support procedure calls Contents



[PDF] Stack Stack pointer and Subroutines in 8085 – With coding examples

location of the most recent data entry on the stack is known as the Stack Top How does a stack work in assembly language? We use two main instructions to 



[PDF] Stacks and Frames

variables and arguments by using offsets to the frame pointer assembly code actually contains the following two instructions: – pushl ebp

  • What is stack pointer assembly?

    The Stack Pointer is a special register in I/O Memory that points to space allocated in SRAM, referred to as The Stack. The Stack is used to temporarily store register values and return addresses when subroutines are called. The Stack Pointer is a 16-bit register defined in include files as SPH and SPL.
  • How to initialize stack pointer in assembly?

    To set up the stack pointers, enter each mode with interrupts disabled, and assign the appropriate value to the stack pointer. The stack pointer value set up in the reset handler is automatically passed as a parameter to __user_initial_stackheap() by C library initialization code.
  • What is SPL in AVR?

    SPH holds the most significant address byte, SPL the least significant. This is only true, if the AVR type has more than 256 byte SRAM. If not, SPH is undefined and must not and cannot be used.
  • Stack registers in x86
    In 8086, the main stack register is called stack pointer - SP. The stack segment register (SS) is usually used to store information about the memory segment that stores the call stack of currently executed program. SP points to current stack top.

WhyProcedures?Whyfunctions?Whymethods?ProceduralAbstractionintcontains_char(char* haystack, char needle) {while (*haystack != '\0') {if (*haystack == needle) return 1 ;haystack++;}return 0;}

CallChainyoo(...){••who();••}who(...){• • •ru();• • •ru();• • •}ru(...){•••}yoowhoruExampleCallChainru4

CallChainExampleyoo(...){••who();••}who(...){• • •amI();• • •amI();• • •}amI(...){••amI();••}yoowhoamIamIamIExampleCallChainamIProcedureamIisrecursive(callsitself)14

who(...){• • •amI();• • •amI();• • •}ExampleyoowhoamIamIamIamIyoo%ebp%espStackwho17

who(...){• • •amI();• • •amI();• • •}ExampleyoowhoamIamIamIamIyoo%ebp%espStackwho23

who(...){• • •amI();• • •amI();• • •}ExampleyoowhoamIamIamIamIyoo%ebp%espStackwho25

ProcedureControlFlowInstructionsProcedurecall:call label1.Pushreturnaddressonstack2.JumptolabelReturnaddress:Addressofinstructionaftercall.Example:804854e:e8 3d 06 00 00 call8048b90

8048553:50 pushl%eaxProcedurereturn:ret1.Popreturnaddressfromstack2.Jumptoaddress27

%esp%eip0x804854eProcedureCall/Return:10x1080x10c0x1101230x108804854e:e8 3d 06 00 00 call 8048b90

8048553:50 pushl%eax28call 8048b90%eip=instructionpointer=programcounter

%esp%eip%esp%eip0x804854e0x1080x1080x10c0x1100x1040x804854e123ProcedureCall/Return:20x1080x10c0x1101230x108call 8048b90804854e:e8 3d 06 00 00 call 8048b90

8048553:50 pushl%eax0x80485530x104%eip=instructionpointer=programcounter290x8048553

%esp%eip%esp%eip0x80485530x1080x1080x10c0x1100x1040x804854e0x8048553123ProcedureCall/Return:30x1080x10c0x1101230x108call 8048b90804854e:e8 3d 06 00 00 call 8048b90

8048553:50 pushl%eax0x8048b900x104%eip: programcounter30+ 0x000063dPC-relativeaddress

%esp%eip0x104%esp%eip0x80485910x80485910x1040x1040x1080x10c0x1100x8048553123ProcedureCall/Return:40x1080x10c0x1101238048591:c3 ret0x8048553%eip: programcounter31ret0x80485530x108

Revisitingswapintzip1 = 02481;intzip2 = 98195;void call_swap() {swap(&zip1, &zip2);}void swap(int*xp, int*yp) {intt0 = *xp;intt1 = *yp;*xp= t1;*yp= t0;}call_swap:• • •pushl$zip2# Global Varpushl$zip1# Global Varcall swap• • •&zip2&zip1Rtnadr%espResultingStack•••Callingswapfromcall_swap34

Revisitingswapvoid swap(int*xp, int*yp) {intt0 = *xp;intt1 = *yp;*xp= t1;*yp= t0;}swap:pushl%ebpmovl%esp,%ebppushl%ebxmovl12(%ebp),%ecxmovl8(%ebp),%edxmovl(%ecx),%eaxmovl(%edx),%ebxmovl%eax,(%edx)movl%ebx,(%ecx)movl-4(%ebp),%ebxmovl%ebp,%esppopl%ebpretBodySetUpFinish35

1284swapBody&zip2&zip1Rtnadr%espEnteringStack•••%ebpypxpRtnadrOld%ebp•••ResultingStackOld%ebxmovl 12(%ebp),%ecx # get ypmovl 8(%ebp),%edx # get xp. . .Offsetrelativetonew%ebp39Bodycall_swapframe%esp%ebp

Disassembledswap080483a4 :80483a4: 55 push %ebp80483a5: 89 e5 mov%esp,%ebp80483a7: 53 push %ebx80483a8: 8b 55 08 mov0x8(%ebp),%edx80483ab: 8b 4d 0c mov0xc(%ebp),%ecx80483ae: 8b 1a mov(%edx),%ebx80483b0: 8b 01 mov(%ecx),%eax80483b2: 89 02 mov%eax,(%edx)80483b4: 89 19 mov%ebx,(%ecx)80483b6: 5b pop %ebx80483b7: c9 leave 80483b8: c3 ret 8048409: e8 96 ff ff ff call 80483a4 804840e: 8b 45 f8 mov0xfffffff8(%ebp),%eaxCallingCode44mov%ebp,%esppop %ebprelativeaddress(littleendian)

RegisterSavingConventionsWhenprocedureyoocallswho:yooisthecallerwhoisthecalleeWillregistercontentsstillbethereafteraprocedurecall?yoo:• • •movl$12345, %edxcall whoaddl%edx, %eax• • •retwho:• • •movl8(%ebp), %edxaddl$98195, %edx• • •ret47?

IA32/LinuxRegisterSavingConventions%eax, %edx, %ecxCallersavespriortocallifneedsvaluesaftercall%eaxalsousedtoreturnintegervalue%ebx, %esi, %ediCalleesavesifwilluse%esp, %ebpspecialformofcalleesaverestoredtooriginalvaluesbeforereturning%eax%edx%ecx%ebx%esi%edi%esp%ebpCaller-SaveTemporariesCallee-SaveTemporariesSpecial49

APuzzle*p = d;return x -c;movsbl12(%ebp),%edxmovl16(%ebp),%eaxmovl%edx,(%eax)movswl8(%ebp),%eaxmovl20(%ebp),%edxsubl%eax,%edxmovl%edx,%eaxWritetheCfunctionheader,types,andorderofparameters.Cfunctionbody:assembly:movsbl=movesign-extendingabytetoalong(4-byte)movswl=movesign-extendingaword(2-byte)toalong(4-byte)

Example:PointerstoLocalVariables51void s_helper(intx, int*accum) {if (x <= 1) {return;} else {intz = *accum* x;*accum= z;s_helper(x-1,accum);}}intsfact(intx) {intval= 1;s_helper(x, &val);return val;}Top-LevelCallRecursiveProcedurePasspointertoupdatelocationsfact(3)val=1s_helper(3,&val)val=3s_helper(2,&val)val=6s_helper(1,&val)val=6.

Temp.Space%espCreating&InitializingPointer52intsfact(intx) {intval= 1;s_helper(x, &val);return val;}_sfact:pushl %ebp# Save %ebpmovl %esp,%ebp# Set %ebpsubl $16,%esp# Add 16 bytes movl 8(%ebp),%edx# edx = xmovl $1,-4(%ebp)# val = 1InitialpartofsfactxRtnadrOld%ebp0 4 8 -4 val = 1Unused-12 -8 -16_sfact:pushl%ebp# Save %ebpmovl%esp,%ebp# Set %ebpsubl$16,%esp# Add 16 bytes movl8(%ebp),%edx# edx= xmovl$1,-4(%ebp)# val= 1_sfact:pushl%ebp# Save %ebpmovl%esp,%ebp# Set %ebpsubl$16,%esp# Add 16 bytes movl8(%ebp),%edx# edx= xmovl$1,-4(%ebp)# val= 1_sfact:pushl%ebp# Save %ebpmovl%esp,%ebp# Set %ebpsubl$16,%esp# Add 16 bytes movl8(%ebp),%edx# edx= xmovl$1,-4(%ebp)# val= 1%esp%esp%ebpMuststorevalinmemory(stack);registersdonothaveaddresses.

PassingPointer53intsfact(intx) {intval= 1;s_helper(x, &val);return val;}leal-4(%ebp),%eax# Compute &valpushl%eax# Push on stackpushl%edx# Push xcall s_helper# callmovl-4(%ebp),%eax# Return val•••# FinishCallings_helperfromsfactxRtnadrOld%ebp%ebp0 4 8 val = 1-4 Unused-12 -8 -16%espx&valStackattimeofcall:leal-4(%ebp),%eax# Compute &valpushl%eax# Push on stackpushl%edx# Push xcall s_helper# callmovl-4(%ebp),%eax# Return val•••# Finishleal-4(%ebp),%eax# Compute &valpushl%eax# Push on stackpushl%edx# Push xcall s_helper# callmovl-4(%ebp),%eax# Return val•••# Finishval=x!Muststorevalinmemory(stack);registersdonothaveaddresses.

IA32/LinuxProcedureSummarycall, ret, push, popStackdisciplinefitsprocedurecall/return.*IfPcallsQ,QreturnsbeforeP,includingrecursion.Conventionssupportarbitraryfunctioncalls.Safelystoreper-callvaluesinlocalstackframeandcallee-savedregistersPushfunctionargumentsattopofstackbeforecallResultreturnedin%eax*Take251tolearnaboutlanguageswhereitdoesn't.ReturnAddrSavedRegisters+LocalVariablesArgumentBuildOld%ebpArgumentsCallerFrame%ebp%esp54

quotesdbs_dbs14.pdfusesText_20
[PDF] stack pointer diagram

[PDF] stack pointer example

[PDF] stack pointer in 8086

[PDF] stack program in c pdf

[PDF] stack variable c++

[PDF] stack variable constructor

[PDF] stack vs heap data structures

[PDF] stacked cups program in c

[PDF] stacks are known as ________ data structures.

[PDF] stade de france 13 novembre 2019

[PDF] stagecoach

[PDF] stagecoach b5

[PDF] stagecoach banbury

[PDF] stages of child language acquisition a level

[PDF] stages of child language acquisition slideshare