CSC 406: Computer System I, 2020 Fall, Assignment #3

Last revised 2020 October 22

Purpose:

To:
  1. Go over the basics of assembly language
  2. Go over how to use a debugger
  3. Go over the layout of activation records/stack frames

Assignment

Please do the following:
  1. Download the program called toAnalyzeCDM.zip from COL
  2. Use an sftp program like filezilla to upload it to a ctilinux machine (like 140.192.36.184) Do not bother unzipping it on your local machine.
  3. On the same cdmlinux machine unzip it with:
    $ unzip toAnalyzeCDM.zip
    
  4. Do
    $ chmod u+x toAnalyze
    
    to make tell Unix that it is an executable program
  5. Analyze it with gdb: gdb toAnalyze. It has a structure like:
    int kale (/* some number of args */)
    {
      /* Some number of local vars */
    
      return( /* something */ );
    }
    
    
    int collardGreen (/* some number of args */)
    {
      /* Some number of local vars */
    
      /* Some loop */
    
        /* Some code, including call(s) to kale() */
    
      return( /* something */ );
    }
    
    
    int main ()
    {
      /* Some number of local vars, including call(s) to collardGreen() */
      return(0);
    }
    	

Answer the following:

  1. (20 Points) Assembly language understanding (1):

    The assembly language for kale() is:
    (gdb) disass kale
    Dump of assembler code for function kale:
       0x00000000004004ed <+0>:	push   %rbp
       0x00000000004004ee <+1>:	mov    %rsp,%rbp
       0x00000000004004f1 <+4>:	mov    %edi,-0x14(%rbp)
       0x00000000004004f4 <+7>:	mov    -0x14(%rbp),%eax
       0x00000000004004f7 <+10>:	add    $0x1,%eax
       0x00000000004004fa <+13>:	mov    %eax,-0x4(%rbp)
       0x00000000004004fd <+16>:	mov    -0x4(%rbp),%eax
       0x0000000000400500 <+19>:	add    $0x1,%eax
       0x0000000000400503 <+22>:	mov    %eax,-0x8(%rbp)
       0x0000000000400506 <+25>:	mov    -0x4(%rbp),%eax
       0x0000000000400509 <+28>:	imul   -0x8(%rbp),%eax
       0x000000000040050d <+32>:	pop    %rbp
       0x000000000040050e <+33>:	retq   
    End of assembler dump.
    	
    Give a 1-2 sentence description of the purpose of each instruction.
    I am more interested in the why than the what.
    Instruction: Purpose:
    push %rbp ___________________________________________________________
    mov %rsp,%rbp ___________________________________________________________
    mov %edi,-0x14(%rbp) ___________________________________________________________
    mov -0x14(%rbp),%eax ___________________________________________________________
    add $0x1,%eax ___________________________________________________________
    mov %eax,-0x4(%rbp) ___________________________________________________________
    mov -0x4(%rbp),%eax ___________________________________________________________
    add $0x1,%eax ___________________________________________________________
    mov %eax,-0x8(%rbp) ___________________________________________________________
    mov -0x4(%rbp),%eax ___________________________________________________________
    imul -0x8(%rbp),%eax ___________________________________________________________
    pop %rbp ___________________________________________________________
    retq ___________________________________________________________
  2. (10 Points) Assembly language understanding (2):

    Write a C function that does what kale() does.
    You won't be able to figure out the names of my parameters var(s) and local var(s); just make up your own name(s).
  3. (20 Points) Activation Records (1):

    Stop the program at its fifth call to kale(). When I did so I got the following:
    (gdb) break kale
    Breakpoint 1 at 0x4004f1
    (gdb) run
    Starting program: /home/instructor/Documents/Academic/DePaul/Classes/CSC373/202021-1Fal_406/Assign3/toAnalyze 
    
    Breakpoint 1, 0x00000000004004f1 in kale ()
    Missing separate debuginfos, use: debuginfo-install glibc-2.17-307.el7.1.x86_64
    (gdb) c
    Continuing.
    
    Breakpoint 1, 0x00000000004004f1 in kale ()
    (gdb) c
    Continuing.
    
    Breakpoint 1, 0x00000000004004f1 in kale ()
    (gdb) c
    Continuing.
    
    Breakpoint 1, 0x00000000004004f1 in kale ()
    (gdb) c
    Continuing.
    
    Breakpoint 1, 0x00000000004004f1 in kale ()
    (gdb) stepi
    0x00000000004004f4 in kale ()
    (gdb) stepi
    0x00000000004004f7 in kale ()
    (gdb) stepi
    0x00000000004004fa in kale ()
    (gdb) stepi
    0x00000000004004fd in kale ()
    (gdb) stepi
    0x0000000000400500 in kale ()
    (gdb) stepi
    0x0000000000400503 in kale ()
    (gdb) stepi
    0x0000000000400506 in kale ()
    (gdb) stepi
    0x0000000000400509 in kale ()
    (gdb) stepi
    0x000000000040050d in kale ()
    (gdb) info reg
    rax            0x14	20
    rbx            0xc	12
    rcx            0x400590	4195728
    rdx            0x7fffffffdd28	140737488346408
    rsi            0x3	3
    rdi            0x3	3
    rbp            0x7fffffffdbe0	0x7fffffffdbe0
    rsp            0x7fffffffdbe0	0x7fffffffdbe0
    r8             0x7ffff7dd5e80	140737351868032
    r9             0x0	0
    r10            0x7fffffffd720	140737488344864
    r11            0x7ffff7a2f460	140737348039776
    r12            0x400400	4195328
    r13            0x7fffffffdd10	140737488346384
    r14            0x0	0
    r15            0x0	0
    rip            0x40050d	0x40050d <kale+32>
    eflags         0x206	[ PF IF ]
    cs             0x33	51
    ss             0x2b	43
    ds             0x0	0
    es             0x0	0
    fs             0x0	0
    gs             0x0	0
    	
    Write the activation record (a.k.a. stack frame) for kale() when %rip gets to 0x000000000040050d.
    Under Value put the numeric value held at that address.
    Under Purpose put one of the following:
    1. not part of kale()'s activation record
    2. argument to kale()
    3. the address in collardGreen() to which rip should return
    4. the stored rbp address for collardGreen()
    5. local variable to kale()
    6. in the activation record of kale(), but not used
    Address: Value: Purpose:
    rbp + 0x10 ___________ ___________
    rbp + 0xC ___________ ___________
    rbp + 0x8 ___________ ___________
    rbp + 0x4 ___________ ___________
    rbp --> rbp + 0x0 ___________ ___________
    rbp - 0x4 ___________ ___________
    rbp - 0x8 ___________ ___________
    rbp - 0xC ___________ ___________
    rbp - 0x10 ___________ ___________
    rbp - 0x14 ___________ ___________
    rbp - 0x18 ___________ ___________
  4. (10 Points) Assembly language understanding (3):

    What are the value(s) that collardGreen() obtains as argument(s) from main()?
    In which register(s) are they passed?
  5. (10 Points) Assembly language understanding (4):

    How many local variables does collardGreen() have?
    Where are they on the stack?
    Give an offset from rbp from within collardGreen()'s activation record. (Include arguments passed in registers that are subsequently placed on the stack as local variables, too.)
  6. (20 Points) Debugger usage (1):

    collardGreen() has a loop. Inside of collardGreen() what are the values of both its arguments and local variables the first time, second time, third time and fourth time rip is 0x0000000000400553? At the top of the table give the offset from rbp (the hexadecimal number added to rbp to get the address of the variable) of the parameter or local variable. (I may have tried to fool you the the number of variables.)

    In the body of the table write the values that that variable has when you hit address local variables.

    Call: rbp + _____ rbp + _____ rbp + _____ rbp + _____ rbp + _____ rbp + _____
    1 ________ ________ ________ ________ ________ ________
    2 ________ ________ ________ ________ ________ ________
    3 ________ ________ ________ ________ ________ ________
    4 ________ ________ ________ ________ ________ ________
  7. (5 Points) Debugger usage (2):

    What value does collardGreen() return to main()?
  8. (5 Points) Assembly language understanding (5):

    collardGreen() calls kale() twice. kale() starts at address 0x00000000004004ed. If you look at the machine code for collardGreen()'s second call to kale(), however, you'll see that the actual number in the function call is 0xFFFFFFA7
    000000000040050f <collardGreen>:
      . . .
      40053f:	89 c7                	mov    %eax,%edi
      400541:	e8 a7 ff ff ff       	callq  4004ed <kale>
      400546:	0f af c3             	imul   %ebx,%eax
      . . .
    	
    1. To what number did the CPU add with 0xFFFFFFA7 to get the address of kale(), 0x00000000004004ed?
    2. Do this addition. Compute 0x0040,04ED.