0% found this document useful (0 votes)
83 views

Recursion & Pointers: Instructor: Alvin R. Lebeck

This document provides an overview of recursion and pointers in a Computer Science 104 lecture at Duke University. It discusses procedure calls at the instruction set architecture, C, and assembly levels. Specifically, it covers the IA32/Linux stack frame structure, register saving conventions, and provides an example of a C function call to a sum_array assembly function that calculates the sum of array elements.

Uploaded by

merlin2007
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Recursion & Pointers: Instructor: Alvin R. Lebeck

This document provides an overview of recursion and pointers in a Computer Science 104 lecture at Duke University. It discusses procedure calls at the instruction set architecture, C, and assembly levels. Specifically, it covers the IA32/Linux stack frame structure, register saving conventions, and provides an example of a C function call to a sum_array assembly function that calculates the sum of array elements.

Uploaded by

merlin2007
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Computer Science 104 Duke University

Recursion  &  Pointers  


Lecture  8  

Instructor:    
Alvin  R.  Lebeck  

Some  Slides  based  on  those  from    


Randy  Bryant  and  Dave  O’Hallaron  

Computer Science 104 Duke University

Admin  
  Homework  #3  Due  Monday  Feb  14  11:59pm  
  Reading:  Chapter  3  

  Midterm  Wednesday  Feb  23  

Assembly  Programming  (x86)  


  Procedure  Calls  

  Recursion  

  Linked  List  

1
Computer Science 104 Duke University

Procedure  Call  GAP  


ISA  Level  
  call  and  return  instrucWons  

C  Level  
  Local  Name  Scope  
  change  tsame  to  same  
  Recursion  
  Arguments/parameters  and  Return  Value  (funcWons)  

Assembly  Level  
  Must  bridge  gap  between  HLL  and  ISA  

  SupporWng  Local  Names  

  Passing  Arguments/Parameters  (arbitrary  number?  

  What  data  structure?  

Computer Science 104 Duke University

IA32/Linux  Stack  Frame  


  Current  Stack  Frame  (“Top”  to  Bo_om)  
  “Argument  build:”  
Parameters  for  funcWon  about  to  call   Caller
Frame  
  Local  variables  
If  can’t  keep  in  registers   Arguments  

  Saved  register  context   Frame  pointer   Return  Addr  


  Old  frame  pointer   %ebp Old  %ebp  

Saved  
Registers  
  Caller  Stack  Frame   +  
  Return  address   Local  
Pushed  by  call  instrucWon  
  Variables  
  Arguments  for  this  call  
Argument  
Stack  pointer
Build  
%esp

3
Computer Science 104 Duke University

Register  Saving  ConvenWons  


  When  procedure  yoo  calls  who:  
  yoo  is  the  caller  
  who  is  the  callee  
  Can  Register  be  used  for  temporary  storage?  
  ConvenWons  

  “Caller  Save”  
  Caller  saves  temporary  values  in  its  frame  before  the  call  
  “Callee  Save”  
  Callee  saves  temporary  values  in  its  frame  before  using  

Computer Science 104 Duke University

IA32/Linux+Windows  Register  Usage  


  %eax,  %edx,  %ecx  
  Caller  saves  prior  to  call  if  values  are  
used  later   %eax
Caller-­‐Save
Temporaries   %edx
  %eax  
  also  used  to  return  integer  or  pointer  
%ecx
value   %ebx
Callee-­‐Save
Temporaries   %esi
  %ebx,  %esi,  %edi  
  Callee  saves  if  wants  to  use  them   %edi
%esp
Special  
  %esp,  %ebp   %ebp
  special  form  of  callee  save  
  Restored  to  original  values  upon  exit  
from  procedure  

5
Computer Science 104 Duke University

Sum  array  
Task: sum together the integers stored in memory  
.text  
.globl  _sum  

_sum:  
# Fill in what goes here

.data  
num_array:  .long  35,  16,  42,  19,  55,  91,  24,  61,  53  

Computer Science 104 Duke University

C  FuncWon  call  with  one  parameter  


#include  <stdio.h>  
#include  <stdlib.h>  
//  declare  the  funcWon  as  externally  defined  
//  computes  sum  of  elements  0  to  i  of  an  array  defined  in  sum_array  
extern  int  sum_array(int  i);    

int  main(void)  {  
 int  result;  
 result  =  sum_array(7);  
 prinn("Array  sum  =  %d\n",result);  
 return  EXIT_SUCCESS;  
}  

7
Computer Science 104 Duke University

Sample  FuncWon  
.text ! ! !# declare the text segment!
.globl _sum_array ! !# declare the function label (note the _ in this label)!
! ! ! !# the C program calls sum(int)!
_sum_array:!
!pushl %ebp ! !# save old frame pointer!
!movl %esp, %ebp ! !# set new stack pointer!
!movl 8(%ebp), %eax !# read arg1 from stack, put into %eax!
!leal num_array, %edx !# load address of num_array into %edx (p = &num_array)!
!leal (%edx,%eax,4), %ecx !# load address of num_array+arg into %ecx!
!movl $0, %eax ! !# move 0 to running sum (%eax)!
loop: ! ! !# label for loop structure!
!addl (%edx), %eax! !# add value *p to running sum (%eax)!
!addl $4, %edx ! !# increment pointer in memory (p++)!
!cmpl %ecx, %edx ! !# compare pointer to termination (p < (num_array+arg1))!
!jl loop! ! !# jump to loop if (p < (num_array+arg1))!
!leave ! ! !# prepare stack for return (movl %ebp, %esp; popl %ebp)!
!ret ! ! !# return to calling routine (result is in %eax)!

.data ! ! !# declare data segment and array with 9 32-bit integers!


num_array: .long 35, 16, 42, 19, 55, 91, 24, 61, 53!

Computer Science 104 Duke University

The  C  code  
#include  <stdio.h>  
#include  <stdlib.h>  
extern  int  sum_i_sqr();  
int  main(void)  {  
int  result;  
 result  =  sum_i_sqr(100);    
 prinn("Sum  =  %d\n",result);  
 return  EXIT_SUCCESS;  
}  
int  sum_i_sqr(int  max){  
 int  i;  
 int  sum  =  0;   Write a recursive
 for(i=0;  i  <=  100;  i++)     version of this
   sum  =  sum  +  i*i  ;  
 return(sum);  //  put  sum  into  %eax  
}   9
Computer Science 104 Duke University

Fibonacci  Number  
F  (n)    =  0      if  n  =  0  
   =  1      if  n  =  1  
   =  F(n-­‐1)  +  F(n-­‐2)  if  n  >  1;  

CPS 104

Computer Science 104 Duke University

Find  a  node  in  a  linked  list  


struct  node  {  
   int  me;  
   struct  node  *next;  
};  
extern  int  asm_find_it(int  key,    struct  node  *ptr);  

  Write  recursive  asm_find_it  

11

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy