lectures.alex.balgavy.eu

Lecture notes from university.
git clone git://git.alex.balgavy.eu/lectures.alex.balgavy.eu.git
Log | Files | Refs | Submodules

Subroutines & the Stack.html (2922B)


      1 <?xml version="1.0" encoding="UTF-8"?>
      2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      3 <html><head><link rel="stylesheet" href="sitewide.css" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><meta name="exporter-version" content="Evernote Mac 6.13.1 (455785)"/><meta name="altitude" content="-0.1067453771829605"/><meta name="author" content="Alex Balgavy"/><meta name="created" content="2017-12-11 7:05:54 PM +0000"/><meta name="latitude" content="52.37358216328297"/><meta name="longitude" content="4.836162576452838"/><meta name="source" content="desktop.mac"/><meta name="updated" content="2017-12-11 10:35:46 PM +0000"/><title>Subroutines &amp; the Stack</title></head><body><div><span style="font-weight: bold;">Subroutines</span></div><div>allow a block of instructions to be called any number of times without having to rewrite it</div><div>calling a subroutine:</div><ol><li>Store contents of PC in link register (contains return address)</li><li>Branch to target address specified by call instruction</li><li>Execute whatever code is there</li><li>When return is called, branch to address in link register</li></ol><div><br style="font-weight: bold;"/></div><div>when nesting, the return address is pushed to the stack by the caller. on return the callee pops the saved return address from the stack into the link register.</div><div><br style="font-weight: bold;"/></div><div>parameters can be passed by:</div><ul><li>registers — convenient &amp; efficient, but may not be enough registers</li><li>processor stack — can pass an arbitrary amount of parameters</li></ul><div><br/></div><div><span style="font-weight: bold;">Stack</span></div><div>a pile of stuff where the stuff can only be added to or taken from the top</div><div>also called LIFO (last-in-first-out)</div><div>push (add elements)</div><div><ol><li>subtract 4 from SP (32-bit)</li><li>move value from register to address stored in SP</li></ol></div><div><br/></div><div>pop (remove elements)</div><div><ol><li>move value from byte at address stored in SP to register</li><li>add 4 to SP (32-bit)</li></ol></div><div><br/></div><div>processor stack is used for storing data, e.g. parameters, registers</div><div>stack pointer register (SP) holds address of top element in stack</div><div>‘grows downwards’ — from high to low addresses, decreasing size</div><div><br/></div><div>stack fame — ‘private workspace’ for a subroutine</div><div><ul><li>allocated at start, deallocated at end of subroutine</li><li>can also be used for local memory variables</li><li>base/frame pointer points to base of current frame, can easily access parameters by using offset(%rbp)</li></ul><div><br/></div></div><div><img src="Subroutines%20&amp;%20the%20Stack.resources/screenshot.png" height="719" width="706"/><br/></div><div><br/></div></body></html>