<jupyter_start><jupyter_text>SLS Lecture 11 :  Program Anatomy II : Functions<jupyter_code># setup for sumit examples
appdir=os.getenv('HOME')
appdir=appdir + "/sum"
#print(movdir)
output=runTermCmd("[[ -d " + appdir + " ]] &&  rm -rf "+ appdir + 
             ";mkdir " + appdir + 
             ";cp ../src/Makefile ../src/*.bin ../src/setup.gdb ../src/usesumABC.gdb ../src/usesum*.S ../src/sumit*.S " + appdir)
#TermShellCmd("ls", cwd=appdir)
display(Markdown('''
- create a directory `mkdir sum; cd sum`
- copy sumit and usesumit code examples
- add a `Makefile` to automate assembling and linking
    - we are going run the commands by hand this time to highlight the details
- add our `setup.gdb` and `usesumABC.gdb` to make working in gdb easier
- normally you would want to track everything in git
'''))<jupyter_output><empty_output><jupyter_text>Examples used in previous slides `sumit` as per last lectureHardcode `int3` stops us from saving our sum as we would like<jupyter_code>display(Markdown(FileCodeBox(
    file="../src/sumit.S", 
    lang="gas", 
    title="<b>CODE: asm - sumit.S : Version 1",
    h="100%", 
    w="107em"
)))
display(Markdown("**Corresponding: usesum**"))
display(Markdown(FileCodeBox(
    file="../src/usesum.S", 
    lang="gas", 
    title="<b>CODE: asm - sumit.S : Version 1",
    h="100%", 
    w="107em"
)))
TermShellCmd("make usesum", cwd=appdir, prompt='')<jupyter_output><empty_output><jupyter_text>see last lecture notes to in terms of how to use `gdb` to run and play with `usesum`  Made `sumit` more generic but ...1. Make the address of the array a parameter passed to `sumit`. 2. Changed `sumit` code to use this instead of `XARRAY`3. Now have `usesum` leave space for three arrays and have three jumps to `sumit`4. remove `int3` from `sumit` and replace it with a `jmp` back to the address after the first    jump.5. BUT WE HAVE PROBLEMS IN `sumit`:  - how do we `jump` back to the right spot?<jupyter_code>display(Markdown(FileCodeBox(
    file="../src/sumit2.S", 
    lang="gas", 
    title="<b>CODE: asm - sumit2.S : Version 2",
    h="100%", 
    w="107em"
)))
display(Markdown("**Corresponding: usesum**"))
display(Markdown(FileCodeBox(
    file="../src/usesum2.S", 
    lang="gas", 
    title="<b>CODE: asm - sumit2.S : Version 2",
    h="100%", 
    w="107em"
)))
TermShellCmd("make usesum2", cwd=appdir, prompt='')
display(Markdown(FileCodeBox(
    file="../src/usesumABC.gdb", 
    lang="gdb", 
    title="<b>GDB commands to use",
    h="100%", 
    w="107em"
)))<jupyter_output><empty_output><jupyter_text>Now you can single step or continue as you like  Version 3:  Fix our problem by passing in the return address in a register.  In our case we use 'rdx' and jump back via this register.Note this approach requires one register for each call to hold the return address.  This has problems if we were to nest calls.<jupyter_code>display(Markdown(FileCodeBox(
    file="../src/sumit3.S", 
    lang="gas", 
    title="<b>CODE: asm - sumit3.S : Version 3",
    h="100%", 
    w="107em"
)))
display(Markdown("**Corresponding: usesum**"))
display(Markdown(FileCodeBox(
    file="../src/usesum3.S", 
    lang="gas", 
    title="<b>CODE: asm - usesum3.S : Version 3",
    h="100%", 
    w="107em"
)))
TermShellCmd("make usesum3", cwd=appdir, prompt='')<jupyter_output><empty_output><jupyter_text>Version 4:  Use stack to spill `rdx` before making any call.  Although we don't use it this would allow us to make nested calls assuming we always used the same register to pass the return address in.  In our case `rdx`.  This approach effectively uses the stack to spill `rdx` before we use it to make a call.  In this way the caller is using the stack to hang on to where it needs to return before making a new call.<jupyter_code>display(Markdown(FileCodeBox(
    file="../src/sumit3.S", 
    lang="gas", 
    title="<b>CODE: asm - sumit3.S : Version 4 no change use version 3",
    h="100%", 
    w="107em"
)))
display(Markdown("**Corresponding: usesum**"))
display(Markdown(FileCodeBox(
    file="../src/usesum4.S", 
    lang="gas", 
    title="<b>CODE: asm - usesum4.S : Version 4",
    h="100%", 
    w="107em"
)))
TermShellCmd("make usesum4", cwd=appdir, prompt='')<jupyter_output><empty_output><jupyter_text>Version 5: Use `call` and `ret` instructions to turn our code into an "real" X86 function<jupyter_code>display(Markdown(FileCodeBox(
    file="../src/sumit5.S", 
    lang="gas", 
    title="<b>CODE: asm - sumit5.S : Version 5",
    h="100%", 
    w="107em"
)))
display(Markdown("**Corresponding: usesum**"))
display(Markdown(FileCodeBox(
    file="../src/usesum5.S", 
    lang="gas", 
    title="<b>CODE: asm - usesum5.S : Version 5",
    h="100%", 
    w="107em"
)))
TermShellCmd("make usesum5", cwd=appdir, prompt='')<jupyter_output><empty_output><jupyter_text>Version 6 improving code by "spilling" `rdi` in `sumIt`<jupyter_code>display(Markdown(FileCodeBox(
    file="../src/sumit6.S", 
    lang="gas", 
    title="<b>CODE: asm - sumit6.S : Version 6",
    h="100%", 
    w="107em"
)))
display(Markdown("**Corresponding: usesum**"))
display(Markdown(FileCodeBox(
    file="../src/usesum5.S", 
    lang="gas", 
    title="<b>CODE: asm - usesum5.S : Version 6 no change to usesum use Version 5",
    h="100%", 
    w="107em"
)))
TermShellCmd("make usesum6", cwd=appdir, prompt='')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1576794347341019, 'fraction_numerical': 0.024544440312383786, 'mean_word_length': 3.9393939393939394, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '217704', 'license': 'MIT', 'n_tokens_mistral': 2019, 'n_tokens_neox': 1872, 'n_words': 533, 'path': 'underthecovers/assembly/L11.ipynb', 'repo_name': 'sanskriti-s/UndertheCovers', 'size': '5378'}